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

185 lines, 122 significant
1#### or short circuits
2shopt -s ysh:upgrade
3
4var x = [1, 2]
5if (true or x[3]) {
6 echo OK
7}
8## STDOUT:
9OK
10## END
11
12#### and short circuits
13shopt -s ysh:upgrade
14
15var x = [1, 2]
16if (false and x[3]) {
17 echo bad
18} else {
19 echo OK
20}
21
22## STDOUT:
23OK
24## END
25
26#### not operator behaves like Python
27
28# consistent with if statement, ternary if, and, or
29
30pp test_ (not "s")
31pp test_ (not 3)
32pp test_ (not 4.5)
33pp test_ (not {})
34pp test_ (not [])
35pp test_ (not false)
36pp test_ (not true)
37
38## STDOUT:
39(Bool) false
40(Bool) false
41(Bool) false
42(Bool) true
43(Bool) true
44(Bool) true
45(Bool) false
46## END
47
48#### not, and, or
49
50var a = not true
51echo $a
52var b = true and false
53echo $b
54var c = true or false
55echo $c
56
57## STDOUT:
58false
59false
60true
61## END
62
63
64#### and-or chains for typed data
65
66python2 -c 'print(None or "s")'
67python2 -c 'print(None and "s")'
68
69python2 -c 'print("x" or "y")'
70python2 -c 'print("x" and "y")'
71
72python2 -c 'print("" or "y")'
73python2 -c 'print("" and "y")'
74
75python2 -c 'print(42 or 0)'
76python2 -c 'print(42 and 0)'
77
78python2 -c 'print(0 or 42)'
79python2 -c 'print(0 and 42)'
80
81python2 -c 'print(0.0 or 0.5)'
82python2 -c 'print(0.0 and 0.5)'
83
84python2 -c 'print(["a"] or [])'
85python2 -c 'print(["a"] and [])'
86
87python2 -c 'print({"d": 1} or {})'
88python2 -c 'print({"d": 1} and {})'
89
90python2 -c 'print(0 or 0.0 or False or [] or {} or "OR")'
91python2 -c 'print(1 and 1.0 and True and [5] and {"d":1} and "AND")'
92
93echo ---
94
95json write (null or "s")
96json write (null and "s")
97
98echo $["x" or "y"]
99echo $["x" and "y"]
100
101echo $["" or "y"]
102echo $["" and "y"]
103
104echo $[42 or 0]
105echo $[42 and 0]
106
107echo $[0 or 42]
108echo $[0 and 42]
109
110echo $[0.0 or 0.5]
111echo $[0.0 and 0.5]
112
113pp test_ (["a"] or [])
114pp test_ (["a"] and [])
115
116pp test_ ({"d": 1} or {})
117pp test_ ({"d": 1} and {})
118
119echo $[0 or 0.0 or false or [] or {} or "OR"]
120echo $[1 and 1.0 and true and [5] and {"d":1} and "AND"]
121
122## STDOUT:
123s
124None
125x
126y
127y
128
12942
1300
13142
1320
1330.5
1340.0
135['a']
136[]
137{'d': 1}
138{}
139OR
140AND
141---
142"s"
143null
144x
145y
146y
147
14842
1490
15042
1510
1520.5
1530.0
154(List) ["a"]
155(List) []
156(Dict) {"d":1}
157(Dict) {}
158OR
159AND
160## END
161
162#### or BashArray, or BashAssoc
163declare -a array=(1 2 3)
164pp test_ (array or 'yy')
165
166declare -A assoc=([k]=v)
167pp test_ (assoc or 'zz')
168
169## STDOUT:
170{"type":"BashArray","data":{"0":"1","1":"2","2":"3"}}
171{"type":"BashAssoc","data":{"k":"v"}}
172## END
173
174#### x if b else y
175var b = true
176var i = 42
177var t = i+1 if b else i-1
178echo $t
179var f = i+1 if false else i-1
180echo $f
181## STDOUT:
18243
18341
184## END
185