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

158 lines, 79 significant
1## our_shell: ysh
2
3#### join()
4var x = :|a b 'c d'|
5
6var y = join(x)
7argv.py $y
8
9var z = join(x, ":")
10argv.py $z
11## STDOUT:
12['abc d']
13['a:b:c d']
14## END
15
16#### abs
17
18source --builtin math.ysh
19
20# Also test smooshing
21write $[abs(-5)]$[abs(-0)]$[abs(5)]
22write $[abs(-5)] $[abs(-0)] $[abs(5)]
23## STDOUT:
24505
255
260
275
28## END
29
30#### any() and all()
31source --builtin list.ysh
32
33var a1 = all( :|yes yes| )
34var a2 = all( :|yes ''| )
35var a3 = all( :|'' ''| )
36# This should be true and false or what?
37write $a1 $a2 $a3
38write __
39
40var x1 = any( :|yes yes| )
41var x2 = any( :|yes ''| )
42var x3 = any( :|'' ''| )
43write $x1 $x2 $x3
44
45## STDOUT:
46true
47false
48false
49__
50true
51true
52false
53## END
54
55#### sum()
56source --builtin list.ysh
57
58var start = 42
59
60write $[sum( 0 .. 3 )]
61write $[sum( 0 .. 3; start=42)]
62write $[sum( 0 .. 0, start=42)]
63
64## STDOUT:
653
6645
6742
68## END
69
70#### @[split(x)] respects IFS
71setvar IFS = ":"
72var x = "one:two:three"
73argv.py @[split(x)]
74## STDOUT:
75['one', 'two', 'three']
76## END
77
78#### @[maybe(x)]
79setvar empty = ''
80setvar x = 'X'
81argv.py a @[maybe(empty)] @[maybe(x)] b
82
83setvar n = null
84argv.py a @[maybe(n)] b
85
86## STDOUT:
87['a', 'X', 'b']
88['a', 'b']
89## END
90
91#### maybe() on invalid type is fatal error
92
93# not allowed
94setvar marray = :||
95argv.py a @[maybe(marray)] b
96echo done
97## status: 3
98## STDOUT:
99## END
100
101#### split() on invalid type is fatal error
102var myarray = :| --all --long |
103write -- @[myarray]
104write -- @[split(myarray)]
105## status: 3
106## STDOUT:
107--all
108--long
109## END
110
111#### @[glob(x)]
112
113# empty glob
114write -- A @[glob('__nope__')] B
115echo ___
116
117touch -- a.z b.z -.z
118write -- @[glob('?.z')]
119echo ___
120
121# add it back
122shopt -s dashglob
123write -- @[glob('?.z')]
124
125## STDOUT:
126A
127B
128___
129a.z
130b.z
131___
132-.z
133a.z
134b.z
135## END
136
137
138#### smoke test for stream.ysh and table.ysh
139
140shopt --set redefine_proc_func # byo-maybe-main
141
142source --builtin stream.ysh
143source --builtin table.ysh
144
145# Also run tests
146
147# I guess this should not require being in a specific dir.
148# Also I guess it doesn't need to be executable?
149
150# we can run tests under bash for OSH/YSH code, so there's no coupling.
151
152$REPO_ROOT/test/byo-client.sh run-tests $SH $REPO_ROOT/stdlib/stream.ysh
153$REPO_ROOT/test/byo-client.sh run-tests $SH $REPO_ROOT/stdlib/table.ysh
154
155#$SH $REPO_ROOT/stdlib/stream.ysh run-byo-tests
156# $SH $REPO_ROOT/stdlib/table.ysh run-byo-tests
157
158## status: 0