summaryrefslogtreecommitdiffstats
path: root/tests/parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/parser.c')
-rw-r--r--tests/parser.c122
1 files changed, 122 insertions, 0 deletions
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