M .gitignore => .gitignore +2 -0
@@ 1,2 1,4 @@
build
.clangd
+subprojects/*
+!subprojects/*.wrap
M README.md => README.md +4 -0
@@ 15,6 15,10 @@ You may then compile (or re-compile) with:
ninja -C build
+You can run the tests with:
+
+ ninja -C build test
+
When you would like to commit your changes, please ensure that you have
pre-commit hooks installed, so that automated checks can run on your code:
M meson.build => meson.build +12 -0
@@ 21,6 21,11 @@ libsc_argparse_dep = declare_dependency(
link_with : libsc_argparse,
)
+unity_dep = dependency(
+ 'Unity',
+ fallback: ['Unity', 'unity_dep'],
+)
+
pkg = import('pkgconfig')
pkg.generate(
libraries: libsc_argparse,
@@ 37,5 42,12 @@ executable(
dependencies: libsc_argparse_dep,
)
+tests = executable(
+ 'unity-test',
+ ['tests/main.c'],
+ dependencies: [libsc_argparse_dep, unity_dep],
+)
+test('unit test', tests)
+
# For each public header in "include/":
install_headers('include/sc-argparse.h', subdir : 'sc-argparse')
A subprojects/Unity.wrap => subprojects/Unity.wrap +6 -0
@@ 0,0 1,6 @@
+[wrap-file]
+directory = Unity-2.5.1
+
+source_url = https://github.com/ThrowTheSwitch/Unity/archive/v2.5.1.tar.gz
+source_filename = Unity-2.5.1.tar.gz
+source_hash = 5ce08ef62f5f64d18f8137b3eaa6d29199ee81d1fc952cef0eea96660a2caf47
A tests/main.c => tests/main.c +93 -0
@@ 0,0 1,93 @@
+#include <unity.h>
+
+#include "sc-argparse.h"
+
+#define nelem(x) (sizeof(x) / sizeof(x[0]))
+
+static void reset_args(struct sc_arg args[])
+{
+ int i;
+ for (i = 0; args[i].type; i++) {
+ args[i].val_int = 0;
+ args[i].val_string = NULL;
+ args[i].seen = false;
+ }
+}
+
+void setUp(void)
+{
+ // set stuff up here
+}
+
+void tearDown(void)
+{
+ // clean stuff up here
+}
+
+void test_countargs(void)
+{
+ char **testarg;
+ struct sc_arg argspec[] = {
+ SC_ARG_COUNT('v', "--verbose", "use many times for output"),
+ SC_ARG_END(),
+ };
+
+ testarg = (char *[]){ "-v" };
+ 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_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_TRUE(argspec[0].seen);
+ TEST_ASSERT_EQUAL(4, argspec[0].val_int);
+ reset_args(argspec);
+
+ testarg = (char *[]){};
+ sc_argparse(argspec, 0, testarg);
+ TEST_ASSERT_FALSE(argspec[0].seen);
+ TEST_ASSERT_EQUAL(0, argspec[0].val_int);
+ reset_args(argspec);
+}
+
+void test_intargs(void)
+{
+ char **testarg;
+ struct sc_arg argspec[] = {
+ SC_ARG_DEF_INT('c', "--count", 5, "help text"),
+ SC_ARG_INT('b', "--argb", "help text"),
+ SC_ARG_END(),
+ };
+
+ testarg = (char *[]){ "-b", "1" };
+ 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);
+ TEST_ASSERT_FALSE(argspec[0].seen);
+
+ reset_args(argspec);
+ argspec[1].val_int = 5; /* need to reset default */
+
+ testarg = (char *[]){ "-b", "-500", "-c", "1234" };
+ sc_argparse(argspec, 4, testarg);
+ TEST_ASSERT_EQUAL(1234, argspec[0].val_int);
+ TEST_ASSERT_EQUAL(-500, argspec[1].val_int);
+ TEST_ASSERT_TRUE(argspec[0].seen);
+ TEST_ASSERT_TRUE(argspec[1].seen);
+}
+
+int main(void)
+{
+ UNITY_BEGIN();
+ RUN_TEST(test_countargs);
+ RUN_TEST(test_intargs);
+ return UNITY_END();
+}