summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2022-05-20 13:15:14 +0300
committergramanas <anastasis.gramm2@gmail.com>2022-05-20 13:15:14 +0300
commitb30cd3e1b8d1162c529bfdd602d0d56ccd2e38c7 (patch)
treebfae4137f78a0bf223518f6590db2bd436e82185 /tests
parent3515199c9e158d4b6fe5d064539bdf288dca7b81 (diff)
downloadfoodtools-b30cd3e1b8d1162c529bfdd602d0d56ccd2e38c7.tar.gz
foodtools-b30cd3e1b8d1162c529bfdd602d0d56ccd2e38c7.tar.bz2
foodtools-b30cd3e1b8d1162c529bfdd602d0d56ccd2e38c7.zip
aha
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am3
-rw-r--r--tests/foodtest.in7
-rw-r--r--tests/parser.c122
3 files changed, 131 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d92a287..18d6314 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -7,7 +7,8 @@ TESTS = foodtest
EXTRA_DIST = maketests.sh \
foodtest.in \
types.c \
- util.c
+ util.c \
+ parser.c
check_PROGRAMS = foodtest
diff --git a/tests/foodtest.in b/tests/foodtest.in
index 484cfeb..8d45f26 100644
--- a/tests/foodtest.in
+++ b/tests/foodtest.in
@@ -4,8 +4,14 @@
/*
* Source files check by this suite:
*/
+#include "../src/parser.c"
#include "../src/types.c"
#include "../src/util.c"
+
+#define is1(arg) ck_assert_int_eq(arg, 1)
+#define is0(arg) ck_assert_int_eq(arg, 0)
+
+
/*
* Test suite based on check
*
@@ -19,6 +25,7 @@
*/
#include "types.c"
#include "util.c"
+#include "parser.c"
Suite * foodtest_suite(void)
{
diff --git a/tests/parser.c b/tests/parser.c
new file mode 100644
index 0000000..694653b
--- /dev/null
+++ b/tests/parser.c
@@ -0,0 +1,122 @@
+/* -*- eval: (outline-minor-mode); outline-regexp: "START_TEST("; -*- */
+
+static FILE *
+file_from_str(char * s) {
+ if (!s) return NULL;
+ return fmemopen(s, strlen(s), "r");
+}
+
+START_TEST(check_next_escaped_line_full_file)
+{
+ char * file =
+ " \n"
+ "call #2\n"
+ " \n"
+ "ca\\\n"
+ "ll #4\n"
+ "\n"
+ "call \\ #6\n"
+ "\n"
+ "c\\\n"
+ "a\\\n"
+ "l\\\n"
+ "l \\\n"
+ "\\\n"
+ "#\\\n"
+ "8\\\n"
+ "\n"
+ " call \\\n"
+ " #9\n"
+ "last call";
+
+
+ FILE * f = file_from_str(file);
+ if (!f) ck_abort_msg("Couldn't fmemopen");
+
+ char line[LINE_SIZE] = "";
+ int actual_line = 0;
+ char error[LINE_SIZE] = "";
+
+ is0(next_escaped_line(f, line, &actual_line, error));
+ ck_assert_int_eq(actual_line, 1);
+ ck_assert_str_eq(line, "");
+
+ is0(next_escaped_line(f, line, &actual_line, error));
+ ck_assert_int_eq(actual_line, 2);
+ ck_assert_str_eq(line, "call #2");
+
+ is0(next_escaped_line(f, line, &actual_line, error));
+ ck_assert_int_eq(actual_line, 3);
+ ck_assert_str_eq(line, "");
+
+ is0(next_escaped_line(f, line, &actual_line, error));
+ ck_assert_int_eq(actual_line, 5);
+ ck_assert_str_eq(line, "call #4");
+
+ is0(next_escaped_line(f, line, &actual_line, error));
+ ck_assert_int_eq(actual_line, 6);
+ ck_assert_str_eq(line, "");
+
+ is0(next_escaped_line(f, line, &actual_line, error));
+ ck_assert_int_eq(actual_line, 7);
+ ck_assert_str_eq(line, "call \\ #6");
+
+ is0(next_escaped_line(f, line, &actual_line, error));
+ ck_assert_int_eq(actual_line, 8);
+ ck_assert_str_eq(line, "");
+
+ is0(next_escaped_line(f, line, &actual_line, error));
+ ck_assert_int_eq(actual_line, 16);
+ ck_assert_str_eq(line, "call #8");
+
+ is0(next_escaped_line(f, line, &actual_line, error));
+ ck_assert_int_eq(actual_line, 18);
+ ck_assert_str_eq(line, "call #9");
+
+ is0(next_escaped_line(f, line, &actual_line, error));
+ ck_assert_int_eq(actual_line, 19);
+ ck_assert_str_eq(line, "last call");
+
+ is1(next_escaped_line(f, line, &actual_line, error));
+}
+END_TEST
+
+START_TEST(check_next_escaped_line_chunks)
+{
+ char line[LINE_SIZE] = "";
+ int actual_line = 0;
+ char error[LINE_SIZE] = "";
+
+ next_escaped_line(file_from_str("check \\\nthis \\\nout!"),
+ line, &actual_line, error);
+ ck_assert_int_eq(actual_line, 3);
+ ck_assert_str_eq(line, "check this out!");
+
+ actual_line = 0;
+ next_escaped_line(file_from_str(" "),
+ line, &actual_line, error);
+ ck_assert_int_eq(actual_line, 1);
+ ck_assert_str_eq(line, "");
+}
+END_TEST
+
+START_TEST(check_parse_item)
+{
+ recipe * r = new_recipe();
+ char error[1000] = "";
+
+ is0(parse_item("test = val", r, (pt *)NULL, error));
+ ck_assert_str_eq(r->i[r->in - 1]->name, "test");
+ ck_assert_str_eq(r->i[r->in - 1]->qty, "val");
+
+ is1(parse_item("wrong!", r, (pt *)NULL, error));
+ ck_assert_str_eq(r->i[r->in - 1]->name, "test");
+ ck_assert_str_eq(r->i[r->in - 1]->qty, "val");
+
+ is0(parse_item("---", r, (pt *)NULL, error));
+ ck_assert_str_eq(r->i[r->in - 1]->name, "test");
+ ck_assert_str_eq(r->i[r->in - 1]->qty, "val");
+
+ free_recipe(r);
+}
+END_TEST