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