source: tester.c @ 478dc8e

release-1.10
Last change on this file since 478dc8e was 6a8b519, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
Remove pointless return code from owl_variable_add_defaults
  • Property mode set to 100644
File size: 35.6 KB
Line 
1#define OWL_PERL
2#define WINDOW FAKE_WINDOW
3#include "owl.h"
4#undef WINDOW
5#include "filterproc.h"
6
7#include <stdio.h>
8
9#undef instr
10#include <ncursesw/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);
25int owl_history_regtest(void);
26int call_filter_regtest(void);
27
28extern void owl_perl_xs_init(pTHX);
29
30int main(int argc, char **argv, char **env)
31{
32  FILE *rnull;
33  FILE *wnull;
34  char *perlerr;
35  int status = 0;
36  SCREEN *screen;
37
38  if (argc <= 1) {
39    fprintf(stderr, "Usage: %s --builtin|TEST.t|-le CODE\n", argv[0]);
40    return 1;
41  }
42
43  /* initialize a fake ncurses, detached from std{in,out} */
44  wnull = fopen("/dev/null", "w");
45  rnull = fopen("/dev/null", "r");
46  screen = newterm("xterm", wnull, rnull);
47  /* initialize global structures */
48  owl_global_init(&g);
49
50  perlerr = owl_perlconfig_initperl(NULL, &argc, &argv, &env);
51  if (perlerr) {
52    endwin();
53    fprintf(stderr, "Internal perl error: %s\n", perlerr);
54    status = 1;
55    goto out;
56  }
57
58  owl_global_complete_setup(&g);
59  owl_global_setup_default_filters(&g);
60
61  owl_view_create(owl_global_get_current_view(&g), "main",
62                  owl_global_get_filter(&g, "all"),
63                  owl_global_get_style_by_name(&g, "default"));
64
65  owl_function_firstmsg();
66
67  ENTER;
68  SAVETMPS;
69
70  if (strcmp(argv[1], "--builtin") == 0) {
71    status = owl_regtest();
72  } else if (strcmp(argv[1], "-le") == 0 && argc > 2) {
73    /*
74     * 'prove' runs its harness perl with '-le CODE' to get some
75     * information out.
76     */
77    moreswitches("l");
78    eval_pv(argv[2], true);
79  } else {
80    sv_setpv(get_sv("0", false), argv[1]);
81    sv_setpv(get_sv("main::test_prog", TRUE), argv[1]);
82
83    eval_pv("do $main::test_prog; die($@) if($@)", true);
84  }
85
86  status = 0;
87
88  FREETMPS;
89  LEAVE;
90
91 out:
92  perl_destruct(owl_global_get_perlinterp(&g));
93  perl_free(owl_global_get_perlinterp(&g));
94  /* probably not necessary, but tear down the screen */
95  endwin();
96  delscreen(screen);
97  fclose(rnull);
98  fclose(wnull);
99  return status;
100}
101
102int owl_regtest(void) {
103  numtests = 0;
104  int numfailures=0;
105  /*
106    printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS
107    +OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS);
108  */
109  numfailures += owl_util_regtest();
110  numfailures += owl_dict_regtest();
111  numfailures += owl_variable_regtest();
112  numfailures += owl_filter_regtest();
113  numfailures += owl_editwin_regtest();
114  numfailures += owl_fmtext_regtest();
115  numfailures += owl_smartfilter_regtest();
116  numfailures += owl_history_regtest();
117  numfailures += call_filter_regtest();
118  if (numfailures) {
119      fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
120  }
121  printf("1..%d\n", numtests);
122
123  return(numfailures);
124}
125
126#define FAIL_UNLESS(desc,pred) do { int __pred = (pred);                \
127    numtests++;                                                         \
128    printf("%s %d %s", (__pred) ? "ok" : (numfailed++, "not ok"), numtests, desc); \
129    if(!(__pred)) printf("\t(%s:%d)", __FILE__, __LINE__); printf("%c", '\n'); } while(0)
130
131
132int owl_util_regtest(void)
133{
134  int numfailed=0;
135  char *path, *home;
136
137  printf("# BEGIN testing owl_util\n");
138
139#define CHECK_STR_AND_FREE(desc, expected, expr)         \
140    do {                                                 \
141      char *__value = (expr);                            \
142      FAIL_UNLESS((desc), !strcmp((expected), __value)); \
143      g_free(__value);                                 \
144    } while (0)
145
146  CHECK_STR_AND_FREE("owl_text_substitute 2", "fYZYZ",
147                     owl_text_substitute("foo", "o", "YZ"));
148  CHECK_STR_AND_FREE("owl_text_substitute 3", "foo",
149                     owl_text_substitute("fYZYZ", "YZ", "o"));
150  CHECK_STR_AND_FREE("owl_text_substitute 4", "/u/foo/meep",
151                     owl_text_substitute("~/meep", "~", "/u/foo"));
152
153  FAIL_UNLESS("skiptokens 1", 
154              !strcmp("bar quux", skiptokens("foo bar quux", 1)));
155  FAIL_UNLESS("skiptokens 2", 
156              !strcmp("meep", skiptokens("foo 'bar quux' meep", 2)));
157
158  CHECK_STR_AND_FREE("expand_tabs 1", "        hi", owl_text_expand_tabs("\thi"));
159
160  CHECK_STR_AND_FREE("expand_tabs 2", "        hi\nword    tab",
161                     owl_text_expand_tabs("\thi\nword\ttab"));
162
163  CHECK_STR_AND_FREE("expand_tabs 3", "                2 tabs",
164                     owl_text_expand_tabs("\t\t2 tabs"));
165  CHECK_STR_AND_FREE("expand_tabs 4", "α       ααααααα!        ",
166                     owl_text_expand_tabs(\tααααααα!\t"));
167  CHECK_STR_AND_FREE("expand_tabs 5", "A      AAA!!        ",
168                     owl_text_expand_tabs("A\tAAA!!\t"));
169
170  FAIL_UNLESS("skiptokens 1",
171              !strcmp("world", skiptokens("hello world", 1)));
172
173  FAIL_UNLESS("skiptokens 2",
174              !strcmp("c d e", skiptokens("a   b c d e", 2)));
175
176  FAIL_UNLESS("skiptokens 3",
177              !strcmp("\"b\" c d e", skiptokens("a \"b\" c d e", 1)));
178
179  FAIL_UNLESS("skiptokens 4",
180              !strcmp("c d e", skiptokens("a \"b\" c d e", 2)));
181
182  FAIL_UNLESS("skiptokens 5",
183              !strcmp("c d e", skiptokens("a \"'\" c d e", 2)));
184
185#define CHECK_QUOTING(desc, unquoted, quoted)           \
186  do {                                                  \
187      int __argc;                                       \
188      char *__quoted = owl_arg_quote(unquoted);         \
189      char **__argv;                                    \
190      FAIL_UNLESS(desc, !strcmp(quoted, __quoted));     \
191      __argv = owl_parseline(__quoted, &__argc);        \
192      FAIL_UNLESS(desc " - arg count", __argc == 1);    \
193      FAIL_UNLESS(desc " - null-terminated",            \
194                  __argv[__argc] == NULL);              \
195      FAIL_UNLESS(desc " - parsed",                     \
196                  !strcmp(__argv[0], unquoted));        \
197      g_strfreev(__argv);                               \
198      g_free(__quoted);                         \
199    } while (0)
200
201  CHECK_QUOTING("boring text", "mango", "mango");
202  CHECK_QUOTING("spaces", "mangos are tasty", "'mangos are tasty'");
203  CHECK_QUOTING("single quotes", "mango's", "\"mango's\"");
204  CHECK_QUOTING("double quotes", "he said \"mangos are tasty\"",
205                "'he said \"mangos are tasty\"'");
206  CHECK_QUOTING("both quotes",
207                "he said \"mango's are tasty even when you put in "
208                "a random apostrophe\"",
209                "\"he said \"'\"'\"mango's are tasty even when you put in "
210                "a random apostrophe\"'\"'\"\"");
211  CHECK_QUOTING("quote monster", "'\"\"'\"'''\"",
212                "\""
213                "'"
214                "\"'\"'\""
215                "\"'\"'\""
216                "'"
217                "\"'\"'\""
218                "'"
219                "'"
220                "'"
221                "\"'\"'\""
222                "\"");
223
224  GString *quoted = g_string_new("");
225  owl_string_appendf_quoted(quoted, "%q foo %q%q %s %", "hello", "world is", "can't");
226  FAIL_UNLESS("owl_string_appendf",
227              !strcmp(quoted->str, "hello foo 'world is'\"can't\" %s %"));
228  g_string_free(quoted, true);
229
230
231  CHECK_STR_AND_FREE("baseclass barnowl",
232                     "barnowl", owl_util_baseclass("barnowl"));
233  CHECK_STR_AND_FREE("baseclass unbarnowl",
234                     "barnowl", owl_util_baseclass("unbarnowl"));
235  CHECK_STR_AND_FREE("baseclass unununbarnowl.d.d",
236                     "barnowl", owl_util_baseclass("unununbarnowl.d.d"));
237  CHECK_STR_AND_FREE("baseclass ununun.d.d",
238                     "", owl_util_baseclass("ununun.d.d"));
239  CHECK_STR_AND_FREE("baseclass d.d.d.d",
240                     "d", owl_util_baseclass("d.d.d.d"));
241  CHECK_STR_AND_FREE("baseclass n.d.d.d",
242                     "n", owl_util_baseclass("n.d.d.d"));
243  CHECK_STR_AND_FREE("baseclass ununun.",
244                     ".", owl_util_baseclass("ununun."));
245  CHECK_STR_AND_FREE("baseclass unununu",
246                     "u", owl_util_baseclass("unununu"));
247
248
249  CHECK_STR_AND_FREE("makepath foo/bar",
250                     "foo/bar", owl_util_makepath("foo/bar"));
251  CHECK_STR_AND_FREE("makepath //foo///bar",
252                     "/foo/bar", owl_util_makepath("//foo///bar"));
253  CHECK_STR_AND_FREE("makepath foo/~//bar/",
254                     "foo/~/bar/", owl_util_makepath("foo/~//bar/"));
255  CHECK_STR_AND_FREE("makepath ~thisuserhadreallybetternotexist/foobar/",
256                     "~thisuserhadreallybetternotexist/foobar/",
257                     owl_util_makepath("~thisuserhadreallybetternotexist/foobar/"));
258
259  home = g_strdup(owl_global_get_homedir(&g));
260  CHECK_STR_AND_FREE("makepath ~",
261                     home, owl_util_makepath("~"));
262
263  path = g_build_filename(home, "foo/bar/baz", NULL);
264  CHECK_STR_AND_FREE("makepath ~///foo/bar//baz",
265                     path, owl_util_makepath("~///foo/bar//baz"));
266  g_free(path);
267  g_free(home);
268
269  home = owl_util_homedir_for_user("root");
270  if (home == NULL) {
271    /* Just make some noise so we notice. */
272    home = g_strdup("<WHAT>");
273    fprintf(stderr, "owl_util_homedir_for_user failed");
274  }
275
276  CHECK_STR_AND_FREE("makepath ~root",
277                     home, owl_util_makepath("~root"));
278
279  path = g_build_filename(home, "foo/bar/baz", NULL);
280  CHECK_STR_AND_FREE("makepath ~root///foo/bar//baz",
281                     path, owl_util_makepath("~root///foo/bar//baz"));
282  g_free(path);
283  g_free(home);
284
285  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
286  printf("# END testing owl_util (%d failures)\n", numfailed);
287  return(numfailed);
288}
289
290int owl_dict_regtest(void) {
291  owl_dict d;
292  GPtrArray *l;
293  int numfailed=0;
294  char *av = g_strdup("aval"), *bv = g_strdup("bval"), *cv = g_strdup("cval"),
295    *dv = g_strdup("dval");
296
297  printf("# BEGIN testing owl_dict\n");
298  owl_dict_create(&d);
299  FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete));
300  FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete));
301  FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete));
302  FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete));
303  FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
304  FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
305  FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
306  FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
307  FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
308  FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
309  FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
310  FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
311
312  FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
313  l = owl_dict_get_keys(&d);
314  FAIL_UNLESS("get_keys result size", 3 == l->len);
315 
316  /* these assume the returned keys are sorted */
317  FAIL_UNLESS("get_keys result val", 0 == strcmp("a", l->pdata[0]));
318  FAIL_UNLESS("get_keys result val", 0 == strcmp("b", l->pdata[1]));
319  FAIL_UNLESS("get_keys result val", 0 == strcmp("c", l->pdata[2]));
320
321  owl_ptr_array_free(l, g_free);
322  owl_dict_cleanup(&d, NULL);
323
324  g_free(av);
325  g_free(bv);
326  g_free(cv);
327  g_free(dv);
328
329  /*  if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
330  printf("# END testing owl_dict (%d failures)\n", numfailed);
331  return(numfailed);
332}
333
334int owl_variable_regtest(void) {
335  owl_vardict vd;
336  owl_variable *var;
337  int numfailed=0;
338  char *value;
339
340  printf("# BEGIN testing owl_variable\n");
341  owl_variable_dict_setup(&vd);
342
343  FAIL_UNLESS("get bool var", NULL != (var = owl_variable_get_var(&vd, "rxping")));
344  FAIL_UNLESS("get bool", 0 == owl_variable_get_bool(var));
345  FAIL_UNLESS("get bool (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
346  FAIL_UNLESS("get bool as string",
347              !strcmp((value = owl_variable_get_tostring(var)), "off"));
348  g_free(value);
349  FAIL_UNLESS("set bool 1", 0 == owl_variable_set_bool_on(var));
350  FAIL_UNLESS("get bool 2", 1 == owl_variable_get_bool(var));
351  FAIL_UNLESS("set bool 3", 0 == owl_variable_set_fromstring(var, "off", 0));
352  FAIL_UNLESS("get bool 4", 0 == owl_variable_get_bool(var));
353  FAIL_UNLESS("set bool 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
354  FAIL_UNLESS("get bool 6", 0 == owl_variable_get_bool(var));
355
356
357  FAIL_UNLESS("get string var", NULL != (var = owl_variable_get_var(&vd, "logpath")));
358  FAIL_UNLESS("get string", 0 == strcmp("~/zlog/people", owl_variable_get_string(var)));
359  FAIL_UNLESS("set string 7", 0 == owl_variable_set_string(var, "whee"));
360  FAIL_UNLESS("get string", !strcmp("whee", owl_variable_get_string(var)));
361
362  FAIL_UNLESS("get int var", NULL != (var = owl_variable_get_var(&vd, "typewinsize")));
363  FAIL_UNLESS("get int", 8 == owl_variable_get_int(var));
364  FAIL_UNLESS("get int (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
365  FAIL_UNLESS("get int as string",
366              !strcmp((value = owl_variable_get_tostring(var)), "8"));
367  g_free(value);
368  FAIL_UNLESS("set int 1", 0 == owl_variable_set_int(var, 12));
369  FAIL_UNLESS("get int 2", 12 == owl_variable_get_int(var));
370  FAIL_UNLESS("set int 1b", -1 == owl_variable_set_int(var, -3));
371  FAIL_UNLESS("get int 2b", 12 == owl_variable_get_int(var));
372  FAIL_UNLESS("set int 3", 0 == owl_variable_set_fromstring(var, "9", 0));
373  FAIL_UNLESS("get int 4", 9 == owl_variable_get_int(var));
374  FAIL_UNLESS("set int 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
375  FAIL_UNLESS("set int 6", -1 == owl_variable_set_fromstring(var, "", 0));
376  FAIL_UNLESS("get int 7", 9 == owl_variable_get_int(var));
377
378  FAIL_UNLESS("get enum var", NULL != (var = owl_variable_get_var(&vd, "scrollmode")));
379  FAIL_UNLESS("get enum", OWL_SCROLLMODE_NORMAL == owl_variable_get_int(var));
380  FAIL_UNLESS("get enum as string",
381              !strcmp((value = owl_variable_get_tostring(var)), "normal"));
382  g_free(value);
383  FAIL_UNLESS("set enum 1", 0 == owl_variable_set_int(var, OWL_SCROLLMODE_TOP));
384  FAIL_UNLESS("get enum 1", OWL_SCROLLMODE_TOP == owl_variable_get_int(var));
385  FAIL_UNLESS("set enum 2a", -1 == owl_variable_set_int(var, -1));
386  FAIL_UNLESS("set enum 2b", -1 == owl_variable_set_int(var, OWL_SCROLLMODE_PAGEDCENTER + 1));
387  FAIL_UNLESS("get enum 2", OWL_SCROLLMODE_TOP == owl_variable_get_int(var));
388  FAIL_UNLESS("set enum 3", 0 == owl_variable_set_fromstring(var, "center", 0));
389  FAIL_UNLESS("get enum 4", OWL_SCROLLMODE_CENTER == owl_variable_get_int(var));
390  FAIL_UNLESS("set enum 5", -1 == owl_variable_set_fromstring(var, "bogus", 0));
391  FAIL_UNLESS("set enum 6", -1 == owl_variable_set_fromstring(var, "", 0));
392  FAIL_UNLESS("get enum 7", OWL_SCROLLMODE_CENTER == owl_variable_get_int(var));
393
394  owl_variable_dict_newvar_string(&vd, "stringvar", "testval", "", "");
395  FAIL_UNLESS("get new string var", NULL != (var = owl_variable_get_var(&vd, "stringvar")));
396  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(var)));
397  owl_variable_set_string(var, "new val");
398  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(var)));
399
400  owl_variable_dict_newvar_int(&vd, "intvar", 47, "", "");
401  FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar")));
402  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var));
403  owl_variable_set_int(var, 17);
404  FAIL_UNLESS("update int val", 17 == owl_variable_get_int(var));
405
406  owl_variable_dict_newvar_bool(&vd, "boolvar", true, "", "");
407  FAIL_UNLESS("get new bool var", NULL != (var = owl_variable_get_var(&vd, "boolvar")));
408  FAIL_UNLESS("get new bool val", owl_variable_get_bool(var));
409  owl_variable_set_bool_off(var);
410  FAIL_UNLESS("update bool val", !owl_variable_get_bool(var));
411
412  owl_variable_dict_newvar_string(&vd, "nullstringvar", NULL, "", "");
413  FAIL_UNLESS("get new string (NULL) var", NULL != (var = owl_variable_get_var(&vd, "nullstringvar")));
414  FAIL_UNLESS("get string (NULL)", NULL == (value = owl_variable_get_tostring(var)));
415  g_free(value);
416  var = owl_variable_get_var(&vd, "zsigproc");
417  FAIL_UNLESS("get string (NULL) 2", NULL == (value = owl_variable_get_tostring(var)));
418  g_free(value);
419
420  owl_variable_dict_newvar_enum(&vd, "enumvar", 0, "", "", "a,b,c,d");
421  FAIL_UNLESS("get new enum var", NULL != (var = owl_variable_get_var(&vd, "enumvar")));
422  FAIL_UNLESS("get new enum val", 0 == owl_variable_get_int(var));
423  owl_variable_set_fromstring(var, "c", 0);
424  FAIL_UNLESS("update enum val", 2 == owl_variable_get_int(var));
425
426  owl_variable_dict_cleanup(&vd);
427
428  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
429  printf("# END testing owl_variable (%d failures)\n", numfailed);
430  return(numfailed);
431}
432
433static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
434{
435  owl_filter *f;
436  int ok;
437  int failed = 0;
438  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
439    printf("not ok can't parse %s\n", filt);
440    failed = 1;
441    goto out;
442  }
443  ok = owl_filter_message_match(f, m);
444  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
445    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
446    failed = 1;
447  }
448 out:
449  owl_filter_delete(f);
450  if(!failed) {
451    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
452  }
453  return failed;
454}
455
456int owl_filter_regtest(void) {
457  int numfailed=0;
458  owl_message m;
459  owl_filter *f1, *f2, *f3, *f4, *f5;
460
461  owl_message_init(&m);
462  owl_message_set_type_zephyr(&m);
463  owl_message_set_direction_in(&m);
464  owl_message_set_class(&m, "owl");
465  owl_message_set_instance(&m, "tester");
466  owl_message_set_sender(&m, "owl-user");
467  owl_message_set_recipient(&m, "joe");
468  owl_message_set_attribute(&m, "foo", "bar");
469
470#define TEST_FILTER(f, e) do {                          \
471    numtests++;                                         \
472    numfailed += owl_filter_test_string(f, &m, e);      \
473      } while(0)
474
475  TEST_FILTER("true", 1);
476  TEST_FILTER("false", 0);
477  TEST_FILTER("( true )", 1);
478  TEST_FILTER("not false", 1);
479  TEST_FILTER("( true ) or ( false )", 1);
480  TEST_FILTER("true and false", 0);
481  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
482
483  TEST_FILTER("class owl", 1);
484  TEST_FILTER("class ^owl$", 1);
485  TEST_FILTER("instance test", 1);
486  TEST_FILTER("instance ^test$", 0);
487  TEST_FILTER("instance ^tester$", 1);
488
489  TEST_FILTER("foo bar", 1);
490  TEST_FILTER("class owl and instance tester", 1);
491  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
492
493  /* Order of operations and precedence */
494  TEST_FILTER("not true or false", 0);
495  TEST_FILTER("true or true and false", 0);
496  TEST_FILTER("true and true and false or true", 1);
497  TEST_FILTER("false and false or true", 1);
498  TEST_FILTER("true and false or false", 0);
499
500  f1 = owl_filter_new_fromstring("f1", "class owl");
501  owl_global_add_filter(&g, f1);
502  TEST_FILTER("filter f1", 1);
503  owl_global_remove_filter(&g, "f1");
504
505  /* Test recursion prevention */
506  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
507  owl_filter_delete(f2);
508
509  /* mutual recursion */
510  f3 = owl_filter_new_fromstring("f3", "filter f4");
511  owl_global_add_filter(&g, f3);
512  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
513  owl_global_remove_filter(&g, "f3");
514  owl_filter_delete(f4);
515
516  /* support referencing a filter several times */
517  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
518  owl_filter_delete(f5);
519
520  owl_message_cleanup(&m);
521
522  return 0;
523}
524
525int owl_editwin_regtest(void) {
526  int numfailed = 0;
527  const char *p;
528  owl_editwin *oe;
529  const char *autowrap_string = "we feel our owls should live "
530                                "closer to our ponies.";
531
532  printf("# BEGIN testing owl_editwin\n");
533
534  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
535
536  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
537
538  /* check paragraph fill */
539  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
540  owl_editwin_move_to_top(oe);
541  owl_editwin_fill_paragraph(oe);
542  p = owl_editwin_get_text(oe);
543  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
544                                                            "blah blah blah.\n"
545                                                            "\n"
546                                                            "blah"));
547
548  owl_editwin_unref(oe); oe = NULL;
549  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
550
551  /* check that lines ending with ". " correctly fill */
552  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
553  owl_editwin_move_to_top(oe);
554  owl_editwin_fill_paragraph(oe);
555  p = owl_editwin_get_text(oe);
556  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
557                                                            "blah blah blah. \n"
558                                                            "\n"
559                                                            "blah"));
560
561  owl_editwin_unref(oe); oe = NULL;
562
563  /* Test owl_editwin_move_to_beginning_of_line. */
564  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
565  owl_editwin_insert_string(oe, "\n");
566  owl_editwin_insert_string(oe, "12345678\n");
567  owl_editwin_insert_string(oe, "\n");
568  owl_editwin_insert_string(oe, "abcdefg\n");
569  owl_editwin_move_to_top(oe);
570  FAIL_UNLESS("already at beginning of line",
571              owl_editwin_move_to_beginning_of_line(oe) == 0);
572  owl_editwin_line_move(oe, 1);
573  owl_editwin_point_move(oe, 5);
574  FAIL_UNLESS("find beginning of line after empty first line",
575              owl_editwin_move_to_beginning_of_line(oe) == -5);
576  owl_editwin_line_move(oe, 1);
577  FAIL_UNLESS("find beginning empty middle line",
578              owl_editwin_move_to_beginning_of_line(oe) == 0);
579  owl_editwin_line_move(oe, 1);
580  owl_editwin_point_move(oe, 2);
581  FAIL_UNLESS("find beginning of line after empty middle line",
582              owl_editwin_move_to_beginning_of_line(oe) == -2);
583  owl_editwin_unref(oe); oe = NULL;
584
585  /* Test automatic line-wrapping. */
586  owl_global_set_edit_maxwrapcols(&g, 10);
587  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
588  for (p = autowrap_string; *p; p++) {
589    owl_input j;
590    j.ch = *p;
591    j.uch = *p; /* Assuming ASCII. */
592    owl_editwin_process_char(oe, j);
593  }
594  p = owl_editwin_get_text(oe);
595  FAIL_UNLESS("text was automatically wrapped",
596              p && !strcmp(p, "we feel\n"
597                           "our owls\n"
598                           "should\n"
599                           "live\n"
600                           "closer to\n"
601                           "our\n"
602                           "ponies."));
603  owl_editwin_unref(oe); oe = NULL;
604  owl_global_set_edit_maxwrapcols(&g, 70);
605
606  /* Test owl_editwin_current_column. */
607  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
608  FAIL_UNLESS("initial column zero", owl_editwin_current_column(oe) == 0);
609  owl_editwin_insert_string(oe, "abcdef");
610  FAIL_UNLESS("simple insert", owl_editwin_current_column(oe) == 6);
611  owl_editwin_insert_string(oe, "\t");
612  FAIL_UNLESS("insert tabs", owl_editwin_current_column(oe) == 8);
613  owl_editwin_insert_string(oe, "123\n12\t3");
614  FAIL_UNLESS("newline with junk", owl_editwin_current_column(oe) == 9);
615  owl_editwin_move_to_beginning_of_line(oe);
616  FAIL_UNLESS("beginning of line", owl_editwin_current_column(oe) == 0);
617  owl_editwin_unref(oe); oe = NULL;
618
619  printf("# END testing owl_editwin (%d failures)\n", numfailed);
620
621  return numfailed;
622}
623
624int owl_fmtext_regtest(void) {
625  int numfailed = 0;
626  int start, end;
627  owl_fmtext fm1;
628  owl_fmtext fm2;
629  owl_regex re;
630  char *str;
631
632  printf("# BEGIN testing owl_fmtext\n");
633
634  owl_fmtext_init_null(&fm1);
635  owl_fmtext_init_null(&fm2);
636
637  /* Verify text gets correctly appended. */
638  owl_fmtext_append_normal(&fm1, "1234567898");
639  owl_fmtext_append_fmtext(&fm2, &fm1);
640  FAIL_UNLESS("string lengths correct",
641              owl_fmtext_num_bytes(&fm2) == strlen(owl_fmtext_get_text(&fm2)));
642
643  /* Test owl_fmtext_num_lines. */
644  owl_fmtext_clear(&fm1);
645  FAIL_UNLESS("empty line correct", owl_fmtext_num_lines(&fm1) == 0);
646  owl_fmtext_append_normal(&fm1, "12345\n67898");
647  FAIL_UNLESS("trailing chars correct", owl_fmtext_num_lines(&fm1) == 2);
648  owl_fmtext_append_normal(&fm1, "\n");
649  FAIL_UNLESS("trailing newline correct", owl_fmtext_num_lines(&fm1) == 2);
650  owl_fmtext_append_bold(&fm1, "");
651  FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2);
652
653  /* Test owl_fmtext_truncate_lines */
654  owl_fmtext_clear(&fm1);
655  owl_fmtext_append_normal(&fm1, "0\n1\n2\n3\n4\n");
656
657  owl_fmtext_clear(&fm2);
658  owl_fmtext_truncate_lines(&fm1, 1, 3, &fm2);
659  str = owl_fmtext_print_plain(&fm2);
660  FAIL_UNLESS("lines corrected truncated",
661              str && !strcmp(str, "1\n2\n3\n"));
662  g_free(str);
663
664  owl_fmtext_clear(&fm2);
665  owl_fmtext_truncate_lines(&fm1, 1, 5, &fm2);
666  str = owl_fmtext_print_plain(&fm2);
667  FAIL_UNLESS("lines corrected truncated",
668              str && !strcmp(str, "1\n2\n3\n4\n"));
669  g_free(str);
670
671  /* Test owl_fmtext_truncate_cols. */
672  owl_fmtext_clear(&fm1);
673  owl_fmtext_append_normal(&fm1, "123456789012345\n");
674  owl_fmtext_append_normal(&fm1, "123456789\n");
675  owl_fmtext_append_normal(&fm1, "1234567890\n");
676
677  owl_fmtext_clear(&fm2);
678  owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2);
679  str = owl_fmtext_print_plain(&fm2);
680  FAIL_UNLESS("columns correctly truncated",
681              str && !strcmp(str, "567890"
682                                  "56789\n"
683                                  "567890"));
684  g_free(str);
685
686  owl_fmtext_clear(&fm1);
687  owl_fmtext_append_normal(&fm1, "12\t1234");
688  owl_fmtext_append_bold(&fm1, "56\n");
689  owl_fmtext_append_bold(&fm1, "12345678\t\n");
690
691  owl_fmtext_clear(&fm2);
692  owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2);
693  str = owl_fmtext_print_plain(&fm2);
694  FAIL_UNLESS("columns correctly truncated",
695              str && !strcmp(str, "    123456"
696                                  "5678      "));
697  g_free(str);
698
699  /* Test owl_fmtext_expand_tabs. */
700  owl_fmtext_clear(&fm1);
701  owl_fmtext_append_normal(&fm1, "12\t1234");
702  owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
703  owl_fmtext_clear(&fm2);
704  owl_fmtext_expand_tabs(&fm1, &fm2, 0);
705  str = owl_fmtext_print_plain(&fm2);
706  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
707  FAIL_UNLESS("tabs corrected expanded",
708              str && !strcmp(str, "12      1234567 1\n"
709                                  "12345678        1"));
710  g_free(str);
711
712  owl_fmtext_clear(&fm2);
713  owl_fmtext_expand_tabs(&fm1, &fm2, 1);
714  str = owl_fmtext_print_plain(&fm2);
715  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
716  FAIL_UNLESS("tabs corrected expanded",
717              str && !strcmp(str, "12     1234567 1\n"
718                                  "12345678       1"));
719  g_free(str);
720
721  /* Test owl_fmtext_search. */
722  owl_fmtext_clear(&fm1);
723  owl_fmtext_append_normal(&fm1, "123123123123");
724  owl_regex_create(&re, "12");
725  {
726    int count = 0, offset;
727    offset = owl_fmtext_search(&fm1, &re, 0);
728    while (offset >= 0) {
729      FAIL_UNLESS("search matches",
730                  !strncmp("12", owl_fmtext_get_text(&fm1) + offset, 2));
731      count++;
732      offset = owl_fmtext_search(&fm1, &re, offset+1);
733    }
734    FAIL_UNLESS("exactly four matches", count == 4);
735  }
736  owl_regex_cleanup(&re);
737
738  /* Test owl_fmtext_line_number. */
739  owl_fmtext_clear(&fm1);
740  owl_fmtext_append_normal(&fm1, "123\n456\n");
741  owl_fmtext_append_bold(&fm1, "");
742  FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0));
743  FAIL_UNLESS("trailing formatting characters part of false line",
744              2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1)));
745  owl_regex_create_quoted(&re, "456");
746  FAIL_UNLESS("correctly find second line (line 1)",
747              1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0)));
748  owl_regex_cleanup(&re);
749
750  /* Test owl_fmtext_line_extents. */
751  owl_fmtext_clear(&fm1);
752  owl_fmtext_append_normal(&fm1, "123\n456\n789");
753  owl_fmtext_line_extents(&fm1, 1, &start, &end);
754  FAIL_UNLESS("line contents",
755              !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start));
756  owl_fmtext_line_extents(&fm1, 2, &start, &end);
757  FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1));
758
759  owl_fmtext_cleanup(&fm1);
760  owl_fmtext_cleanup(&fm2);
761
762  printf("# END testing owl_fmtext (%d failures)\n", numfailed);
763
764  return numfailed;
765}
766
767static int owl_smartfilter_test_equals(const char *filtname, const char *expected) {
768  owl_filter *f = NULL;
769  char *filtstr = NULL;
770  int failed = 0;
771  f = owl_global_get_filter(&g, filtname);
772  if (f == NULL) {
773    printf("not ok filter missing: %s\n", filtname);
774    failed = 1;
775    goto out;
776  }
777
778  /* TODO: Come up with a better way to test this. */
779  filtstr = owl_filter_print(f);
780  if (strcmp(expected, filtstr)) {
781    printf("not ok filter incorrect: |%s| instead of |%s|\n",
782           filtstr, expected);
783    failed = 1;
784    goto out;
785  }
786
787 out:
788  g_free(filtstr);
789  return failed;
790}
791
792static int owl_classinstfilt_test(const char *c, const char *i, int related, const char *expected) {
793  char *filtname = NULL;
794  int failed = 0;
795
796  filtname = owl_function_classinstfilt(c, i, related);
797  if (filtname == NULL) {
798    printf("not ok null filtname: %s %s %s\n", c, i ? i : "(null)",
799           related ? "related" : "not related");
800    failed = 1;
801    goto out;
802  }
803  if (owl_smartfilter_test_equals(filtname, expected)) {
804    failed = 1;
805    goto out;
806  }
807 out:
808  if (!failed) {
809    printf("ok %s\n", filtname);
810  }
811  if (filtname)
812    owl_global_remove_filter(&g, filtname);
813  g_free(filtname);
814  return failed;
815}
816
817static int owl_zuserfilt_test(const char *longuser, const char *expected) {
818  char *filtname = NULL;
819  int failed = 0;
820
821  filtname = owl_function_zuserfilt(longuser);
822  if (filtname == NULL) {
823    printf("not ok null filtname: %s\n", longuser);
824    failed = 1;
825    goto out;
826  }
827  if (owl_smartfilter_test_equals(filtname, expected)) {
828    failed = 1;
829    goto out;
830  }
831 out:
832  if (!failed) {
833    printf("ok %s\n", filtname);
834  }
835  if (filtname)
836    owl_global_remove_filter(&g, filtname);
837  g_free(filtname);
838  return failed;
839}
840
841
842int owl_smartfilter_regtest(void) {
843  int numfailed = 0;
844
845  printf("# BEGIN testing owl_smartfilter\n");
846
847  /* Check classinst making. */
848
849#define TEST_CLASSINSTFILT(c, i, r, e) do {             \
850    numtests++;                                         \
851    numfailed += owl_classinstfilt_test(c, i, r, e);    \
852  } while (0)
853  TEST_CLASSINSTFILT("message", NULL, false,
854                     "class ^message$\n");
855  TEST_CLASSINSTFILT("message", NULL, true,
856                     "class ^(un)*message(\\.d)*$\n");
857  TEST_CLASSINSTFILT("message", "personal", false,
858                     "class ^message$ and instance ^personal$\n");
859  TEST_CLASSINSTFILT("message", "personal", true,
860                     "class ^(un)*message(\\.d)*$ and ( instance ^(un)*personal(\\.d)*$ )\n");
861
862  TEST_CLASSINSTFILT("message", "evil\tinstance", false,
863                     "class ^message$ and instance '^evil\tinstance$'\n");
864  TEST_CLASSINSTFILT("message", "evil instance", false,
865                     "class ^message$ and instance '^evil instance$'\n");
866  TEST_CLASSINSTFILT("message", "evil'instance", false,
867                     "class ^message$ and instance \"^evil'instance$\"\n");
868  TEST_CLASSINSTFILT("message", "evil\"instance", false,
869                     "class ^message$ and instance '^evil\"instance$'\n");
870  TEST_CLASSINSTFILT("message", "evil$instance", false,
871                     "class ^message$ and instance ^evil\\$instance$\n");
872
873#define TEST_ZUSERFILT(l, e) do {                       \
874    numtests++;                                         \
875    numfailed += owl_zuserfilt_test(l, e);              \
876  } while (0)
877  TEST_ZUSERFILT("user",
878                 "( type ^zephyr$ and filter personal and "
879                 "( ( direction ^in$ and sender "
880                 "^user$"
881                 " ) or ( direction ^out$ and recipient "
882                 "^user$"
883                 " ) ) ) or ( ( class ^login$ ) and ( sender "
884                 "^user$"
885                 " ) )\n");
886  TEST_ZUSERFILT("very evil\t.user",
887                 "( type ^zephyr$ and filter personal and "
888                 "( ( direction ^in$ and sender "
889                 "'^very evil\t\\.user$'"
890                 " ) or ( direction ^out$ and recipient "
891                 "'^very evil\t\\.user$'"
892                 " ) ) ) or ( ( class ^login$ ) and ( sender "
893                 "'^very evil\t\\.user$'"
894                 " ) )\n");
895
896  printf("# END testing owl_smartfilter (%d failures)\n", numfailed);
897
898  return numfailed;
899}
900
901int owl_history_regtest(void)
902{
903  int numfailed = 0;
904  int i;
905  owl_history h;
906
907  printf("# BEGIN testing owl_history\n");
908  owl_history_init(&h);
909
910  /* Operations on empty history. */
911  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
912  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
913  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
914
915  /* Insert a few records. */
916  owl_history_store(&h, "a", false);
917  owl_history_store(&h, "b", false);
918  owl_history_store(&h, "c", false);
919  owl_history_store(&h, "d", true);
920
921  /* Walk up and down the history a bit. */
922  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
923  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
924  FAIL_UNLESS("touched", owl_history_is_touched(&h));
925  FAIL_UNLESS("next d", strcmp(owl_history_get_next(&h), "d") == 0);
926  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
927  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
928  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
929  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
930  FAIL_UNLESS("prev a", strcmp(owl_history_get_prev(&h), "a") == 0);
931  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
932
933  /* Now insert something. It should reset and blow away 'd'. */
934  owl_history_store(&h, "e", false);
935  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
936  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
937  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
938  FAIL_UNLESS("touched", owl_history_is_touched(&h));
939  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
940  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
941
942  /* Lines get de-duplicated on insert. */
943  owl_history_store(&h, "e", false);
944  owl_history_store(&h, "e", false);
945  owl_history_store(&h, "e", false);
946  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
947  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
948
949  /* But a partial is not deduplicated, as it'll go away soon. */
950  owl_history_store(&h, "e", true);
951  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
952  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
953  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
954  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
955
956  /* Reset moves to the front... */
957  owl_history_store(&h, "f", true);
958  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
959  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
960  owl_history_reset(&h);
961  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
962  /* ...and destroys any pending partial entry... */
963  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
964  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
965  /* ...but not non-partial ones. */
966  owl_history_reset(&h);
967  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
968
969  /* Finally, check we are bounded by OWL_HISTORYSIZE. */
970  for (i = 0; i < OWL_HISTORYSIZE; i++) {
971    char *string = g_strdup_printf("mango%d", i);
972    owl_history_store(&h, string, false);
973    g_free(string);
974  }
975  /* The OWL_HISTORYSIZE'th prev gets NULL. */
976  for (i = OWL_HISTORYSIZE - 2; i >= 0; i--) {
977    char *string = g_strdup_printf("mango%d", i);
978    FAIL_UNLESS("prev mango_N", strcmp(owl_history_get_prev(&h), string) == 0);
979    g_free(string);
980  }
981  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
982
983  owl_history_cleanup(&h);
984
985  printf("# END testing owl_history (%d failures)\n", numfailed);
986  return numfailed;
987}
988
989int call_filter_regtest(void)
990{
991  int numfailed = 0;
992  int ret;
993  char *out = NULL;
994  int status;
995
996  printf("# BEGIN testing call_filter\n");
997
998  const char *cat_argv[] = { "cat", NULL };
999  ret = call_filter(cat_argv, "Mangos!", &out, &status);
1000  FAIL_UNLESS("call_filter cat", (ret == 0 &&
1001                                  status == 0 &&
1002                                  strcmp(out, "Mangos!") == 0));
1003  g_free(out); out = NULL;
1004
1005  ret = call_filter(cat_argv, "", &out, &status);
1006  FAIL_UNLESS("call_filter cat", (ret == 0 &&
1007                                  status == 0 &&
1008                                  strcmp(out, "") == 0));
1009  g_free(out); out = NULL;
1010
1011  ret = call_filter(cat_argv, NULL, &out, &status);
1012  FAIL_UNLESS("call_filter cat", (ret == 0 &&
1013                                  status == 0 &&
1014                                  strcmp(out, "") == 0));
1015  g_free(out); out = NULL;
1016
1017  printf("# END testing call_filter (%d failures)\n", numfailed);
1018  return numfailed;
1019}
Note: See TracBrowser for help on using the repository browser.