source: tester.c @ bbc31e4

release-1.10
Last change on this file since bbc31e4 was 1a4508b, checked in by David Benjamin <davidben@mit.edu>, 12 years ago
Fix the unit tests The arguments got reordered slightly to match the nasty macros.
  • Property mode set to 100644
File size: 34.2 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  FAIL_UNLESS("setup", 0==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  owl_variable_dict_newvar_string(&vd, "stringvar", "testval", "", "");
379  FAIL_UNLESS("get new string var", NULL != (var = owl_variable_get_var(&vd, "stringvar")));
380  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(var)));
381  owl_variable_set_string(var, "new val");
382  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(var)));
383
384  owl_variable_dict_newvar_int(&vd, "intvar", 47, "", "");
385  FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar")));
386  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var));
387  owl_variable_set_int(var, 17);
388  FAIL_UNLESS("update int val", 17 == owl_variable_get_int(var));
389
390  owl_variable_dict_newvar_bool(&vd, "boolvar", true, "", "");
391  FAIL_UNLESS("get new bool var", NULL != (var = owl_variable_get_var(&vd, "boolvar")));
392  FAIL_UNLESS("get new bool val", owl_variable_get_bool(var));
393  owl_variable_set_bool_off(var);
394  FAIL_UNLESS("update bool val", !owl_variable_get_bool(var));
395
396  owl_variable_dict_newvar_string(&vd, "nullstringvar", NULL, "", "");
397  FAIL_UNLESS("get new string (NULL) var", NULL != (var = owl_variable_get_var(&vd, "nullstringvar")));
398  FAIL_UNLESS("get string (NULL)", NULL == (value = owl_variable_get_tostring(var)));
399  g_free(value);
400  var = owl_variable_get_var(&vd, "zsigproc");
401  FAIL_UNLESS("get string (NULL) 2", NULL == (value = owl_variable_get_tostring(var)));
402  g_free(value);
403
404  owl_variable_dict_cleanup(&vd);
405
406  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
407  printf("# END testing owl_variable (%d failures)\n", numfailed);
408  return(numfailed);
409}
410
411static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
412{
413  owl_filter *f;
414  int ok;
415  int failed = 0;
416  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
417    printf("not ok can't parse %s\n", filt);
418    failed = 1;
419    goto out;
420  }
421  ok = owl_filter_message_match(f, m);
422  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
423    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
424    failed = 1;
425  }
426 out:
427  owl_filter_delete(f);
428  if(!failed) {
429    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
430  }
431  return failed;
432}
433
434int owl_filter_regtest(void) {
435  int numfailed=0;
436  owl_message m;
437  owl_filter *f1, *f2, *f3, *f4, *f5;
438
439  owl_message_init(&m);
440  owl_message_set_type_zephyr(&m);
441  owl_message_set_direction_in(&m);
442  owl_message_set_class(&m, "owl");
443  owl_message_set_instance(&m, "tester");
444  owl_message_set_sender(&m, "owl-user");
445  owl_message_set_recipient(&m, "joe");
446  owl_message_set_attribute(&m, "foo", "bar");
447
448#define TEST_FILTER(f, e) do {                          \
449    numtests++;                                         \
450    numfailed += owl_filter_test_string(f, &m, e);      \
451      } while(0)
452
453  TEST_FILTER("true", 1);
454  TEST_FILTER("false", 0);
455  TEST_FILTER("( true )", 1);
456  TEST_FILTER("not false", 1);
457  TEST_FILTER("( true ) or ( false )", 1);
458  TEST_FILTER("true and false", 0);
459  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
460
461  TEST_FILTER("class owl", 1);
462  TEST_FILTER("class ^owl$", 1);
463  TEST_FILTER("instance test", 1);
464  TEST_FILTER("instance ^test$", 0);
465  TEST_FILTER("instance ^tester$", 1);
466
467  TEST_FILTER("foo bar", 1);
468  TEST_FILTER("class owl and instance tester", 1);
469  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
470
471  /* Order of operations and precedence */
472  TEST_FILTER("not true or false", 0);
473  TEST_FILTER("true or true and false", 0);
474  TEST_FILTER("true and true and false or true", 1);
475  TEST_FILTER("false and false or true", 1);
476  TEST_FILTER("true and false or false", 0);
477
478  f1 = owl_filter_new_fromstring("f1", "class owl");
479  owl_global_add_filter(&g, f1);
480  TEST_FILTER("filter f1", 1);
481  owl_global_remove_filter(&g, "f1");
482
483  /* Test recursion prevention */
484  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
485  owl_filter_delete(f2);
486
487  /* mutual recursion */
488  f3 = owl_filter_new_fromstring("f3", "filter f4");
489  owl_global_add_filter(&g, f3);
490  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
491  owl_global_remove_filter(&g, "f3");
492  owl_filter_delete(f4);
493
494  /* support referencing a filter several times */
495  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
496  owl_filter_delete(f5);
497
498  owl_message_cleanup(&m);
499
500  return 0;
501}
502
503int owl_editwin_regtest(void) {
504  int numfailed = 0;
505  const char *p;
506  owl_editwin *oe;
507  const char *autowrap_string = "we feel our owls should live "
508                                "closer to our ponies.";
509
510  printf("# BEGIN testing owl_editwin\n");
511
512  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
513
514  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
515
516  /* check paragraph fill */
517  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
518  owl_editwin_move_to_top(oe);
519  owl_editwin_fill_paragraph(oe);
520  p = owl_editwin_get_text(oe);
521  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
522                                                            "blah blah blah.\n"
523                                                            "\n"
524                                                            "blah"));
525
526  owl_editwin_unref(oe); oe = NULL;
527  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
528
529  /* check that lines ending with ". " correctly fill */
530  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
531  owl_editwin_move_to_top(oe);
532  owl_editwin_fill_paragraph(oe);
533  p = owl_editwin_get_text(oe);
534  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
535                                                            "blah blah blah. \n"
536                                                            "\n"
537                                                            "blah"));
538
539  owl_editwin_unref(oe); oe = NULL;
540
541  /* Test owl_editwin_move_to_beginning_of_line. */
542  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
543  owl_editwin_insert_string(oe, "\n");
544  owl_editwin_insert_string(oe, "12345678\n");
545  owl_editwin_insert_string(oe, "\n");
546  owl_editwin_insert_string(oe, "abcdefg\n");
547  owl_editwin_move_to_top(oe);
548  FAIL_UNLESS("already at beginning of line",
549              owl_editwin_move_to_beginning_of_line(oe) == 0);
550  owl_editwin_line_move(oe, 1);
551  owl_editwin_point_move(oe, 5);
552  FAIL_UNLESS("find beginning of line after empty first line",
553              owl_editwin_move_to_beginning_of_line(oe) == -5);
554  owl_editwin_line_move(oe, 1);
555  FAIL_UNLESS("find beginning empty middle line",
556              owl_editwin_move_to_beginning_of_line(oe) == 0);
557  owl_editwin_line_move(oe, 1);
558  owl_editwin_point_move(oe, 2);
559  FAIL_UNLESS("find beginning of line after empty middle line",
560              owl_editwin_move_to_beginning_of_line(oe) == -2);
561  owl_editwin_unref(oe); oe = NULL;
562
563  /* Test automatic line-wrapping. */
564  owl_global_set_edit_maxwrapcols(&g, 10);
565  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
566  for (p = autowrap_string; *p; p++) {
567    owl_input j;
568    j.ch = *p;
569    j.uch = *p; /* Assuming ASCII. */
570    owl_editwin_process_char(oe, j);
571  }
572  p = owl_editwin_get_text(oe);
573  FAIL_UNLESS("text was automatically wrapped",
574              p && !strcmp(p, "we feel\n"
575                           "our owls\n"
576                           "should\n"
577                           "live\n"
578                           "closer to\n"
579                           "our\n"
580                           "ponies."));
581  owl_editwin_unref(oe); oe = NULL;
582  owl_global_set_edit_maxwrapcols(&g, 70);
583
584  /* Test owl_editwin_current_column. */
585  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
586  FAIL_UNLESS("initial column zero", owl_editwin_current_column(oe) == 0);
587  owl_editwin_insert_string(oe, "abcdef");
588  FAIL_UNLESS("simple insert", owl_editwin_current_column(oe) == 6);
589  owl_editwin_insert_string(oe, "\t");
590  FAIL_UNLESS("insert tabs", owl_editwin_current_column(oe) == 8);
591  owl_editwin_insert_string(oe, "123\n12\t3");
592  FAIL_UNLESS("newline with junk", owl_editwin_current_column(oe) == 9);
593  owl_editwin_move_to_beginning_of_line(oe);
594  FAIL_UNLESS("beginning of line", owl_editwin_current_column(oe) == 0);
595  owl_editwin_unref(oe); oe = NULL;
596
597  printf("# END testing owl_editwin (%d failures)\n", numfailed);
598
599  return numfailed;
600}
601
602int owl_fmtext_regtest(void) {
603  int numfailed = 0;
604  int start, end;
605  owl_fmtext fm1;
606  owl_fmtext fm2;
607  owl_regex re;
608  char *str;
609
610  printf("# BEGIN testing owl_fmtext\n");
611
612  owl_fmtext_init_null(&fm1);
613  owl_fmtext_init_null(&fm2);
614
615  /* Verify text gets correctly appended. */
616  owl_fmtext_append_normal(&fm1, "1234567898");
617  owl_fmtext_append_fmtext(&fm2, &fm1);
618  FAIL_UNLESS("string lengths correct",
619              owl_fmtext_num_bytes(&fm2) == strlen(owl_fmtext_get_text(&fm2)));
620
621  /* Test owl_fmtext_num_lines. */
622  owl_fmtext_clear(&fm1);
623  FAIL_UNLESS("empty line correct", owl_fmtext_num_lines(&fm1) == 0);
624  owl_fmtext_append_normal(&fm1, "12345\n67898");
625  FAIL_UNLESS("trailing chars correct", owl_fmtext_num_lines(&fm1) == 2);
626  owl_fmtext_append_normal(&fm1, "\n");
627  FAIL_UNLESS("trailing newline correct", owl_fmtext_num_lines(&fm1) == 2);
628  owl_fmtext_append_bold(&fm1, "");
629  FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2);
630
631  /* Test owl_fmtext_truncate_lines */
632  owl_fmtext_clear(&fm1);
633  owl_fmtext_append_normal(&fm1, "0\n1\n2\n3\n4\n");
634
635  owl_fmtext_clear(&fm2);
636  owl_fmtext_truncate_lines(&fm1, 1, 3, &fm2);
637  str = owl_fmtext_print_plain(&fm2);
638  FAIL_UNLESS("lines corrected truncated",
639              str && !strcmp(str, "1\n2\n3\n"));
640  g_free(str);
641
642  owl_fmtext_clear(&fm2);
643  owl_fmtext_truncate_lines(&fm1, 1, 5, &fm2);
644  str = owl_fmtext_print_plain(&fm2);
645  FAIL_UNLESS("lines corrected truncated",
646              str && !strcmp(str, "1\n2\n3\n4\n"));
647  g_free(str);
648
649  /* Test owl_fmtext_truncate_cols. */
650  owl_fmtext_clear(&fm1);
651  owl_fmtext_append_normal(&fm1, "123456789012345\n");
652  owl_fmtext_append_normal(&fm1, "123456789\n");
653  owl_fmtext_append_normal(&fm1, "1234567890\n");
654
655  owl_fmtext_clear(&fm2);
656  owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2);
657  str = owl_fmtext_print_plain(&fm2);
658  FAIL_UNLESS("columns correctly truncated",
659              str && !strcmp(str, "567890"
660                                  "56789\n"
661                                  "567890"));
662  g_free(str);
663
664  owl_fmtext_clear(&fm1);
665  owl_fmtext_append_normal(&fm1, "12\t1234");
666  owl_fmtext_append_bold(&fm1, "56\n");
667  owl_fmtext_append_bold(&fm1, "12345678\t\n");
668
669  owl_fmtext_clear(&fm2);
670  owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2);
671  str = owl_fmtext_print_plain(&fm2);
672  FAIL_UNLESS("columns correctly truncated",
673              str && !strcmp(str, "    123456"
674                                  "5678      "));
675  g_free(str);
676
677  /* Test owl_fmtext_expand_tabs. */
678  owl_fmtext_clear(&fm1);
679  owl_fmtext_append_normal(&fm1, "12\t1234");
680  owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
681  owl_fmtext_clear(&fm2);
682  owl_fmtext_expand_tabs(&fm1, &fm2, 0);
683  str = owl_fmtext_print_plain(&fm2);
684  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
685  FAIL_UNLESS("tabs corrected expanded",
686              str && !strcmp(str, "12      1234567 1\n"
687                                  "12345678        1"));
688  g_free(str);
689
690  owl_fmtext_clear(&fm2);
691  owl_fmtext_expand_tabs(&fm1, &fm2, 1);
692  str = owl_fmtext_print_plain(&fm2);
693  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
694  FAIL_UNLESS("tabs corrected expanded",
695              str && !strcmp(str, "12     1234567 1\n"
696                                  "12345678       1"));
697  g_free(str);
698
699  /* Test owl_fmtext_search. */
700  owl_fmtext_clear(&fm1);
701  owl_fmtext_append_normal(&fm1, "123123123123");
702  owl_regex_create(&re, "12");
703  {
704    int count = 0, offset;
705    offset = owl_fmtext_search(&fm1, &re, 0);
706    while (offset >= 0) {
707      FAIL_UNLESS("search matches",
708                  !strncmp("12", owl_fmtext_get_text(&fm1) + offset, 2));
709      count++;
710      offset = owl_fmtext_search(&fm1, &re, offset+1);
711    }
712    FAIL_UNLESS("exactly four matches", count == 4);
713  }
714  owl_regex_cleanup(&re);
715
716  /* Test owl_fmtext_line_number. */
717  owl_fmtext_clear(&fm1);
718  owl_fmtext_append_normal(&fm1, "123\n456\n");
719  owl_fmtext_append_bold(&fm1, "");
720  FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0));
721  FAIL_UNLESS("trailing formatting characters part of false line",
722              2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1)));
723  owl_regex_create_quoted(&re, "456");
724  FAIL_UNLESS("correctly find second line (line 1)",
725              1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0)));
726  owl_regex_cleanup(&re);
727
728  /* Test owl_fmtext_line_extents. */
729  owl_fmtext_clear(&fm1);
730  owl_fmtext_append_normal(&fm1, "123\n456\n789");
731  owl_fmtext_line_extents(&fm1, 1, &start, &end);
732  FAIL_UNLESS("line contents",
733              !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start));
734  owl_fmtext_line_extents(&fm1, 2, &start, &end);
735  FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1));
736
737  owl_fmtext_cleanup(&fm1);
738  owl_fmtext_cleanup(&fm2);
739
740  printf("# END testing owl_fmtext (%d failures)\n", numfailed);
741
742  return numfailed;
743}
744
745static int owl_smartfilter_test_equals(const char *filtname, const char *expected) {
746  owl_filter *f = NULL;
747  char *filtstr = NULL;
748  int failed = 0;
749  f = owl_global_get_filter(&g, filtname);
750  if (f == NULL) {
751    printf("not ok filter missing: %s\n", filtname);
752    failed = 1;
753    goto out;
754  }
755
756  /* TODO: Come up with a better way to test this. */
757  filtstr = owl_filter_print(f);
758  if (strcmp(expected, filtstr)) {
759    printf("not ok filter incorrect: |%s| instead of |%s|\n",
760           filtstr, expected);
761    failed = 1;
762    goto out;
763  }
764
765 out:
766  g_free(filtstr);
767  return failed;
768}
769
770static int owl_classinstfilt_test(const char *c, const char *i, int related, const char *expected) {
771  char *filtname = NULL;
772  int failed = 0;
773
774  filtname = owl_function_classinstfilt(c, i, related);
775  if (filtname == NULL) {
776    printf("not ok null filtname: %s %s %s\n", c, i ? i : "(null)",
777           related ? "related" : "not related");
778    failed = 1;
779    goto out;
780  }
781  if (owl_smartfilter_test_equals(filtname, expected)) {
782    failed = 1;
783    goto out;
784  }
785 out:
786  if (!failed) {
787    printf("ok %s\n", filtname);
788  }
789  if (filtname)
790    owl_global_remove_filter(&g, filtname);
791  g_free(filtname);
792  return failed;
793}
794
795static int owl_zuserfilt_test(const char *longuser, const char *expected) {
796  char *filtname = NULL;
797  int failed = 0;
798
799  filtname = owl_function_zuserfilt(longuser);
800  if (filtname == NULL) {
801    printf("not ok null filtname: %s\n", longuser);
802    failed = 1;
803    goto out;
804  }
805  if (owl_smartfilter_test_equals(filtname, expected)) {
806    failed = 1;
807    goto out;
808  }
809 out:
810  if (!failed) {
811    printf("ok %s\n", filtname);
812  }
813  if (filtname)
814    owl_global_remove_filter(&g, filtname);
815  g_free(filtname);
816  return failed;
817}
818
819
820int owl_smartfilter_regtest(void) {
821  int numfailed = 0;
822
823  printf("# BEGIN testing owl_smartfilter\n");
824
825  /* Check classinst making. */
826
827#define TEST_CLASSINSTFILT(c, i, r, e) do {             \
828    numtests++;                                         \
829    numfailed += owl_classinstfilt_test(c, i, r, e);    \
830  } while (0)
831  TEST_CLASSINSTFILT("message", NULL, false,
832                     "class ^message$\n");
833  TEST_CLASSINSTFILT("message", NULL, true,
834                     "class ^(un)*message(\\.d)*$\n");
835  TEST_CLASSINSTFILT("message", "personal", false,
836                     "class ^message$ and instance ^personal$\n");
837  TEST_CLASSINSTFILT("message", "personal", true,
838                     "class ^(un)*message(\\.d)*$ and ( instance ^(un)*personal(\\.d)*$ )\n");
839
840  TEST_CLASSINSTFILT("message", "evil\tinstance", false,
841                     "class ^message$ and instance '^evil\tinstance$'\n");
842  TEST_CLASSINSTFILT("message", "evil instance", false,
843                     "class ^message$ and instance '^evil instance$'\n");
844  TEST_CLASSINSTFILT("message", "evil'instance", false,
845                     "class ^message$ and instance \"^evil'instance$\"\n");
846  TEST_CLASSINSTFILT("message", "evil\"instance", false,
847                     "class ^message$ and instance '^evil\"instance$'\n");
848  TEST_CLASSINSTFILT("message", "evil$instance", false,
849                     "class ^message$ and instance ^evil\\$instance$\n");
850
851#define TEST_ZUSERFILT(l, e) do {                       \
852    numtests++;                                         \
853    numfailed += owl_zuserfilt_test(l, e);              \
854  } while (0)
855  TEST_ZUSERFILT("user",
856                 "( type ^zephyr$ and filter personal and "
857                 "( ( direction ^in$ and sender "
858                 "^user$"
859                 " ) or ( direction ^out$ and recipient "
860                 "^user$"
861                 " ) ) ) or ( ( class ^login$ ) and ( sender "
862                 "^user$"
863                 " ) )\n");
864  TEST_ZUSERFILT("very evil\t.user",
865                 "( type ^zephyr$ and filter personal and "
866                 "( ( direction ^in$ and sender "
867                 "'^very evil\t\\.user$'"
868                 " ) or ( direction ^out$ and recipient "
869                 "'^very evil\t\\.user$'"
870                 " ) ) ) or ( ( class ^login$ ) and ( sender "
871                 "'^very evil\t\\.user$'"
872                 " ) )\n");
873
874  printf("# END testing owl_smartfilter (%d failures)\n", numfailed);
875
876  return numfailed;
877}
878
879int owl_history_regtest(void)
880{
881  int numfailed = 0;
882  int i;
883  owl_history h;
884
885  printf("# BEGIN testing owl_history\n");
886  owl_history_init(&h);
887
888  /* Operations on empty history. */
889  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
890  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
891  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
892
893  /* Insert a few records. */
894  owl_history_store(&h, "a", false);
895  owl_history_store(&h, "b", false);
896  owl_history_store(&h, "c", false);
897  owl_history_store(&h, "d", true);
898
899  /* Walk up and down the history a bit. */
900  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
901  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
902  FAIL_UNLESS("touched", owl_history_is_touched(&h));
903  FAIL_UNLESS("next d", strcmp(owl_history_get_next(&h), "d") == 0);
904  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
905  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
906  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
907  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
908  FAIL_UNLESS("prev a", strcmp(owl_history_get_prev(&h), "a") == 0);
909  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
910
911  /* Now insert something. It should reset and blow away 'd'. */
912  owl_history_store(&h, "e", false);
913  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
914  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
915  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
916  FAIL_UNLESS("touched", owl_history_is_touched(&h));
917  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
918  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
919
920  /* Lines get de-duplicated on insert. */
921  owl_history_store(&h, "e", false);
922  owl_history_store(&h, "e", false);
923  owl_history_store(&h, "e", false);
924  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
925  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
926
927  /* But a partial is not deduplicated, as it'll go away soon. */
928  owl_history_store(&h, "e", true);
929  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
930  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
931  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
932  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
933
934  /* Reset moves to the front... */
935  owl_history_store(&h, "f", true);
936  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
937  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
938  owl_history_reset(&h);
939  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
940  /* ...and destroys any pending partial entry... */
941  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
942  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
943  /* ...but not non-partial ones. */
944  owl_history_reset(&h);
945  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
946
947  /* Finally, check we are bounded by OWL_HISTORYSIZE. */
948  for (i = 0; i < OWL_HISTORYSIZE; i++) {
949    char *string = g_strdup_printf("mango%d", i);
950    owl_history_store(&h, string, false);
951    g_free(string);
952  }
953  /* The OWL_HISTORYSIZE'th prev gets NULL. */
954  for (i = OWL_HISTORYSIZE - 2; i >= 0; i--) {
955    char *string = g_strdup_printf("mango%d", i);
956    FAIL_UNLESS("prev mango_N", strcmp(owl_history_get_prev(&h), string) == 0);
957    g_free(string);
958  }
959  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
960
961  owl_history_cleanup(&h);
962
963  printf("# END testing owl_history (%d failures)\n", numfailed);
964  return numfailed;
965}
966
967int call_filter_regtest(void)
968{
969  int numfailed = 0;
970  int ret;
971  char *out = NULL;
972  int status;
973
974  printf("# BEGIN testing call_filter\n");
975
976  const char *cat_argv[] = { "cat", NULL };
977  ret = call_filter(cat_argv, "Mangos!", &out, &status);
978  FAIL_UNLESS("call_filter cat", (ret == 0 &&
979                                  status == 0 &&
980                                  strcmp(out, "Mangos!") == 0));
981  g_free(out); out = NULL;
982
983  ret = call_filter(cat_argv, "", &out, &status);
984  FAIL_UNLESS("call_filter cat", (ret == 0 &&
985                                  status == 0 &&
986                                  strcmp(out, "") == 0));
987  g_free(out); out = NULL;
988
989  ret = call_filter(cat_argv, NULL, &out, &status);
990  FAIL_UNLESS("call_filter cat", (ret == 0 &&
991                                  status == 0 &&
992                                  strcmp(out, "") == 0));
993  g_free(out); out = NULL;
994
995  printf("# END testing call_filter (%d failures)\n", numfailed);
996  return numfailed;
997}
Note: See TracBrowser for help on using the repository browser.