source: tester.c @ 06e04a9

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