[5aa33fd] | 1 | #define OWL_PERL |
---|
| 2 | #define WINDOW FAKE_WINDOW |
---|
[7d4fbcd] | 3 | #include "owl.h" |
---|
[5aa33fd] | 4 | #undef WINDOW |
---|
| 5 | |
---|
[bf5e6a2] | 6 | #include <errno.h> |
---|
[7d4fbcd] | 7 | #include <unistd.h> |
---|
[bf5e6a2] | 8 | #include <pwd.h> |
---|
| 9 | #include <stdio.h> |
---|
[7d4fbcd] | 10 | #include <stdlib.h> |
---|
[bf5e6a2] | 11 | #include <string.h> |
---|
| 12 | #include <sys/types.h> |
---|
[7d4fbcd] | 13 | |
---|
[5aa33fd] | 14 | #undef instr |
---|
| 15 | #include <curses.h> |
---|
| 16 | |
---|
[b2b0773] | 17 | owl_global g; |
---|
| 18 | |
---|
[a2b3289] | 19 | int numtests; |
---|
[1cf3f8d3] | 20 | |
---|
[c2673ab] | 21 | int owl_regtest(void); |
---|
[8bce750] | 22 | int owl_util_regtest(void); |
---|
| 23 | int owl_dict_regtest(void); |
---|
| 24 | int owl_variable_regtest(void); |
---|
| 25 | int owl_filter_regtest(void); |
---|
| 26 | int owl_obarray_regtest(void); |
---|
[4cc02605] | 27 | int owl_editwin_regtest(void); |
---|
[6772d19] | 28 | int owl_fmtext_regtest(void); |
---|
[b31252d] | 29 | int owl_smartfilter_regtest(void); |
---|
[25891a8] | 30 | int owl_history_regtest(void); |
---|
[8bce750] | 31 | |
---|
[5aa33fd] | 32 | extern void owl_perl_xs_init(pTHX); |
---|
| 33 | |
---|
[094009a] | 34 | int main(int argc, char **argv, char **env) |
---|
| 35 | { |
---|
[95414bf] | 36 | FILE *rnull; |
---|
| 37 | FILE *wnull; |
---|
[c2673ab] | 38 | char *perlerr; |
---|
| 39 | int status = 0; |
---|
[e93dd78] | 40 | SCREEN *screen; |
---|
[95414bf] | 41 | |
---|
| 42 | if (argc <= 1) { |
---|
| 43 | fprintf(stderr, "Usage: %s --builtin|TEST.t|-le CODE\n", argv[0]); |
---|
| 44 | return 1; |
---|
| 45 | } |
---|
| 46 | |
---|
[f034ac0] | 47 | /* initialize a fake ncurses, detached from std{in,out} */ |
---|
[95414bf] | 48 | wnull = fopen("/dev/null", "w"); |
---|
| 49 | rnull = fopen("/dev/null", "r"); |
---|
[e93dd78] | 50 | screen = newterm("xterm", wnull, rnull); |
---|
[f034ac0] | 51 | /* initialize global structures */ |
---|
| 52 | owl_global_init(&g); |
---|
[124aebc] | 53 | |
---|
[c2673ab] | 54 | perlerr = owl_perlconfig_initperl(NULL, &argc, &argv, &env); |
---|
| 55 | if (perlerr) { |
---|
| 56 | endwin(); |
---|
| 57 | fprintf(stderr, "Internal perl error: %s\n", perlerr); |
---|
| 58 | status = 1; |
---|
| 59 | goto out; |
---|
| 60 | } |
---|
[98d7757] | 61 | |
---|
[c2673ab] | 62 | owl_global_complete_setup(&g); |
---|
[04b16f8] | 63 | owl_global_setup_default_filters(&g); |
---|
| 64 | |
---|
| 65 | owl_view_create(owl_global_get_current_view(&g), "main", |
---|
| 66 | owl_global_get_filter(&g, "all"), |
---|
| 67 | owl_global_get_style_by_name(&g, "default")); |
---|
[22e02cd] | 68 | |
---|
[98d7757] | 69 | owl_function_firstmsg(); |
---|
[c2673ab] | 70 | |
---|
[95414bf] | 71 | ENTER; |
---|
| 72 | SAVETMPS; |
---|
[5aa33fd] | 73 | |
---|
[95414bf] | 74 | if (strcmp(argv[1], "--builtin") == 0) { |
---|
| 75 | status = owl_regtest(); |
---|
| 76 | } else if (strcmp(argv[1], "-le") == 0 && argc > 2) { |
---|
| 77 | /* |
---|
| 78 | * 'prove' runs its harness perl with '-le CODE' to get some |
---|
| 79 | * information out. |
---|
| 80 | */ |
---|
| 81 | moreswitches("l"); |
---|
| 82 | eval_pv(argv[2], true); |
---|
| 83 | } else { |
---|
| 84 | sv_setpv(get_sv("0", false), argv[1]); |
---|
| 85 | sv_setpv(get_sv("main::test_prog", TRUE), argv[1]); |
---|
[5aa33fd] | 86 | |
---|
[95414bf] | 87 | eval_pv("do $main::test_prog; die($@) if($@)", true); |
---|
| 88 | } |
---|
[5aa33fd] | 89 | |
---|
[95414bf] | 90 | status = 0; |
---|
[5aa33fd] | 91 | |
---|
[95414bf] | 92 | FREETMPS; |
---|
| 93 | LEAVE; |
---|
[c2673ab] | 94 | |
---|
| 95 | out: |
---|
[5aa33fd] | 96 | perl_destruct(owl_global_get_perlinterp(&g)); |
---|
| 97 | perl_free(owl_global_get_perlinterp(&g)); |
---|
[c2673ab] | 98 | /* probably not necessary, but tear down the screen */ |
---|
| 99 | endwin(); |
---|
[e93dd78] | 100 | delscreen(screen); |
---|
[c2673ab] | 101 | fclose(rnull); |
---|
| 102 | fclose(wnull); |
---|
| 103 | return status; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | int owl_regtest(void) { |
---|
[a2b3289] | 107 | numtests = 0; |
---|
[7d4fbcd] | 108 | int numfailures=0; |
---|
[a2b3289] | 109 | /* |
---|
| 110 | printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS |
---|
| 111 | +OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS); |
---|
| 112 | */ |
---|
[3381399] | 113 | numfailures += owl_util_regtest(); |
---|
| 114 | numfailures += owl_dict_regtest(); |
---|
| 115 | numfailures += owl_variable_regtest(); |
---|
| 116 | numfailures += owl_filter_regtest(); |
---|
[4cc02605] | 117 | numfailures += owl_editwin_regtest(); |
---|
[6772d19] | 118 | numfailures += owl_fmtext_regtest(); |
---|
[b31252d] | 119 | numfailures += owl_smartfilter_regtest(); |
---|
[25891a8] | 120 | numfailures += owl_history_regtest(); |
---|
[3381399] | 121 | if (numfailures) { |
---|
[1cf3f8d3] | 122 | fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures); |
---|
[7d4fbcd] | 123 | } |
---|
[a2b3289] | 124 | printf("1..%d\n", numtests); |
---|
[f034ac0] | 125 | |
---|
[3381399] | 126 | return(numfailures); |
---|
[7d4fbcd] | 127 | } |
---|
[8bce750] | 128 | |
---|
| 129 | #define FAIL_UNLESS(desc,pred) do { int __pred = (pred); \ |
---|
| 130 | numtests++; \ |
---|
[14c9e05] | 131 | printf("%s %d %s", (__pred) ? "ok" : (numfailed++, "not ok"), numtests, desc); \ |
---|
[8bce750] | 132 | if(!(__pred)) printf("\t(%s:%d)", __FILE__, __LINE__); printf("%c", '\n'); } while(0) |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | int owl_util_regtest(void) |
---|
| 136 | { |
---|
| 137 | int numfailed=0; |
---|
[bf5e6a2] | 138 | const char *home; |
---|
| 139 | char *s, *path; |
---|
| 140 | struct passwd *pw; |
---|
[8bce750] | 141 | |
---|
| 142 | printf("# BEGIN testing owl_util\n"); |
---|
| 143 | |
---|
[a04218c] | 144 | #define CHECK_STR_AND_FREE(desc, expected, expr) \ |
---|
| 145 | do { \ |
---|
| 146 | char *__value = (expr); \ |
---|
| 147 | FAIL_UNLESS((desc), !strcmp((expected), __value)); \ |
---|
[ddbbcffa] | 148 | g_free(__value); \ |
---|
[a04218c] | 149 | } while (0) |
---|
| 150 | |
---|
| 151 | CHECK_STR_AND_FREE("owl_text_substitute 2", "fYZYZ", |
---|
| 152 | owl_text_substitute("foo", "o", "YZ")); |
---|
| 153 | CHECK_STR_AND_FREE("owl_text_substitute 3", "foo", |
---|
| 154 | owl_text_substitute("fYZYZ", "YZ", "o")); |
---|
| 155 | CHECK_STR_AND_FREE("owl_text_substitute 4", "/u/foo/meep", |
---|
| 156 | owl_text_substitute("~/meep", "~", "/u/foo")); |
---|
[8bce750] | 157 | |
---|
| 158 | FAIL_UNLESS("skiptokens 1", |
---|
| 159 | !strcmp("bar quux", skiptokens("foo bar quux", 1))); |
---|
| 160 | FAIL_UNLESS("skiptokens 2", |
---|
| 161 | !strcmp("meep", skiptokens("foo 'bar quux' meep", 2))); |
---|
| 162 | |
---|
[a04218c] | 163 | CHECK_STR_AND_FREE("expand_tabs 1", " hi", owl_text_expand_tabs("\thi")); |
---|
[8bce750] | 164 | |
---|
[a04218c] | 165 | CHECK_STR_AND_FREE("expand_tabs 2", " hi\nword tab", |
---|
| 166 | owl_text_expand_tabs("\thi\nword\ttab")); |
---|
[8bce750] | 167 | |
---|
[a04218c] | 168 | CHECK_STR_AND_FREE("expand_tabs 3", " 2 tabs", |
---|
| 169 | owl_text_expand_tabs("\t\t2 tabs")); |
---|
| 170 | CHECK_STR_AND_FREE("expand_tabs 4", "α ααααααα! ", |
---|
| 171 | owl_text_expand_tabs("α\tααααααα!\t")); |
---|
| 172 | CHECK_STR_AND_FREE("expand_tabs 5", "A AAA!! ", |
---|
| 173 | owl_text_expand_tabs("A\tAAA!!\t")); |
---|
[8bce750] | 174 | |
---|
[e30ed92] | 175 | FAIL_UNLESS("skiptokens 1", |
---|
| 176 | !strcmp("world", skiptokens("hello world", 1))); |
---|
| 177 | |
---|
| 178 | FAIL_UNLESS("skiptokens 2", |
---|
| 179 | !strcmp("c d e", skiptokens("a b c d e", 2))); |
---|
| 180 | |
---|
| 181 | FAIL_UNLESS("skiptokens 3", |
---|
| 182 | !strcmp("\"b\" c d e", skiptokens("a \"b\" c d e", 1))); |
---|
| 183 | |
---|
| 184 | FAIL_UNLESS("skiptokens 4", |
---|
| 185 | !strcmp("c d e", skiptokens("a \"b\" c d e", 2))); |
---|
| 186 | |
---|
| 187 | FAIL_UNLESS("skiptokens 5", |
---|
| 188 | !strcmp("c d e", skiptokens("a \"'\" c d e", 2))); |
---|
| 189 | |
---|
[2bc6ad35] | 190 | #define CHECK_QUOTING(desc, unquoted, quoted) \ |
---|
| 191 | do { \ |
---|
| 192 | int __argc; \ |
---|
| 193 | char *__quoted = owl_arg_quote(unquoted); \ |
---|
| 194 | char **__argv; \ |
---|
| 195 | FAIL_UNLESS(desc, !strcmp(quoted, __quoted)); \ |
---|
| 196 | __argv = owl_parseline(__quoted, &__argc); \ |
---|
| 197 | FAIL_UNLESS(desc " - arg count", __argc == 1); \ |
---|
[65c753e] | 198 | FAIL_UNLESS(desc " - null-terminated", \ |
---|
| 199 | __argv[__argc] == NULL); \ |
---|
[2bc6ad35] | 200 | FAIL_UNLESS(desc " - parsed", \ |
---|
| 201 | !strcmp(__argv[0], unquoted)); \ |
---|
[e56303f] | 202 | g_strfreev(__argv); \ |
---|
[ddbbcffa] | 203 | g_free(__quoted); \ |
---|
[2bc6ad35] | 204 | } while (0) |
---|
| 205 | |
---|
| 206 | CHECK_QUOTING("boring text", "mango", "mango"); |
---|
| 207 | CHECK_QUOTING("spaces", "mangos are tasty", "'mangos are tasty'"); |
---|
| 208 | CHECK_QUOTING("single quotes", "mango's", "\"mango's\""); |
---|
| 209 | CHECK_QUOTING("double quotes", "he said \"mangos are tasty\"", |
---|
| 210 | "'he said \"mangos are tasty\"'"); |
---|
| 211 | CHECK_QUOTING("both quotes", |
---|
| 212 | "he said \"mango's are tasty even when you put in " |
---|
| 213 | "a random apostrophe\"", |
---|
| 214 | "\"he said \"'\"'\"mango's are tasty even when you put in " |
---|
| 215 | "a random apostrophe\"'\"'\"\""); |
---|
| 216 | CHECK_QUOTING("quote monster", "'\"\"'\"'''\"", |
---|
| 217 | "\"" |
---|
| 218 | "'" |
---|
| 219 | "\"'\"'\"" |
---|
| 220 | "\"'\"'\"" |
---|
| 221 | "'" |
---|
| 222 | "\"'\"'\"" |
---|
| 223 | "'" |
---|
| 224 | "'" |
---|
| 225 | "'" |
---|
| 226 | "\"'\"'\"" |
---|
| 227 | "\""); |
---|
| 228 | |
---|
[f47696f] | 229 | GString *g = g_string_new(""); |
---|
| 230 | owl_string_appendf_quoted(g, "%q foo %q%q %s %", "hello", "world is", "can't"); |
---|
| 231 | FAIL_UNLESS("owl_string_appendf", |
---|
[596c8b10] | 232 | !strcmp(g->str, "hello foo 'world is'\"can't\" %s %")); |
---|
| 233 | g_string_free(g, true); |
---|
[f47696f] | 234 | |
---|
[7feba19] | 235 | |
---|
| 236 | s = owl_util_baseclass("barnowl"); |
---|
| 237 | FAIL_UNLESS("baseclass barnowl", !strcmp("barnowl", s)); |
---|
| 238 | g_free(s); |
---|
| 239 | s = owl_util_baseclass("unbarnowl"); |
---|
| 240 | FAIL_UNLESS("baseclass unbarnowl", !strcmp("barnowl", s)); |
---|
| 241 | g_free(s); |
---|
| 242 | s = owl_util_baseclass("unununbarnowl.d.d"); |
---|
| 243 | FAIL_UNLESS("baseclass unununbarnowl.d.d", !strcmp("barnowl", s)); |
---|
| 244 | g_free(s); |
---|
| 245 | s = owl_util_baseclass("ununun.d.d"); |
---|
| 246 | FAIL_UNLESS("baseclass ununun.d.d", !strcmp("", s)); |
---|
| 247 | g_free(s); |
---|
| 248 | s = owl_util_baseclass("d.d.d.d"); |
---|
| 249 | FAIL_UNLESS("baseclass d.d.d.d", !strcmp("d", s)); |
---|
| 250 | g_free(s); |
---|
| 251 | s = owl_util_baseclass("n.d.d.d"); |
---|
| 252 | FAIL_UNLESS("baseclass n.d.d.d", !strcmp("n", s)); |
---|
| 253 | g_free(s); |
---|
| 254 | s = owl_util_baseclass("ununun."); |
---|
| 255 | FAIL_UNLESS("baseclass ununun.", !strcmp(".", s)); |
---|
| 256 | g_free(s); |
---|
| 257 | s = owl_util_baseclass("unununu"); |
---|
| 258 | FAIL_UNLESS("baseclass unununu", !strcmp("u", s)); |
---|
| 259 | g_free(s); |
---|
| 260 | |
---|
| 261 | |
---|
[bf5e6a2] | 262 | s = owl_util_makepath("foo/bar"); |
---|
| 263 | FAIL_UNLESS("makepath foo/bar", !strcmp("foo/bar", s)); |
---|
| 264 | g_free(s); |
---|
| 265 | s = owl_util_makepath("//foo///bar"); |
---|
| 266 | FAIL_UNLESS("makepath //foo///bar", !strcmp("/foo/bar", s)); |
---|
| 267 | g_free(s); |
---|
| 268 | s = owl_util_makepath("foo/~//bar/"); |
---|
| 269 | FAIL_UNLESS("makepath foo/~//bar/", !strcmp("foo/~/bar/", s)); |
---|
| 270 | g_free(s); |
---|
| 271 | s = owl_util_makepath("~thisuserhadreallybetternotexist/foobar/"); |
---|
| 272 | FAIL_UNLESS("makepath ~thisuserhadreallybetternotexist/foobar/", |
---|
| 273 | !strcmp("~thisuserhadreallybetternotexist/foobar/", s)); |
---|
| 274 | g_free(s); |
---|
| 275 | |
---|
| 276 | errno = 0; |
---|
| 277 | pw = getpwuid(getuid()); |
---|
| 278 | if (pw) { |
---|
| 279 | home = pw->pw_dir; |
---|
| 280 | } else { |
---|
| 281 | /* Just make some noise so we notice. */ |
---|
| 282 | home = "<WHAT>"; |
---|
| 283 | fprintf(stderr, "getpwuid: %s", errno ? strerror(errno) : "No such user"); |
---|
| 284 | } |
---|
| 285 | s = owl_util_makepath("~"); |
---|
| 286 | FAIL_UNLESS("makepath ~", !strcmp(home, s)); |
---|
| 287 | g_free(s); |
---|
| 288 | |
---|
| 289 | path = g_strconcat(home, "/foo/bar/baz", NULL); |
---|
| 290 | s = owl_util_makepath("~///foo/bar//baz"); |
---|
| 291 | FAIL_UNLESS("makepath ~///foo/bar//baz", !strcmp(path, s)); |
---|
| 292 | g_free(s); |
---|
| 293 | g_free(path); |
---|
| 294 | |
---|
| 295 | errno = 0; |
---|
| 296 | pw = getpwnam("root"); |
---|
| 297 | if (pw) { |
---|
| 298 | home = pw->pw_dir; |
---|
| 299 | } else { |
---|
| 300 | /* Just make some noise so we notice. */ |
---|
| 301 | home = "<WHAT>"; |
---|
| 302 | fprintf(stderr, "getpwnam: %s", errno ? strerror(errno) : "No such user"); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | s = owl_util_makepath("~root"); |
---|
| 306 | FAIL_UNLESS("makepath ~root", !strcmp(home, s)); |
---|
| 307 | g_free(s); |
---|
| 308 | |
---|
| 309 | path = g_strconcat(home, "/foo/bar/baz", NULL); |
---|
| 310 | s = owl_util_makepath("~root///foo/bar//baz"); |
---|
| 311 | FAIL_UNLESS("makepath ~root///foo/bar//baz", !strcmp(path, s)); |
---|
| 312 | g_free(s); |
---|
| 313 | g_free(path); |
---|
[7feba19] | 314 | |
---|
[8bce750] | 315 | /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */ |
---|
| 316 | printf("# END testing owl_util (%d failures)\n", numfailed); |
---|
| 317 | return(numfailed); |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | int owl_dict_regtest(void) { |
---|
| 321 | owl_dict d; |
---|
[ce68f23] | 322 | GPtrArray *l; |
---|
[8bce750] | 323 | int numfailed=0; |
---|
[4e37d56] | 324 | char *av = g_strdup("aval"), *bv = g_strdup("bval"), *cv = g_strdup("cval"), |
---|
| 325 | *dv = g_strdup("dval"); |
---|
[8bce750] | 326 | |
---|
| 327 | printf("# BEGIN testing owl_dict\n"); |
---|
[4c7c21f] | 328 | owl_dict_create(&d); |
---|
[a1074de] | 329 | FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete)); |
---|
| 330 | FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete)); |
---|
| 331 | FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete)); |
---|
| 332 | FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete)); |
---|
[8bce750] | 333 | FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0)); |
---|
| 334 | FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a")); |
---|
| 335 | FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b")); |
---|
| 336 | FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c")); |
---|
| 337 | FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d")); |
---|
| 338 | FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e")); |
---|
| 339 | FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d")); |
---|
| 340 | FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d")); |
---|
| 341 | |
---|
| 342 | FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d)); |
---|
[ce68f23] | 343 | l = owl_dict_get_keys(&d); |
---|
| 344 | FAIL_UNLESS("get_keys result size", 3 == l->len); |
---|
[8bce750] | 345 | |
---|
| 346 | /* these assume the returned keys are sorted */ |
---|
[ce68f23] | 347 | FAIL_UNLESS("get_keys result val", 0 == strcmp("a", l->pdata[0])); |
---|
| 348 | FAIL_UNLESS("get_keys result val", 0 == strcmp("b", l->pdata[1])); |
---|
| 349 | FAIL_UNLESS("get_keys result val", 0 == strcmp("c", l->pdata[2])); |
---|
[8bce750] | 350 | |
---|
[ce68f23] | 351 | owl_ptr_array_free(l, g_free); |
---|
[bf7aa1d] | 352 | owl_dict_cleanup(&d, NULL); |
---|
[8bce750] | 353 | |
---|
[4e37d56] | 354 | g_free(av); |
---|
| 355 | g_free(bv); |
---|
| 356 | g_free(cv); |
---|
| 357 | g_free(dv); |
---|
| 358 | |
---|
[8bce750] | 359 | /* if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */ |
---|
| 360 | printf("# END testing owl_dict (%d failures)\n", numfailed); |
---|
| 361 | return(numfailed); |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | int owl_variable_regtest(void) { |
---|
| 365 | owl_vardict vd; |
---|
[ca54fd6] | 366 | owl_variable *var; |
---|
[8bce750] | 367 | int numfailed=0; |
---|
[010a951] | 368 | char *value; |
---|
[8bce750] | 369 | const void *v; |
---|
| 370 | |
---|
| 371 | printf("# BEGIN testing owl_variable\n"); |
---|
| 372 | FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd)); |
---|
| 373 | |
---|
[ca54fd6] | 374 | FAIL_UNLESS("get bool var", NULL != (var = owl_variable_get_var(&vd, "rxping"))); |
---|
| 375 | FAIL_UNLESS("get bool", 0 == owl_variable_get_bool(var)); |
---|
| 376 | FAIL_UNLESS("get bool (no such)", NULL == owl_variable_get_var(&vd, "mumble")); |
---|
[010a951] | 377 | FAIL_UNLESS("get bool as string", |
---|
[ca54fd6] | 378 | !strcmp((value = owl_variable_get_tostring(var)), "off")); |
---|
[010a951] | 379 | g_free(value); |
---|
[ca54fd6] | 380 | FAIL_UNLESS("set bool 1", 0 == owl_variable_set_bool_on(var)); |
---|
| 381 | FAIL_UNLESS("get bool 2", 1 == owl_variable_get_bool(var)); |
---|
| 382 | FAIL_UNLESS("set bool 3", 0 == owl_variable_set_fromstring(var, "off", 0)); |
---|
| 383 | FAIL_UNLESS("get bool 4", 0 == owl_variable_get_bool(var)); |
---|
| 384 | FAIL_UNLESS("set bool 5", -1 == owl_variable_set_fromstring(var, "xxx", 0)); |
---|
| 385 | FAIL_UNLESS("get bool 6", 0 == owl_variable_get_bool(var)); |
---|
| 386 | |
---|
| 387 | |
---|
| 388 | FAIL_UNLESS("get string var", NULL != (var = owl_variable_get_var(&vd, "logpath"))); |
---|
| 389 | FAIL_UNLESS("get string", 0 == strcmp("~/zlog/people", owl_variable_get_string(var))); |
---|
| 390 | FAIL_UNLESS("set string 7", 0 == owl_variable_set_string(var, "whee")); |
---|
| 391 | FAIL_UNLESS("get string", !strcmp("whee", owl_variable_get_string(var))); |
---|
| 392 | |
---|
| 393 | FAIL_UNLESS("get int var", NULL != (var = owl_variable_get_var(&vd, "typewinsize"))); |
---|
| 394 | FAIL_UNLESS("get int", 8 == owl_variable_get_int(var)); |
---|
| 395 | FAIL_UNLESS("get int (no such)", NULL == owl_variable_get_var(&vd, "mumble")); |
---|
[010a951] | 396 | FAIL_UNLESS("get int as string", |
---|
[ca54fd6] | 397 | !strcmp((value = owl_variable_get_tostring(var)), "8")); |
---|
[010a951] | 398 | g_free(value); |
---|
[ca54fd6] | 399 | FAIL_UNLESS("set int 1", 0 == owl_variable_set_int(var, 12)); |
---|
| 400 | FAIL_UNLESS("get int 2", 12 == owl_variable_get_int(var)); |
---|
| 401 | FAIL_UNLESS("set int 1b", -1 == owl_variable_set_int(var, -3)); |
---|
| 402 | FAIL_UNLESS("get int 2b", 12 == owl_variable_get_int(var)); |
---|
| 403 | FAIL_UNLESS("set int 3", 0 == owl_variable_set_fromstring(var, "9", 0)); |
---|
| 404 | FAIL_UNLESS("get int 4", 9 == owl_variable_get_int(var)); |
---|
| 405 | FAIL_UNLESS("set int 5", -1 == owl_variable_set_fromstring(var, "xxx", 0)); |
---|
| 406 | FAIL_UNLESS("set int 6", -1 == owl_variable_set_fromstring(var, "", 0)); |
---|
| 407 | FAIL_UNLESS("get int 7", 9 == owl_variable_get_int(var)); |
---|
[8bce750] | 408 | |
---|
| 409 | owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval"); |
---|
[ca54fd6] | 410 | FAIL_UNLESS("get new string var", NULL != (var = owl_variable_get_var(&vd, "stringvar"))); |
---|
| 411 | FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(var))); |
---|
| 412 | FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(var))); |
---|
| 413 | owl_variable_set_string(var, "new val"); |
---|
| 414 | FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(var))); |
---|
[8bce750] | 415 | |
---|
| 416 | owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47); |
---|
[ca54fd6] | 417 | FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar"))); |
---|
| 418 | FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(var))); |
---|
| 419 | FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var)); |
---|
| 420 | owl_variable_set_int(var, 17); |
---|
| 421 | FAIL_UNLESS("update int val", 17 == owl_variable_get_int(var)); |
---|
[8bce750] | 422 | |
---|
| 423 | owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1); |
---|
[ca54fd6] | 424 | FAIL_UNLESS("get new bool var", NULL != (var = owl_variable_get_var(&vd, "boolvar"))); |
---|
| 425 | FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(var))); |
---|
| 426 | FAIL_UNLESS("get new bool val", owl_variable_get_bool(var)); |
---|
| 427 | owl_variable_set_bool_off(var); |
---|
| 428 | FAIL_UNLESS("update bool val", !owl_variable_get_bool(var)); |
---|
[8bce750] | 429 | |
---|
[b4a678a] | 430 | owl_variable_dict_newvar_string(&vd, "nullstringvar", "", "", NULL); |
---|
| 431 | FAIL_UNLESS("get new string (NULL) var", NULL != (var = owl_variable_get_var(&vd, "nullstringvar"))); |
---|
| 432 | FAIL_UNLESS("get string (NULL)", NULL == (value = owl_variable_get_tostring(var))); |
---|
| 433 | g_free(value); |
---|
| 434 | var = owl_variable_get_var(&vd, "zsigproc"); |
---|
| 435 | FAIL_UNLESS("get string (NULL) 2", NULL == (value = owl_variable_get_tostring(var))); |
---|
| 436 | g_free(value); |
---|
| 437 | |
---|
[0fef6eb] | 438 | owl_variable_dict_cleanup(&vd); |
---|
[8bce750] | 439 | |
---|
| 440 | /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */ |
---|
| 441 | printf("# END testing owl_variable (%d failures)\n", numfailed); |
---|
| 442 | return(numfailed); |
---|
| 443 | } |
---|
| 444 | |
---|
[e9c6fc8] | 445 | static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch) |
---|
| 446 | { |
---|
[23fddad] | 447 | owl_filter *f; |
---|
[8bce750] | 448 | int ok; |
---|
| 449 | int failed = 0; |
---|
[23fddad] | 450 | if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) { |
---|
[8bce750] | 451 | printf("not ok can't parse %s\n", filt); |
---|
| 452 | failed = 1; |
---|
| 453 | goto out; |
---|
| 454 | } |
---|
[23fddad] | 455 | ok = owl_filter_message_match(f, m); |
---|
[8bce750] | 456 | if((shouldmatch && !ok) || (!shouldmatch && ok)) { |
---|
| 457 | printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch); |
---|
| 458 | failed = 1; |
---|
| 459 | } |
---|
| 460 | out: |
---|
[23fddad] | 461 | owl_filter_delete(f); |
---|
[8bce750] | 462 | if(!failed) { |
---|
| 463 | printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt); |
---|
| 464 | } |
---|
| 465 | return failed; |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | int owl_filter_regtest(void) { |
---|
| 469 | int numfailed=0; |
---|
| 470 | owl_message m; |
---|
[23fddad] | 471 | owl_filter *f1, *f2, *f3, *f4, *f5; |
---|
[8bce750] | 472 | |
---|
| 473 | owl_message_init(&m); |
---|
| 474 | owl_message_set_type_zephyr(&m); |
---|
| 475 | owl_message_set_direction_in(&m); |
---|
| 476 | owl_message_set_class(&m, "owl"); |
---|
| 477 | owl_message_set_instance(&m, "tester"); |
---|
| 478 | owl_message_set_sender(&m, "owl-user"); |
---|
| 479 | owl_message_set_recipient(&m, "joe"); |
---|
| 480 | owl_message_set_attribute(&m, "foo", "bar"); |
---|
| 481 | |
---|
| 482 | #define TEST_FILTER(f, e) do { \ |
---|
| 483 | numtests++; \ |
---|
| 484 | numfailed += owl_filter_test_string(f, &m, e); \ |
---|
| 485 | } while(0) |
---|
| 486 | |
---|
| 487 | TEST_FILTER("true", 1); |
---|
| 488 | TEST_FILTER("false", 0); |
---|
| 489 | TEST_FILTER("( true )", 1); |
---|
| 490 | TEST_FILTER("not false", 1); |
---|
| 491 | TEST_FILTER("( true ) or ( false )", 1); |
---|
| 492 | TEST_FILTER("true and false", 0); |
---|
| 493 | TEST_FILTER("( true or true ) or ( ( false ) )", 1); |
---|
| 494 | |
---|
| 495 | TEST_FILTER("class owl", 1); |
---|
| 496 | TEST_FILTER("class ^owl$", 1); |
---|
| 497 | TEST_FILTER("instance test", 1); |
---|
| 498 | TEST_FILTER("instance ^test$", 0); |
---|
| 499 | TEST_FILTER("instance ^tester$", 1); |
---|
| 500 | |
---|
| 501 | TEST_FILTER("foo bar", 1); |
---|
| 502 | TEST_FILTER("class owl and instance tester", 1); |
---|
| 503 | TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1); |
---|
| 504 | |
---|
| 505 | /* Order of operations and precedence */ |
---|
| 506 | TEST_FILTER("not true or false", 0); |
---|
| 507 | TEST_FILTER("true or true and false", 0); |
---|
| 508 | TEST_FILTER("true and true and false or true", 1); |
---|
| 509 | TEST_FILTER("false and false or true", 1); |
---|
| 510 | TEST_FILTER("true and false or false", 0); |
---|
| 511 | |
---|
[23fddad] | 512 | f1 = owl_filter_new_fromstring("f1", "class owl"); |
---|
| 513 | owl_global_add_filter(&g, f1); |
---|
[8bce750] | 514 | TEST_FILTER("filter f1", 1); |
---|
[23fddad] | 515 | owl_global_remove_filter(&g, "f1"); |
---|
[8bce750] | 516 | |
---|
| 517 | /* Test recursion prevention */ |
---|
[23fddad] | 518 | FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL); |
---|
| 519 | owl_filter_delete(f2); |
---|
[8bce750] | 520 | |
---|
| 521 | /* mutual recursion */ |
---|
[23fddad] | 522 | f3 = owl_filter_new_fromstring("f3", "filter f4"); |
---|
| 523 | owl_global_add_filter(&g, f3); |
---|
| 524 | FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL); |
---|
| 525 | owl_global_remove_filter(&g, "f3"); |
---|
| 526 | owl_filter_delete(f4); |
---|
[8bce750] | 527 | |
---|
| 528 | /* support referencing a filter several times */ |
---|
[23fddad] | 529 | FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL); |
---|
| 530 | owl_filter_delete(f5); |
---|
[8bce750] | 531 | |
---|
| 532 | return 0; |
---|
| 533 | } |
---|
| 534 | |
---|
[4cc02605] | 535 | int owl_editwin_regtest(void) { |
---|
| 536 | int numfailed = 0; |
---|
| 537 | const char *p; |
---|
[35a30f9] | 538 | owl_editwin *oe; |
---|
[58a16cc] | 539 | const char *autowrap_string = "we feel our owls should live " |
---|
| 540 | "closer to our ponies."; |
---|
[4cc02605] | 541 | |
---|
| 542 | printf("# BEGIN testing owl_editwin\n"); |
---|
| 543 | |
---|
[4123da1] | 544 | oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL); |
---|
[4cc02605] | 545 | |
---|
| 546 | /* TODO: make the strings a little more lenient w.r.t trailing whitespace */ |
---|
| 547 | |
---|
| 548 | /* check paragraph fill */ |
---|
| 549 | owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah"); |
---|
| 550 | owl_editwin_move_to_top(oe); |
---|
| 551 | owl_editwin_fill_paragraph(oe); |
---|
| 552 | p = owl_editwin_get_text(oe); |
---|
| 553 | FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n" |
---|
| 554 | "blah blah blah.\n" |
---|
| 555 | "\n" |
---|
| 556 | "blah")); |
---|
| 557 | |
---|
[9190285] | 558 | owl_editwin_unref(oe); oe = NULL; |
---|
[4123da1] | 559 | oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL); |
---|
| 560 | |
---|
[4cc02605] | 561 | /* check that lines ending with ". " correctly fill */ |
---|
| 562 | owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah"); |
---|
| 563 | owl_editwin_move_to_top(oe); |
---|
| 564 | owl_editwin_fill_paragraph(oe); |
---|
| 565 | p = owl_editwin_get_text(oe); |
---|
| 566 | FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n" |
---|
| 567 | "blah blah blah. \n" |
---|
| 568 | "\n" |
---|
| 569 | "blah")); |
---|
| 570 | |
---|
[9190285] | 571 | owl_editwin_unref(oe); oe = NULL; |
---|
[4cc02605] | 572 | |
---|
[e5c9d3de] | 573 | /* Test owl_editwin_move_to_beginning_of_line. */ |
---|
| 574 | oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL); |
---|
| 575 | owl_editwin_insert_string(oe, "\n"); |
---|
| 576 | owl_editwin_insert_string(oe, "12345678\n"); |
---|
| 577 | owl_editwin_insert_string(oe, "\n"); |
---|
| 578 | owl_editwin_insert_string(oe, "abcdefg\n"); |
---|
| 579 | owl_editwin_move_to_top(oe); |
---|
| 580 | FAIL_UNLESS("already at beginning of line", |
---|
| 581 | owl_editwin_move_to_beginning_of_line(oe) == 0); |
---|
| 582 | owl_editwin_line_move(oe, 1); |
---|
| 583 | owl_editwin_point_move(oe, 5); |
---|
| 584 | FAIL_UNLESS("find beginning of line after empty first line", |
---|
| 585 | owl_editwin_move_to_beginning_of_line(oe) == -5); |
---|
| 586 | owl_editwin_line_move(oe, 1); |
---|
| 587 | FAIL_UNLESS("find beginning empty middle line", |
---|
| 588 | owl_editwin_move_to_beginning_of_line(oe) == 0); |
---|
| 589 | owl_editwin_line_move(oe, 1); |
---|
| 590 | owl_editwin_point_move(oe, 2); |
---|
| 591 | FAIL_UNLESS("find beginning of line after empty middle line", |
---|
| 592 | owl_editwin_move_to_beginning_of_line(oe) == -2); |
---|
[9190285] | 593 | owl_editwin_unref(oe); oe = NULL; |
---|
[e5c9d3de] | 594 | |
---|
[58a16cc] | 595 | /* Test automatic line-wrapping. */ |
---|
| 596 | owl_global_set_edit_maxwrapcols(&g, 10); |
---|
| 597 | oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL); |
---|
| 598 | for (p = autowrap_string; *p; p++) { |
---|
| 599 | owl_input j; |
---|
| 600 | j.ch = *p; |
---|
| 601 | j.uch = *p; /* Assuming ASCII. */ |
---|
| 602 | owl_editwin_process_char(oe, j); |
---|
| 603 | } |
---|
| 604 | p = owl_editwin_get_text(oe); |
---|
| 605 | FAIL_UNLESS("text was automatically wrapped", |
---|
| 606 | p && !strcmp(p, "we feel\n" |
---|
| 607 | "our owls\n" |
---|
| 608 | "should\n" |
---|
| 609 | "live\n" |
---|
| 610 | "closer to\n" |
---|
| 611 | "our\n" |
---|
| 612 | "ponies.")); |
---|
| 613 | owl_editwin_unref(oe); oe = NULL; |
---|
| 614 | owl_global_set_edit_maxwrapcols(&g, 70); |
---|
| 615 | |
---|
[ac6d4e4] | 616 | /* Test owl_editwin_current_column. */ |
---|
| 617 | oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL); |
---|
| 618 | FAIL_UNLESS("initial column zero", owl_editwin_current_column(oe) == 0); |
---|
| 619 | owl_editwin_insert_string(oe, "abcdef"); |
---|
| 620 | FAIL_UNLESS("simple insert", owl_editwin_current_column(oe) == 6); |
---|
| 621 | owl_editwin_insert_string(oe, "\t"); |
---|
| 622 | FAIL_UNLESS("insert tabs", owl_editwin_current_column(oe) == 8); |
---|
| 623 | owl_editwin_insert_string(oe, "123\n12\t3"); |
---|
| 624 | FAIL_UNLESS("newline with junk", owl_editwin_current_column(oe) == 9); |
---|
| 625 | owl_editwin_move_to_beginning_of_line(oe); |
---|
| 626 | FAIL_UNLESS("beginning of line", owl_editwin_current_column(oe) == 0); |
---|
| 627 | owl_editwin_unref(oe); oe = NULL; |
---|
| 628 | |
---|
[4cc02605] | 629 | printf("# END testing owl_editwin (%d failures)\n", numfailed); |
---|
| 630 | |
---|
| 631 | return numfailed; |
---|
| 632 | } |
---|
[6772d19] | 633 | |
---|
| 634 | int owl_fmtext_regtest(void) { |
---|
| 635 | int numfailed = 0; |
---|
[7ba2ad4] | 636 | int start, end; |
---|
[6772d19] | 637 | owl_fmtext fm1; |
---|
| 638 | owl_fmtext fm2; |
---|
[f7456bc] | 639 | owl_regex re; |
---|
[6772d19] | 640 | char *str; |
---|
| 641 | |
---|
| 642 | printf("# BEGIN testing owl_fmtext\n"); |
---|
| 643 | |
---|
| 644 | owl_fmtext_init_null(&fm1); |
---|
| 645 | owl_fmtext_init_null(&fm2); |
---|
| 646 | |
---|
| 647 | /* Verify text gets correctly appended. */ |
---|
| 648 | owl_fmtext_append_normal(&fm1, "1234567898"); |
---|
| 649 | owl_fmtext_append_fmtext(&fm2, &fm1); |
---|
| 650 | FAIL_UNLESS("string lengths correct", |
---|
| 651 | owl_fmtext_num_bytes(&fm2) == strlen(owl_fmtext_get_text(&fm2))); |
---|
| 652 | |
---|
| 653 | /* Test owl_fmtext_num_lines. */ |
---|
| 654 | owl_fmtext_clear(&fm1); |
---|
| 655 | FAIL_UNLESS("empty line correct", owl_fmtext_num_lines(&fm1) == 0); |
---|
| 656 | owl_fmtext_append_normal(&fm1, "12345\n67898"); |
---|
| 657 | FAIL_UNLESS("trailing chars correct", owl_fmtext_num_lines(&fm1) == 2); |
---|
| 658 | owl_fmtext_append_normal(&fm1, "\n"); |
---|
| 659 | FAIL_UNLESS("trailing newline correct", owl_fmtext_num_lines(&fm1) == 2); |
---|
| 660 | owl_fmtext_append_bold(&fm1, ""); |
---|
| 661 | FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2); |
---|
| 662 | |
---|
| 663 | /* Test owl_fmtext_truncate_lines */ |
---|
| 664 | owl_fmtext_clear(&fm1); |
---|
| 665 | owl_fmtext_append_normal(&fm1, "0\n1\n2\n3\n4\n"); |
---|
| 666 | |
---|
| 667 | owl_fmtext_clear(&fm2); |
---|
| 668 | owl_fmtext_truncate_lines(&fm1, 1, 3, &fm2); |
---|
| 669 | str = owl_fmtext_print_plain(&fm2); |
---|
| 670 | FAIL_UNLESS("lines corrected truncated", |
---|
| 671 | str && !strcmp(str, "1\n2\n3\n")); |
---|
[ddbbcffa] | 672 | g_free(str); |
---|
[6772d19] | 673 | |
---|
| 674 | owl_fmtext_clear(&fm2); |
---|
| 675 | owl_fmtext_truncate_lines(&fm1, 1, 5, &fm2); |
---|
| 676 | str = owl_fmtext_print_plain(&fm2); |
---|
| 677 | FAIL_UNLESS("lines corrected truncated", |
---|
| 678 | str && !strcmp(str, "1\n2\n3\n4\n")); |
---|
[ddbbcffa] | 679 | g_free(str); |
---|
[6772d19] | 680 | |
---|
| 681 | /* Test owl_fmtext_truncate_cols. */ |
---|
| 682 | owl_fmtext_clear(&fm1); |
---|
| 683 | owl_fmtext_append_normal(&fm1, "123456789012345\n"); |
---|
| 684 | owl_fmtext_append_normal(&fm1, "123456789\n"); |
---|
| 685 | owl_fmtext_append_normal(&fm1, "1234567890\n"); |
---|
| 686 | |
---|
| 687 | owl_fmtext_clear(&fm2); |
---|
| 688 | owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2); |
---|
| 689 | str = owl_fmtext_print_plain(&fm2); |
---|
| 690 | FAIL_UNLESS("columns correctly truncated", |
---|
| 691 | str && !strcmp(str, "567890" |
---|
| 692 | "56789\n" |
---|
| 693 | "567890")); |
---|
[ddbbcffa] | 694 | g_free(str); |
---|
[6772d19] | 695 | |
---|
[e0022d2] | 696 | owl_fmtext_clear(&fm1); |
---|
| 697 | owl_fmtext_append_normal(&fm1, "12\t1234"); |
---|
| 698 | owl_fmtext_append_bold(&fm1, "56\n"); |
---|
| 699 | owl_fmtext_append_bold(&fm1, "12345678\t\n"); |
---|
| 700 | |
---|
| 701 | owl_fmtext_clear(&fm2); |
---|
| 702 | owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2); |
---|
| 703 | str = owl_fmtext_print_plain(&fm2); |
---|
| 704 | FAIL_UNLESS("columns correctly truncated", |
---|
| 705 | str && !strcmp(str, " 123456" |
---|
| 706 | "5678 ")); |
---|
[ddbbcffa] | 707 | g_free(str); |
---|
[e0022d2] | 708 | |
---|
[2b83ad6] | 709 | /* Test owl_fmtext_expand_tabs. */ |
---|
| 710 | owl_fmtext_clear(&fm1); |
---|
| 711 | owl_fmtext_append_normal(&fm1, "12\t1234"); |
---|
| 712 | owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1"); |
---|
| 713 | owl_fmtext_clear(&fm2); |
---|
| 714 | owl_fmtext_expand_tabs(&fm1, &fm2, 0); |
---|
| 715 | str = owl_fmtext_print_plain(&fm2); |
---|
| 716 | FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL); |
---|
| 717 | FAIL_UNLESS("tabs corrected expanded", |
---|
| 718 | str && !strcmp(str, "12 1234567 1\n" |
---|
| 719 | "12345678 1")); |
---|
[ddbbcffa] | 720 | g_free(str); |
---|
[2b83ad6] | 721 | |
---|
| 722 | owl_fmtext_clear(&fm2); |
---|
| 723 | owl_fmtext_expand_tabs(&fm1, &fm2, 1); |
---|
| 724 | str = owl_fmtext_print_plain(&fm2); |
---|
| 725 | FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL); |
---|
| 726 | FAIL_UNLESS("tabs corrected expanded", |
---|
| 727 | str && !strcmp(str, "12 1234567 1\n" |
---|
| 728 | "12345678 1")); |
---|
[ddbbcffa] | 729 | g_free(str); |
---|
[2b83ad6] | 730 | |
---|
[dc9665a] | 731 | /* Test owl_fmtext_search. */ |
---|
| 732 | owl_fmtext_clear(&fm1); |
---|
| 733 | owl_fmtext_append_normal(&fm1, "123123123123"); |
---|
| 734 | owl_regex_create(&re, "12"); |
---|
| 735 | { |
---|
| 736 | int count = 0, offset; |
---|
| 737 | offset = owl_fmtext_search(&fm1, &re, 0); |
---|
| 738 | while (offset >= 0) { |
---|
| 739 | FAIL_UNLESS("search matches", |
---|
| 740 | !strncmp("12", owl_fmtext_get_text(&fm1) + offset, 2)); |
---|
| 741 | count++; |
---|
| 742 | offset = owl_fmtext_search(&fm1, &re, offset+1); |
---|
| 743 | } |
---|
| 744 | FAIL_UNLESS("exactly four matches", count == 4); |
---|
| 745 | } |
---|
| 746 | owl_regex_cleanup(&re); |
---|
| 747 | |
---|
[f7456bc] | 748 | /* Test owl_fmtext_line_number. */ |
---|
| 749 | owl_fmtext_clear(&fm1); |
---|
| 750 | owl_fmtext_append_normal(&fm1, "123\n456\n"); |
---|
| 751 | owl_fmtext_append_bold(&fm1, ""); |
---|
| 752 | FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0)); |
---|
| 753 | FAIL_UNLESS("trailing formatting characters part of false line", |
---|
| 754 | 2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1))); |
---|
| 755 | owl_regex_create_quoted(&re, "456"); |
---|
| 756 | FAIL_UNLESS("correctly find second line (line 1)", |
---|
| 757 | 1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0))); |
---|
| 758 | owl_regex_cleanup(&re); |
---|
| 759 | |
---|
[7ba2ad4] | 760 | /* Test owl_fmtext_line_extents. */ |
---|
| 761 | owl_fmtext_clear(&fm1); |
---|
| 762 | owl_fmtext_append_normal(&fm1, "123\n456\n789"); |
---|
| 763 | owl_fmtext_line_extents(&fm1, 1, &start, &end); |
---|
| 764 | FAIL_UNLESS("line contents", |
---|
| 765 | !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start)); |
---|
| 766 | owl_fmtext_line_extents(&fm1, 2, &start, &end); |
---|
| 767 | FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1)); |
---|
| 768 | |
---|
[6772d19] | 769 | owl_fmtext_cleanup(&fm1); |
---|
| 770 | owl_fmtext_cleanup(&fm2); |
---|
| 771 | |
---|
| 772 | printf("# END testing owl_fmtext (%d failures)\n", numfailed); |
---|
| 773 | |
---|
| 774 | return numfailed; |
---|
| 775 | } |
---|
[b31252d] | 776 | |
---|
| 777 | static int owl_smartfilter_test_equals(const char *filtname, const char *expected) { |
---|
| 778 | owl_filter *f = NULL; |
---|
| 779 | char *filtstr = NULL; |
---|
| 780 | int failed = 0; |
---|
| 781 | f = owl_global_get_filter(&g, filtname); |
---|
| 782 | if (f == NULL) { |
---|
| 783 | printf("not ok filter missing: %s\n", filtname); |
---|
| 784 | failed = 1; |
---|
| 785 | goto out; |
---|
| 786 | } |
---|
| 787 | |
---|
| 788 | /* TODO: Come up with a better way to test this. */ |
---|
| 789 | filtstr = owl_filter_print(f); |
---|
| 790 | if (strcmp(expected, filtstr)) { |
---|
| 791 | printf("not ok filter incorrect: |%s| instead of |%s|\n", |
---|
| 792 | filtstr, expected); |
---|
| 793 | failed = 1; |
---|
| 794 | goto out; |
---|
| 795 | } |
---|
| 796 | |
---|
| 797 | out: |
---|
[ddbbcffa] | 798 | g_free(filtstr); |
---|
[b31252d] | 799 | return failed; |
---|
| 800 | } |
---|
| 801 | |
---|
| 802 | static int owl_classinstfilt_test(const char *c, const char *i, int related, const char *expected) { |
---|
| 803 | char *filtname = NULL; |
---|
| 804 | int failed = 0; |
---|
| 805 | |
---|
| 806 | filtname = owl_function_classinstfilt(c, i, related); |
---|
| 807 | if (filtname == NULL) { |
---|
| 808 | printf("not ok null filtname: %s %s %s\n", c, i ? i : "(null)", |
---|
| 809 | related ? "related" : "not related"); |
---|
| 810 | failed = 1; |
---|
| 811 | goto out; |
---|
| 812 | } |
---|
| 813 | if (owl_smartfilter_test_equals(filtname, expected)) { |
---|
| 814 | failed = 1; |
---|
| 815 | goto out; |
---|
| 816 | } |
---|
| 817 | out: |
---|
| 818 | if (!failed) { |
---|
| 819 | printf("ok %s\n", filtname); |
---|
| 820 | } |
---|
| 821 | if (filtname) |
---|
| 822 | owl_global_remove_filter(&g, filtname); |
---|
[ddbbcffa] | 823 | g_free(filtname); |
---|
[b31252d] | 824 | return failed; |
---|
| 825 | } |
---|
| 826 | |
---|
[443dcfa] | 827 | static int owl_zuserfilt_test(const char *longuser, const char *expected) { |
---|
| 828 | char *filtname = NULL; |
---|
| 829 | int failed = 0; |
---|
| 830 | |
---|
| 831 | filtname = owl_function_zuserfilt(longuser); |
---|
| 832 | if (filtname == NULL) { |
---|
| 833 | printf("not ok null filtname: %s\n", longuser); |
---|
| 834 | failed = 1; |
---|
| 835 | goto out; |
---|
| 836 | } |
---|
| 837 | if (owl_smartfilter_test_equals(filtname, expected)) { |
---|
| 838 | failed = 1; |
---|
| 839 | goto out; |
---|
| 840 | } |
---|
| 841 | out: |
---|
| 842 | if (!failed) { |
---|
| 843 | printf("ok %s\n", filtname); |
---|
| 844 | } |
---|
| 845 | if (filtname) |
---|
| 846 | owl_global_remove_filter(&g, filtname); |
---|
[ddbbcffa] | 847 | g_free(filtname); |
---|
[443dcfa] | 848 | return failed; |
---|
| 849 | } |
---|
| 850 | |
---|
| 851 | |
---|
[b31252d] | 852 | int owl_smartfilter_regtest(void) { |
---|
| 853 | int numfailed = 0; |
---|
| 854 | |
---|
| 855 | printf("# BEGIN testing owl_smartfilter\n"); |
---|
| 856 | |
---|
| 857 | /* Check classinst making. */ |
---|
| 858 | |
---|
| 859 | #define TEST_CLASSINSTFILT(c, i, r, e) do { \ |
---|
| 860 | numtests++; \ |
---|
| 861 | numfailed += owl_classinstfilt_test(c, i, r, e); \ |
---|
| 862 | } while (0) |
---|
| 863 | TEST_CLASSINSTFILT("message", NULL, false, |
---|
| 864 | "class ^message$\n"); |
---|
| 865 | TEST_CLASSINSTFILT("message", NULL, true, |
---|
| 866 | "class ^(un)*message(\\.d)*$\n"); |
---|
| 867 | TEST_CLASSINSTFILT("message", "personal", false, |
---|
| 868 | "class ^message$ and instance ^personal$\n"); |
---|
| 869 | TEST_CLASSINSTFILT("message", "personal", true, |
---|
| 870 | "class ^(un)*message(\\.d)*$ and ( instance ^(un)*personal(\\.d)*$ )\n"); |
---|
| 871 | |
---|
| 872 | TEST_CLASSINSTFILT("message", "evil\tinstance", false, |
---|
| 873 | "class ^message$ and instance '^evil\tinstance$'\n"); |
---|
| 874 | TEST_CLASSINSTFILT("message", "evil instance", false, |
---|
| 875 | "class ^message$ and instance '^evil instance$'\n"); |
---|
| 876 | TEST_CLASSINSTFILT("message", "evil'instance", false, |
---|
| 877 | "class ^message$ and instance \"^evil'instance$\"\n"); |
---|
| 878 | TEST_CLASSINSTFILT("message", "evil\"instance", false, |
---|
| 879 | "class ^message$ and instance '^evil\"instance$'\n"); |
---|
| 880 | TEST_CLASSINSTFILT("message", "evil$instance", false, |
---|
| 881 | "class ^message$ and instance ^evil\\$instance$\n"); |
---|
| 882 | |
---|
[443dcfa] | 883 | #define TEST_ZUSERFILT(l, e) do { \ |
---|
| 884 | numtests++; \ |
---|
| 885 | numfailed += owl_zuserfilt_test(l, e); \ |
---|
| 886 | } while (0) |
---|
| 887 | TEST_ZUSERFILT("user", |
---|
| 888 | "( type ^zephyr$ and filter personal and " |
---|
| 889 | "( ( direction ^in$ and sender " |
---|
| 890 | "^user$" |
---|
| 891 | " ) or ( direction ^out$ and recipient " |
---|
| 892 | "^user$" |
---|
| 893 | " ) ) ) or ( ( class ^login$ ) and ( sender " |
---|
| 894 | "^user$" |
---|
| 895 | " ) )\n"); |
---|
| 896 | TEST_ZUSERFILT("very evil\t.user", |
---|
| 897 | "( type ^zephyr$ and filter personal and " |
---|
| 898 | "( ( direction ^in$ and sender " |
---|
| 899 | "'^very evil\t\\.user$'" |
---|
| 900 | " ) or ( direction ^out$ and recipient " |
---|
| 901 | "'^very evil\t\\.user$'" |
---|
| 902 | " ) ) ) or ( ( class ^login$ ) and ( sender " |
---|
| 903 | "'^very evil\t\\.user$'" |
---|
| 904 | " ) )\n"); |
---|
[b31252d] | 905 | |
---|
| 906 | printf("# END testing owl_smartfilter (%d failures)\n", numfailed); |
---|
| 907 | |
---|
| 908 | return numfailed; |
---|
| 909 | } |
---|
[25891a8] | 910 | |
---|
| 911 | int owl_history_regtest(void) |
---|
| 912 | { |
---|
| 913 | int numfailed = 0; |
---|
| 914 | int i; |
---|
| 915 | owl_history h; |
---|
| 916 | |
---|
| 917 | printf("# BEGIN testing owl_history\n"); |
---|
| 918 | owl_history_init(&h); |
---|
| 919 | |
---|
| 920 | /* Operations on empty history. */ |
---|
| 921 | FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL); |
---|
| 922 | FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL); |
---|
| 923 | FAIL_UNLESS("untouched", !owl_history_is_touched(&h)); |
---|
| 924 | |
---|
| 925 | /* Insert a few records. */ |
---|
| 926 | owl_history_store(&h, "a", false); |
---|
| 927 | owl_history_store(&h, "b", false); |
---|
| 928 | owl_history_store(&h, "c", false); |
---|
| 929 | owl_history_store(&h, "d", true); |
---|
| 930 | |
---|
| 931 | /* Walk up and down the history a bit. */ |
---|
| 932 | FAIL_UNLESS("untouched", !owl_history_is_touched(&h)); |
---|
| 933 | FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0); |
---|
| 934 | FAIL_UNLESS("touched", owl_history_is_touched(&h)); |
---|
| 935 | FAIL_UNLESS("next d", strcmp(owl_history_get_next(&h), "d") == 0); |
---|
| 936 | FAIL_UNLESS("untouched", !owl_history_is_touched(&h)); |
---|
| 937 | FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL); |
---|
| 938 | FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0); |
---|
| 939 | FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0); |
---|
| 940 | FAIL_UNLESS("prev a", strcmp(owl_history_get_prev(&h), "a") == 0); |
---|
| 941 | FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL); |
---|
| 942 | |
---|
| 943 | /* Now insert something. It should reset and blow away 'd'. */ |
---|
| 944 | owl_history_store(&h, "e", false); |
---|
| 945 | FAIL_UNLESS("untouched", !owl_history_is_touched(&h)); |
---|
| 946 | FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL); |
---|
| 947 | FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0); |
---|
| 948 | FAIL_UNLESS("touched", owl_history_is_touched(&h)); |
---|
| 949 | FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0); |
---|
| 950 | FAIL_UNLESS("untouched", !owl_history_is_touched(&h)); |
---|
| 951 | |
---|
| 952 | /* Lines get de-duplicated on insert. */ |
---|
| 953 | owl_history_store(&h, "e", false); |
---|
| 954 | owl_history_store(&h, "e", false); |
---|
| 955 | owl_history_store(&h, "e", false); |
---|
| 956 | FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0); |
---|
| 957 | FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0); |
---|
| 958 | |
---|
| 959 | /* But a partial is not deduplicated, as it'll go away soon. */ |
---|
| 960 | owl_history_store(&h, "e", true); |
---|
| 961 | FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0); |
---|
| 962 | FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0); |
---|
| 963 | FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0); |
---|
| 964 | FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0); |
---|
| 965 | |
---|
| 966 | /* Reset moves to the front... */ |
---|
| 967 | owl_history_store(&h, "f", true); |
---|
| 968 | FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0); |
---|
| 969 | FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0); |
---|
| 970 | owl_history_reset(&h); |
---|
| 971 | FAIL_UNLESS("untouched", !owl_history_is_touched(&h)); |
---|
| 972 | /* ...and destroys any pending partial entry... */ |
---|
| 973 | FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0); |
---|
| 974 | FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0); |
---|
| 975 | /* ...but not non-partial ones. */ |
---|
| 976 | owl_history_reset(&h); |
---|
| 977 | FAIL_UNLESS("untouched", !owl_history_is_touched(&h)); |
---|
| 978 | |
---|
| 979 | /* Finally, check we are bounded by OWL_HISTORYSIZE. */ |
---|
| 980 | for (i = 0; i < OWL_HISTORYSIZE; i++) { |
---|
| 981 | char *string = g_strdup_printf("mango%d", i); |
---|
| 982 | owl_history_store(&h, string, false); |
---|
| 983 | g_free(string); |
---|
| 984 | } |
---|
| 985 | /* The OWL_HISTORYSIZE'th prev gets NULL. */ |
---|
| 986 | for (i = OWL_HISTORYSIZE - 2; i >= 0; i--) { |
---|
| 987 | char *string = g_strdup_printf("mango%d", i); |
---|
| 988 | FAIL_UNLESS("prev mango_N", strcmp(owl_history_get_prev(&h), string) == 0); |
---|
| 989 | g_free(string); |
---|
| 990 | } |
---|
| 991 | FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL); |
---|
| 992 | |
---|
| 993 | owl_history_cleanup(&h); |
---|
| 994 | |
---|
| 995 | printf("# END testing owl_history (%d failures)\n", numfailed); |
---|
| 996 | return numfailed; |
---|
| 997 | } |
---|