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

171 lines, 103 significant
1
2
3#### bool() conversion
4echo "$[bool(1234)]"
5echo "$[bool(0)]"
6echo "$[bool('foo')]"
7echo "$[bool(true)]"
8echo "$[bool(1.234)]"
9echo "$[bool([])]"
10echo "$[bool({})]"
11echo "$[bool(null)]"
12echo "$[bool(len)]"
13echo "$[bool('foo'->startsWith)]"
14echo "$[bool(1..3)]"
15## STDOUT:
16true
17false
18true
19true
20true
21false
22false
23false
24true
25true
26true
27## END
28
29#### bool() more
30var a = bool( :|| )
31var b = bool( :|foo| )
32write $a $b
33## STDOUT:
34false
35true
36## END
37
38
39#### int() conversion
40echo "$[int(1234)]"
41echo "$[int('1234')]"
42echo "$[int(1.234)]"
43## STDOUT:
441234
451234
461
47## END
48
49#### int() more
50var a = int("3")
51var b = int("-35")
52write $a $b
53
54var c = int("bad")
55echo 'should not get here'
56
57## status: 3
58## STDOUT:
593
60-35
61## END
62
63#### float() conversion
64echo "$[float(1234)]"
65echo "$[float('1.234')]"
66echo "$[float(2.345)]"
67## STDOUT:
681234.0
691.234
702.345
71## END
72
73#### float() overflow / underflow
74
75var a = float("1.2")
76var b = float("3.4")
77
78var c = float("42.1e500")
79var d = float("-42.1e500")
80
81write $a $b $c $d
82## STDOUT:
831.2
843.4
85inf
86-inf
87## END
88
89#### str() conversion
90echo "$[str(1234)]"
91echo "$[str(1.234)]"
92echo "$[str('foo')]"
93## STDOUT:
941234
951.234
96foo
97## END
98
99#### dict() converts from BashAssoc to Dict
100declare -A foo
101foo=([a]=1 [b]=2 [c]=3)
102
103json write (type(foo))
104json write (dict(foo))
105## STDOUT:
106"BashAssoc"
107{
108 "a": "1",
109 "b": "2",
110 "c": "3"
111}
112## END
113
114#### dict() does shallow copy
115var d = {'a': 1}
116var d2 = d
117setvar d2['b'] = 2
118echo $['b' in d] # d2 should be an alias for d
119
120var d3 = dict(d)
121setvar d3['c'] = 3
122
123# d3 should NOT be an alias
124echo $['c' in d]
125echo $['c' in d3]
126## STDOUT:
127true
128false
129true
130## END
131
132#### list() does shallow copy
133var l = [1]
134var l2 = l
135call l2->append(2)
136echo $[len(l)] # d2 should be an alias for d
137
138var l3 = list(l)
139call l3->append(3)
140
141# l3 should NOT be an alias
142echo $[len(l)]
143echo $[len(l3)]
144## STDOUT:
1452
1462
1473
148## END
149
150#### list() from Dict
151shopt -s ysh:upgrade
152
153var a = list({'a': 1, 'foo': 'bar'})
154write @a
155## STDOUT:
156a
157foo
158## END
159
160
161#### list() from range
162shopt -s ysh:upgrade
163
164var mylist = list(0..3)
165write @mylist
166## STDOUT:
1670
1681
1692
170## END
171