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

181 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 line (not "s")
31pp line (not 3)
32pp line (not 4.5)
33pp line (not {})
34pp line (not [])
35pp line (not false)
36pp line (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 line (["a"] or [])
114pp line (["a"] and [])
115
116pp line ({"d": 1} or {})
117pp line ({"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
122declare -a array=(1 2 3)
123pp line (array or 'yy')
124
125declare -A assoc=([k]=v)
126pp line (assoc or 'zz')
127
128## STDOUT:
129s
130None
131x
132y
133y
134
13542
1360
13742
1380
1390.5
1400.0
141['a']
142[]
143{'d': 1}
144{}
145OR
146AND
147---
148"s"
149null
150x
151y
152y
153
15442
1550
15642
1570
1580.5
1590.0
160(List) ["a"]
161(List) []
162(Dict) {"d":1}
163(Dict) {}
164OR
165AND
166(BashArray) ["1","2","3"]
167(BashAssoc) {"k":"v"}
168## END
169
170#### x if b else y
171var b = true
172var i = 42
173var t = i+1 if b else i-1
174echo $t
175var f = i+1 if false else i-1
176echo $f
177## STDOUT:
17843
17941
180## END
181