1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
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
|