OILS / spec / ysh-expr-bool.test.sh View on Github | oils.pub

186 lines, 123 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
65shopt --set parse_ysh_expr_sub
66
67python2 -c 'print(None or "s")'
68python2 -c 'print(None and "s")'
69
70python2 -c 'print("x" or "y")'
71python2 -c 'print("x" and "y")'
72
73python2 -c 'print("" or "y")'
74python2 -c 'print("" and "y")'
75
76python2 -c 'print(42 or 0)'
77python2 -c 'print(42 and 0)'
78
79python2 -c 'print(0 or 42)'
80python2 -c 'print(0 and 42)'
81
82python2 -c 'print(0.0 or 0.5)'
83python2 -c 'print(0.0 and 0.5)'
84
85python2 -c 'print(["a"] or [])'
86python2 -c 'print(["a"] and [])'
87
88python2 -c 'print({"d": 1} or {})'
89python2 -c 'print({"d": 1} and {})'
90
91python2 -c 'print(0 or 0.0 or False or [] or {} or "OR")'
92python2 -c 'print(1 and 1.0 and True and [5] and {"d":1} and "AND")'
93
94echo ---
95
96json write (null or "s")
97json write (null and "s")
98
99echo $["x" or "y"]
100echo $["x" and "y"]
101
102echo $["" or "y"]
103echo $["" and "y"]
104
105echo $[42 or 0]
106echo $[42 and 0]
107
108echo $[0 or 42]
109echo $[0 and 42]
110
111echo $[0.0 or 0.5]
112echo $[0.0 and 0.5]
113
114pp test_ (["a"] or [])
115pp test_ (["a"] and [])
116
117pp test_ ({"d": 1} or {})
118pp test_ ({"d": 1} and {})
119
120echo $[0 or 0.0 or false or [] or {} or "OR"]
121echo $[1 and 1.0 and true and [5] and {"d":1} and "AND"]
122
123## STDOUT:
124s
125None
126x
127y
128y
129
13042
1310
13242
1330
1340.5
1350.0
136['a']
137[]
138{'d': 1}
139{}
140OR
141AND
142---
143"s"
144null
145x
146y
147y
148
14942
1500
15142
1520
1530.5
1540.0
155(List) ["a"]
156(List) []
157(Dict) {"d":1}
158(Dict) {}
159OR
160AND
161## END
162
163#### or BashArray, or BashAssoc
164declare -a array=(1 2 3)
165pp test_ (array or 'yy')
166
167declare -A assoc=([k]=v)
168pp test_ (assoc or 'zz')
169
170## STDOUT:
171{"type":"BashArray","data":{"0":"1","1":"2","2":"3"}}
172{"type":"BashAssoc","data":{"k":"v"}}
173## END
174
175#### x if b else y
176var b = true
177var i = 42
178var t = i+1 if b else i-1
179echo $t
180var f = i+1 if false else i-1
181echo $f
182## STDOUT:
18343
18441
185## END
186