~brenns10/sc-argparse

e263556c42b46e46e73c985efe8fb5ff9f94e7c6 — Stephen Brennan 4 years ago 37b20cf
Tests for posargs
1 files changed, 31 insertions(+), 12 deletions(-)

M tests/main.c
M tests/main.c => tests/main.c +31 -12
@@ 33,25 33,25 @@ void test_countargs(void)
	};

	testarg = (char *[]){ "-v" };
	sc_argparse(argspec, 1, testarg);
	TEST_ASSERT_EQUAL(0, sc_argparse(argspec, 1, testarg));
	TEST_ASSERT_TRUE(argspec[0].seen);
	TEST_ASSERT_EQUAL(1, argspec[0].val_int);
	reset_args(argspec);

	testarg = (char *[]){ "--verbose" };
	sc_argparse(argspec, 1, testarg);
	TEST_ASSERT_EQUAL(0, sc_argparse(argspec, 1, testarg));
	TEST_ASSERT_TRUE(argspec[0].seen);
	TEST_ASSERT_EQUAL(1, argspec[0].val_int);
	reset_args(argspec);

	testarg = (char *[]){ "--verbose", "-vv", "--verbose" };
	sc_argparse(argspec, 3, testarg);
	TEST_ASSERT_EQUAL(0, sc_argparse(argspec, 3, testarg));
	TEST_ASSERT_TRUE(argspec[0].seen);
	TEST_ASSERT_EQUAL(4, argspec[0].val_int);
	reset_args(argspec);

	testarg = (char *[]){};
	sc_argparse(argspec, 0, testarg);
	TEST_ASSERT_EQUAL(0, sc_argparse(argspec, 0, testarg));
	TEST_ASSERT_FALSE(argspec[0].seen);
	TEST_ASSERT_EQUAL(0, argspec[0].val_int);
	reset_args(argspec);


@@ 71,7 71,7 @@ void test_intargs(void)
	};

	testarg = (char *[]){ "-b", "1" };
	sc_argparse(argspec, 2, testarg);
	TEST_ASSERT_EQUAL(0, sc_argparse(argspec, 2, testarg));
	TEST_ASSERT_EQUAL(5, argspec[0].val_int);
	TEST_ASSERT_EQUAL(1, argspec[1].val_int);
	TEST_ASSERT_TRUE(argspec[1].seen);


@@ 81,7 81,7 @@ void test_intargs(void)
	argspec[0].val_int = 5; /* need to reset default */

	testarg = (char *[]){ "--argb=-500", "-c", "1234" };
	sc_argparse(argspec, 3, testarg);
	TEST_ASSERT_EQUAL(0, sc_argparse(argspec, 3, testarg));
	TEST_ASSERT_EQUAL(1234, argspec[0].val_int);
	TEST_ASSERT_EQUAL(-500, argspec[1].val_int);
	TEST_ASSERT_TRUE(argspec[0].seen);


@@ 121,7 121,7 @@ void test_stringargs(void)
	/* 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));
	TEST_ASSERT_EQUAL(0, sc_argparse(argspec, 2, testarg));

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


@@ 155,7 155,7 @@ void test_stringargs(void)
	argspec[1].val_string = "default b";

	testarg = (char *[]){ "--arga", "value" };
	TEST_ASSERT_GREATER_OR_EQUAL_INT(0, sc_argparse(argspec, 2, testarg));
	TEST_ASSERT_EQUAL(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);



@@ 163,7 163,7 @@ void test_stringargs(void)
	argspec[1].val_string = "default b";

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



@@ 171,7 171,7 @@ void test_stringargs(void)
	argspec[1].val_string = "default b";

	testarg = (char *[]){ "--arga=value", "--argb", "valforb" };
	TEST_ASSERT_GREATER_OR_EQUAL_INT(0, sc_argparse(argspec, 3, testarg));
	TEST_ASSERT_EQUAL(0, sc_argparse(argspec, 3, testarg));
	TEST_ASSERT_EQUAL_STRING("value", argspec[0].val_string);
	TEST_ASSERT_EQUAL_STRING("valforb", argspec[1].val_string);



@@ 180,7 180,7 @@ void test_stringargs(void)

	/* Should be able to have args after a short flag w/o arg */
	testarg = (char *[]){ "-ca", "a special value" };
	TEST_ASSERT_GREATER_OR_EQUAL_INT(0, sc_argparse(argspec, 2, testarg));
	TEST_ASSERT_EQUAL(0, sc_argparse(argspec, 2, testarg));
	TEST_ASSERT_EQUAL_STRING("a special value", argspec[0].val_string);
}



@@ 215,7 215,7 @@ void test_stop_parsing(void)
	};

	testarg = (char *[]){ "-a", "1", "--", "-c" };
	TEST_ASSERT_GREATER_OR_EQUAL_INT(0, sc_argparse(argspec, 4, testarg));
	TEST_ASSERT_EQUAL(1, sc_argparse(argspec, 4, testarg));
	TEST_ASSERT_EQUAL(1, argspec[0].val_int);
	TEST_ASSERT_EQUAL_STRING("default b", argspec[1].val_string);
	TEST_ASSERT_EQUAL(0, argspec[2].val_int);


@@ 224,6 224,24 @@ void test_stop_parsing(void)
	TEST_ASSERT_FALSE(argspec[2].seen);
}

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

	testarg =
	        (char *[]){ "-a", "1", "2", "-cc", "-", "posarg", "--argb=yo" };
	TEST_ASSERT_EQUAL(3, sc_argparse(argspec, 7, testarg));
	TEST_ASSERT_EQUAL_STRING(testarg[0], "2");
	TEST_ASSERT_EQUAL_STRING(testarg[1], "-");
	TEST_ASSERT_EQUAL_STRING(testarg[2], "posarg");
}

int main(void)
{
	UNITY_BEGIN();


@@ 232,5 250,6 @@ int main(void)
	RUN_TEST(test_stringargs);
	RUN_TEST(test_unknown_args);
	RUN_TEST(test_stop_parsing);
	RUN_TEST(test_posargs);
	return UNITY_END();
}