source: tester.c @ b31252d

release-1.10release-1.8release-1.9
Last change on this file since b31252d was b31252d, checked in by David Benjamin <davidben@mit.edu>, 13 years ago
Add a few unit tests for classinstfilt They currently fail because our quoting is buggy.
  • Property mode set to 100644
File size: 25.6 KB
Line 
1#define OWL_PERL
2#define WINDOW FAKE_WINDOW
3#include "owl.h"
4#undef WINDOW
5
6#include <unistd.h>
7#include <stdlib.h>
8
9#undef instr
10#include <curses.h>
11
12owl_global g;
13
14int numtests;
15
16int owl_regtest(void);
17int owl_util_regtest(void);
18int owl_dict_regtest(void);
19int owl_variable_regtest(void);
20int owl_filter_regtest(void);
21int owl_obarray_regtest(void);
22int owl_editwin_regtest(void);
23int owl_fmtext_regtest(void);
24int owl_smartfilter_regtest(void);
25
26extern void owl_perl_xs_init(pTHX);
27
28int main(int argc, char **argv, char **env)
29{
30  FILE *rnull;
31  FILE *wnull;
32  char *perlerr;
33  int status = 0;
34
35  if (argc <= 1) {
36    fprintf(stderr, "Usage: %s --builtin|TEST.t|-le CODE\n", argv[0]);
37    return 1;
38  }
39
40  /* initialize a fake ncurses, detached from std{in,out} */
41  wnull = fopen("/dev/null", "w");
42  rnull = fopen("/dev/null", "r");
43  newterm("xterm", wnull, rnull);
44  /* initialize global structures */
45  owl_global_init(&g);
46
47  perlerr = owl_perlconfig_initperl(NULL, &argc, &argv, &env);
48  if (perlerr) {
49    endwin();
50    fprintf(stderr, "Internal perl error: %s\n", perlerr);
51    status = 1;
52    goto out;
53  }
54
55  owl_global_complete_setup(&g);
56  owl_global_setup_default_filters(&g);
57
58  owl_view_create(owl_global_get_current_view(&g), "main",
59                  owl_global_get_filter(&g, "all"),
60                  owl_global_get_style_by_name(&g, "default"));
61
62  owl_function_firstmsg();
63
64  ENTER;
65  SAVETMPS;
66
67  if (strcmp(argv[1], "--builtin") == 0) {
68    status = owl_regtest();
69  } else if (strcmp(argv[1], "-le") == 0 && argc > 2) {
70    /*
71     * 'prove' runs its harness perl with '-le CODE' to get some
72     * information out.
73     */
74    moreswitches("l");
75    eval_pv(argv[2], true);
76  } else {
77    sv_setpv(get_sv("0", false), argv[1]);
78    sv_setpv(get_sv("main::test_prog", TRUE), argv[1]);
79
80    eval_pv("do $main::test_prog; die($@) if($@)", true);
81  }
82
83  status = 0;
84
85  FREETMPS;
86  LEAVE;
87
88 out:
89  perl_destruct(owl_global_get_perlinterp(&g));
90  perl_free(owl_global_get_perlinterp(&g));
91  /* probably not necessary, but tear down the screen */
92  endwin();
93  fclose(rnull);
94  fclose(wnull);
95  return status;
96}
97
98int owl_regtest(void) {
99  numtests = 0;
100  int numfailures=0;
101  /*
102    printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS
103    +OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS);
104  */
105  numfailures += owl_util_regtest();
106  numfailures += owl_dict_regtest();
107  numfailures += owl_variable_regtest();
108  numfailures += owl_filter_regtest();
109  numfailures += owl_editwin_regtest();
110  numfailures += owl_fmtext_regtest();
111  numfailures += owl_smartfilter_regtest();
112  if (numfailures) {
113      fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
114  }
115  printf("1..%d\n", numtests);
116
117  return(numfailures);
118}
119
120#define FAIL_UNLESS(desc,pred) do { int __pred = (pred);                \
121    numtests++;                                                         \
122    printf("%s %s", (__pred)?"ok":(numfailed++,"not ok"), desc);        \
123    if(!(__pred)) printf("\t(%s:%d)", __FILE__, __LINE__); printf("%c", '\n'); } while(0)
124
125
126int owl_util_regtest(void)
127{
128  int numfailed=0;
129
130  printf("# BEGIN testing owl_util\n");
131
132#define CHECK_STR_AND_FREE(desc, expected, expr)         \
133    do {                                                 \
134      char *__value = (expr);                            \
135      FAIL_UNLESS((desc), !strcmp((expected), __value)); \
136      owl_free(__value);                                 \
137    } while (0)
138
139  CHECK_STR_AND_FREE("owl_util_substitute 1", "foo",
140                     owl_text_substitute("foo", "", "Y"));
141  CHECK_STR_AND_FREE("owl_text_substitute 2", "fYZYZ",
142                     owl_text_substitute("foo", "o", "YZ"));
143  CHECK_STR_AND_FREE("owl_text_substitute 3", "foo",
144                     owl_text_substitute("fYZYZ", "YZ", "o"));
145  CHECK_STR_AND_FREE("owl_text_substitute 4", "/u/foo/meep",
146                     owl_text_substitute("~/meep", "~", "/u/foo"));
147
148  FAIL_UNLESS("skiptokens 1", 
149              !strcmp("bar quux", skiptokens("foo bar quux", 1)));
150  FAIL_UNLESS("skiptokens 2", 
151              !strcmp("meep", skiptokens("foo 'bar quux' meep", 2)));
152
153  CHECK_STR_AND_FREE("expand_tabs 1", "        hi", owl_text_expand_tabs("\thi"));
154
155  CHECK_STR_AND_FREE("expand_tabs 2", "        hi\nword    tab",
156                     owl_text_expand_tabs("\thi\nword\ttab"));
157
158  CHECK_STR_AND_FREE("expand_tabs 3", "                2 tabs",
159                     owl_text_expand_tabs("\t\t2 tabs"));
160  CHECK_STR_AND_FREE("expand_tabs 4", "α       ααααααα!        ",
161                     owl_text_expand_tabs(\tααααααα!\t"));
162  CHECK_STR_AND_FREE("expand_tabs 5", "A      AAA!!        ",
163                     owl_text_expand_tabs("A\tAAA!!\t"));
164
165  FAIL_UNLESS("skiptokens 1",
166              !strcmp("world", skiptokens("hello world", 1)));
167
168  FAIL_UNLESS("skiptokens 2",
169              !strcmp("c d e", skiptokens("a   b c d e", 2)));
170
171  FAIL_UNLESS("skiptokens 3",
172              !strcmp("\"b\" c d e", skiptokens("a \"b\" c d e", 1)));
173
174  FAIL_UNLESS("skiptokens 4",
175              !strcmp("c d e", skiptokens("a \"b\" c d e", 2)));
176
177  FAIL_UNLESS("skiptokens 5",
178              !strcmp("c d e", skiptokens("a \"'\" c d e", 2)));
179
180#define CHECK_QUOTING(desc, unquoted, quoted)           \
181  do {                                                  \
182      int __argc;                                       \
183      char *__quoted = owl_arg_quote(unquoted);         \
184      char **__argv;                                    \
185      FAIL_UNLESS(desc, !strcmp(quoted, __quoted));     \
186      __argv = owl_parseline(__quoted, &__argc);        \
187      FAIL_UNLESS(desc " - arg count", __argc == 1);    \
188      FAIL_UNLESS(desc " - parsed",                     \
189                  !strcmp(__argv[0], unquoted));        \
190      owl_parse_delete(__argv, __argc);                 \
191      owl_free(__quoted);                               \
192    } while (0)
193
194  CHECK_QUOTING("boring text", "mango", "mango");
195  CHECK_QUOTING("spaces", "mangos are tasty", "'mangos are tasty'");
196  CHECK_QUOTING("single quotes", "mango's", "\"mango's\"");
197  CHECK_QUOTING("double quotes", "he said \"mangos are tasty\"",
198                "'he said \"mangos are tasty\"'");
199  CHECK_QUOTING("both quotes",
200                "he said \"mango's are tasty even when you put in "
201                "a random apostrophe\"",
202                "\"he said \"'\"'\"mango's are tasty even when you put in "
203                "a random apostrophe\"'\"'\"\"");
204  CHECK_QUOTING("quote monster", "'\"\"'\"'''\"",
205                "\""
206                "'"
207                "\"'\"'\""
208                "\"'\"'\""
209                "'"
210                "\"'\"'\""
211                "'"
212                "'"
213                "'"
214                "\"'\"'\""
215                "\"");
216
217  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
218  printf("# END testing owl_util (%d failures)\n", numfailed);
219  return(numfailed);
220}
221
222int owl_dict_regtest(void) {
223  owl_dict d;
224  owl_list l;
225  int numfailed=0;
226  char *av="aval", *bv="bval", *cv="cval", *dv="dval";
227
228  printf("# BEGIN testing owl_dict\n");
229  FAIL_UNLESS("create", 0==owl_dict_create(&d));
230  FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete));
231  FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete));
232  FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete));
233  FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete));
234  FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
235  FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
236  FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
237  FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
238  FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
239  FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
240  FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
241  FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
242
243  FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
244  FAIL_UNLESS("get_keys", 0==owl_dict_get_keys(&d, &l));
245  FAIL_UNLESS("get_keys result size", 3==owl_list_get_size(&l));
246 
247  /* these assume the returned keys are sorted */
248  FAIL_UNLESS("get_keys result val",0==strcmp("a",owl_list_get_element(&l,0)));
249  FAIL_UNLESS("get_keys result val",0==strcmp("b",owl_list_get_element(&l,1)));
250  FAIL_UNLESS("get_keys result val",0==strcmp("c",owl_list_get_element(&l,2)));
251
252  owl_list_cleanup(&l, owl_free);
253  owl_dict_cleanup(&d, NULL);
254
255  /*  if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
256  printf("# END testing owl_dict (%d failures)\n", numfailed);
257  return(numfailed);
258}
259
260int owl_variable_regtest(void) {
261  owl_vardict vd;
262  int numfailed=0;
263  char buf[1024];
264  const void *v;
265
266  printf("# BEGIN testing owl_variable\n");
267  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
268
269  FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"rxping"));
270  FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble"));
271  FAIL_UNLESS("get bool as string 1", 0==owl_variable_get_tostring(&vd,"rxping", buf, 1024));
272  FAIL_UNLESS("get bool as string 2", 0==strcmp(buf,"off"));
273  FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"rxping"));
274  FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"rxping"));
275  FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"rxping","off",0,0));
276  FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"rxping"));
277  FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"rxping","xxx",0,0));
278  FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"rxping"));
279
280
281  FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath")));
282  FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee"));
283  FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath")));
284
285  FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize"));
286  FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble"));
287  FAIL_UNLESS("get int as string 1", 0==owl_variable_get_tostring(&vd,"typewinsize", buf, 1024));
288  FAIL_UNLESS("get int as string 2", 0==strcmp(buf,"8"));
289  FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12));
290  FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize"));
291  FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3));
292  FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize"));
293  FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0,0));
294  FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize"));
295  FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0,0));
296  FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0,0));
297  FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize"));
298
299  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
300  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(&vd, "stringvar", OWL_VARIABLE_STRING)));
301  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(&vd, "stringvar")));
302  owl_variable_set_string(&vd, "stringvar", "new val");
303  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(&vd, "stringvar")));
304
305  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
306  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(&vd, "intvar", OWL_VARIABLE_INT)));
307  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(&vd, "intvar"));
308  owl_variable_set_int(&vd, "intvar", 17);
309  FAIL_UNLESS("update bool val", 17 == owl_variable_get_int(&vd, "intvar"));
310
311  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
312  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(&vd, "boolvar", OWL_VARIABLE_BOOL)));
313  FAIL_UNLESS("get new bool val", owl_variable_get_bool(&vd, "boolvar"));
314  owl_variable_set_bool_off(&vd, "boolvar");
315  FAIL_UNLESS("update string val", !owl_variable_get_bool(&vd, "boolvar"));
316
317  owl_variable_dict_cleanup(&vd);
318
319  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
320  printf("# END testing owl_variable (%d failures)\n", numfailed);
321  return(numfailed);
322}
323
324static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
325{
326  owl_filter *f;
327  int ok;
328  int failed = 0;
329  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
330    printf("not ok can't parse %s\n", filt);
331    failed = 1;
332    goto out;
333  }
334  ok = owl_filter_message_match(f, m);
335  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
336    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
337    failed = 1;
338  }
339 out:
340  owl_filter_delete(f);
341  if(!failed) {
342    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
343  }
344  return failed;
345}
346
347int owl_filter_regtest(void) {
348  int numfailed=0;
349  owl_message m;
350  owl_filter *f1, *f2, *f3, *f4, *f5;
351
352  owl_message_init(&m);
353  owl_message_set_type_zephyr(&m);
354  owl_message_set_direction_in(&m);
355  owl_message_set_class(&m, "owl");
356  owl_message_set_instance(&m, "tester");
357  owl_message_set_sender(&m, "owl-user");
358  owl_message_set_recipient(&m, "joe");
359  owl_message_set_attribute(&m, "foo", "bar");
360
361#define TEST_FILTER(f, e) do {                          \
362    numtests++;                                         \
363    numfailed += owl_filter_test_string(f, &m, e);      \
364      } while(0)
365
366  TEST_FILTER("true", 1);
367  TEST_FILTER("false", 0);
368  TEST_FILTER("( true )", 1);
369  TEST_FILTER("not false", 1);
370  TEST_FILTER("( true ) or ( false )", 1);
371  TEST_FILTER("true and false", 0);
372  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
373
374  TEST_FILTER("class owl", 1);
375  TEST_FILTER("class ^owl$", 1);
376  TEST_FILTER("instance test", 1);
377  TEST_FILTER("instance ^test$", 0);
378  TEST_FILTER("instance ^tester$", 1);
379
380  TEST_FILTER("foo bar", 1);
381  TEST_FILTER("class owl and instance tester", 1);
382  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
383
384  /* Order of operations and precedence */
385  TEST_FILTER("not true or false", 0);
386  TEST_FILTER("true or true and false", 0);
387  TEST_FILTER("true and true and false or true", 1);
388  TEST_FILTER("false and false or true", 1);
389  TEST_FILTER("true and false or false", 0);
390
391  f1 = owl_filter_new_fromstring("f1", "class owl");
392  owl_global_add_filter(&g, f1);
393  TEST_FILTER("filter f1", 1);
394  owl_global_remove_filter(&g, "f1");
395
396  /* Test recursion prevention */
397  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
398  owl_filter_delete(f2);
399
400  /* mutual recursion */
401  f3 = owl_filter_new_fromstring("f3", "filter f4");
402  owl_global_add_filter(&g, f3);
403  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
404  owl_global_remove_filter(&g, "f3");
405  owl_filter_delete(f4);
406
407  /* support referencing a filter several times */
408  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
409  owl_filter_delete(f5);
410
411  return 0;
412}
413
414int owl_editwin_regtest(void) {
415  int numfailed = 0;
416  const char *p;
417  owl_editwin *oe;
418  const char *autowrap_string = "we feel our owls should live "
419                                "closer to our ponies.";
420
421  printf("# BEGIN testing owl_editwin\n");
422
423  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
424
425  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
426
427  /* check paragraph fill */
428  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
429  owl_editwin_move_to_top(oe);
430  owl_editwin_fill_paragraph(oe);
431  p = owl_editwin_get_text(oe);
432  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
433                                                            "blah blah blah.\n"
434                                                            "\n"
435                                                            "blah"));
436
437  owl_editwin_unref(oe); oe = NULL;
438  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
439
440  /* check that lines ending with ". " correctly fill */
441  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
442  owl_editwin_move_to_top(oe);
443  owl_editwin_fill_paragraph(oe);
444  p = owl_editwin_get_text(oe);
445  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
446                                                            "blah blah blah. \n"
447                                                            "\n"
448                                                            "blah"));
449
450  owl_editwin_unref(oe); oe = NULL;
451
452  /* Test owl_editwin_move_to_beginning_of_line. */
453  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
454  owl_editwin_insert_string(oe, "\n");
455  owl_editwin_insert_string(oe, "12345678\n");
456  owl_editwin_insert_string(oe, "\n");
457  owl_editwin_insert_string(oe, "abcdefg\n");
458  owl_editwin_move_to_top(oe);
459  FAIL_UNLESS("already at beginning of line",
460              owl_editwin_move_to_beginning_of_line(oe) == 0);
461  owl_editwin_line_move(oe, 1);
462  owl_editwin_point_move(oe, 5);
463  FAIL_UNLESS("find beginning of line after empty first line",
464              owl_editwin_move_to_beginning_of_line(oe) == -5);
465  owl_editwin_line_move(oe, 1);
466  FAIL_UNLESS("find beginning empty middle line",
467              owl_editwin_move_to_beginning_of_line(oe) == 0);
468  owl_editwin_line_move(oe, 1);
469  owl_editwin_point_move(oe, 2);
470  FAIL_UNLESS("find beginning of line after empty middle line",
471              owl_editwin_move_to_beginning_of_line(oe) == -2);
472  owl_editwin_unref(oe); oe = NULL;
473
474  /* Test automatic line-wrapping. */
475  owl_global_set_edit_maxwrapcols(&g, 10);
476  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
477  for (p = autowrap_string; *p; p++) {
478    owl_input j;
479    j.ch = *p;
480    j.uch = *p; /* Assuming ASCII. */
481    owl_editwin_process_char(oe, j);
482  }
483  p = owl_editwin_get_text(oe);
484  FAIL_UNLESS("text was automatically wrapped",
485              p && !strcmp(p, "we feel\n"
486                           "our owls\n"
487                           "should\n"
488                           "live\n"
489                           "closer to\n"
490                           "our\n"
491                           "ponies."));
492  owl_editwin_unref(oe); oe = NULL;
493  owl_global_set_edit_maxwrapcols(&g, 70);
494
495  /* Test owl_editwin_current_column. */
496  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
497  FAIL_UNLESS("initial column zero", owl_editwin_current_column(oe) == 0);
498  owl_editwin_insert_string(oe, "abcdef");
499  FAIL_UNLESS("simple insert", owl_editwin_current_column(oe) == 6);
500  owl_editwin_insert_string(oe, "\t");
501  FAIL_UNLESS("insert tabs", owl_editwin_current_column(oe) == 8);
502  owl_editwin_insert_string(oe, "123\n12\t3");
503  FAIL_UNLESS("newline with junk", owl_editwin_current_column(oe) == 9);
504  owl_editwin_move_to_beginning_of_line(oe);
505  FAIL_UNLESS("beginning of line", owl_editwin_current_column(oe) == 0);
506  owl_editwin_unref(oe); oe = NULL;
507
508  printf("# END testing owl_editwin (%d failures)\n", numfailed);
509
510  return numfailed;
511}
512
513int owl_fmtext_regtest(void) {
514  int numfailed = 0;
515  int start, end;
516  owl_fmtext fm1;
517  owl_fmtext fm2;
518  owl_regex re;
519  char *str;
520
521  printf("# BEGIN testing owl_fmtext\n");
522
523  owl_fmtext_init_null(&fm1);
524  owl_fmtext_init_null(&fm2);
525
526  /* Verify text gets correctly appended. */
527  owl_fmtext_append_normal(&fm1, "1234567898");
528  owl_fmtext_append_fmtext(&fm2, &fm1);
529  FAIL_UNLESS("string lengths correct",
530              owl_fmtext_num_bytes(&fm2) == strlen(owl_fmtext_get_text(&fm2)));
531
532  /* Test owl_fmtext_num_lines. */
533  owl_fmtext_clear(&fm1);
534  FAIL_UNLESS("empty line correct", owl_fmtext_num_lines(&fm1) == 0);
535  owl_fmtext_append_normal(&fm1, "12345\n67898");
536  FAIL_UNLESS("trailing chars correct", owl_fmtext_num_lines(&fm1) == 2);
537  owl_fmtext_append_normal(&fm1, "\n");
538  FAIL_UNLESS("trailing newline correct", owl_fmtext_num_lines(&fm1) == 2);
539  owl_fmtext_append_bold(&fm1, "");
540  FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2);
541
542  /* Test owl_fmtext_truncate_lines */
543  owl_fmtext_clear(&fm1);
544  owl_fmtext_append_normal(&fm1, "0\n1\n2\n3\n4\n");
545
546  owl_fmtext_clear(&fm2);
547  owl_fmtext_truncate_lines(&fm1, 1, 3, &fm2);
548  str = owl_fmtext_print_plain(&fm2);
549  FAIL_UNLESS("lines corrected truncated",
550              str && !strcmp(str, "1\n2\n3\n"));
551  owl_free(str);
552
553  owl_fmtext_clear(&fm2);
554  owl_fmtext_truncate_lines(&fm1, 1, 5, &fm2);
555  str = owl_fmtext_print_plain(&fm2);
556  FAIL_UNLESS("lines corrected truncated",
557              str && !strcmp(str, "1\n2\n3\n4\n"));
558  owl_free(str);
559
560  /* Test owl_fmtext_truncate_cols. */
561  owl_fmtext_clear(&fm1);
562  owl_fmtext_append_normal(&fm1, "123456789012345\n");
563  owl_fmtext_append_normal(&fm1, "123456789\n");
564  owl_fmtext_append_normal(&fm1, "1234567890\n");
565
566  owl_fmtext_clear(&fm2);
567  owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2);
568  str = owl_fmtext_print_plain(&fm2);
569  FAIL_UNLESS("columns correctly truncated",
570              str && !strcmp(str, "567890"
571                                  "56789\n"
572                                  "567890"));
573  owl_free(str);
574
575  owl_fmtext_clear(&fm1);
576  owl_fmtext_append_normal(&fm1, "12\t1234");
577  owl_fmtext_append_bold(&fm1, "56\n");
578  owl_fmtext_append_bold(&fm1, "12345678\t\n");
579
580  owl_fmtext_clear(&fm2);
581  owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2);
582  str = owl_fmtext_print_plain(&fm2);
583  FAIL_UNLESS("columns correctly truncated",
584              str && !strcmp(str, "    123456"
585                                  "5678      "));
586  owl_free(str);
587
588  /* Test owl_fmtext_expand_tabs. */
589  owl_fmtext_clear(&fm1);
590  owl_fmtext_append_normal(&fm1, "12\t1234");
591  owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
592  owl_fmtext_clear(&fm2);
593  owl_fmtext_expand_tabs(&fm1, &fm2, 0);
594  str = owl_fmtext_print_plain(&fm2);
595  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
596  FAIL_UNLESS("tabs corrected expanded",
597              str && !strcmp(str, "12      1234567 1\n"
598                                  "12345678        1"));
599  owl_free(str);
600
601  owl_fmtext_clear(&fm2);
602  owl_fmtext_expand_tabs(&fm1, &fm2, 1);
603  str = owl_fmtext_print_plain(&fm2);
604  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
605  FAIL_UNLESS("tabs corrected expanded",
606              str && !strcmp(str, "12     1234567 1\n"
607                                  "12345678       1"));
608  owl_free(str);
609
610  /* Test owl_fmtext_search. */
611  owl_fmtext_clear(&fm1);
612  owl_fmtext_append_normal(&fm1, "123123123123");
613  owl_regex_create(&re, "12");
614  {
615    int count = 0, offset;
616    offset = owl_fmtext_search(&fm1, &re, 0);
617    while (offset >= 0) {
618      FAIL_UNLESS("search matches",
619                  !strncmp("12", owl_fmtext_get_text(&fm1) + offset, 2));
620      count++;
621      offset = owl_fmtext_search(&fm1, &re, offset+1);
622    }
623    FAIL_UNLESS("exactly four matches", count == 4);
624  }
625  owl_regex_cleanup(&re);
626
627  /* Test owl_fmtext_line_number. */
628  owl_fmtext_clear(&fm1);
629  owl_fmtext_append_normal(&fm1, "123\n456\n");
630  owl_fmtext_append_bold(&fm1, "");
631  FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0));
632  FAIL_UNLESS("trailing formatting characters part of false line",
633              2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1)));
634  owl_regex_create_quoted(&re, "456");
635  FAIL_UNLESS("correctly find second line (line 1)",
636              1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0)));
637  owl_regex_cleanup(&re);
638
639  /* Test owl_fmtext_line_extents. */
640  owl_fmtext_clear(&fm1);
641  owl_fmtext_append_normal(&fm1, "123\n456\n789");
642  owl_fmtext_line_extents(&fm1, 1, &start, &end);
643  FAIL_UNLESS("line contents",
644              !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start));
645  owl_fmtext_line_extents(&fm1, 2, &start, &end);
646  FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1));
647
648  owl_fmtext_cleanup(&fm1);
649  owl_fmtext_cleanup(&fm2);
650
651  printf("# END testing owl_fmtext (%d failures)\n", numfailed);
652
653  return numfailed;
654}
655
656static int owl_smartfilter_test_equals(const char *filtname, const char *expected) {
657  owl_filter *f = NULL;
658  char *filtstr = NULL;
659  int failed = 0;
660  f = owl_global_get_filter(&g, filtname);
661  if (f == NULL) {
662    printf("not ok filter missing: %s\n", filtname);
663    failed = 1;
664    goto out;
665  }
666
667  /* TODO: Come up with a better way to test this. */
668  filtstr = owl_filter_print(f);
669  if (strcmp(expected, filtstr)) {
670    printf("not ok filter incorrect: |%s| instead of |%s|\n",
671           filtstr, expected);
672    failed = 1;
673    goto out;
674  }
675
676 out:
677  owl_free(filtstr);
678  return failed;
679}
680
681static int owl_classinstfilt_test(const char *c, const char *i, int related, const char *expected) {
682  char *filtname = NULL;
683  int failed = 0;
684
685  filtname = owl_function_classinstfilt(c, i, related);
686  if (filtname == NULL) {
687    printf("not ok null filtname: %s %s %s\n", c, i ? i : "(null)",
688           related ? "related" : "not related");
689    failed = 1;
690    goto out;
691  }
692  if (owl_smartfilter_test_equals(filtname, expected)) {
693    failed = 1;
694    goto out;
695  }
696 out:
697  if (!failed) {
698    printf("ok %s\n", filtname);
699  }
700  if (filtname)
701    owl_global_remove_filter(&g, filtname);
702  owl_free(filtname);
703  return failed;
704}
705
706int owl_smartfilter_regtest(void) {
707  int numfailed = 0;
708
709  printf("# BEGIN testing owl_smartfilter\n");
710
711  /* Check classinst making. */
712
713#define TEST_CLASSINSTFILT(c, i, r, e) do {             \
714    numtests++;                                         \
715    numfailed += owl_classinstfilt_test(c, i, r, e);    \
716  } while (0)
717  TEST_CLASSINSTFILT("message", NULL, false,
718                     "class ^message$\n");
719  TEST_CLASSINSTFILT("message", NULL, true,
720                     "class ^(un)*message(\\.d)*$\n");
721  TEST_CLASSINSTFILT("message", "personal", false,
722                     "class ^message$ and instance ^personal$\n");
723  TEST_CLASSINSTFILT("message", "personal", true,
724                     "class ^(un)*message(\\.d)*$ and ( instance ^(un)*personal(\\.d)*$ )\n");
725
726  TEST_CLASSINSTFILT("message", "evil\tinstance", false,
727                     "class ^message$ and instance '^evil\tinstance$'\n");
728  TEST_CLASSINSTFILT("message", "evil instance", false,
729                     "class ^message$ and instance '^evil instance$'\n");
730  TEST_CLASSINSTFILT("message", "evil'instance", false,
731                     "class ^message$ and instance \"^evil'instance$\"\n");
732  TEST_CLASSINSTFILT("message", "evil\"instance", false,
733                     "class ^message$ and instance '^evil\"instance$'\n");
734  TEST_CLASSINSTFILT("message", "evil$instance", false,
735                     "class ^message$ and instance ^evil\\$instance$\n");
736
737
738  printf("# END testing owl_smartfilter (%d failures)\n", numfailed);
739
740  return numfailed;
741}
Note: See TracBrowser for help on using the repository browser.