summaryrefslogblamecommitdiffstats
path: root/tests/parser.c
blob: 744c8cac825384b3782875d5eb2f41979eba8b99 (plain) (tree)














































































































                                                                        





                                                                         






                                                          

                                               

                                                    



                 
/* -*- 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