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