source: tester.c @ e56303f

release-1.8release-1.9
Last change on this file since e56303f was e56303f, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
Replace owl_parse_delete with g_strfreev Also cut the pointless g_strdup in owl_cmddict_execute and allow argc to be NULL if we don't care about the value.
  • Property mode set to 100644
File size: 26.9 KB
RevLine 
[5aa33fd]1#define OWL_PERL
2#define WINDOW FAKE_WINDOW
[7d4fbcd]3#include "owl.h"
[5aa33fd]4#undef WINDOW
5
[7d4fbcd]6#include <unistd.h>
7#include <stdlib.h>
8
[5aa33fd]9#undef instr
10#include <curses.h>
11
[b2b0773]12owl_global g;
13
[a2b3289]14int numtests;
[1cf3f8d3]15
[c2673ab]16int owl_regtest(void);
[8bce750]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);
[4cc02605]22int owl_editwin_regtest(void);
[6772d19]23int owl_fmtext_regtest(void);
[b31252d]24int owl_smartfilter_regtest(void);
[8bce750]25
[5aa33fd]26extern void owl_perl_xs_init(pTHX);
27
[094009a]28int main(int argc, char **argv, char **env)
29{
[95414bf]30  FILE *rnull;
31  FILE *wnull;
[c2673ab]32  char *perlerr;
33  int status = 0;
[95414bf]34
35  if (argc <= 1) {
36    fprintf(stderr, "Usage: %s --builtin|TEST.t|-le CODE\n", argv[0]);
37    return 1;
38  }
39
[f034ac0]40  /* initialize a fake ncurses, detached from std{in,out} */
[95414bf]41  wnull = fopen("/dev/null", "w");
42  rnull = fopen("/dev/null", "r");
[f034ac0]43  newterm("xterm", wnull, rnull);
44  /* initialize global structures */
45  owl_global_init(&g);
[124aebc]46
[c2673ab]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  }
[98d7757]54
[c2673ab]55  owl_global_complete_setup(&g);
[04b16f8]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"));
[22e02cd]61
[98d7757]62  owl_function_firstmsg();
[c2673ab]63
[95414bf]64  ENTER;
65  SAVETMPS;
[5aa33fd]66
[95414bf]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]);
[5aa33fd]79
[95414bf]80    eval_pv("do $main::test_prog; die($@) if($@)", true);
81  }
[5aa33fd]82
[95414bf]83  status = 0;
[5aa33fd]84
[95414bf]85  FREETMPS;
86  LEAVE;
[c2673ab]87
88 out:
[5aa33fd]89  perl_destruct(owl_global_get_perlinterp(&g));
90  perl_free(owl_global_get_perlinterp(&g));
[c2673ab]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) {
[a2b3289]99  numtests = 0;
[7d4fbcd]100  int numfailures=0;
[a2b3289]101  /*
102    printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS
103    +OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS);
104  */
[3381399]105  numfailures += owl_util_regtest();
106  numfailures += owl_dict_regtest();
107  numfailures += owl_variable_regtest();
108  numfailures += owl_filter_regtest();
[4cc02605]109  numfailures += owl_editwin_regtest();
[6772d19]110  numfailures += owl_fmtext_regtest();
[b31252d]111  numfailures += owl_smartfilter_regtest();
[3381399]112  if (numfailures) {
[1cf3f8d3]113      fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
[7d4fbcd]114  }
[a2b3289]115  printf("1..%d\n", numtests);
[f034ac0]116
[3381399]117  return(numfailures);
[7d4fbcd]118}
[8bce750]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
[a04218c]132#define CHECK_STR_AND_FREE(desc, expected, expr)         \
133    do {                                                 \
134      char *__value = (expr);                            \
135      FAIL_UNLESS((desc), !strcmp((expected), __value)); \
[ddbbcffa]136      g_free(__value);                                 \
[a04218c]137    } while (0)
138
139  CHECK_STR_AND_FREE("owl_text_substitute 2", "fYZYZ",
140                     owl_text_substitute("foo", "o", "YZ"));
141  CHECK_STR_AND_FREE("owl_text_substitute 3", "foo",
142                     owl_text_substitute("fYZYZ", "YZ", "o"));
143  CHECK_STR_AND_FREE("owl_text_substitute 4", "/u/foo/meep",
144                     owl_text_substitute("~/meep", "~", "/u/foo"));
[8bce750]145
146  FAIL_UNLESS("skiptokens 1", 
147              !strcmp("bar quux", skiptokens("foo bar quux", 1)));
148  FAIL_UNLESS("skiptokens 2", 
149              !strcmp("meep", skiptokens("foo 'bar quux' meep", 2)));
150
[a04218c]151  CHECK_STR_AND_FREE("expand_tabs 1", "        hi", owl_text_expand_tabs("\thi"));
[8bce750]152
[a04218c]153  CHECK_STR_AND_FREE("expand_tabs 2", "        hi\nword    tab",
154                     owl_text_expand_tabs("\thi\nword\ttab"));
[8bce750]155
[a04218c]156  CHECK_STR_AND_FREE("expand_tabs 3", "                2 tabs",
157                     owl_text_expand_tabs("\t\t2 tabs"));
158  CHECK_STR_AND_FREE("expand_tabs 4", "α       ααααααα!        ",
159                     owl_text_expand_tabs(\tααααααα!\t"));
160  CHECK_STR_AND_FREE("expand_tabs 5", "A      AAA!!        ",
161                     owl_text_expand_tabs("A\tAAA!!\t"));
[8bce750]162
[e30ed92]163  FAIL_UNLESS("skiptokens 1",
164              !strcmp("world", skiptokens("hello world", 1)));
165
166  FAIL_UNLESS("skiptokens 2",
167              !strcmp("c d e", skiptokens("a   b c d e", 2)));
168
169  FAIL_UNLESS("skiptokens 3",
170              !strcmp("\"b\" c d e", skiptokens("a \"b\" c d e", 1)));
171
172  FAIL_UNLESS("skiptokens 4",
173              !strcmp("c d e", skiptokens("a \"b\" c d e", 2)));
174
175  FAIL_UNLESS("skiptokens 5",
176              !strcmp("c d e", skiptokens("a \"'\" c d e", 2)));
177
[2bc6ad35]178#define CHECK_QUOTING(desc, unquoted, quoted)           \
179  do {                                                  \
180      int __argc;                                       \
181      char *__quoted = owl_arg_quote(unquoted);         \
182      char **__argv;                                    \
183      FAIL_UNLESS(desc, !strcmp(quoted, __quoted));     \
184      __argv = owl_parseline(__quoted, &__argc);        \
185      FAIL_UNLESS(desc " - arg count", __argc == 1);    \
[65c753e]186      FAIL_UNLESS(desc " - null-terminated",            \
187                  __argv[__argc] == NULL);              \
[2bc6ad35]188      FAIL_UNLESS(desc " - parsed",                     \
189                  !strcmp(__argv[0], unquoted));        \
[e56303f]190      g_strfreev(__argv);                               \
[ddbbcffa]191      g_free(__quoted);                         \
[2bc6ad35]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
[f47696f]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
[8bce750]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));
[a1074de]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));
[8bce750]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
[ddbbcffa]258  owl_list_cleanup(&l, g_free);
[bf7aa1d]259  owl_dict_cleanup(&d, NULL);
[8bce750]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;
[010a951]269  char *value;
[8bce750]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"));
[010a951]277  FAIL_UNLESS("get bool as string",
278              !strcmp((value = owl_variable_get_tostring(&vd,"rxping")), "off"));
279  g_free(value);
[8bce750]280  FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"rxping"));
281  FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"rxping"));
282  FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"rxping","off",0,0));
283  FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"rxping"));
284  FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"rxping","xxx",0,0));
285  FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"rxping"));
286
287
288  FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath")));
289  FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee"));
290  FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath")));
291
292  FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize"));
293  FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble"));
[010a951]294  FAIL_UNLESS("get int as string",
295              !strcmp((value = owl_variable_get_tostring(&vd,"typewinsize")), "8"));
296  g_free(value);
[8bce750]297  FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12));
298  FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize"));
299  FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3));
300  FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize"));
301  FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0,0));
302  FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize"));
303  FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0,0));
304  FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0,0));
305  FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize"));
306
307  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
308  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(&vd, "stringvar", OWL_VARIABLE_STRING)));
309  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(&vd, "stringvar")));
310  owl_variable_set_string(&vd, "stringvar", "new val");
311  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(&vd, "stringvar")));
312
313  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
314  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(&vd, "intvar", OWL_VARIABLE_INT)));
315  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(&vd, "intvar"));
316  owl_variable_set_int(&vd, "intvar", 17);
317  FAIL_UNLESS("update bool val", 17 == owl_variable_get_int(&vd, "intvar"));
318
319  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
320  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(&vd, "boolvar", OWL_VARIABLE_BOOL)));
321  FAIL_UNLESS("get new bool val", owl_variable_get_bool(&vd, "boolvar"));
322  owl_variable_set_bool_off(&vd, "boolvar");
323  FAIL_UNLESS("update string val", !owl_variable_get_bool(&vd, "boolvar"));
324
[0fef6eb]325  owl_variable_dict_cleanup(&vd);
[8bce750]326
327  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
328  printf("# END testing owl_variable (%d failures)\n", numfailed);
329  return(numfailed);
330}
331
[e9c6fc8]332static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
333{
[23fddad]334  owl_filter *f;
[8bce750]335  int ok;
336  int failed = 0;
[23fddad]337  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
[8bce750]338    printf("not ok can't parse %s\n", filt);
339    failed = 1;
340    goto out;
341  }
[23fddad]342  ok = owl_filter_message_match(f, m);
[8bce750]343  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
344    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
345    failed = 1;
346  }
347 out:
[23fddad]348  owl_filter_delete(f);
[8bce750]349  if(!failed) {
350    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
351  }
352  return failed;
353}
354
355int owl_filter_regtest(void) {
356  int numfailed=0;
357  owl_message m;
[23fddad]358  owl_filter *f1, *f2, *f3, *f4, *f5;
[8bce750]359
360  owl_message_init(&m);
361  owl_message_set_type_zephyr(&m);
362  owl_message_set_direction_in(&m);
363  owl_message_set_class(&m, "owl");
364  owl_message_set_instance(&m, "tester");
365  owl_message_set_sender(&m, "owl-user");
366  owl_message_set_recipient(&m, "joe");
367  owl_message_set_attribute(&m, "foo", "bar");
368
369#define TEST_FILTER(f, e) do {                          \
370    numtests++;                                         \
371    numfailed += owl_filter_test_string(f, &m, e);      \
372      } while(0)
373
374  TEST_FILTER("true", 1);
375  TEST_FILTER("false", 0);
376  TEST_FILTER("( true )", 1);
377  TEST_FILTER("not false", 1);
378  TEST_FILTER("( true ) or ( false )", 1);
379  TEST_FILTER("true and false", 0);
380  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
381
382  TEST_FILTER("class owl", 1);
383  TEST_FILTER("class ^owl$", 1);
384  TEST_FILTER("instance test", 1);
385  TEST_FILTER("instance ^test$", 0);
386  TEST_FILTER("instance ^tester$", 1);
387
388  TEST_FILTER("foo bar", 1);
389  TEST_FILTER("class owl and instance tester", 1);
390  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
391
392  /* Order of operations and precedence */
393  TEST_FILTER("not true or false", 0);
394  TEST_FILTER("true or true and false", 0);
395  TEST_FILTER("true and true and false or true", 1);
396  TEST_FILTER("false and false or true", 1);
397  TEST_FILTER("true and false or false", 0);
398
[23fddad]399  f1 = owl_filter_new_fromstring("f1", "class owl");
400  owl_global_add_filter(&g, f1);
[8bce750]401  TEST_FILTER("filter f1", 1);
[23fddad]402  owl_global_remove_filter(&g, "f1");
[8bce750]403
404  /* Test recursion prevention */
[23fddad]405  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
406  owl_filter_delete(f2);
[8bce750]407
408  /* mutual recursion */
[23fddad]409  f3 = owl_filter_new_fromstring("f3", "filter f4");
410  owl_global_add_filter(&g, f3);
411  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
412  owl_global_remove_filter(&g, "f3");
413  owl_filter_delete(f4);
[8bce750]414
415  /* support referencing a filter several times */
[23fddad]416  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
417  owl_filter_delete(f5);
[8bce750]418
419  return 0;
420}
421
[4cc02605]422int owl_editwin_regtest(void) {
423  int numfailed = 0;
424  const char *p;
[35a30f9]425  owl_editwin *oe;
[58a16cc]426  const char *autowrap_string = "we feel our owls should live "
427                                "closer to our ponies.";
[4cc02605]428
429  printf("# BEGIN testing owl_editwin\n");
430
[4123da1]431  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
[4cc02605]432
433  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
434
435  /* check paragraph fill */
436  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
437  owl_editwin_move_to_top(oe);
438  owl_editwin_fill_paragraph(oe);
439  p = owl_editwin_get_text(oe);
440  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
441                                                            "blah blah blah.\n"
442                                                            "\n"
443                                                            "blah"));
444
[9190285]445  owl_editwin_unref(oe); oe = NULL;
[4123da1]446  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
447
[4cc02605]448  /* check that lines ending with ". " correctly fill */
449  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
450  owl_editwin_move_to_top(oe);
451  owl_editwin_fill_paragraph(oe);
452  p = owl_editwin_get_text(oe);
453  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
454                                                            "blah blah blah. \n"
455                                                            "\n"
456                                                            "blah"));
457
[9190285]458  owl_editwin_unref(oe); oe = NULL;
[4cc02605]459
[e5c9d3de]460  /* Test owl_editwin_move_to_beginning_of_line. */
461  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
462  owl_editwin_insert_string(oe, "\n");
463  owl_editwin_insert_string(oe, "12345678\n");
464  owl_editwin_insert_string(oe, "\n");
465  owl_editwin_insert_string(oe, "abcdefg\n");
466  owl_editwin_move_to_top(oe);
467  FAIL_UNLESS("already at beginning of line",
468              owl_editwin_move_to_beginning_of_line(oe) == 0);
469  owl_editwin_line_move(oe, 1);
470  owl_editwin_point_move(oe, 5);
471  FAIL_UNLESS("find beginning of line after empty first line",
472              owl_editwin_move_to_beginning_of_line(oe) == -5);
473  owl_editwin_line_move(oe, 1);
474  FAIL_UNLESS("find beginning empty middle line",
475              owl_editwin_move_to_beginning_of_line(oe) == 0);
476  owl_editwin_line_move(oe, 1);
477  owl_editwin_point_move(oe, 2);
478  FAIL_UNLESS("find beginning of line after empty middle line",
479              owl_editwin_move_to_beginning_of_line(oe) == -2);
[9190285]480  owl_editwin_unref(oe); oe = NULL;
[e5c9d3de]481
[58a16cc]482  /* Test automatic line-wrapping. */
483  owl_global_set_edit_maxwrapcols(&g, 10);
484  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
485  for (p = autowrap_string; *p; p++) {
486    owl_input j;
487    j.ch = *p;
488    j.uch = *p; /* Assuming ASCII. */
489    owl_editwin_process_char(oe, j);
490  }
491  p = owl_editwin_get_text(oe);
492  FAIL_UNLESS("text was automatically wrapped",
493              p && !strcmp(p, "we feel\n"
494                           "our owls\n"
495                           "should\n"
496                           "live\n"
497                           "closer to\n"
498                           "our\n"
499                           "ponies."));
500  owl_editwin_unref(oe); oe = NULL;
501  owl_global_set_edit_maxwrapcols(&g, 70);
502
[ac6d4e4]503  /* Test owl_editwin_current_column. */
504  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
505  FAIL_UNLESS("initial column zero", owl_editwin_current_column(oe) == 0);
506  owl_editwin_insert_string(oe, "abcdef");
507  FAIL_UNLESS("simple insert", owl_editwin_current_column(oe) == 6);
508  owl_editwin_insert_string(oe, "\t");
509  FAIL_UNLESS("insert tabs", owl_editwin_current_column(oe) == 8);
510  owl_editwin_insert_string(oe, "123\n12\t3");
511  FAIL_UNLESS("newline with junk", owl_editwin_current_column(oe) == 9);
512  owl_editwin_move_to_beginning_of_line(oe);
513  FAIL_UNLESS("beginning of line", owl_editwin_current_column(oe) == 0);
514  owl_editwin_unref(oe); oe = NULL;
515
[4cc02605]516  printf("# END testing owl_editwin (%d failures)\n", numfailed);
517
518  return numfailed;
519}
[6772d19]520
521int owl_fmtext_regtest(void) {
522  int numfailed = 0;
[7ba2ad4]523  int start, end;
[6772d19]524  owl_fmtext fm1;
525  owl_fmtext fm2;
[f7456bc]526  owl_regex re;
[6772d19]527  char *str;
528
529  printf("# BEGIN testing owl_fmtext\n");
530
531  owl_fmtext_init_null(&fm1);
532  owl_fmtext_init_null(&fm2);
533
534  /* Verify text gets correctly appended. */
535  owl_fmtext_append_normal(&fm1, "1234567898");
536  owl_fmtext_append_fmtext(&fm2, &fm1);
537  FAIL_UNLESS("string lengths correct",
538              owl_fmtext_num_bytes(&fm2) == strlen(owl_fmtext_get_text(&fm2)));
539
540  /* Test owl_fmtext_num_lines. */
541  owl_fmtext_clear(&fm1);
542  FAIL_UNLESS("empty line correct", owl_fmtext_num_lines(&fm1) == 0);
543  owl_fmtext_append_normal(&fm1, "12345\n67898");
544  FAIL_UNLESS("trailing chars correct", owl_fmtext_num_lines(&fm1) == 2);
545  owl_fmtext_append_normal(&fm1, "\n");
546  FAIL_UNLESS("trailing newline correct", owl_fmtext_num_lines(&fm1) == 2);
547  owl_fmtext_append_bold(&fm1, "");
548  FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2);
549
550  /* Test owl_fmtext_truncate_lines */
551  owl_fmtext_clear(&fm1);
552  owl_fmtext_append_normal(&fm1, "0\n1\n2\n3\n4\n");
553
554  owl_fmtext_clear(&fm2);
555  owl_fmtext_truncate_lines(&fm1, 1, 3, &fm2);
556  str = owl_fmtext_print_plain(&fm2);
557  FAIL_UNLESS("lines corrected truncated",
558              str && !strcmp(str, "1\n2\n3\n"));
[ddbbcffa]559  g_free(str);
[6772d19]560
561  owl_fmtext_clear(&fm2);
562  owl_fmtext_truncate_lines(&fm1, 1, 5, &fm2);
563  str = owl_fmtext_print_plain(&fm2);
564  FAIL_UNLESS("lines corrected truncated",
565              str && !strcmp(str, "1\n2\n3\n4\n"));
[ddbbcffa]566  g_free(str);
[6772d19]567
568  /* Test owl_fmtext_truncate_cols. */
569  owl_fmtext_clear(&fm1);
570  owl_fmtext_append_normal(&fm1, "123456789012345\n");
571  owl_fmtext_append_normal(&fm1, "123456789\n");
572  owl_fmtext_append_normal(&fm1, "1234567890\n");
573
574  owl_fmtext_clear(&fm2);
575  owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2);
576  str = owl_fmtext_print_plain(&fm2);
577  FAIL_UNLESS("columns correctly truncated",
578              str && !strcmp(str, "567890"
579                                  "56789\n"
580                                  "567890"));
[ddbbcffa]581  g_free(str);
[6772d19]582
[e0022d2]583  owl_fmtext_clear(&fm1);
584  owl_fmtext_append_normal(&fm1, "12\t1234");
585  owl_fmtext_append_bold(&fm1, "56\n");
586  owl_fmtext_append_bold(&fm1, "12345678\t\n");
587
588  owl_fmtext_clear(&fm2);
589  owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2);
590  str = owl_fmtext_print_plain(&fm2);
591  FAIL_UNLESS("columns correctly truncated",
592              str && !strcmp(str, "    123456"
593                                  "5678      "));
[ddbbcffa]594  g_free(str);
[e0022d2]595
[2b83ad6]596  /* Test owl_fmtext_expand_tabs. */
597  owl_fmtext_clear(&fm1);
598  owl_fmtext_append_normal(&fm1, "12\t1234");
599  owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
600  owl_fmtext_clear(&fm2);
601  owl_fmtext_expand_tabs(&fm1, &fm2, 0);
602  str = owl_fmtext_print_plain(&fm2);
603  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
604  FAIL_UNLESS("tabs corrected expanded",
605              str && !strcmp(str, "12      1234567 1\n"
606                                  "12345678        1"));
[ddbbcffa]607  g_free(str);
[2b83ad6]608
609  owl_fmtext_clear(&fm2);
610  owl_fmtext_expand_tabs(&fm1, &fm2, 1);
611  str = owl_fmtext_print_plain(&fm2);
612  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
613  FAIL_UNLESS("tabs corrected expanded",
614              str && !strcmp(str, "12     1234567 1\n"
615                                  "12345678       1"));
[ddbbcffa]616  g_free(str);
[2b83ad6]617
[dc9665a]618  /* Test owl_fmtext_search. */
619  owl_fmtext_clear(&fm1);
620  owl_fmtext_append_normal(&fm1, "123123123123");
621  owl_regex_create(&re, "12");
622  {
623    int count = 0, offset;
624    offset = owl_fmtext_search(&fm1, &re, 0);
625    while (offset >= 0) {
626      FAIL_UNLESS("search matches",
627                  !strncmp("12", owl_fmtext_get_text(&fm1) + offset, 2));
628      count++;
629      offset = owl_fmtext_search(&fm1, &re, offset+1);
630    }
631    FAIL_UNLESS("exactly four matches", count == 4);
632  }
633  owl_regex_cleanup(&re);
634
[f7456bc]635  /* Test owl_fmtext_line_number. */
636  owl_fmtext_clear(&fm1);
637  owl_fmtext_append_normal(&fm1, "123\n456\n");
638  owl_fmtext_append_bold(&fm1, "");
639  FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0));
640  FAIL_UNLESS("trailing formatting characters part of false line",
641              2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1)));
642  owl_regex_create_quoted(&re, "456");
643  FAIL_UNLESS("correctly find second line (line 1)",
644              1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0)));
645  owl_regex_cleanup(&re);
646
[7ba2ad4]647  /* Test owl_fmtext_line_extents. */
648  owl_fmtext_clear(&fm1);
649  owl_fmtext_append_normal(&fm1, "123\n456\n789");
650  owl_fmtext_line_extents(&fm1, 1, &start, &end);
651  FAIL_UNLESS("line contents",
652              !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start));
653  owl_fmtext_line_extents(&fm1, 2, &start, &end);
654  FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1));
655
[6772d19]656  owl_fmtext_cleanup(&fm1);
657  owl_fmtext_cleanup(&fm2);
658
659  printf("# END testing owl_fmtext (%d failures)\n", numfailed);
660
661  return numfailed;
662}
[b31252d]663
664static int owl_smartfilter_test_equals(const char *filtname, const char *expected) {
665  owl_filter *f = NULL;
666  char *filtstr = NULL;
667  int failed = 0;
668  f = owl_global_get_filter(&g, filtname);
669  if (f == NULL) {
670    printf("not ok filter missing: %s\n", filtname);
671    failed = 1;
672    goto out;
673  }
674
675  /* TODO: Come up with a better way to test this. */
676  filtstr = owl_filter_print(f);
677  if (strcmp(expected, filtstr)) {
678    printf("not ok filter incorrect: |%s| instead of |%s|\n",
679           filtstr, expected);
680    failed = 1;
681    goto out;
682  }
683
684 out:
[ddbbcffa]685  g_free(filtstr);
[b31252d]686  return failed;
687}
688
689static int owl_classinstfilt_test(const char *c, const char *i, int related, const char *expected) {
690  char *filtname = NULL;
691  int failed = 0;
692
693  filtname = owl_function_classinstfilt(c, i, related);
694  if (filtname == NULL) {
695    printf("not ok null filtname: %s %s %s\n", c, i ? i : "(null)",
696           related ? "related" : "not related");
697    failed = 1;
698    goto out;
699  }
700  if (owl_smartfilter_test_equals(filtname, expected)) {
701    failed = 1;
702    goto out;
703  }
704 out:
705  if (!failed) {
706    printf("ok %s\n", filtname);
707  }
708  if (filtname)
709    owl_global_remove_filter(&g, filtname);
[ddbbcffa]710  g_free(filtname);
[b31252d]711  return failed;
712}
713
[443dcfa]714static int owl_zuserfilt_test(const char *longuser, const char *expected) {
715  char *filtname = NULL;
716  int failed = 0;
717
718  filtname = owl_function_zuserfilt(longuser);
719  if (filtname == NULL) {
720    printf("not ok null filtname: %s\n", longuser);
721    failed = 1;
722    goto out;
723  }
724  if (owl_smartfilter_test_equals(filtname, expected)) {
725    failed = 1;
726    goto out;
727  }
728 out:
729  if (!failed) {
730    printf("ok %s\n", filtname);
731  }
732  if (filtname)
733    owl_global_remove_filter(&g, filtname);
[ddbbcffa]734  g_free(filtname);
[443dcfa]735  return failed;
736}
737
738
[b31252d]739int owl_smartfilter_regtest(void) {
740  int numfailed = 0;
741
742  printf("# BEGIN testing owl_smartfilter\n");
743
744  /* Check classinst making. */
745
746#define TEST_CLASSINSTFILT(c, i, r, e) do {             \
747    numtests++;                                         \
748    numfailed += owl_classinstfilt_test(c, i, r, e);    \
749  } while (0)
750  TEST_CLASSINSTFILT("message", NULL, false,
751                     "class ^message$\n");
752  TEST_CLASSINSTFILT("message", NULL, true,
753                     "class ^(un)*message(\\.d)*$\n");
754  TEST_CLASSINSTFILT("message", "personal", false,
755                     "class ^message$ and instance ^personal$\n");
756  TEST_CLASSINSTFILT("message", "personal", true,
757                     "class ^(un)*message(\\.d)*$ and ( instance ^(un)*personal(\\.d)*$ )\n");
758
759  TEST_CLASSINSTFILT("message", "evil\tinstance", false,
760                     "class ^message$ and instance '^evil\tinstance$'\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  TEST_CLASSINSTFILT("message", "evil$instance", false,
768                     "class ^message$ and instance ^evil\\$instance$\n");
769
[443dcfa]770#define TEST_ZUSERFILT(l, e) do {                       \
771    numtests++;                                         \
772    numfailed += owl_zuserfilt_test(l, e);              \
773  } while (0)
774  TEST_ZUSERFILT("user",
775                 "( type ^zephyr$ and filter personal and "
776                 "( ( direction ^in$ and sender "
777                 "^user$"
778                 " ) or ( direction ^out$ and recipient "
779                 "^user$"
780                 " ) ) ) or ( ( class ^login$ ) and ( sender "
781                 "^user$"
782                 " ) )\n");
783  TEST_ZUSERFILT("very evil\t.user",
784                 "( type ^zephyr$ and filter personal and "
785                 "( ( direction ^in$ and sender "
786                 "'^very evil\t\\.user$'"
787                 " ) or ( direction ^out$ and recipient "
788                 "'^very evil\t\\.user$'"
789                 " ) ) ) or ( ( class ^login$ ) and ( sender "
790                 "'^very evil\t\\.user$'"
791                 " ) )\n");
[b31252d]792
793  printf("# END testing owl_smartfilter (%d failures)\n", numfailed);
794
795  return numfailed;
796}
Note: See TracBrowser for help on using the repository browser.