forked from fish-shell/fish-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fish_tests.cpp
2861 lines (2435 loc) · 91.8 KB
/
fish_tests.cpp
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** \file fish_tests.c
Various bug and feature tests. Compiled and run by make test.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdarg.h>
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <signal.h>
#include <locale.h>
#include <dirent.h>
#include <time.h>
#include "fallback.h"
#include "util.h"
#include "common.h"
#include "proc.h"
#include "reader.h"
#include "builtin.h"
#include "function.h"
#include "autoload.h"
#include "complete.h"
#include "wutil.h"
#include "env.h"
#include "expand.h"
#include "parser.h"
#include "tokenizer.h"
#include "output.h"
#include "exec.h"
#include "event.h"
#include "path.h"
#include "history.h"
#include "highlight.h"
#include "iothread.h"
#include "postfork.h"
#include "signal.h"
#include "parse_tree.h"
#include "parse_util.h"
#include "pager.h"
static const char * const * s_arguments;
static int s_test_run_count = 0;
/* Indicate if we should test the given function. Either we test everything (all arguments) or we run only tests that have a prefix in s_arguments */
static bool should_test_function(const char *func_name)
{
/* No args, test everything */
bool result = false;
if (! s_arguments || ! s_arguments[0])
{
result = true;
}
else
{
for (size_t i=0; s_arguments[i] != NULL; i++)
{
if (! strncmp(func_name, s_arguments[i], strlen(s_arguments[i])))
{
/* Prefix match */
result = true;
break;
}
}
}
if (result)
s_test_run_count++;
return result;
}
/**
The number of tests to run
*/
#define ESCAPE_TEST_COUNT 100000
/**
The average length of strings to unescape
*/
#define ESCAPE_TEST_LENGTH 100
/**
The higest character number of character to try and escape
*/
#define ESCAPE_TEST_CHAR 4000
/**
Number of laps to run performance testing loop
*/
#define LAPS 50
/**
The result of one of the test passes
*/
#define NUM_ANS L"-7 99999999 1234567 deadbeef DEADBEEFDEADBEEF"
/**
Number of encountered errors
*/
static int err_count=0;
/**
Print formatted output
*/
static void say(const wchar_t *blah, ...)
{
va_list va;
va_start(va, blah);
vwprintf(blah, va);
va_end(va);
wprintf(L"\n");
}
/**
Print formatted error string
*/
static void err(const wchar_t *blah, ...)
{
va_list va;
va_start(va, blah);
err_count++;
wprintf(L"Error: ");
vwprintf(blah, va);
va_end(va);
wprintf(L"\n");
}
#define do_test(e) do { if (! (e)) err(L"Test failed on line %lu: %s", __LINE__, #e); } while (0)
/* Test sane escapes */
static void test_unescape_sane()
{
const struct test_t
{
const wchar_t * input;
const wchar_t * expected;
} tests[] =
{
{L"abcd", L"abcd"},
{L"'abcd'", L"abcd"},
{L"'abcd\\n'", L"abcd\\n"},
{L"\"abcd\\n\"", L"abcd\\n"},
{L"\"abcd\\n\"", L"abcd\\n"},
{L"\\143", L"c"},
{L"'\\143'", L"\\143"},
{L"\\n", L"\n"} // \n normally becomes newline
};
wcstring output;
for (size_t i=0; i < sizeof tests / sizeof *tests; i++)
{
bool ret = unescape_string(tests[i].input, &output, UNESCAPE_DEFAULT);
if (! ret)
{
err(L"Failed to unescape '%ls'\n", tests[i].input);
}
else if (output != tests[i].expected)
{
err(L"In unescaping '%ls', expected '%ls' but got '%ls'\n", tests[i].input, tests[i].expected, output.c_str());
}
}
// test for overflow
if (unescape_string(L"echo \\UFFFFFF", &output, UNESCAPE_DEFAULT))
{
err(L"Should not have been able to unescape \\UFFFFFF\n");
}
if (unescape_string(L"echo \\U110000", &output, UNESCAPE_DEFAULT))
{
err(L"Should not have been able to unescape \\U110000\n");
}
if (! unescape_string(L"echo \\U10FFFF", &output, UNESCAPE_DEFAULT))
{
err(L"Should have been able to unescape \\U10FFFF\n");
}
}
/**
Test the escaping/unescaping code by escaping/unescaping random
strings and verifying that the original string comes back.
*/
static void test_escape_crazy()
{
say(L"Testing escaping and unescaping");
wcstring random_string;
wcstring escaped_string;
wcstring unescaped_string;
for (size_t i=0; i<ESCAPE_TEST_COUNT; i++)
{
random_string.clear();
while (rand() % ESCAPE_TEST_LENGTH)
{
random_string.push_back((rand() % ESCAPE_TEST_CHAR) +1);
}
escaped_string = escape_string(random_string, ESCAPE_ALL);
bool unescaped_success = unescape_string(escaped_string, &unescaped_string, UNESCAPE_DEFAULT);
if (! unescaped_success)
{
err(L"Failed to unescape string <%ls>", escaped_string.c_str());
}
else if (unescaped_string != random_string)
{
err(L"Escaped and then unescaped string '%ls', but got back a different string '%ls'", random_string.c_str(), unescaped_string.c_str());
}
}
}
static void test_format(void)
{
say(L"Testing formatting functions");
struct
{
unsigned long long val;
const char *expected;
} tests[] =
{
{ 0, "empty" },
{ 1, "1B" },
{ 2, "2B" },
{ 1024, "1kB" },
{ 1870, "1.8kB" },
{ 4322911, "4.1MB" }
};
size_t i;
for (i=0; i < sizeof tests / sizeof *tests; i++)
{
char buff[128];
format_size_safe(buff, tests[i].val);
do_test(! strcmp(buff, tests[i].expected));
}
for (int j=-129; j <= 129; j++)
{
char buff1[128], buff2[128];
format_long_safe(buff1, j);
sprintf(buff2, "%d", j);
do_test(! strcmp(buff1, buff2));
}
long q = LONG_MIN;
char buff1[128], buff2[128];
format_long_safe(buff1, q);
sprintf(buff2, "%ld", q);
do_test(! strcmp(buff1, buff2));
}
/**
Test wide/narrow conversion by creating random strings and
verifying that the original string comes back thorugh double
conversion.
*/
static void test_convert()
{
/* char o[] =
{
-17, -128, -121, -68, 0
}
;
wchar_t *w = str2wcs(o);
char *n = wcs2str(w);
int i;
for( i=0; o[i]; i++ )
{
bitprint(o[i]);;
//wprintf(L"%d ", o[i]);
}
wprintf(L"\n");
for( i=0; w[i]; i++ )
{
wbitprint(w[i]);;
//wprintf(L"%d ", w[i]);
}
wprintf(L"\n");
for( i=0; n[i]; i++ )
{
bitprint(n[i]);;
//wprintf(L"%d ", n[i]);
}
wprintf(L"\n");
return;
*/
int i;
std::vector<char> sb;
say(L"Testing wide/narrow string conversion");
for (i=0; i<ESCAPE_TEST_COUNT; i++)
{
const char *o, *n;
char c;
sb.clear();
while (rand() % ESCAPE_TEST_LENGTH)
{
c = rand();
sb.push_back(c);
}
c = 0;
sb.push_back(c);
o = &sb.at(0);
const wcstring w = str2wcstring(o);
n = wcs2str(w.c_str());
if (!o || !n)
{
err(L"Line %d - Conversion cycle of string %s produced null pointer on %s", __LINE__, o, L"wcs2str");
}
if (strcmp(o, n))
{
err(L"Line %d - %d: Conversion cycle of string %s produced different string %s", __LINE__, i, o, n);
}
free((void *)n);
}
}
/* Verify correct behavior with embedded nulls */
static void test_convert_nulls(void)
{
say(L"Testing embedded nulls in string conversion");
const wchar_t in[] = L"AAA\0BBB";
const size_t in_len = (sizeof in / sizeof *in) - 1;
const wcstring in_str = wcstring(in, in_len);
std::string out_str = wcs2string(in_str);
if (out_str.size() != in_len)
{
err(L"Embedded nulls mishandled in wcs2string");
}
for (size_t i=0; i < in_len; i++)
{
if (in[i] != out_str.at(i))
{
err(L"Embedded nulls mishandled in wcs2string at index %lu", (unsigned long)i);
}
}
wcstring out_wstr = str2wcstring(out_str);
if (out_wstr.size() != in_len)
{
err(L"Embedded nulls mishandled in str2wcstring");
}
for (size_t i=0; i < in_len; i++)
{
if (in[i] != out_wstr.at(i))
{
err(L"Embedded nulls mishandled in str2wcstring at index %lu", (unsigned long)i);
}
}
}
/**
Test the tokenizer
*/
static void test_tok()
{
say(L"Testing tokenizer");
say(L"Testing invalid input");
tokenizer_t t(NULL, 0);
if (tok_last_type(&t) != TOK_ERROR)
{
err(L"Invalid input to tokenizer was undetected");
}
say(L"Testing use of broken tokenizer");
if (!tok_has_next(&t))
{
err(L"tok_has_next() should return 1 once on broken tokenizer");
}
tok_next(&t);
if (tok_last_type(&t) != TOK_ERROR)
{
err(L"Invalid input to tokenizer was undetected");
}
/*
This should crash if there is a bug. No reliable way to detect otherwise.
*/
say(L"Test destruction of broken tokenizer");
{
const wchar_t *str = L"string <redirection 2>&1 'nested \"quoted\" '(string containing subshells ){and,brackets}$as[$well (as variable arrays)] not_a_redirect^ ^ ^^is_a_redirect";
const int types[] =
{
TOK_STRING, TOK_REDIRECT_IN, TOK_STRING, TOK_REDIRECT_FD, TOK_STRING, TOK_STRING, TOK_STRING, TOK_REDIRECT_OUT, TOK_REDIRECT_APPEND, TOK_STRING, TOK_END
};
say(L"Test correct tokenization");
tokenizer_t t(str, 0);
for (size_t i=0; i < sizeof types / sizeof *types; i++, tok_next(&t))
{
if (types[i] != tok_last_type(&t))
{
err(L"Tokenization error:");
wprintf(L"Token number %d of string \n'%ls'\n, expected token type %ls, got token '%ls' of type %ls\n",
i+1,
str,
tok_get_desc(types[i]),
tok_last(&t),
tok_get_desc(tok_last_type(&t)));
}
}
}
/* Test redirection_type_for_string */
if (redirection_type_for_string(L"<") != TOK_REDIRECT_IN) err(L"redirection_type_for_string failed on line %ld", (long)__LINE__);
if (redirection_type_for_string(L"^") != TOK_REDIRECT_OUT) err(L"redirection_type_for_string failed on line %ld", (long)__LINE__);
if (redirection_type_for_string(L">") != TOK_REDIRECT_OUT) err(L"redirection_type_for_string failed on line %ld", (long)__LINE__);
if (redirection_type_for_string(L"2>") != TOK_REDIRECT_OUT) err(L"redirection_type_for_string failed on line %ld", (long)__LINE__);
if (redirection_type_for_string(L">>") != TOK_REDIRECT_APPEND) err(L"redirection_type_for_string failed on line %ld", (long)__LINE__);
if (redirection_type_for_string(L"2>>") != TOK_REDIRECT_APPEND) err(L"redirection_type_for_string failed on line %ld", (long)__LINE__);
if (redirection_type_for_string(L"2>?") != TOK_REDIRECT_NOCLOB) err(L"redirection_type_for_string failed on line %ld", (long)__LINE__);
if (redirection_type_for_string(L"9999999999999999>?") != TOK_NONE) err(L"redirection_type_for_string failed on line %ld", (long)__LINE__);
if (redirection_type_for_string(L"2>&3") != TOK_REDIRECT_FD) err(L"redirection_type_for_string failed on line %ld", (long)__LINE__);
if (redirection_type_for_string(L"2>|") != TOK_NONE) err(L"redirection_type_for_string failed on line %ld", (long)__LINE__);
}
// Little function that runs in the main thread
static int test_iothread_main_call(int *addr)
{
*addr += 1;
return *addr;
}
// Little function that runs in a background thread, bouncing to the main
static int test_iothread_thread_call(int *addr)
{
int before = *addr;
iothread_perform_on_main(test_iothread_main_call, addr);
int after = *addr;
// Must have incremented it at least once
if (before >= after)
{
err(L"Failed to increment from background thread");
}
return after;
}
static void test_iothread(void)
{
say(L"Testing iothreads");
int *int_ptr = new int(0);
int iterations = 1000;
for (int i=0; i < iterations; i++)
{
iothread_perform(test_iothread_thread_call, (void (*)(int *, int))NULL, int_ptr);
}
// Now wait until we're done
iothread_drain_all();
// Should have incremented it once per thread
if (*int_ptr != iterations)
{
say(L"Expected int to be %d, but instead it was %d", iterations, *int_ptr);
}
delete int_ptr;
}
/**
Test the parser
*/
static void test_parser()
{
say(L"Testing parser");
parser_t parser(PARSER_TYPE_GENERAL, true);
say(L"Testing block nesting");
if (!parse_util_detect_errors(L"if; end"))
{
err(L"Incomplete if statement undetected");
}
if (!parse_util_detect_errors(L"if test; echo"))
{
err(L"Missing end undetected");
}
if (!parse_util_detect_errors(L"if test; end; end"))
{
err(L"Unbalanced end undetected");
}
say(L"Testing detection of invalid use of builtin commands");
if (!parse_util_detect_errors(L"case foo"))
{
err(L"'case' command outside of block context undetected");
}
if (!parse_util_detect_errors(L"switch ggg; if true; case foo;end;end"))
{
err(L"'case' command outside of switch block context undetected");
}
if (!parse_util_detect_errors(L"else"))
{
err(L"'else' command outside of conditional block context undetected");
}
if (!parse_util_detect_errors(L"else if"))
{
err(L"'else if' command outside of conditional block context undetected");
}
if (!parse_util_detect_errors(L"if false; else if; end"))
{
err(L"'else if' missing command undetected");
}
if (!parse_util_detect_errors(L"break"))
{
err(L"'break' command outside of loop block context undetected");
}
if (parse_util_detect_errors(L"break --help"))
{
err(L"'break --help' incorrectly marked as error");
}
if (! parse_util_detect_errors(L"while false ; function foo ; break ; end ; end "))
{
err(L"'break' command inside function allowed to break from loop outside it");
}
if (!parse_util_detect_errors(L"exec ls|less") || !parse_util_detect_errors(L"echo|return"))
{
err(L"Invalid pipe command undetected");
}
if (parse_util_detect_errors(L"for i in foo ; switch $i ; case blah ; break; end; end "))
{
err(L"'break' command inside switch falsely reported as error");
}
if (parse_util_detect_errors(L"or cat | cat") || parse_util_detect_errors(L"and cat | cat"))
{
err(L"boolean command at beginning of pipeline falsely reported as error");
}
if (! parse_util_detect_errors(L"cat | and cat"))
{
err(L"'and' command in pipeline not reported as error");
}
if (! parse_util_detect_errors(L"cat | exec") || ! parse_util_detect_errors(L"exec | cat"))
{
err(L"'exec' command in pipeline not reported as error");
}
say(L"Testing basic evaluation");
#if 0
/* This fails now since the parser takes a wcstring&, and NULL converts to wchar_t * converts to wcstring which crashes (thanks C++) */
if (!parser.eval(0, 0, TOP))
{
err(L"Null input when evaluating undetected");
}
#endif
if (!parser.eval(L"ls", io_chain_t(), WHILE))
{
err(L"Invalid block mode when evaluating undetected");
}
/* Ensure that we don't crash on infinite self recursion and mutual recursion. These must use the principal parser because we cannot yet execute jobs on other parsers (!) */
say(L"Testing recursion detection");
parser_t::principal_parser().eval(L"function recursive ; recursive ; end ; recursive; ", io_chain_t(), TOP);
#if 0
/* This is disabled since it produces a long backtrace. We should find a way to either visually compress the backtrace, or disable error spewing */
parser_t::principal_parser().eval(L"function recursive1 ; recursive2 ; end ; function recursive2 ; recursive1 ; end ; recursive1; ", io_chain_t(), TOP);
#endif
}
/* Wait a while and then SIGINT the main thread */
struct test_cancellation_info_t
{
pthread_t thread;
double delay;
};
static int signal_main(test_cancellation_info_t *info)
{
usleep(info->delay * 1E6);
pthread_kill(info->thread, SIGINT);
return 0;
}
static void test_1_cancellation(const wchar_t *src)
{
shared_ptr<io_buffer_t> out_buff(io_buffer_t::create(false, STDOUT_FILENO));
const io_chain_t io_chain(out_buff);
test_cancellation_info_t ctx = {pthread_self(), 0.25 /* seconds */ };
iothread_perform(signal_main, (void (*)(test_cancellation_info_t *, int))NULL, &ctx);
parser_t::principal_parser().eval(src, io_chain, TOP);
out_buff->read();
if (out_buff->out_buffer_size() != 0)
{
err(L"Expected 0 bytes in out_buff, but instead found %lu bytes\n", out_buff->out_buffer_size());
}
iothread_drain_all();
}
static void test_cancellation()
{
say(L"Testing Ctrl-C cancellation. If this hangs, that's a bug!");
/* Enable fish's signal handling here. We need to make this interactive for fish to install its signal handlers */
proc_push_interactive(1);
signal_set_handlers();
/* This tests that we can correctly ctrl-C out of certain loop constructs, and that nothing gets printed if we do */
/* Here the command substitution is an infinite loop. echo never even gets its argument, so when we cancel we expect no output */
test_1_cancellation(L"echo (while true ; echo blah ; end)");
fprintf(stderr, ".");
/* Nasty infinite loop that doesn't actually execute anything */
test_1_cancellation(L"echo (while true ; end) (while true ; end) (while true ; end)");
fprintf(stderr, ".");
test_1_cancellation(L"while true ; end");
fprintf(stderr, ".");
test_1_cancellation(L"for i in (while true ; end) ; end");
fprintf(stderr, ".");
fprintf(stderr, "\n");
/* Restore signal handling */
proc_pop_interactive();
signal_reset_handlers();
/* Ensure that we don't think we should cancel */
reader_reset_interrupted();
}
static void test_indents()
{
say(L"Testing indents");
// Here are the components of our source and the indents we expect those to be
struct indent_component_t
{
const wchar_t *txt;
int indent;
};
const indent_component_t components1[] =
{
{L"if foo", 0},
{L"end", 0},
{NULL, -1}
};
const indent_component_t components2[] =
{
{L"if foo", 0},
{L"", 1}, //trailing newline!
{NULL, -1}
};
const indent_component_t components3[] =
{
{L"if foo", 0},
{L"foo", 1},
{L"end", 0}, //trailing newline!
{NULL, -1}
};
const indent_component_t components4[] =
{
{L"if foo", 0},
{L"if bar", 1},
{L"end", 1},
{L"end", 0},
{L"", 0},
{NULL, -1}
};
const indent_component_t components5[] =
{
{L"if foo", 0},
{L"if bar", 1},
{L"", 2},
{NULL, -1}
};
const indent_component_t components6[] =
{
{L"begin", 0},
{L"foo", 1},
{L"", 1},
{NULL, -1}
};
const indent_component_t components7[] =
{
{L"begin; end", 0},
{L"foo", 0},
{L"", 0},
{NULL, -1}
};
const indent_component_t components8[] =
{
{L"if foo", 0},
{L"if bar", 1},
{L"baz", 2},
{L"end", 1},
{L"", 1},
{NULL, -1}
};
const indent_component_t components9[] =
{
{L"switch foo", 0},
{L"", 1},
{NULL, -1}
};
const indent_component_t components10[] =
{
{L"switch foo", 0},
{L"case bar", 1},
{L"case baz", 1},
{L"quux", 2},
{L"", 2},
{NULL, -1}
};
const indent_component_t components11[] =
{
{L"switch foo", 0},
{L"cas", 1}, //parse error indentation handling
{NULL, -1}
};
const indent_component_t *tests[] = {components1, components2, components3, components4, components5, components6, components7, components8, components9, components10, components11};
for (size_t which = 0; which < sizeof tests / sizeof *tests; which++)
{
const indent_component_t *components = tests[which];
// Count how many we have
size_t component_count = 0;
while (components[component_count].txt != NULL)
{
component_count++;
}
// Generate the expected indents
wcstring text;
std::vector<int> expected_indents;
for (size_t i=0; i < component_count; i++)
{
if (i > 0)
{
text.push_back(L'\n');
expected_indents.push_back(components[i].indent);
}
text.append(components[i].txt);
expected_indents.resize(text.size(), components[i].indent);
}
do_test(expected_indents.size() == text.size());
// Compute the indents
std::vector<int> indents = parse_util_compute_indents(text);
if (expected_indents.size() != indents.size())
{
err(L"Indent vector has wrong size! Expected %lu, actual %lu", expected_indents.size(), indents.size());
}
do_test(expected_indents.size() == indents.size());
for (size_t i=0; i < text.size(); i++)
{
if (expected_indents.at(i) != indents.at(i))
{
err(L"Wrong indent at index %lu in test #%lu (expected %d, actual %d):\n%ls\n", i, which + 1, expected_indents.at(i), indents.at(i), text.c_str());
break; //don't keep showing errors for the rest of the line
}
}
}
}
static void test_utils()
{
say(L"Testing utils");
const wchar_t *a = L"echo (echo (echo hi";
const wchar_t *begin = NULL, *end = NULL;
parse_util_cmdsubst_extent(a, 0, &begin, &end);
if (begin != a || end != begin + wcslen(begin)) err(L"parse_util_cmdsubst_extent failed on line %ld", (long)__LINE__);
parse_util_cmdsubst_extent(a, 1, &begin, &end);
if (begin != a || end != begin + wcslen(begin)) err(L"parse_util_cmdsubst_extent failed on line %ld", (long)__LINE__);
parse_util_cmdsubst_extent(a, 2, &begin, &end);
if (begin != a || end != begin + wcslen(begin)) err(L"parse_util_cmdsubst_extent failed on line %ld", (long)__LINE__);
parse_util_cmdsubst_extent(a, 3, &begin, &end);
if (begin != a || end != begin + wcslen(begin)) err(L"parse_util_cmdsubst_extent failed on line %ld", (long)__LINE__);
parse_util_cmdsubst_extent(a, 8, &begin, &end);
if (begin != a + wcslen(L"echo (")) err(L"parse_util_cmdsubst_extent failed on line %ld", (long)__LINE__);
parse_util_cmdsubst_extent(a, 17, &begin, &end);
if (begin != a + wcslen(L"echo (echo (")) err(L"parse_util_cmdsubst_extent failed on line %ld", (long)__LINE__);
}
static void test_escape_sequences(void)
{
say(L"Testing escape codes");
if (escape_code_length(L"") != 0) err(L"test_escape_sequences failed on line %d\n", __LINE__);
if (escape_code_length(L"abcd") != 0) err(L"test_escape_sequences failed on line %d\n", __LINE__);
if (escape_code_length(L"\x1b[2J") != 4) err(L"test_escape_sequences failed on line %d\n", __LINE__);
if (escape_code_length(L"\x1b[38;5;123mABC") != strlen("\x1b[38;5;123m")) err(L"test_escape_sequences failed on line %d\n", __LINE__);
if (escape_code_length(L"\x1b@") != 2) err(L"test_escape_sequences failed on line %d\n", __LINE__);
}
class lru_node_test_t : public lru_node_t
{
public:
lru_node_test_t(const wcstring &tmp) : lru_node_t(tmp) { }
};
class test_lru_t : public lru_cache_t<lru_node_test_t>
{
public:
test_lru_t() : lru_cache_t<lru_node_test_t>(16) { }
std::vector<lru_node_test_t *> evicted_nodes;
virtual void node_was_evicted(lru_node_test_t *node)
{
do_test(find(evicted_nodes.begin(), evicted_nodes.end(), node) == evicted_nodes.end());
evicted_nodes.push_back(node);
}
};
static void test_lru(void)
{
say(L"Testing LRU cache");
test_lru_t cache;
std::vector<lru_node_test_t *> expected_evicted;
size_t total_nodes = 20;
for (size_t i=0; i < total_nodes; i++)
{
do_test(cache.size() == std::min(i, (size_t)16));
lru_node_test_t *node = new lru_node_test_t(to_string(i));
if (i < 4) expected_evicted.push_back(node);
// Adding the node the first time should work, and subsequent times should fail
do_test(cache.add_node(node));
do_test(! cache.add_node(node));
}
do_test(cache.evicted_nodes == expected_evicted);
cache.evict_all_nodes();
do_test(cache.evicted_nodes.size() == total_nodes);
while (! cache.evicted_nodes.empty())
{
lru_node_t *node = cache.evicted_nodes.back();
cache.evicted_nodes.pop_back();
delete node;
}
}
/**
Perform parameter expansion and test if the output equals the zero-terminated parameter list supplied.
\param in the string to expand
\param flags the flags to send to expand_string
*/
static int expand_test(const wchar_t *in, int flags, ...)
{
std::vector<completion_t> output;
va_list va;
size_t i=0;
int res=1;
wchar_t *arg;
if (expand_string(in, output, flags))
{
}
#if 0
printf("input: %ls\n", in);
for (size_t idx=0; idx < output.size(); idx++)
{
printf("%ls\n", output.at(idx).completion.c_str());
}
#endif
va_start(va, flags);
while ((arg=va_arg(va, wchar_t *))!= 0)
{
if (output.size() == i)
{
res=0;
break;
}
if (output.at(i).completion != arg)
{
res=0;
break;
}
i++;
}
va_end(va);
return res;
}
/**
Test globbing and other parameter expansion
*/
static void test_expand()
{
say(L"Testing parameter expansion");
if (!expand_test(L"foo", 0, L"foo", 0))
{
err(L"Strings do not expand to themselves");
}
if (!expand_test(L"a{b,c,d}e", 0, L"abe", L"ace", L"ade", 0))
{
err(L"Bracket expansion is broken");
}
if (!expand_test(L"a*", EXPAND_SKIP_WILDCARDS, L"a*", 0))
{
err(L"Cannot skip wildcard expansion");
}
if (system("mkdir -p /tmp/fish_expand_test/")) err(L"mkdir failed");
if (system("touch /tmp/fish_expand_test/.foo")) err(L"touch failed");
if (system("touch /tmp/fish_expand_test/bar")) err(L"touch failed");
// This is checking that .* does NOT match . and .. (https://github.com/fish-shell/fish-shell/issues/270). But it does have to match literal components (e.g. "./*" has to match the same as "*"
if (! expand_test(L"/tmp/fish_expand_test/.*", 0, L"/tmp/fish_expand_test/.foo", 0))
{
err(L"Expansion not correctly handling dotfiles");
}
if (! expand_test(L"/tmp/fish_expand_test/./.*", 0, L"/tmp/fish_expand_test/./.foo", 0))
{
err(L"Expansion not correctly handling literal path components in dotfiles");
}
system("rm -Rf /tmp/fish_expand_test");
}
static void test_fuzzy_match(void)
{