summaryrefslogtreecommitdiffstats
path: root/tests/parser.c
blob: 744c8cac825384b3782875d5eb2f41979eba8b99 (plain) (blame)
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
/* -*- 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");

  is0(parse_item("test2, test3=another val     ", r, (pt *)NULL, error));
  ck_assert_str_eq(r->i[r->in - 2]->name, "test2");
  ck_assert_str_eq(r->i[r->in - 2]->qty, "another val");
  ck_assert_str_eq(r->i[r->in - 1]->name, "test3");
  ck_assert_str_eq(r->i[r->in - 1]->qty, "another val");

  is0(parse_item("noqty", r, (pt *)NULL, error));
  ck_assert_str_eq(r->i[r->in - 1]->name, "noqty");
  ck_assert_str_eq(r->i[r->in - 1]->qty, "*");

  is0(parse_item("noqty2, noqty3", r, (pt *)NULL, error));
  ck_assert_str_eq(r->i[r->in - 1]->name, "noqty3");
  ck_assert_str_eq(r->i[r->in - 1]->qty, "*");

  is0(parse_item("---", r, (pt *)NULL, error));
  ck_assert_str_eq(r->i[r->in - 1]->name, "noqty3");
  ck_assert_str_eq(r->i[r->in - 1]->qty, "*");

  free_recipe(r);
}
END_TEST