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

385 lines, 220 significant
1## oils_failures_allowed: 0
2
3#### Int
4= -123
5## STDOUT:
6(Int) -123
7## END
8
9#### Float
10= -0.00
11## STDOUT:
12(Float) -0.0
13## END
14
15#### Null
16= null
17## STDOUT:
18(Null) null
19## END
20
21#### Bool
22= true
23= false
24## STDOUT:
25(Bool) true
26(Bool) false
27## END
28
29#### String
30= "double quoted"
31= 'single quoted'
32## STDOUT:
33(Str) 'double quoted'
34(Str) 'single quoted'
35## END
36
37#### Range
38var x = 1..<100
39
40pp value (x)
41
42# TODO: show type here, like (Range 1 ..< 100)
43
44pp value ({k: x})
45
46echo
47
48pp test_ (x)
49pp test_ ({k: x})
50
51## STDOUT:
52(Range 1 ..< 100)
53(Dict) {k: (Range 1 ..< 100)}
54
55<Range>
56(Dict) {"k":<Range>}
57## END
58
59
60#### Eggex (reference type)
61var pat = /d+/
62
63remove-addr() {
64 sed 's/0x[0-9a-f]\+/0x---/'
65}
66
67pp value (pat) | remove-addr
68
69pp value ({k: pat}) | remove-addr
70
71# TODO: change this
72
73echo
74
75pp test_ (pat)
76pp test_ ({k: pat})
77
78## STDOUT:
79<Eggex 0x--->
80(Dict) {k: <Eggex 0x--->}
81
82<Eggex>
83(Dict) {"k":<Eggex>}
84## END
85
86#### SparseArray, new representation for bash array
87declare -a empty=()
88declare -a array_1=(hello)
89array_1[5]=5
90
91var empty = _a2sp(empty)
92var array_1 = _a2sp(array_1)
93
94pp value (empty)
95pp value (array_1)
96echo
97
98pp value ({k: empty})
99pp value ({k: array_1})
100echo
101
102pp test_ (empty)
103pp test_ (array_1)
104echo
105
106pp test_ ({k: empty})
107pp test_ ({k: array_1})
108
109## STDOUT:
110(SparseArray)
111(SparseArray [0]='hello' [5]='5')
112
113(Dict) {k: (SparseArray)}
114(Dict) {k: (SparseArray [0]='hello' [5]='5')}
115
116{"type":"SparseArray","data":{}}
117{"type":"SparseArray","data":{"0":"hello","5":"5"}}
118
119(Dict) {"k":{"type":"SparseArray","data":{}}}
120(Dict) {"k":{"type":"SparseArray","data":{"0":"hello","5":"5"}}}
121## END
122
123#### BashArray, short
124declare -a empty=()
125declare -a array_1=(hello)
126
127pp value (empty)
128pp value (array_1)
129echo
130
131pp value ({k: empty})
132pp value ({k: array_1})
133echo
134
135pp test_ (empty)
136pp test_ (array_1)
137echo
138
139pp test_ ({k: empty})
140pp test_ ({k: array_1})
141
142## STDOUT:
143(BashArray)
144(BashArray 'hello')
145
146(Dict) {k: (BashArray)}
147(Dict) {k: (BashArray 'hello')}
148
149{"type":"BashArray","data":{}}
150{"type":"BashArray","data":{"0":"hello"}}
151
152(Dict) {"k":{"type":"BashArray","data":{}}}
153(Dict) {"k":{"type":"BashArray","data":{"0":"hello"}}}
154## END
155
156#### BashArray, long
157declare -a array_3
158array_3[0]="world"
159array_3[2]=*.py
160declare array_long=(Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
161do eiusmod.)
162= array_3
163= array_long
164## STDOUT:
165(BashArray 'world' null '*.py')
166(BashArray
167 'Lorem' 'ipsum' 'dolor' 'sit' 'amet,'
168 'consectetur' 'adipiscing' 'elit,' 'sed' 'do'
169 'eiusmod.'
170)
171## END
172
173#### BashAssoc, short
174declare -A empty
175declare -A assoc=(['k']=$'foo \x01\u03bc')
176
177pp value (empty)
178pp value (assoc)
179echo
180
181pp value ({k:empty})
182pp value ({k:assoc})
183echo
184
185pp test_ (empty)
186pp test_ (assoc)
187echo
188
189pp test_ ({k:empty})
190pp test_ ({k:assoc})
191
192## STDOUT:
193(BashAssoc)
194(BashAssoc ['k']=$'foo \u0001μ')
195
196(Dict) {k: (BashAssoc)}
197(Dict) {k: (BashAssoc ['k']=$'foo \u0001μ')}
198
199{"type":"BashAssoc","data":{}}
200{"type":"BashAssoc","data":{"k":"foo \u0001μ"}}
201
202(Dict) {"k":{"type":"BashAssoc","data":{}}}
203(Dict) {"k":{"type":"BashAssoc","data":{"k":"foo \u0001μ"}}}
204## END
205
206
207#### BashAssoc, long
208declare -A assoc_0=()
209declare -A assoc_1=([1]=one)
210declare assoc_3=([1]=one [two]=2 [3]=three)
211declare assoc_long=([Lorem]=ipsum [dolor]="sit amet," ['consectetur adipiscing']="elit, sed" [do]="eiusmod.")
212= assoc_0
213= assoc_1
214= assoc_3
215= assoc_long
216## STDOUT:
217(BashAssoc)
218(BashAssoc ['1']='one')
219(BashAssoc ['1']='one' ['two']='2' ['3']='three')
220(BashAssoc
221 ['Lorem']='ipsum'
222 ['dolor']='sit amet,'
223 ['consectetur adipiscing']='elit, sed'
224 ['do']='eiusmod.'
225)
226## END
227
228#### Simple Cycles
229var cyclic_array = ["one", "two", "three"]
230setvar cyclic_array[2] = cyclic_array
231var cyclic_dict = {"dead_end": null}
232setvar cyclic_dict["live_end"] = cyclic_dict
233= cyclic_array
234= cyclic_dict
235## STDOUT:
236(List) ['one', 'two', [...]]
237(Dict) {dead_end: null, live_end: {...}}
238## END
239
240#### Complex Cycles
241var dict = {}
242setvar dict["nothing"] = null
243var simple_cycle = [["dummy"]]
244setvar simple_cycle[0][0] = simple_cycle
245setvar dict["simple_cycle"] = simple_cycle
246var tricky_cycle = ["dummy"]
247setvar tricky_cycle[0] = dict
248setvar dict["tricky_cycle"] = tricky_cycle
249var dag = [1, 2, "dummy"]
250setvar dag[2] = dag
251setvar dict["dag"] = [dag, dag]
252var alpha = {}
253var omega = {}
254setvar alpha["omega"] = omega
255setvar omega["alpha"] = alpha
256setvar dict["key_alpha"] = alpha
257setvar dict["key_omega"] = omega
258= dict
259## STDOUT:
260(Dict)
261{
262 nothing: null,
263 simple_cycle: [[[...]]],
264 tricky_cycle: [{...}],
265 dag: [[1, 2, [...]], [1, 2, [...]]],
266 key_alpha: {omega: {alpha: {...}}},
267 key_omega: {alpha: {omega: {...}}}
268}
269## END
270
271#### pp test_: List cycle
272
273var no_cycle = [5, 6]
274pp test_ (no_cycle)
275
276var two = [no_cycle, no_cycle]
277pp test_ (two)
278#pp value (two)
279
280echo
281
282var L = [42]
283call L->append(L)
284pp test_ (L)
285#pp value (L)
286
287var two = [L, L]
288pp test_ (two)
289#pp value (two)
290
291## STDOUT:
292(List) [5,6]
293(List) [[5,6],[5,6]]
294
295(List) [42,[...]]
296(List) [[42,[...]],[42,[...]]]
297## END
298
299#### pp test_: Dict cycle
300
301var no_cycle = {z: 99}
302pp test_ (no_cycle)
303
304var two = [no_cycle, no_cycle]
305pp test_ (two)
306
307#pp value (two)
308
309echo
310
311var d = {k: 42}
312setvar d.cycle = d
313pp test_ (d)
314#pp value (d)
315
316var two = [d, d]
317pp test_ (two)
318#pp value (two)
319
320
321## STDOUT:
322(Dict) {"z":99}
323(List) [{"z":99},{"z":99}]
324
325(Dict) {"k":42,"cycle":{...}}
326(List) [{"k":42,"cycle":{...}},{"k":42,"cycle":{...}}]
327## END
328
329#### pp test_: Obj cycle
330
331var methods = Object(null, {__foo__: null})
332var obj = Object(methods, {z: 99})
333pp test_ (obj)
334
335setvar obj.cycle = obj
336pp test_ (obj)
337
338echo
339
340var two = [obj, obj]
341pp test_ (two)
342
343## STDOUT:
344(Obj) ("z":99) --> ("__foo__":null)
345(Obj) ("z":99,"cycle":(...)) --> ("__foo__":null)
346
347(List) [("z":99,"cycle":(...)) --> ("__foo__":null),("z":99,"cycle":(...)) --> ("__foo__":null)]
348## END
349
350
351
352#### pp test_: Obj with dict cycle
353
354var methods = Object(null, {__foo__: null})
355var no_cycle = Object(methods, {z: 99})
356pp test_ (no_cycle)
357
358var two = [no_cycle, no_cycle]
359pp test_ (two)
360
361echo
362
363var d = {k: 42}
364setvar d.cycle = d
365
366# This cycle detection doesn't quite work
367# Because we're only considering the object itself
368
369var o = Object(null, d)
370pp test_ (o)
371
372var two = [o, o]
373pp test_ (two)
374
375#var o2 = Object(o, {z: 99})
376#pp test_ (o2)
377
378## STDOUT:
379(Obj) ("z":99) --> ("__foo__":null)
380(List) [("z":99) --> ("__foo__":null),("z":99) --> ("__foo__":null)]
381
382(Obj) ("k":42,"cycle":{"k":42,"cycle":{...}})
383(List) [("k":42,"cycle":{"k":42,"cycle":{...}}),("k":42,"cycle":{"k":42,"cycle":{...}})]
384## END
385