sangaboard-firmware/test/test_argument_parsing.cpp
Richard Bowman 3997d66cde Merge in com port switching from sangaboard v4
I've refactored the use of the serial port, to store the
current serial port in a global variable in a module.
This is set to the most recent port to receive data,
and so we should always be replying to the port
that was used to initiate a command.

If SECONDARY_SERIAL_PORT is not defined, this
should result in no change - if it is, we should be
able to communicate on either port.
2023-02-22 12:05:48 +00:00

91 lines
2.3 KiB
C++

#include <Arduino.h>
#include <unity.h>
#include "main.h"
void test_single_argument_parsing(void)
{
char * expected_single = "3123.123asdf";
String all_args = F("3123.123asdf");
char * args[1];
uint8_t ret = 0;
ret = parse_arguments(args, all_args, 1);
TEST_ASSERT_EQUAL_STRING(expected_single, args[0]);
TEST_ASSERT_EQUAL(1, ret);
free(args[0]);
char * args2[1];
all_args = F("3123.123asdf fail");
ret = parse_arguments(args2, all_args, 1);
TEST_ASSERT_EQUAL_STRING(expected_single, args2[0]);
TEST_ASSERT_EQUAL(1, ret);
free(args2[0]);
char * args3[1];
all_args = F(" 3123.123asdf fail");
ret = parse_arguments(args3, all_args, 1);
TEST_ASSERT_EQUAL_STRING(expected_single, args3[0]);
TEST_ASSERT_EQUAL(1, ret);
free(args3[0]);
}
void test_multiple_argument_parsing(void)
{
char * expected[] = {"3123.123asdf", "second", "321./"};
String all_args = F("3123.123asdf second 321./");
char * args[3];
int i;
uint8_t ret;
ret = parse_arguments(args, all_args, 3);
TEST_ASSERT_EQUAL(3, ret);
for (i = 0; i < 3; i++)
{
TEST_ASSERT_EQUAL_STRING(expected[i], args[i]);
free(args[i]);
}
all_args = F("3123.123asdf second 321./ fail");
ret = parse_arguments(args, all_args, 3);
TEST_ASSERT_EQUAL(3, ret);
for (i = 0; i < 3; i++)
{
TEST_ASSERT_EQUAL_STRING(expected[i], args[i]);
free(args[i]);
}
all_args = F(" 3123.123asdf second");
ret = parse_arguments(args, all_args, 3);
TEST_ASSERT_EQUAL(2, ret);
for (i = 0; i < ret; i++)
{
TEST_ASSERT_EQUAL_STRING(expected[i], args[i]);
free(args[i]);
}
}
void setup() {
comPort->begin(115200);
// NOTE!!! Wait for >2 secs
// if board doesn't support software reset via comPort->DTR/RTS
delay(2000);
UNITY_BEGIN(); // IMPORTANT LINE!
//bizzare string issues caused this to fail in specific sequences, so we run it multiple times
RUN_TEST(test_multiple_argument_parsing);
RUN_TEST(test_single_argument_parsing);
RUN_TEST(test_multiple_argument_parsing);
RUN_TEST(test_single_argument_parsing);
RUN_TEST(test_single_argument_parsing);
RUN_TEST(test_multiple_argument_parsing);
UNITY_END();
}
void loop() {
;
}