-
Notifications
You must be signed in to change notification settings - Fork 4
/
unittest_test.sh
1234 lines (1049 loc) · 37.7 KB
/
unittest_test.sh
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
# Copyright 2014 Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Google Inc. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
shtk_import unittest
shtk_unittest_add_fixture add_fixture
add_fixture_fixture() {
shtk_unittest_add_test default_methods
default_methods_test() {
shtk_unittest_add_fixture first \
|| fail "Failed to register fixture"
( first_fixture >out 2>err ) \
&& fail "fixture method not properly defined"
expect_file inline:"unittest_test: E: first_fixture not defined\n" err
}
shtk_unittest_add_test duplicate_error
duplicate_error_test() {
shtk_unittest_add_fixture first || fail "add_fixture failed"
shtk_unittest_add_fixture dup || fail "add_fixture failed"
shtk_unittest_add_fixture last || fail "add_fixture failed"
shtk_unittest_add_test first || fail "add_test failed"
( shtk_unittest_add_fixture dup >out 2>err ) \
&& fail "add_fixture did not fail for a duplicate test case"
expect_file \
inline:"unittest_test: E: Duplicate test fixture found: dup\n" err
}
}
shtk_unittest_add_fixture add_test
add_test_fixture() {
shtk_unittest_add_test default_methods
default_methods_test() {
shtk_unittest_add_test first \
|| fail "Failed to register test case"
( first_test >out 2>err ) \
&& fail "test method not properly defined"
expect_file inline:"unittest_test: E: first_test not defined\n" err
}
shtk_unittest_add_test duplicate_error
duplicate_error_test() {
shtk_unittest_add_test first || fail "add_test failed"
shtk_unittest_add_test dup || fail "add_test failed"
shtk_unittest_add_test last || fail "add_test failed"
( shtk_unittest_add_test dup >out 2>err ) \
&& fail "add_test did not fail for a duplicate test case"
expect_file \
inline:"unittest_test: E: Duplicate test case found: dup\n" err
}
}
shtk_unittest_add_fixture delayed_fail
delayed_fail_fixture() {
shtk_unittest_add_test one_argument
one_argument_test() {
( shtk_unittest_delayed_fail "This is a message"; echo "more stuff" ) \
>out 2>err || fail "delayed_fail exited prematurely"
expect_file stdin out <<EOF
more stuff
EOF
expect_file \
inline:"unittest_test: W: Delayed failure: This is a message\n" err
expect_file inline:"1\n" result.delayed-fail
rm -f result.delayed-fail
}
shtk_unittest_add_test argument_concatenation
argument_concatenation_test() {
( shtk_unittest_delayed_fail "One" "more message"; echo "hi" ) \
>out 2>err || fail "delayed_fail exited prematurely"
expect_file stdin out <<EOF
hi
EOF
expect_file \
inline:"unittest_test: W: Delayed failure: One more message\n" err
expect_file inline:"1\n" result.delayed-fail
rm -f result.delayed-fail
}
shtk_unittest_add_test counter
counter_test() {
(
shtk_unittest_delayed_fail "This is a message";
echo "more stuff"
shtk_unittest_delayed_fail "This is another message";
echo "exiting"
) >out 2>err || fail "delayed_fail exited prematurely"
expect_file stdin out <<EOF
more stuff
exiting
EOF
expect_file stdin err <<EOF
unittest_test: W: Delayed failure: This is a message
unittest_test: W: Delayed failure: This is another message
EOF
expect_file inline:"2\n" result.delayed-fail
rm -f result.delayed-fail
}
shtk_unittest_add_test expect_failure
expect_failure_test() {
(
shtk_unittest_set_expect_failure
shtk_unittest_delayed_fail "This is a message"
echo "more stuff"
) >out 2>err
if [ "${?}" -eq 0 ]; then
rm -f result.expect-fail
fail "delayed_fail exited cleanly"
else
rm -f result.expect-fail
fi
expect_file empty out
expect_file stdin err <<EOF
unittest_test: E: delayed_fail does not support expected failures
EOF
[ ! -f result.delayed-fail ] || fail "Found delayed_fail cookie"
}
}
shtk_unittest_add_fixture fail
fail_fixture() {
shtk_unittest_add_test one_argument
one_argument_test() {
( shtk_unittest_fail "This is a message" >out 2>err ) \
&& fail "fail did not exit with an error"
expect_file empty out
expect_file inline:"unittest_test: E: This is a message\n" err
}
shtk_unittest_add_test argument_concatenation
argument_concatenation_test() {
( shtk_unittest_fail "This is" "another message" >out 2>err ) \
&& fail "fail did not exit with an error"
expect_file empty out
expect_file inline:"unittest_test: E: This is another message\n" err
}
shtk_unittest_add_test expect_failure
expect_failure_test() {
(
shtk_unittest_set_expect_failure
shtk_unittest_fail "Some text"
echo "not reached"
) >out 2>err
if [ "${?}" -eq 0 ]; then
rm -f result.expect-fail
fail "fail did not exit with an error"
else
rm -f result.expect-fail
fi
expect_file empty out
expect_file inline:"unittest_test: E: Expected failure: Some text\n" err
}
}
shtk_unittest_add_fixture main
main_fixture() {
setup() {
_Shtk_Unittest_TestCases=
_Shtk_Unittest_TestFixtures=
}
shtk_unittest_add_test run_one_test_that_passes
run_one_test_that_passes_test() {
shtk_unittest_add_test first
first_test() { echo "first passes"; }
( shtk_unittest_main >out 2>err ) \
|| fail "main returned failure but all tests were supposed to pass"
expect_file stdin out <<EOF
first passes
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing first...
unittest_test: I: Testing first... PASSED
unittest_test: I: Ran 1 tests; ALL PASSED
EOF
}
shtk_unittest_add_test run_one_test_that_fails
run_one_test_that_fails_test() {
shtk_unittest_add_test first
first_test() { fail "first fails"; }
( shtk_unittest_main >out 2>err ) \
&& fail "main returned success but all tests were supposed to fail"
expect_file stdin err <<EOF
unittest_test: I: Testing first...
unittest_test: E: first fails
unittest_test: W: Testing first... FAILED
unittest_test: W: Ran 1 tests; 1 FAILED
EOF
}
shtk_unittest_add_test run_some_tests_that_pass
run_some_tests_that_pass_test() {
shtk_unittest_add_test first
first_test() { echo "first passes"; }
shtk_unittest_add_test second
second_test() { skip "second skips"; echo "Not reached"; }
shtk_unittest_add_test third
third_test() { echo "third passes"; }
( shtk_unittest_main >out 2>err ) \
|| fail "main returned failure but all tests were supposed to pass"
expect_file stdin out <<EOF
first passes
third passes
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing first...
unittest_test: I: Testing first... PASSED
unittest_test: I: Testing second...
unittest_test: W: second skips
unittest_test: W: Testing second... SKIPPED
unittest_test: I: Testing third...
unittest_test: I: Testing third... PASSED
unittest_test: I: Ran 3 tests; ALL PASSED
EOF
}
shtk_unittest_add_test run_some_tests_that_fail
run_some_tests_that_fail_test() {
shtk_unittest_add_test first
first_test() { echo "first passes"; }
shtk_unittest_add_test second
second_test() { fail "second fails"; }
shtk_unittest_add_test third
third_test() { fail "third fails"; }
shtk_unittest_add_test fourth
fourth_test() { echo "fourth passes"; }
( shtk_unittest_main >out 2>err ) \
&& fail "main returned success but some tests were supposed to fail"
expect_file stdin out <<EOF
first passes
fourth passes
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing first...
unittest_test: I: Testing first... PASSED
unittest_test: I: Testing second...
unittest_test: E: second fails
unittest_test: W: Testing second... FAILED
unittest_test: I: Testing third...
unittest_test: E: third fails
unittest_test: W: Testing third... FAILED
unittest_test: I: Testing fourth...
unittest_test: I: Testing fourth... PASSED
unittest_test: W: Ran 4 tests; 2 FAILED
EOF
}
shtk_unittest_add_test run_tests_and_fixtures_passing
run_tests_and_fixtures_passing_test() {
shtk_unittest_add_test first
first_test() { echo "first passes"; }
shtk_unittest_add_test second
second_test() { echo "second passes"; }
shtk_unittest_add_fixture third
third_fixture() {
setup() { echo "Fixture setup"; }
teardown() { echo "Fixture teardown"; }
shtk_unittest_add_test first
first_test() { echo "first within third passes"; }
shtk_unittest_add_test other
other_test() { echo "other within third passes"; }
}
( shtk_unittest_main >out 2>err ) \
|| fail "main returned failure but all tests were supposed to pass"
expect_file stdin out <<EOF
first passes
second passes
Fixture setup
first within third passes
Fixture teardown
Fixture setup
other within third passes
Fixture teardown
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing first...
unittest_test: I: Testing first... PASSED
unittest_test: I: Testing second...
unittest_test: I: Testing second... PASSED
unittest_test: I: Testing third__first...
unittest_test: I: Testing third__first... PASSED
unittest_test: I: Testing third__other...
unittest_test: I: Testing third__other... PASSED
unittest_test: I: Ran 4 tests; ALL PASSED
EOF
}
shtk_unittest_add_test run_tests_and_fixtures_failing
run_tests_and_fixtures_failing_test() {
shtk_unittest_add_test first
first_test() { echo "first passes"; }
shtk_unittest_add_test second
second_test() { fail "second fails"; echo "Not run"; }
shtk_unittest_add_fixture third
third_fixture() {
setup() { echo "Fixture setup"; }
teardown() { echo "Fixture teardown"; }
shtk_unittest_add_test first
first_test() { fail "first within third fails"; echo "Not run"; }
shtk_unittest_add_test other
other_test() { echo "other within third passes"; }
}
( shtk_unittest_main >out 2>err ) \
&& fail "main returned success but some tests were supposed to fail"
expect_file stdin out <<EOF
first passes
Fixture setup
Fixture teardown
Fixture setup
other within third passes
Fixture teardown
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing first...
unittest_test: I: Testing first... PASSED
unittest_test: I: Testing second...
unittest_test: E: second fails
unittest_test: W: Testing second... FAILED
unittest_test: I: Testing third__first...
unittest_test: E: first within third fails
unittest_test: W: Testing third__first... FAILED
unittest_test: I: Testing third__other...
unittest_test: I: Testing third__other... PASSED
unittest_test: W: Ran 4 tests; 2 FAILED
EOF
}
shtk_unittest_add_test different_directories
different_directories_test() {
shtk_unittest_add_test first
first_test() {
touch the-file
}
shtk_unittest_add_test second
second_test() {
if [ -f the-file ]; then
fail "Found file from previous test!"
fi
}
( shtk_unittest_main >out 2>err ) \
|| fail "main returned failure but all tests were supposed to pass"
expect_file empty out
expect_file stdin err <<EOF
unittest_test: I: Testing first...
unittest_test: I: Testing first... PASSED
unittest_test: I: Testing second...
unittest_test: I: Testing second... PASSED
unittest_test: I: Ran 2 tests; ALL PASSED
EOF
}
shtk_unittest_add_test one_time_setup_and_teardown
one_time_setup_and_teardown_test() {
one_time_setup() { echo "setting up"; echo "still here" >>"cookie"; }
one_time_teardown() { echo "tearing down"; rm "cookie"; }
shtk_unittest_add_test first
first_test() { echo "first test"; cat ../cookie; }
shtk_unittest_add_test second
second_test() { echo "second test"; cat ../cookie; }
( shtk_unittest_main >out 2>err ) \
|| fail "main returned failure but all tests were supposed to pass"
expect_file stdin out <<EOF
setting up
first test
still here
second test
still here
tearing down
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing first...
unittest_test: I: Testing first... PASSED
unittest_test: I: Testing second...
unittest_test: I: Testing second... PASSED
unittest_test: I: Ran 2 tests; ALL PASSED
EOF
}
define_tests_for_filter() {
shtk_unittest_add_test standalone
standalone_test() { echo "standalone passes"; }
shtk_unittest_add_fixture first
first_fixture() {
shtk_unittest_add_test one
one_test() { echo "one within first passes"; }
shtk_unittest_add_test two
two_test() { fail "two within first fails"; echo "Not run"; }
shtk_unittest_add_test standalone
standalone_test() { echo "standalone should never be run"; }
}
shtk_unittest_add_fixture second
second_fixture() {
shtk_unittest_add_test one
one_test() { fail "one within second fails"; echo "Not run"; }
shtk_unittest_add_test two
two_test() { echo "two within second passes"; }
}
}
shtk_unittest_add_test filter__run_standalone
filter__run_standalone_test() {
define_tests_for_filter
( shtk_unittest_main -t standalone >out 2>err ) \
|| fail "main returned failure but all tests were supposed to pass"
expect_file stdin out <<EOF
standalone passes
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing standalone...
unittest_test: I: Testing standalone... PASSED
unittest_test: I: Ran 1 tests; ALL PASSED
EOF
}
shtk_unittest_add_test filter__run_fixture
filter__run_fixture_test() {
define_tests_for_filter
( shtk_unittest_main -t first__two >out 2>err ) \
&& fail "main returned success but some tests were supposed to fail"
expect_file stdin out <<EOF
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing first__two...
unittest_test: E: two within first fails
unittest_test: W: Testing first__two... FAILED
unittest_test: W: Ran 1 tests; 1 FAILED
EOF
}
shtk_unittest_add_test filter__run_mixture
filter__run_mixture_test() {
define_tests_for_filter
( shtk_unittest_main -t first__two -t standalone >out 2>err ) \
&& fail "main returned success but some tests were supposed to fail"
expect_file stdin out <<EOF
standalone passes
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing standalone...
unittest_test: I: Testing standalone... PASSED
unittest_test: I: Testing first__two...
unittest_test: E: two within first fails
unittest_test: W: Testing first__two... FAILED
unittest_test: W: Ran 2 tests; 1 FAILED
EOF
}
shtk_unittest_add_test usage_error_due_to_unknown_arguments
usage_error_due_to_unknown_arguments_test() {
( shtk_unittest_main foo >out 2>err ) \
&& fail "main did not error out on syntax error"
expect_file stdin err <<EOF
unittest_test: E: No command-line arguments allowed
Type 'man 1 shtk_unittest' for help
EOF
}
shtk_unittest_add_test no_tests_error
no_tests_error_test() {
( shtk_unittest_main >out 2>err ) \
&& fail "main did not error out on no tests"
expect_file stdin err <<EOF
unittest_test: E: No test cases defined; did you call shtk_unittest_add_fixture or shtk_unittest_add_test?
EOF
}
}
shtk_unittest_add_fixture register_check
register_check_fixture() {
_shtk_unittest_check_mock() {
local wrapper_name="${1}"; shift
local fail_function="${1}"; shift
echo "Mock called by ${wrapper_name}"
echo "Mock with arguments: ${*}"
[ "${1}" != fail ] || "${fail_function}" "Failing test"
}
_shtk_unittest_register_check mock
shtk_unittest_add_test assert__ok
assert__ok_test() {
(
assert_mock "this call" "should pass"
echo reached
) >out 2>err || fail "mock call expected to pass but failed"
expect_file stdin out <<EOF
Mock called by shtk_unittest_assert_mock
Mock with arguments: this call should pass
reached
EOF
expect_file empty err
}
shtk_unittest_add_test assert__fail
assert__fail_test() {
(
assert_mock fail
echo reached
) >out 2>err && fail "mock call expected to fail but passed"
expect_file stdin out <<EOF
Mock called by shtk_unittest_assert_mock
Mock with arguments: fail
EOF
expect_file inline:"unittest_test: E: Failing test\n" err
}
shtk_unittest_add_test expect__ok
expect__ok_test() {
(
expect_mock "this call" "should pass"
echo reached
) >out 2>err || fail "mock call expected to pass but failed"
expect_file stdin out <<EOF
Mock called by shtk_unittest_expect_mock
Mock with arguments: this call should pass
reached
EOF
expect_file empty err
}
shtk_unittest_add_test expect__fail
expect__fail_test() {
(
expect_mock fail
echo reached
expect_mock fail
echo reached as well
) >out 2>err || fail "mock call expected to pass but failed"
expect_file stdin out <<EOF
Mock called by shtk_unittest_expect_mock
Mock with arguments: fail
reached
Mock called by shtk_unittest_expect_mock
Mock with arguments: fail
reached as well
EOF
expect_file stdin err <<EOF
unittest_test: W: Delayed failure: Failing test
unittest_test: W: Delayed failure: Failing test
EOF
expect_file inline:"2\n" result.delayed-fail
rm -f result.delayed-fail
}
shtk_unittest_add_test expect__integration_with_run
expect__integration_with_run_test() {
shtk_unittest_add_test always_fails
always_fails_test() {
expect_mock fail
echo reached
expect_mock fail
echo reached as well
}
( _shtk_unittest_run_standalone_test always_fails >out 2>err ) \
&& fail "run_test reported success for failing test case"
expect_file stdin out <<EOF
Mock called by shtk_unittest_expect_mock
Mock with arguments: fail
reached
Mock called by shtk_unittest_expect_mock
Mock with arguments: fail
reached as well
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing always_fails...
unittest_test: W: Delayed failure: Failing test
unittest_test: W: Delayed failure: Failing test
unittest_test: W: Testing always_fails... FAILED (2 delayed failures)
EOF
}
}
shtk_unittest_add_fixture run_fixture_test
run_fixture_test_fixture() {
# Because run_fixture_test and run_standalone_test share most of their code
# via _shtk_unittest_{enter,leave}_test, these tests only verify
# fixture-specific behavior.
shtk_unittest_add_test setup_and_teardown_run_on_successful_exit
setup_and_teardown_run_on_successful_exit_test() {
setup() { echo "This is the setup"; }
teardown() { echo "This is the teardown"; }
shtk_unittest_add_test always_passes
always_passes_test() {
echo "This is the test code"
}
( _shtk_unittest_run_fixture_test container always_passes >out 2>err ) \
|| fail "run_fixture reported failure for passing test case"
expect_file stdin out <<EOF
This is the setup
This is the test code
This is the teardown
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing container__always_passes...
unittest_test: I: Testing container__always_passes... PASSED
EOF
}
shtk_unittest_add_test setup_and_teardown_run_on_failure
setup_and_teardown_run_on_failure_test() {
setup() { echo "This is the setup"; }
teardown() { echo "This is the teardown"; }
shtk_unittest_add_test always_fails
always_fails_test() {
echo "This is the test code"
fail "Oh noes; exiting"
echo "Not reached"
}
( _shtk_unittest_run_fixture_test container always_fails >out 2>err ) \
&& fail "run_fixture reported failure for failing test case"
expect_file stdin out <<EOF
This is the setup
This is the test code
This is the teardown
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing container__always_fails...
unittest_test: E: Oh noes; exiting
unittest_test: W: Testing container__always_fails... FAILED
EOF
}
shtk_unittest_add_test setup_failure_aborts_early
setup_failure_aborts_early_test() {
setup() { echo "This is the setup"; exit 1; }
teardown() { echo "This is the teardown"; }
shtk_unittest_add_test always_passes
always_passes_test() {
echo "This is the test code"
}
( _shtk_unittest_run_fixture_test container always_passes >out 2>err ) \
&& fail "run_fixture reported failure for failing setup"
expect_file stdin out <<EOF
This is the setup
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing container__always_passes...
unittest_test: W: Testing container__always_passes... FAILED
EOF
}
shtk_unittest_add_test teardown_failure_fails_test
teardown_failure_fails_test_test() {
setup() { echo "This is the setup"; }
teardown() { echo "This is the teardown"; exit 1; }
shtk_unittest_add_test always_passes
always_passes_test() {
echo "This is the test code"
}
( _shtk_unittest_run_fixture_test container always_passes >out 2>err ) \
&& fail "run_fixture reported failure for failing teardown"
expect_file stdin out <<EOF
This is the setup
This is the test code
This is the teardown
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing container__always_passes...
unittest_test: W: Testing container__always_passes... FAILED
EOF
}
shtk_unittest_add_test pass_on_fallthrough_even_if_false
pass_on_fallthrough_even_if_false_test() {
setup() { echo "This is the setup"; false; }
teardown() { echo "This is the teardown"; false; }
shtk_unittest_add_test should_pass
should_pass_test() {
echo "This is the test code"
false
}
( _shtk_unittest_run_fixture_test container should_pass >out 2>err ) \
|| fail "run_fixture reported failure for passing test case"
expect_file stdin out <<EOF
This is the setup
This is the test code
This is the teardown
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing container__should_pass...
unittest_test: I: Testing container__should_pass... PASSED
EOF
}
shtk_unittest_add_test process_lifecycle
process_lifecycle_test() {
GLOBAL=empty
setup() {
echo "setup enter: ${GLOBAL}"
[ "${GLOBAL}" = empty ] || fail "Got '${GLOBAL}'; expected 'empty'"
GLOBAL=setup
echo "setup exit: ${GLOBAL}"
}
teardown() {
echo "teardown: ${GLOBAL}"
[ "${GLOBAL}" = setup ] || fail "Got '${GLOBAL}'; expected 'setup'"
}
shtk_unittest_add_test change_env
change_env_test() {
echo "test enter: ${GLOBAL}"
[ "${GLOBAL}" = setup ] || fail "Got '${GLOBAL}'; expected 'setup'"
GLOBAL=body
echo "test exit: ${GLOBAL}"
}
( _shtk_unittest_run_fixture_test container change_env >out 2>err ) \
|| fail "run_fixture reported failure for passing test case"
expect_file stdin out <<EOF
setup enter: empty
setup exit: setup
test enter: setup
test exit: body
teardown: setup
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing container__change_env...
unittest_test: I: Testing container__change_env... PASSED
EOF
}
shtk_unittest_add_test same_directory
same_directory_test() {
setup() {
touch created-by-setup
}
teardown() {
[ -f created-by-setup ] || fail "Cannot find file from setup"
}
shtk_unittest_add_test always_passes
always_passes_test() {
[ -f created-by-setup ] || fail "Cannot find file from setup"
}
( _shtk_unittest_run_fixture_test container always_passes >out 2>err ) \
|| fail "run_fixture reported failure for passing test case"
expect_file empty out
expect_file stdin err <<EOF
unittest_test: I: Testing container__always_passes...
unittest_test: I: Testing container__always_passes... PASSED
EOF
}
shtk_unittest_add_test unregistered_error
unregistered_error_test() {
( _shtk_unittest_run_fixture_test container not_there >out 2>err ) \
&& fail "run_fixture did not fail for an unregistered test case"
expect_file stdin err <<EOF
unittest_test: E: Attempting to run unregistered test case container__not_there
EOF
}
}
shtk_unittest_add_fixture run_standalone_test
run_standalone_test_fixture() {
# These tests are intended to verify the execution of individual test cases
# exhaustively.
#
# These tests indirectly verify _shtk_unittest_{enter,leave}_test.
shtk_unittest_add_test pass_due_to_fallthrough
pass_due_to_fallthrough_test() {
shtk_unittest_add_test always_passes
always_passes_test() {
echo "This is the test code"
}
( _shtk_unittest_run_standalone_test always_passes >out 2>err ) \
|| fail "run_test reported failure for passing test case"
expect_file stdin out <<EOF
This is the test code
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing always_passes...
unittest_test: I: Testing always_passes... PASSED
EOF
}
shtk_unittest_add_test pass_on_fallthrough_even_if_false
pass_on_fallthrough_even_if_false_test() {
shtk_unittest_add_test should_pass
should_pass_test() {
echo "This is the test code"
false
}
( _shtk_unittest_run_standalone_test should_pass >out 2>err ) \
|| fail "run_test reported failure for passing test case"
expect_file stdin out <<EOF
This is the test code
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing should_pass...
unittest_test: I: Testing should_pass... PASSED
EOF
}
shtk_unittest_add_test pass_due_to_exit
pass_due_to_exit_test() {
shtk_unittest_add_test always_passes
always_passes_test() {
echo "This is the test code"
exit 0
}
( _shtk_unittest_run_standalone_test always_passes >out 2>err ) \
|| fail "run_test reported failure for passing test case"
expect_file stdin out <<EOF
This is the test code
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing always_passes...
unittest_test: I: Testing always_passes... PASSED
EOF
}
shtk_unittest_add_test fail_due_to_exit
fail_due_to_exit_test() {
shtk_unittest_add_test always_fails
always_fails_test() {
echo "This is the test code"
exit 1
}
( _shtk_unittest_run_standalone_test always_fails >out 2>err ) \
&& fail "run_test reported success for failing test case"
expect_file stdin out <<EOF
This is the test code
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing always_fails...
unittest_test: W: Testing always_fails... FAILED
EOF
}
shtk_unittest_add_test fail_due_to_fail
fail_due_to_fail_test() {
shtk_unittest_add_test always_fails
always_fails_test() {
echo "This is the test code"
shtk_unittest_fail "Aborting test"
echo "Not reached"
}
( _shtk_unittest_run_standalone_test always_fails >out 2>err ) \
&& fail "run_test reported success for failing test case"
expect_file stdin out <<EOF
This is the test code
EOF
expect_file stdin err <<EOF
unittest_test: I: Testing always_fails...
unittest_test: E: Aborting test
unittest_test: W: Testing always_fails... FAILED
EOF
}
shtk_unittest_add_test fail_due_to_delayed_failures
fail_due_to_delayed_failures_test() {
shtk_unittest_add_test always_fails
always_fails_test() {
shtk_unittest_delayed_fail "first delayed failure"
shtk_unittest_delayed_fail "second delayed failure"
shtk_unittest_delayed_fail "third delayed failure"
}
( _shtk_unittest_run_standalone_test always_fails >out 2>err ) \
&& fail "run_test reported success for failing test case"
expect_file empty out
expect_file stdin err <<EOF
unittest_test: I: Testing always_fails...
unittest_test: W: Delayed failure: first delayed failure
unittest_test: W: Delayed failure: second delayed failure
unittest_test: W: Delayed failure: third delayed failure
unittest_test: W: Testing always_fails... FAILED (3 delayed failures)
EOF
}