From 5c589fff95825e9c7c5350e987ea8d7861fe2978 Mon Sep 17 00:00:00 2001
From: yangfl <yangfl@users.noreply.github.com>
Date: Mon, 16 Dec 2019 18:44:51 +0800
Subject: [PATCH 4/8] tests: add options to unittest

---
 tests/parseargs.c       | 78 +++++++++++++++++++++++++++++++++++++++++
 tests/parseargs.h       | 10 ++++++
 tests/unittest.c        | 24 +++++++------
 tests/unittest_string.c |  9 +++--
 4 files changed, 109 insertions(+), 12 deletions(-)
 create mode 100644 tests/parseargs.c
 create mode 100644 tests/parseargs.h

diff --git a/tests/parseargs.c b/tests/parseargs.c
new file mode 100644
index 0000000..c33d959
--- /dev/null
+++ b/tests/parseargs.c
@@ -0,0 +1,78 @@
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <getopt.h>
+#include <ini.h>
+#include "parseargs.h"
+
+bool ini_handler_lineno = false;
+
+int parseargs(int argc, char **argv)
+{
+    while (1) {
+        int option_index = 0;
+        static const struct option long_options[] = {
+            {"ini_allow_multiline",             required_argument, NULL, 0},
+            {"ini_allow_bom",                   required_argument, NULL, 0},
+            {"ini_start_comment_prefixes",      required_argument, NULL, 0},
+            {"ini_allow_inline_comments",       required_argument, NULL, 0},
+            {"ini_inline_comment_prefixes",     required_argument, NULL, 0},
+            {"ini_use_stack",                   required_argument, NULL, 0},
+            {"ini_max_line",                    required_argument, NULL, 0},
+            {"ini_allow_realloc",               required_argument, NULL, 0},
+            {"ini_initial_alloc",               required_argument, NULL, 0},
+            {"ini_stop_on_first_error",         required_argument, NULL, 0},
+//          {"ini_call_handler_on_new_section", required_argument, NULL, 0},
+            {"ini_handler_lineno",              required_argument, NULL, 0},
+            {"ini_allow_no_value",              required_argument, NULL, 0},
+            {0,                                 0,                 NULL, 0}
+        };
+
+        static const struct {
+            void *val;
+            char type;
+        } optargs[] = {
+            {&ini_allow_multiline,             'b'},
+            {&ini_allow_bom,                   'b'},
+            {&ini_start_comment_prefixes,      's'},
+            {&ini_allow_inline_comments,       'b'},
+            {&ini_inline_comment_prefixes,     's'},
+            {&ini_use_stack,                   'b'},
+            {&ini_max_line,                    'i'},
+            {&ini_allow_realloc,               'b'},
+            {&ini_initial_alloc,               'i'},
+            {&ini_stop_on_first_error,         'b'},
+//          {&ini_call_handler_on_new_section, 'b'},
+            {&ini_handler_lineno,              'b'},
+            {&ini_allow_no_value,              'b'},
+            {0,                                  0}
+        };
+
+        int c = getopt_long(argc, argv, "", long_options, &option_index);
+        if (c == -1)
+            break;
+
+        switch (c) {
+            case 0:
+                switch (optargs[option_index].type) {
+                    case 's':
+                        *((char **) optargs[option_index].val) = strdup(optarg);
+                        break;
+                    case 'i':
+                        *((int *) optargs[option_index].val) = atoi(optarg);
+                        break;
+                    case 'b':
+                        *((bool *) optargs[option_index].val) = atoi(optarg);
+                        break;
+                    default:
+                        break;
+                }
+                break;
+
+            default:
+                printf("?? getopt returned character code 0%o ??\n", c);
+                return 1;
+        }
+    }
+    return 0;
+}
diff --git a/tests/parseargs.h b/tests/parseargs.h
new file mode 100644
index 0000000..353deb0
--- /dev/null
+++ b/tests/parseargs.h
@@ -0,0 +1,10 @@
+#ifndef __INI_TEST_H__
+#define __INI_TEST_H__
+
+#include <stdbool.h>
+
+extern bool ini_handler_lineno;
+
+int parseargs(int argc, char **argv);
+
+#endif /* __INI_TEST_H__ */
diff --git a/tests/unittest.c b/tests/unittest.c
index fde8e61..af96015 100644
--- a/tests/unittest.c
+++ b/tests/unittest.c
@@ -13,18 +13,18 @@ respectively).
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include "../ini.h"
+
+#define INI_HANDLER_LINENO 1
+#include <ini.h>
+#undef INI_HANDLER_LINENO
+
+#include "parseargs.h"
 
 int User;
 char Prev_section[50];
 
-#if INI_HANDLER_LINENO
 int dumper(void* user, const char* section, const char* name,
            const char* value, int lineno)
-#else
-int dumper(void* user, const char* section, const char* name,
-           const char* value)
-#endif
 {
     User = *((int*)user);
     if (!name || strcmp(section, Prev_section)) {
@@ -36,11 +36,11 @@ int dumper(void* user, const char* section, const char* name,
         return 1;
     }
 
-#if INI_HANDLER_LINENO
+if (ini_handler_lineno) {
     printf("... %s%s%s;  line %d\n", name, value ? "=" : "", value ? value : "", lineno);
-#else
+} else {
     printf("... %s%s%s;\n", name, value ? "=" : "", value ? value : "");
-#endif
+}
 
     if (!value) {
         // Happens when INI_ALLOW_NO_VALUE=1 and line has no value (no '=' or ':')
@@ -60,8 +60,12 @@ void parse(const char* fname) {
     u++;
 }
 
-int main(void)
+int main(int argc, char **argv)
 {
+    int e = parseargs(argc, argv);
+    if (e) {
+        return e;
+    }
     parse("no_file.ini");
     parse("normal.ini");
     parse("bad_section.ini");
diff --git a/tests/unittest_string.c b/tests/unittest_string.c
index 4200e6b..8e00c79 100644
--- a/tests/unittest_string.c
+++ b/tests/unittest_string.c
@@ -3,7 +3,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include "../ini.h"
+#include <ini.h>
+#include "parseargs.h"
 
 int User;
 char Prev_section[50];
@@ -31,8 +32,12 @@ void parse(const char* name, const char* string) {
     u++;
 }
 
-int main(void)
+int main(int argc, char **argv)
 {
+    int e = parseargs(argc, argv);
+    if (e) {
+        return e;
+    }
     parse("empty string", "");
     parse("basic", "[section]\nfoo = bar\nbazz = buzz quxx");
     parse("crlf", "[section]\r\nhello = world\r\nforty_two = 42\r\n");
-- 
2.35.1

