~brenns10/sc-argparse

3012e14f5199e0326e5d13d0545bd9a1dd0a94a3 — Stephen Brennan 4 years ago a499100
Add test for string arguments
1 files changed, 57 insertions(+), 2 deletions(-)

M tests/main.c
M tests/main.c => tests/main.c +57 -2
@@ 87,15 87,69 @@ void test_intargs(void)
	argspec[0].val_int = 5; /* need to reset default */

	testarg = (char *[]){ "-c", "not an int" };
	TEST_ASSERT_LESS_THAN_INT(0, sc_argparse(argspec, 4, testarg));
	TEST_ASSERT_LESS_THAN_INT(0, sc_argparse(argspec, 2, testarg));
	TEST_ASSERT_EQUAL(argspec[0].val_int, 5);

	reset_args(argspec);
	argspec[0].val_int = 5; /* need to reset default */

	testarg = (char *[]){ "--argb", "not an int" };
	TEST_ASSERT_LESS_THAN_INT(0, sc_argparse(argspec, 4, testarg));
	TEST_ASSERT_LESS_THAN_INT(0, sc_argparse(argspec, 2, testarg));
	TEST_ASSERT_EQUAL(argspec[0].val_int, 5);

	reset_args(argspec);
	argspec[0].val_int = 5; /* need to reset default */

	testarg = (char *[]){ "--argb" };
	TEST_ASSERT_LESS_THAN_INT(0, sc_argparse(argspec, 1, testarg));
}

void test_stringargs(void)
{
	char **testarg;
	struct sc_arg argspec[] = {
		SC_ARG_STRING('a', "--arga", "help text"),
		SC_ARG_DEF_STRING('b', "--argb", "default b", "help text"),
		SC_ARG_COUNT('c', "--argc", "help text"),
		SC_ARG_END(),
	};

	/* missing argument, you may think. But in fact, -c becomes the string
	 * arg to -a, and that's okay (see behavior of, e.g. gcc -o -a foo.c) */
	testarg = (char *[]){ "-a", "-c" };
	TEST_ASSERT_GREATER_OR_EQUAL_INT(0, sc_argparse(argspec, 2, testarg));

	reset_args(argspec);
	argspec[1].val_string = "default b";

	/* missing argument! */
	testarg = (char *[]){ "-c", "-a" };
	TEST_ASSERT_LESS_THAN_INT(0, sc_argparse(argspec, 2, testarg));

	reset_args(argspec);
	argspec[1].val_string = "default b";

	/* missing argument! */
	testarg = (char *[]){ "--arga" };
	TEST_ASSERT_LESS_THAN_INT(0, sc_argparse(argspec, 1, testarg));

	reset_args(argspec);
	argspec[1].val_string = "default b";

	/* missing argument! */
	testarg = (char *[]){ "--arga", "value" };
	TEST_ASSERT_GREATER_OR_EQUAL_INT(0, sc_argparse(argspec, 2, testarg));
	TEST_ASSERT_EQUAL_STRING("value", argspec[0].val_string);
	TEST_ASSERT_EQUAL_STRING("default b", argspec[1].val_string);

	reset_args(argspec);
	argspec[1].val_string = "default b";

	/* missing argument! */
	testarg = (char *[]){ "--arga", "value", "-b", "valforb" };
	TEST_ASSERT_GREATER_OR_EQUAL_INT(0, sc_argparse(argspec, 4, testarg));
	TEST_ASSERT_EQUAL_STRING("value", argspec[0].val_string);
	TEST_ASSERT_EQUAL_STRING("valforb", argspec[1].val_string);
}

int main(void)


@@ 103,5 157,6 @@ int main(void)
	UNITY_BEGIN();
	RUN_TEST(test_countargs);
	RUN_TEST(test_intargs);
	RUN_TEST(test_stringargs);
	return UNITY_END();
}