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

222 lines, 138 significant
1# spec/ysh-stdlib
2
3## our_shell: ysh
4
5#### identity
6source --builtin funcs.ysh
7
8for x in (['a', 1, null, { foo: 'bar' }, [40, 2]]) {
9 json write (identity(x))
10}
11
12## STDOUT:
13"a"
141
15null
16{
17 "foo": "bar"
18}
19[
20 40,
21 2
22]
23## END
24
25#### max
26source $LIB_YSH/math.ysh
27
28json write (max(1, 2))
29json write (max([1, 2, 3]))
30
31try { call max([]) }
32echo status=$_status
33
34try { call max(1, 2) }
35echo status=$_status
36
37try { call max(1, 2, 3) }
38echo status=$_status
39
40try { call max() }
41echo status=$_status
42
43## STDOUT:
442
453
46status=3
47status=0
48status=3
49status=3
50## END
51
52#### min
53source $LIB_YSH/math.ysh
54
55json write (min(2, 3))
56json write (min([1, 2, 3]))
57
58try { call min([]) }
59echo status=$_status
60
61try { call min(2, 3) }
62echo status=$_status
63
64try { call min(1, 2, 3) }
65echo status=$_status
66
67try { call min() }
68echo status=$_status
69
70## STDOUT:
712
721
73status=3
74status=0
75status=3
76status=3
77## END
78
79#### abs
80source $LIB_YSH/math.ysh
81
82json write (abs(-1))
83json write (abs(0))
84json write (abs(1))
85json write (abs(42))
86json write (abs(-42))
87
88try { call abs(-42) }
89echo status=$_status
90
91## STDOUT:
921
930
941
9542
9642
97status=0
98## END
99
100#### any
101source $LIB_YSH/list.ysh
102
103json write (any([]))
104json write (any([true]))
105json write (any([false]))
106json write (any([true, false]))
107json write (any([false, true]))
108json write (any([false, false]))
109json write (any([false, true, false]))
110json write (any([false, false, null, ""])) # null and "" are falsey
111json write (any(["foo"])) # "foo" is truthy
112## STDOUT:
113false
114true
115false
116true
117true
118false
119true
120false
121true
122## END
123
124#### all
125source $LIB_YSH/list.ysh
126
127json write (all([]))
128json write (all([true]))
129json write (all([false]))
130json write (all([true, true]))
131json write (all([true, false]))
132json write (all([false, true]))
133json write (all([false, false]))
134json write (all([false, true, false]))
135json write (all(["foo"]))
136json write (all([""]))
137## STDOUT:
138true
139true
140false
141true
142false
143false
144false
145false
146true
147false
148## END
149
150#### sum
151source $LIB_YSH/list.ysh
152
153json write (sum([]))
154json write (sum([0]))
155json write (sum([1, 2, 3]))
156## STDOUT:
1570
1580
1596
160## END
161
162
163#### repeat() string
164
165source $LIB_YSH/list.ysh
166
167echo three=$[repeat('foo', 3)]
168echo zero=$[repeat('foo', 0)]
169echo negative=$[repeat('foo', -1)]
170
171## STDOUT:
172three=foofoofoo
173zero=
174negative=
175## END
176
177#### repeat() list
178
179source $LIB_YSH/list.ysh
180
181var L = ['foo', 'bar']
182echo three @[repeat(L, 3)]
183echo zero @[repeat(L, 0)]
184echo negative @[repeat(L, -1)]
185
186## STDOUT:
187three foo bar foo bar foo bar
188zero
189negative
190## END
191
192#### repeat() error
193
194try {
195 $SH -c '
196 source $LIB_YSH/list.ysh
197 pp line (repeat(null, 3))
198 echo bad'
199}
200echo code=$[_error.code]
201
202try {
203 $SH -c '
204 source $LIB_YSH/list.ysh
205 pp line (repeat({}, 3))
206 echo bad'
207}
208echo code=$[_error.code]
209
210try {
211 $SH -c '
212 source $LIB_YSH/list.ysh
213 pp line (repeat(42, 3))
214 echo bad'
215}
216echo code=$[_error.code]
217
218## STDOUT:
219code=10
220code=10
221code=10
222## END