OILS / spec / ysh-stdlib-testing.test.sh View on Github | oilshell.org

130 lines, 78 significant
1## our_shell: ysh
2
3#### Test framework
4
5source --builtin testing.ysh
6setglobal _test_use_color = false
7
8test-suite "three bad tests" {
9 test-case "the assertion is false" {
10 assert [1 > 0]
11 assert [1 > 1]
12 assert [1 > 2]
13 }
14
15 test-case "there is an exception while evaluating the assertion" {
16 assert ["Superman" > "Batman"]
17 }
18
19 test-case "there is an exception outside of any assertion" {
20 error "oops!"
21 }
22}
23
24test-case "one good test case" {
25 assert [1 === 1]
26 assert [2 === 2]
27}
28
29run-tests
30
31## STDOUT:
32begin three bad tests
33 test the assertion is false ...
34 assertion FAILED
35 test there is an exception while evaluating the assertion ...
36 assertion ERRORED: 10
37 test there is an exception outside of any assertion ...
38 ERROR: 10
39end
40test one good test case ... ok
413 / 4 tests failed
42## END
43
44#### Test framework: nested test suites
45
46source --builtin testing.ysh
47setglobal _test_use_color = false
48
49test-case "A" { : }
50test-suite "outer test suite" {
51 test-case "B" { : }
52 test-suite "first inner test suite" {
53 test-case "C" { : }
54 }
55 test-case "D" { : }
56 test-suite "second inner test suite" {
57 test-case "E" { : }
58 }
59 test-case "F" { : }
60}
61test-case "G" { : }
62
63run-tests
64
65## STDOUT:
66test A ... ok
67begin outer test suite
68 test B ... ok
69 begin first inner test suite
70 test C ... ok
71 end
72 test D ... ok
73 begin second inner test suite
74 test E ... ok
75 end
76 test F ... ok
77end
78test G ... ok
797 tests succeeded
80## END
81
82#### Test framework: stdout and stderr
83
84source --builtin testing.ysh
85setglobal _test_use_color = false
86
87proc p {
88 echo STDOUT
89 echo STDERR >& 2
90 return 42
91}
92
93test-case "that it prints to stdout and stderr" {
94 # each case changes to a clean directory?
95 #
96 # and each one is numbered?
97
98 try {
99 p > out 2> err
100 }
101
102 assert [_status === 42]
103 assert [$(<out) === "STDOUT"]
104 assert [$(<err) === "STDERR"]
105}
106
107run-tests
108
109## STDOUT:
110test that it prints to stdout and stderr ... ok
1111 tests succeeded
112## END
113
114# #### ysh --tool test file
115#
116# cat >mytest.ysh <<EOF
117# echo hi
118# EOF
119#
120# # which ysh
121#
122# # the test framework sets $SH to bin/ysh
123# # but ysh is already installed on this machine
124#
125# $SH --tool test mytest.ysh
126#
127# ## STDOUT:
128# ## END
129#
130# # Hm can we do this entirely in user code, not as a builtin?