source: tester.c @ aad166a

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