source: tester.c @ 42115bf9

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