source: tester.c @ 96ee74c

release-1.10release-1.9
Last change on this file since 96ee74c was 97cdbaf5, checked in by David Benjamin <davidben@mit.edu>, 13 years ago
Rewrite call_filter to use g_spawn_async_with_pipes This simplifies the error-handling code. Also fixes a bug where file descriptors get double-closed in call_filter. Also adds a unit test. The separate prog argument is removed to avoid having to deal with G_SPAWN_FILE_AND_ARGV_ZERO, and since we don't really use it anyway.
  • Property mode set to 100644
File size: 34.5 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 *s, *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  s = owl_util_baseclass("barnowl");
232  FAIL_UNLESS("baseclass barnowl", !strcmp("barnowl", s));
233  g_free(s);
234  s = owl_util_baseclass("unbarnowl");
235  FAIL_UNLESS("baseclass unbarnowl", !strcmp("barnowl", s));
236  g_free(s);
237  s = owl_util_baseclass("unununbarnowl.d.d");
238  FAIL_UNLESS("baseclass unununbarnowl.d.d", !strcmp("barnowl", s));
239  g_free(s);
240  s = owl_util_baseclass("ununun.d.d");
241  FAIL_UNLESS("baseclass ununun.d.d", !strcmp("", s));
242  g_free(s);
243  s = owl_util_baseclass("d.d.d.d");
244  FAIL_UNLESS("baseclass d.d.d.d", !strcmp("d", s));
245  g_free(s);
246  s = owl_util_baseclass("n.d.d.d");
247  FAIL_UNLESS("baseclass n.d.d.d", !strcmp("n", s));
248  g_free(s);
249  s = owl_util_baseclass("ununun.");
250  FAIL_UNLESS("baseclass ununun.", !strcmp(".", s));
251  g_free(s);
252  s = owl_util_baseclass("unununu");
253  FAIL_UNLESS("baseclass unununu", !strcmp("u", s));
254  g_free(s);
255
256
257  s = owl_util_makepath("foo/bar");
258  FAIL_UNLESS("makepath foo/bar", !strcmp("foo/bar", s));
259  g_free(s);
260  s = owl_util_makepath("//foo///bar");
261  FAIL_UNLESS("makepath //foo///bar", !strcmp("/foo/bar", s));
262  g_free(s);
263  s = owl_util_makepath("foo/~//bar/");
264  FAIL_UNLESS("makepath foo/~//bar/", !strcmp("foo/~/bar/", s));
265  g_free(s);
266  s = owl_util_makepath("~thisuserhadreallybetternotexist/foobar/");
267  FAIL_UNLESS("makepath ~thisuserhadreallybetternotexist/foobar/",
268              !strcmp("~thisuserhadreallybetternotexist/foobar/", s));
269  g_free(s);
270
271  home = g_strdup(owl_global_get_homedir(&g));
272  s = owl_util_makepath("~");
273  FAIL_UNLESS("makepath ~", !strcmp(home, s));
274  g_free(s);
275
276  path = g_build_filename(home, "foo/bar/baz", NULL);
277  s = owl_util_makepath("~///foo/bar//baz");
278  FAIL_UNLESS("makepath ~///foo/bar//baz", !strcmp(path, s));
279  g_free(s);
280  g_free(path);
281  g_free(home);
282
283  home = owl_util_homedir_for_user("root");
284  if (home == NULL) {
285    /* Just make some noise so we notice. */
286    home = g_strdup("<WHAT>");
287    fprintf(stderr, "owl_util_homedir_for_user failed");
288  }
289
290  s = owl_util_makepath("~root");
291  FAIL_UNLESS("makepath ~root", !strcmp(home, s));
292  g_free(s);
293
294  path = g_build_filename(home, "foo/bar/baz", NULL);
295  s = owl_util_makepath("~root///foo/bar//baz");
296  FAIL_UNLESS("makepath ~root///foo/bar//baz", !strcmp(path, s));
297  g_free(s);
298  g_free(path);
299  g_free(home);
300
301  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
302  printf("# END testing owl_util (%d failures)\n", numfailed);
303  return(numfailed);
304}
305
306int owl_dict_regtest(void) {
307  owl_dict d;
308  GPtrArray *l;
309  int numfailed=0;
310  char *av = g_strdup("aval"), *bv = g_strdup("bval"), *cv = g_strdup("cval"),
311    *dv = g_strdup("dval");
312
313  printf("# BEGIN testing owl_dict\n");
314  owl_dict_create(&d);
315  FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete));
316  FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete));
317  FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete));
318  FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete));
319  FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
320  FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
321  FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
322  FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
323  FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
324  FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
325  FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
326  FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
327
328  FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
329  l = owl_dict_get_keys(&d);
330  FAIL_UNLESS("get_keys result size", 3 == l->len);
331 
332  /* these assume the returned keys are sorted */
333  FAIL_UNLESS("get_keys result val", 0 == strcmp("a", l->pdata[0]));
334  FAIL_UNLESS("get_keys result val", 0 == strcmp("b", l->pdata[1]));
335  FAIL_UNLESS("get_keys result val", 0 == strcmp("c", l->pdata[2]));
336
337  owl_ptr_array_free(l, g_free);
338  owl_dict_cleanup(&d, NULL);
339
340  g_free(av);
341  g_free(bv);
342  g_free(cv);
343  g_free(dv);
344
345  /*  if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
346  printf("# END testing owl_dict (%d failures)\n", numfailed);
347  return(numfailed);
348}
349
350int owl_variable_regtest(void) {
351  owl_vardict vd;
352  owl_variable *var;
353  int numfailed=0;
354  char *value;
355  const void *v;
356
357  printf("# BEGIN testing owl_variable\n");
358  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
359
360  FAIL_UNLESS("get bool var", NULL != (var = owl_variable_get_var(&vd, "rxping")));
361  FAIL_UNLESS("get bool", 0 == owl_variable_get_bool(var));
362  FAIL_UNLESS("get bool (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
363  FAIL_UNLESS("get bool as string",
364              !strcmp((value = owl_variable_get_tostring(var)), "off"));
365  g_free(value);
366  FAIL_UNLESS("set bool 1", 0 == owl_variable_set_bool_on(var));
367  FAIL_UNLESS("get bool 2", 1 == owl_variable_get_bool(var));
368  FAIL_UNLESS("set bool 3", 0 == owl_variable_set_fromstring(var, "off", 0));
369  FAIL_UNLESS("get bool 4", 0 == owl_variable_get_bool(var));
370  FAIL_UNLESS("set bool 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
371  FAIL_UNLESS("get bool 6", 0 == owl_variable_get_bool(var));
372
373
374  FAIL_UNLESS("get string var", NULL != (var = owl_variable_get_var(&vd, "logpath")));
375  FAIL_UNLESS("get string", 0 == strcmp("~/zlog/people", owl_variable_get_string(var)));
376  FAIL_UNLESS("set string 7", 0 == owl_variable_set_string(var, "whee"));
377  FAIL_UNLESS("get string", !strcmp("whee", owl_variable_get_string(var)));
378
379  FAIL_UNLESS("get int var", NULL != (var = owl_variable_get_var(&vd, "typewinsize")));
380  FAIL_UNLESS("get int", 8 == owl_variable_get_int(var));
381  FAIL_UNLESS("get int (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
382  FAIL_UNLESS("get int as string",
383              !strcmp((value = owl_variable_get_tostring(var)), "8"));
384  g_free(value);
385  FAIL_UNLESS("set int 1", 0 == owl_variable_set_int(var, 12));
386  FAIL_UNLESS("get int 2", 12 == owl_variable_get_int(var));
387  FAIL_UNLESS("set int 1b", -1 == owl_variable_set_int(var, -3));
388  FAIL_UNLESS("get int 2b", 12 == owl_variable_get_int(var));
389  FAIL_UNLESS("set int 3", 0 == owl_variable_set_fromstring(var, "9", 0));
390  FAIL_UNLESS("get int 4", 9 == owl_variable_get_int(var));
391  FAIL_UNLESS("set int 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
392  FAIL_UNLESS("set int 6", -1 == owl_variable_set_fromstring(var, "", 0));
393  FAIL_UNLESS("get int 7", 9 == owl_variable_get_int(var));
394
395  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
396  FAIL_UNLESS("get new string var", NULL != (var = owl_variable_get_var(&vd, "stringvar")));
397  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(var)));
398  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(var)));
399  owl_variable_set_string(var, "new val");
400  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(var)));
401
402  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
403  FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar")));
404  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(var)));
405  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var));
406  owl_variable_set_int(var, 17);
407  FAIL_UNLESS("update int val", 17 == owl_variable_get_int(var));
408
409  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
410  FAIL_UNLESS("get new bool var", NULL != (var = owl_variable_get_var(&vd, "boolvar")));
411  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(var)));
412  FAIL_UNLESS("get new bool val", owl_variable_get_bool(var));
413  owl_variable_set_bool_off(var);
414  FAIL_UNLESS("update bool val", !owl_variable_get_bool(var));
415
416  owl_variable_dict_newvar_string(&vd, "nullstringvar", "", "", NULL);
417  FAIL_UNLESS("get new string (NULL) var", NULL != (var = owl_variable_get_var(&vd, "nullstringvar")));
418  FAIL_UNLESS("get string (NULL)", NULL == (value = owl_variable_get_tostring(var)));
419  g_free(value);
420  var = owl_variable_get_var(&vd, "zsigproc");
421  FAIL_UNLESS("get string (NULL) 2", NULL == (value = owl_variable_get_tostring(var)));
422  g_free(value);
423
424  owl_variable_dict_cleanup(&vd);
425
426  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
427  printf("# END testing owl_variable (%d failures)\n", numfailed);
428  return(numfailed);
429}
430
431static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
432{
433  owl_filter *f;
434  int ok;
435  int failed = 0;
436  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
437    printf("not ok can't parse %s\n", filt);
438    failed = 1;
439    goto out;
440  }
441  ok = owl_filter_message_match(f, m);
442  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
443    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
444    failed = 1;
445  }
446 out:
447  owl_filter_delete(f);
448  if(!failed) {
449    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
450  }
451  return failed;
452}
453
454int owl_filter_regtest(void) {
455  int numfailed=0;
456  owl_message m;
457  owl_filter *f1, *f2, *f3, *f4, *f5;
458
459  owl_message_init(&m);
460  owl_message_set_type_zephyr(&m);
461  owl_message_set_direction_in(&m);
462  owl_message_set_class(&m, "owl");
463  owl_message_set_instance(&m, "tester");
464  owl_message_set_sender(&m, "owl-user");
465  owl_message_set_recipient(&m, "joe");
466  owl_message_set_attribute(&m, "foo", "bar");
467
468#define TEST_FILTER(f, e) do {                          \
469    numtests++;                                         \
470    numfailed += owl_filter_test_string(f, &m, e);      \
471      } while(0)
472
473  TEST_FILTER("true", 1);
474  TEST_FILTER("false", 0);
475  TEST_FILTER("( true )", 1);
476  TEST_FILTER("not false", 1);
477  TEST_FILTER("( true ) or ( false )", 1);
478  TEST_FILTER("true and false", 0);
479  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
480
481  TEST_FILTER("class owl", 1);
482  TEST_FILTER("class ^owl$", 1);
483  TEST_FILTER("instance test", 1);
484  TEST_FILTER("instance ^test$", 0);
485  TEST_FILTER("instance ^tester$", 1);
486
487  TEST_FILTER("foo bar", 1);
488  TEST_FILTER("class owl and instance tester", 1);
489  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
490
491  /* Order of operations and precedence */
492  TEST_FILTER("not true or false", 0);
493  TEST_FILTER("true or true and false", 0);
494  TEST_FILTER("true and true and false or true", 1);
495  TEST_FILTER("false and false or true", 1);
496  TEST_FILTER("true and false or false", 0);
497
498  f1 = owl_filter_new_fromstring("f1", "class owl");
499  owl_global_add_filter(&g, f1);
500  TEST_FILTER("filter f1", 1);
501  owl_global_remove_filter(&g, "f1");
502
503  /* Test recursion prevention */
504  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
505  owl_filter_delete(f2);
506
507  /* mutual recursion */
508  f3 = owl_filter_new_fromstring("f3", "filter f4");
509  owl_global_add_filter(&g, f3);
510  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
511  owl_global_remove_filter(&g, "f3");
512  owl_filter_delete(f4);
513
514  /* support referencing a filter several times */
515  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
516  owl_filter_delete(f5);
517
518  owl_message_cleanup(&m);
519
520  return 0;
521}
522
523int owl_editwin_regtest(void) {
524  int numfailed = 0;
525  const char *p;
526  owl_editwin *oe;
527  const char *autowrap_string = "we feel our owls should live "
528                                "closer to our ponies.";
529
530  printf("# BEGIN testing owl_editwin\n");
531
532  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
533
534  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
535
536  /* check paragraph fill */
537  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
538  owl_editwin_move_to_top(oe);
539  owl_editwin_fill_paragraph(oe);
540  p = owl_editwin_get_text(oe);
541  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
542                                                            "blah blah blah.\n"
543                                                            "\n"
544                                                            "blah"));
545
546  owl_editwin_unref(oe); oe = NULL;
547  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
548
549  /* check that lines ending with ". " correctly fill */
550  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
551  owl_editwin_move_to_top(oe);
552  owl_editwin_fill_paragraph(oe);
553  p = owl_editwin_get_text(oe);
554  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
555                                                            "blah blah blah. \n"
556                                                            "\n"
557                                                            "blah"));
558
559  owl_editwin_unref(oe); oe = NULL;
560
561  /* Test owl_editwin_move_to_beginning_of_line. */
562  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
563  owl_editwin_insert_string(oe, "\n");
564  owl_editwin_insert_string(oe, "12345678\n");
565  owl_editwin_insert_string(oe, "\n");
566  owl_editwin_insert_string(oe, "abcdefg\n");
567  owl_editwin_move_to_top(oe);
568  FAIL_UNLESS("already at beginning of line",
569              owl_editwin_move_to_beginning_of_line(oe) == 0);
570  owl_editwin_line_move(oe, 1);
571  owl_editwin_point_move(oe, 5);
572  FAIL_UNLESS("find beginning of line after empty first line",
573              owl_editwin_move_to_beginning_of_line(oe) == -5);
574  owl_editwin_line_move(oe, 1);
575  FAIL_UNLESS("find beginning empty middle line",
576              owl_editwin_move_to_beginning_of_line(oe) == 0);
577  owl_editwin_line_move(oe, 1);
578  owl_editwin_point_move(oe, 2);
579  FAIL_UNLESS("find beginning of line after empty middle line",
580              owl_editwin_move_to_beginning_of_line(oe) == -2);
581  owl_editwin_unref(oe); oe = NULL;
582
583  /* Test automatic line-wrapping. */
584  owl_global_set_edit_maxwrapcols(&g, 10);
585  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
586  for (p = autowrap_string; *p; p++) {
587    owl_input j;
588    j.ch = *p;
589    j.uch = *p; /* Assuming ASCII. */
590    owl_editwin_process_char(oe, j);
591  }
592  p = owl_editwin_get_text(oe);
593  FAIL_UNLESS("text was automatically wrapped",
594              p && !strcmp(p, "we feel\n"
595                           "our owls\n"
596                           "should\n"
597                           "live\n"
598                           "closer to\n"
599                           "our\n"
600                           "ponies."));
601  owl_editwin_unref(oe); oe = NULL;
602  owl_global_set_edit_maxwrapcols(&g, 70);
603
604  /* Test owl_editwin_current_column. */
605  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
606  FAIL_UNLESS("initial column zero", owl_editwin_current_column(oe) == 0);
607  owl_editwin_insert_string(oe, "abcdef");
608  FAIL_UNLESS("simple insert", owl_editwin_current_column(oe) == 6);
609  owl_editwin_insert_string(oe, "\t");
610  FAIL_UNLESS("insert tabs", owl_editwin_current_column(oe) == 8);
611  owl_editwin_insert_string(oe, "123\n12\t3");
612  FAIL_UNLESS("newline with junk", owl_editwin_current_column(oe) == 9);
613  owl_editwin_move_to_beginning_of_line(oe);
614  FAIL_UNLESS("beginning of line", owl_editwin_current_column(oe) == 0);
615  owl_editwin_unref(oe); oe = NULL;
616
617  printf("# END testing owl_editwin (%d failures)\n", numfailed);
618
619  return numfailed;
620}
621
622int owl_fmtext_regtest(void) {
623  int numfailed = 0;
624  int start, end;
625  owl_fmtext fm1;
626  owl_fmtext fm2;
627  owl_regex re;
628  char *str;
629
630  printf("# BEGIN testing owl_fmtext\n");
631
632  owl_fmtext_init_null(&fm1);
633  owl_fmtext_init_null(&fm2);
634
635  /* Verify text gets correctly appended. */
636  owl_fmtext_append_normal(&fm1, "1234567898");
637  owl_fmtext_append_fmtext(&fm2, &fm1);
638  FAIL_UNLESS("string lengths correct",
639              owl_fmtext_num_bytes(&fm2) == strlen(owl_fmtext_get_text(&fm2)));
640
641  /* Test owl_fmtext_num_lines. */
642  owl_fmtext_clear(&fm1);
643  FAIL_UNLESS("empty line correct", owl_fmtext_num_lines(&fm1) == 0);
644  owl_fmtext_append_normal(&fm1, "12345\n67898");
645  FAIL_UNLESS("trailing chars correct", owl_fmtext_num_lines(&fm1) == 2);
646  owl_fmtext_append_normal(&fm1, "\n");
647  FAIL_UNLESS("trailing newline correct", owl_fmtext_num_lines(&fm1) == 2);
648  owl_fmtext_append_bold(&fm1, "");
649  FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2);
650
651  /* Test owl_fmtext_truncate_lines */
652  owl_fmtext_clear(&fm1);
653  owl_fmtext_append_normal(&fm1, "0\n1\n2\n3\n4\n");
654
655  owl_fmtext_clear(&fm2);
656  owl_fmtext_truncate_lines(&fm1, 1, 3, &fm2);
657  str = owl_fmtext_print_plain(&fm2);
658  FAIL_UNLESS("lines corrected truncated",
659              str && !strcmp(str, "1\n2\n3\n"));
660  g_free(str);
661
662  owl_fmtext_clear(&fm2);
663  owl_fmtext_truncate_lines(&fm1, 1, 5, &fm2);
664  str = owl_fmtext_print_plain(&fm2);
665  FAIL_UNLESS("lines corrected truncated",
666              str && !strcmp(str, "1\n2\n3\n4\n"));
667  g_free(str);
668
669  /* Test owl_fmtext_truncate_cols. */
670  owl_fmtext_clear(&fm1);
671  owl_fmtext_append_normal(&fm1, "123456789012345\n");
672  owl_fmtext_append_normal(&fm1, "123456789\n");
673  owl_fmtext_append_normal(&fm1, "1234567890\n");
674
675  owl_fmtext_clear(&fm2);
676  owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2);
677  str = owl_fmtext_print_plain(&fm2);
678  FAIL_UNLESS("columns correctly truncated",
679              str && !strcmp(str, "567890"
680                                  "56789\n"
681                                  "567890"));
682  g_free(str);
683
684  owl_fmtext_clear(&fm1);
685  owl_fmtext_append_normal(&fm1, "12\t1234");
686  owl_fmtext_append_bold(&fm1, "56\n");
687  owl_fmtext_append_bold(&fm1, "12345678\t\n");
688
689  owl_fmtext_clear(&fm2);
690  owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2);
691  str = owl_fmtext_print_plain(&fm2);
692  FAIL_UNLESS("columns correctly truncated",
693              str && !strcmp(str, "    123456"
694                                  "5678      "));
695  g_free(str);
696
697  /* Test owl_fmtext_expand_tabs. */
698  owl_fmtext_clear(&fm1);
699  owl_fmtext_append_normal(&fm1, "12\t1234");
700  owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
701  owl_fmtext_clear(&fm2);
702  owl_fmtext_expand_tabs(&fm1, &fm2, 0);
703  str = owl_fmtext_print_plain(&fm2);
704  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
705  FAIL_UNLESS("tabs corrected expanded",
706              str && !strcmp(str, "12      1234567 1\n"
707                                  "12345678        1"));
708  g_free(str);
709
710  owl_fmtext_clear(&fm2);
711  owl_fmtext_expand_tabs(&fm1, &fm2, 1);
712  str = owl_fmtext_print_plain(&fm2);
713  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
714  FAIL_UNLESS("tabs corrected expanded",
715              str && !strcmp(str, "12     1234567 1\n"
716                                  "12345678       1"));
717  g_free(str);
718
719  /* Test owl_fmtext_search. */
720  owl_fmtext_clear(&fm1);
721  owl_fmtext_append_normal(&fm1, "123123123123");
722  owl_regex_create(&re, "12");
723  {
724    int count = 0, offset;
725    offset = owl_fmtext_search(&fm1, &re, 0);
726    while (offset >= 0) {
727      FAIL_UNLESS("search matches",
728                  !strncmp("12", owl_fmtext_get_text(&fm1) + offset, 2));
729      count++;
730      offset = owl_fmtext_search(&fm1, &re, offset+1);
731    }
732    FAIL_UNLESS("exactly four matches", count == 4);
733  }
734  owl_regex_cleanup(&re);
735
736  /* Test owl_fmtext_line_number. */
737  owl_fmtext_clear(&fm1);
738  owl_fmtext_append_normal(&fm1, "123\n456\n");
739  owl_fmtext_append_bold(&fm1, "");
740  FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0));
741  FAIL_UNLESS("trailing formatting characters part of false line",
742              2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1)));
743  owl_regex_create_quoted(&re, "456");
744  FAIL_UNLESS("correctly find second line (line 1)",
745              1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0)));
746  owl_regex_cleanup(&re);
747
748  /* Test owl_fmtext_line_extents. */
749  owl_fmtext_clear(&fm1);
750  owl_fmtext_append_normal(&fm1, "123\n456\n789");
751  owl_fmtext_line_extents(&fm1, 1, &start, &end);
752  FAIL_UNLESS("line contents",
753              !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start));
754  owl_fmtext_line_extents(&fm1, 2, &start, &end);
755  FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1));
756
757  owl_fmtext_cleanup(&fm1);
758  owl_fmtext_cleanup(&fm2);
759
760  printf("# END testing owl_fmtext (%d failures)\n", numfailed);
761
762  return numfailed;
763}
764
765static int owl_smartfilter_test_equals(const char *filtname, const char *expected) {
766  owl_filter *f = NULL;
767  char *filtstr = NULL;
768  int failed = 0;
769  f = owl_global_get_filter(&g, filtname);
770  if (f == NULL) {
771    printf("not ok filter missing: %s\n", filtname);
772    failed = 1;
773    goto out;
774  }
775
776  /* TODO: Come up with a better way to test this. */
777  filtstr = owl_filter_print(f);
778  if (strcmp(expected, filtstr)) {
779    printf("not ok filter incorrect: |%s| instead of |%s|\n",
780           filtstr, expected);
781    failed = 1;
782    goto out;
783  }
784
785 out:
786  g_free(filtstr);
787  return failed;
788}
789
790static int owl_classinstfilt_test(const char *c, const char *i, int related, const char *expected) {
791  char *filtname = NULL;
792  int failed = 0;
793
794  filtname = owl_function_classinstfilt(c, i, related);
795  if (filtname == NULL) {
796    printf("not ok null filtname: %s %s %s\n", c, i ? i : "(null)",
797           related ? "related" : "not related");
798    failed = 1;
799    goto out;
800  }
801  if (owl_smartfilter_test_equals(filtname, expected)) {
802    failed = 1;
803    goto out;
804  }
805 out:
806  if (!failed) {
807    printf("ok %s\n", filtname);
808  }
809  if (filtname)
810    owl_global_remove_filter(&g, filtname);
811  g_free(filtname);
812  return failed;
813}
814
815static int owl_zuserfilt_test(const char *longuser, const char *expected) {
816  char *filtname = NULL;
817  int failed = 0;
818
819  filtname = owl_function_zuserfilt(longuser);
820  if (filtname == NULL) {
821    printf("not ok null filtname: %s\n", longuser);
822    failed = 1;
823    goto out;
824  }
825  if (owl_smartfilter_test_equals(filtname, expected)) {
826    failed = 1;
827    goto out;
828  }
829 out:
830  if (!failed) {
831    printf("ok %s\n", filtname);
832  }
833  if (filtname)
834    owl_global_remove_filter(&g, filtname);
835  g_free(filtname);
836  return failed;
837}
838
839
840int owl_smartfilter_regtest(void) {
841  int numfailed = 0;
842
843  printf("# BEGIN testing owl_smartfilter\n");
844
845  /* Check classinst making. */
846
847#define TEST_CLASSINSTFILT(c, i, r, e) do {             \
848    numtests++;                                         \
849    numfailed += owl_classinstfilt_test(c, i, r, e);    \
850  } while (0)
851  TEST_CLASSINSTFILT("message", NULL, false,
852                     "class ^message$\n");
853  TEST_CLASSINSTFILT("message", NULL, true,
854                     "class ^(un)*message(\\.d)*$\n");
855  TEST_CLASSINSTFILT("message", "personal", false,
856                     "class ^message$ and instance ^personal$\n");
857  TEST_CLASSINSTFILT("message", "personal", true,
858                     "class ^(un)*message(\\.d)*$ and ( instance ^(un)*personal(\\.d)*$ )\n");
859
860  TEST_CLASSINSTFILT("message", "evil\tinstance", false,
861                     "class ^message$ and instance '^evil\tinstance$'\n");
862  TEST_CLASSINSTFILT("message", "evil instance", false,
863                     "class ^message$ and instance '^evil instance$'\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
871#define TEST_ZUSERFILT(l, e) do {                       \
872    numtests++;                                         \
873    numfailed += owl_zuserfilt_test(l, e);              \
874  } while (0)
875  TEST_ZUSERFILT("user",
876                 "( type ^zephyr$ and filter personal and "
877                 "( ( direction ^in$ and sender "
878                 "^user$"
879                 " ) or ( direction ^out$ and recipient "
880                 "^user$"
881                 " ) ) ) or ( ( class ^login$ ) and ( sender "
882                 "^user$"
883                 " ) )\n");
884  TEST_ZUSERFILT("very evil\t.user",
885                 "( type ^zephyr$ and filter personal and "
886                 "( ( direction ^in$ and sender "
887                 "'^very evil\t\\.user$'"
888                 " ) or ( direction ^out$ and recipient "
889                 "'^very evil\t\\.user$'"
890                 " ) ) ) or ( ( class ^login$ ) and ( sender "
891                 "'^very evil\t\\.user$'"
892                 " ) )\n");
893
894  printf("# END testing owl_smartfilter (%d failures)\n", numfailed);
895
896  return numfailed;
897}
898
899int owl_history_regtest(void)
900{
901  int numfailed = 0;
902  int i;
903  owl_history h;
904
905  printf("# BEGIN testing owl_history\n");
906  owl_history_init(&h);
907
908  /* Operations on empty history. */
909  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
910  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
911  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
912
913  /* Insert a few records. */
914  owl_history_store(&h, "a", false);
915  owl_history_store(&h, "b", false);
916  owl_history_store(&h, "c", false);
917  owl_history_store(&h, "d", true);
918
919  /* Walk up and down the history a bit. */
920  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
921  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
922  FAIL_UNLESS("touched", owl_history_is_touched(&h));
923  FAIL_UNLESS("next d", strcmp(owl_history_get_next(&h), "d") == 0);
924  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
925  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
926  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
927  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
928  FAIL_UNLESS("prev a", strcmp(owl_history_get_prev(&h), "a") == 0);
929  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
930
931  /* Now insert something. It should reset and blow away 'd'. */
932  owl_history_store(&h, "e", false);
933  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
934  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
935  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
936  FAIL_UNLESS("touched", owl_history_is_touched(&h));
937  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
938  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
939
940  /* Lines get de-duplicated on insert. */
941  owl_history_store(&h, "e", false);
942  owl_history_store(&h, "e", false);
943  owl_history_store(&h, "e", false);
944  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
945  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
946
947  /* But a partial is not deduplicated, as it'll go away soon. */
948  owl_history_store(&h, "e", true);
949  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
950  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
951  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
952  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
953
954  /* Reset moves to the front... */
955  owl_history_store(&h, "f", true);
956  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
957  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
958  owl_history_reset(&h);
959  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
960  /* ...and destroys any pending partial entry... */
961  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
962  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
963  /* ...but not non-partial ones. */
964  owl_history_reset(&h);
965  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
966
967  /* Finally, check we are bounded by OWL_HISTORYSIZE. */
968  for (i = 0; i < OWL_HISTORYSIZE; i++) {
969    char *string = g_strdup_printf("mango%d", i);
970    owl_history_store(&h, string, false);
971    g_free(string);
972  }
973  /* The OWL_HISTORYSIZE'th prev gets NULL. */
974  for (i = OWL_HISTORYSIZE - 2; i >= 0; i--) {
975    char *string = g_strdup_printf("mango%d", i);
976    FAIL_UNLESS("prev mango_N", strcmp(owl_history_get_prev(&h), string) == 0);
977    g_free(string);
978  }
979  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
980
981  owl_history_cleanup(&h);
982
983  printf("# END testing owl_history (%d failures)\n", numfailed);
984  return numfailed;
985}
986
987int call_filter_regtest(void)
988{
989  int numfailed = 0;
990  int ret;
991  char *out = NULL;
992  int status;
993
994  printf("# BEGIN testing call_filter\n");
995
996  const char *cat_argv[] = { "cat", NULL };
997  ret = call_filter(cat_argv, "Mangos!", &out, &status);
998  FAIL_UNLESS("call_filter cat", (ret == 0 &&
999                                  status == 0 &&
1000                                  strcmp(out, "Mangos!") == 0));
1001  g_free(out); out = NULL;
1002
1003  ret = call_filter(cat_argv, "", &out, &status);
1004  FAIL_UNLESS("call_filter cat", (ret == 0 &&
1005                                  status == 0 &&
1006                                  strcmp(out, "") == 0));
1007  g_free(out); out = NULL;
1008
1009  ret = call_filter(cat_argv, NULL, &out, &status);
1010  FAIL_UNLESS("call_filter cat", (ret == 0 &&
1011                                  status == 0 &&
1012                                  strcmp(out, "") == 0));
1013  g_free(out); out = NULL;
1014
1015  printf("# END testing call_filter (%d failures)\n", numfailed);
1016  return numfailed;
1017}
Note: See TracBrowser for help on using the repository browser.