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

267 lines, 165 significant
1# Pretty printing tests
2
3#### Int
4= -123
5## stdout: (Int) -123
6
7#### Float
8= -0.00
9## stdout: (Float) -0.0
10
11#### Null
12= null
13## stdout: (Null) null
14
15#### Bool
16= true
17= false
18## STDOUT:
19(Bool) true
20(Bool) false
21## END
22
23#### String
24= "double quoted"
25= 'single quoted'
26## STDOUT:
27(Str) 'double quoted'
28(Str) 'single quoted'
29## END
30
31#### Range
32var x = 1..100
33
34pp (x)
35
36# TODO: show type here, like (Range 1 .. 100)
37
38pp ({k: x})
39
40echo
41
42remove-addr() {
43 sed 's/0x[0-9a-f]\+/0x---/'
44}
45
46pp line (x) | remove-addr
47pp line ({k: x}) | remove-addr
48
49## STDOUT:
50(Range 1 .. 100)
51(Dict) {k: (Range 1 .. 100)}
52
53<Range 0x--->
54(Dict) {"k":<Range 0x--->}
55## END
56
57
58#### Eggex (reference type)
59var pat = /d+/
60
61remove-addr() {
62 sed 's/0x[0-9a-f]\+/0x---/'
63}
64
65pp (pat) | remove-addr
66
67pp ({k: pat}) | remove-addr
68
69# TODO: change this
70
71echo
72
73pp line (pat) | remove-addr
74pp line ({k: pat}) | remove-addr
75
76## STDOUT:
77<Eggex 0x--->
78(Dict) {k: <Eggex 0x--->}
79
80<Eggex 0x--->
81(Dict) {"k":<Eggex 0x--->}
82## END
83
84#### SparseArray, new representation for bash array
85declare -a empty=()
86declare -a array_1=(hello)
87array_1[5]=5
88
89var empty = _a2sp(empty)
90var array_1 = _a2sp(array_1)
91
92pp (empty)
93pp (array_1)
94echo
95
96pp ({k: empty})
97pp ({k: array_1})
98echo
99
100pp line (empty)
101pp line (array_1)
102echo
103
104pp line ({k: empty})
105pp line ({k: array_1})
106
107## STDOUT:
108(SparseArray)
109(SparseArray [0]='hello' [5]='5')
110
111(Dict) {k: (SparseArray)}
112(Dict) {k: (SparseArray [0]='hello' [5]='5')}
113
114{"type":"SparseArray","data":{}}
115{"type":"SparseArray","data":{"0":"hello","5":"5"}}
116
117(Dict) {"k":{"type":"SparseArray","data":{}}}
118(Dict) {"k":{"type":"SparseArray","data":{"0":"hello","5":"5"}}}
119## END
120
121#### BashArray, short
122declare -a empty=()
123declare -a array_1=(hello)
124
125pp (empty)
126pp (array_1)
127echo
128
129pp ({k: empty})
130pp ({k: array_1})
131echo
132
133pp line (empty)
134pp line (array_1)
135echo
136
137pp line ({k: empty})
138pp line ({k: array_1})
139
140## STDOUT:
141(BashArray)
142(BashArray 'hello')
143
144(Dict) {k: (BashArray)}
145(Dict) {k: (BashArray 'hello')}
146
147{"type":"BashArray","data":{}}
148{"type":"BashArray","data":{"0":"hello"}}
149
150(Dict) {"k":{"type":"BashArray","data":{}}}
151(Dict) {"k":{"type":"BashArray","data":{"0":"hello"}}}
152## END
153
154#### BashArray, long
155declare -a array_3
156array_3[0]="world"
157array_3[2]=*.py
158declare array_long=(Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
159do eiusmod.)
160= array_3
161= array_long
162## STDOUT:
163(BashArray 'world' null '*.py')
164(BashArray
165 'Lorem' 'ipsum' 'dolor' 'sit' 'amet,'
166 'consectetur' 'adipiscing' 'elit,' 'sed' 'do'
167 'eiusmod.'
168)
169## END
170
171#### BashAssoc, short
172declare -A empty
173declare -A assoc=(['k']=$'foo \x01\u03bc')
174
175pp (empty)
176pp (assoc)
177echo
178
179pp ({k:empty})
180pp ({k:assoc})
181echo
182
183pp line (empty)
184pp line (assoc)
185echo
186
187pp line ({k:empty})
188pp line ({k:assoc})
189
190## STDOUT:
191(BashAssoc)
192(BashAssoc ['k']=$'foo \u0001μ')
193
194(Dict) {k: (BashAssoc)}
195(Dict) {k: (BashAssoc ['k']=$'foo \u0001μ')}
196
197{"type":"BashAssoc","data":{}}
198{"type":"BashAssoc","data":{"k":"foo \u0001μ"}}
199
200(Dict) {"k":{"type":"BashAssoc","data":{}}}
201(Dict) {"k":{"type":"BashAssoc","data":{"k":"foo \u0001μ"}}}
202## END
203
204
205#### BashAssoc, long
206declare -A assoc_0=()
207declare -A assoc_1=([1]=one)
208declare assoc_3=([1]=one [two]=2 [3]=three)
209declare assoc_long=([Lorem]=ipsum [dolor]="sit amet," ['consectetur adipiscing']="elit, sed" [do]="eiusmod.")
210= assoc_0
211= assoc_1
212= assoc_3
213= assoc_long
214## STDOUT:
215(BashAssoc)
216(BashAssoc ['1']='one')
217(BashAssoc ['1']='one' ['two']='2' ['3']='three')
218(BashAssoc
219 ['Lorem']='ipsum'
220 ['dolor']='sit amet,'
221 ['consectetur adipiscing']='elit, sed'
222 ['do']='eiusmod.'
223)
224## END
225
226#### Simple Cycles
227var cyclic_array = ["one", "two", "three"]
228setvar cyclic_array[2] = cyclic_array
229var cyclic_dict = {"dead_end": null}
230setvar cyclic_dict["live_end"] = cyclic_dict
231= cyclic_array
232= cyclic_dict
233## STDOUT:
234(List) ['one', 'two', [...]]
235(Dict) {dead_end: null, live_end: {...}}
236## END
237
238#### Complex Cycles
239var dict = {}
240setvar dict["nothing"] = null
241var simple_cycle = [["dummy"]]
242setvar simple_cycle[0][0] = simple_cycle
243setvar dict["simple_cycle"] = simple_cycle
244var tricky_cycle = ["dummy"]
245setvar tricky_cycle[0] = dict
246setvar dict["tricky_cycle"] = tricky_cycle
247var dag = [1, 2, "dummy"]
248setvar dag[2] = dag
249setvar dict["dag"] = [dag, dag]
250var alpha = {}
251var omega = {}
252setvar alpha["omega"] = omega
253setvar omega["alpha"] = alpha
254setvar dict["key_alpha"] = alpha
255setvar dict["key_omega"] = omega
256= dict
257## STDOUT:
258(Dict)
259{
260 nothing: null,
261 simple_cycle: [[[...]]],
262 tricky_cycle: [{...}],
263 dag: [[1, 2, [...]], [1, 2, [...]]],
264 key_alpha: {omega: {alpha: {...}}},
265 key_omega: {alpha: {omega: {...}}}
266}
267## END