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

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