OILS / stdlib / ysh / list-test.ysh View on Github | oilshell.org

87 lines, 69 significant
1#!bin/ysh
2
3use $LIB_YSH/list.ysh --pick any all sum repeat
4
5# Change to 'use'?
6source $LIB_OSH/byo-server.sh
7
8proc test-any {
9 assert [false === any([])]
10 assert [true === any([true])]
11 assert [false === any([false])]
12 assert [true === any([true, false])]
13 assert [true === any([false, true])]
14 assert [false === any([false, false])]
15 assert [true === any([false, true, false])]
16 assert [false === any([false, false, null, ""])] # null and "" are falsey
17 assert [true === any(["foo"])] # "foo" is truthy
18}
19
20proc test-all {
21 assert [true === all([])]
22 assert [true === all([true])]
23 assert [false === all([false])]
24 assert [true === all([true, true])]
25 assert [false === all([true, false])]
26 assert [false === all([false, true])]
27 assert [false === all([false, false])]
28 assert [false === all([false, true, false])]
29 assert [true === all(["foo"])]
30 assert [false === all([""])]
31}
32
33proc test-more-any-all {
34 assert [true === all( :|yes yes| ) ]
35 assert [false === all( :|yes ''| ) ]
36 assert [false === all( :|'' ''| ) ]
37
38 assert [true === any( :|yes yes| ) ]
39 assert [true === any( :|yes ''| ) ]
40 assert [false === any( :|'' ''| ) ]
41}
42
43proc test-sum {
44 assert [0 === sum([])]
45 assert [0 === sum([0])]
46 assert [6 === sum([1, 2, 3])]
47
48 assert [3 === sum( 0 .. 3 )]
49 assert [45 === sum( 0 .. 3; start=42)]
50 assert [42 === sum( 0 .. 0, start=42)]
51}
52
53proc test-repeat-str {
54 assert ['foofoofoo' === repeat('foo', 3)]
55 assert ['' === repeat('foo', 0)]
56 assert ['' === repeat('foo',-1)]
57}
58
59proc test-repeat-list {
60 var L = ['foo', 'bar']
61 assert [ :|foo bar foo bar foo bar| === repeat(L, 3) ]
62 assert [ :|| === repeat(L, 0) ]
63 assert [ :|| === repeat(L, -1) ]
64}
65
66proc test-repeat-error {
67 #pp test_ (repeat(null, 3))
68
69 try {
70 pp test_ (repeat(null, 3))
71 }
72 assert [10 === _error.code]
73
74 try {
75 pp test_ (repeat({}, 3))
76 }
77 assert [10 === _error.code]
78
79 try {
80 pp test_ (repeat(42, 3))
81 }
82 assert [10 === _error.code]
83}
84
85if is-main {
86 byo-maybe-run
87}