1 ## our_shell: ysh
2 ## oils_failures_allowed: 3
3
4 #### !== operator
5 var a = 'bar'
6
7 if (a !== 'foo') {
8 echo 'not equal'
9 }
10
11 if (a !== 'bar') {
12 echo 'should not get here'
13 }
14
15 # NOTE: a !== foo is idiomatic)
16 if ("$a" !== 'bar') {
17 echo 'should not get here'
18 }
19
20 ## STDOUT:
21 not equal
22 ## END
23
24
25 #### elif bug
26 if (true) {
27 echo A
28 } elif (true) {
29 echo B
30 } elif (true) {
31 echo C
32 } else {
33 echo else
34 }
35 ## STDOUT:
36 A
37 ## END
38
39 #### global vars
40 builtin set -u
41
42 main() {
43 source $[ENV.REPO_ROOT]/spec/testdata/global-lib.sh
44 }
45
46 main
47 test_func
48
49 ## status: 1
50 ## STDOUT:
51 ## END
52
53 #### Julia port
54
55 $[ENV.SH] $[ENV.REPO_ROOT]/spec/testdata/ysh-user-feedback.sh
56
57 ## STDOUT:
58 git
59 branch
60 -D
61 foo
62 baz
63 ___
64 foo
65 baz
66 ## END
67
68 #### readonly in loop: explains why const doesn't work
69
70 # TODO: Might want to change const in Oil...
71 # bash actually prevents assignment and prints a warning, DOH.
72
73 seq 3 | while read -r line; do
74 readonly stripped=${line//1/x}
75 #declare stripped=${line//1/x}
76 echo $stripped
77 done
78 ## status: 1
79 ## STDOUT:
80 x
81 ## END
82
83
84 #### Eggex bug in a loop
85
86 # https://oilshell.zulipchat.com/#narrow/stream/121540-oil-discuss/topic/A.20list.20of.20feedback
87 for i in @(seq 2) {
88 # BUG: This crashes here, but NOT when extracted! Bad.
89 var pat = / 'test' word+ /
90 if ("test$i" ~ pat) {
91 echo yes
92 }
93 }
94 ## STDOUT:
95 yes
96 yes
97 ## END
98
99
100 #### Append object onto Array
101 var e = []
102
103 # %() is also acceptable, but we prefer Python-like [] for objects.
104 # %() is more for an array of strings
105 # var e = %()
106
107 for i in @(seq 2) {
108 var o = {}
109 setvar o[i] = "Test $i"
110
111 # push builtin is only for strings
112
113 call e->append(o)
114 }
115
116 json write (e)
117
118 ## STDOUT:
119 [
120 {
121 "1": "Test 1"
122 },
123 {
124 "2": "Test 2"
125 }
126 ]
127 ## END
128
129 #### Invalid op on string
130 shopt -s ysh:all
131
132 var clients = {'email': 'foo', 'e2': 'bar'}
133 for c in (clients) {
134 echo $c
135 # A user tickled this. I think this should make the whole 'const' line fail
136 # with code 1 or 2?
137 const e = c.email
138 }
139 ## status: 3
140 ## STDOUT:
141 email
142 ## END
143
144
145 #### var in both branches of if (Julian)
146
147 # remove static check?
148
149 proc scopetest(; var) {
150 if (var === 2) {
151 var tmp = "hello"
152 write $tmp
153 } else {
154 var tmp = "world"
155 write $tmp
156 }
157 }
158
159 scopetest (1)
160 scopetest (2)
161
162 ## STDOUT:
163 ## END
164
165 #### var in branches of case (Aidan)
166 shopt -s ysh:all
167
168 # at top level, there is no static check
169 case (1) {
170 (1) { var name = "one" }
171 (2) { var name = "two" }
172 }
173 echo name=$name
174
175 proc my-proc {
176 case (1) {
177 (1) { var name = "one" }
178 (2) { var name = "two" }
179 }
180 }
181
182 ## STDOUT:
183 ## END
184
185 #### Modify for loop variable with var or setvar? (Machine Stops)
186
187 proc do-var {
188 for x in a b {
189 echo $x
190 var x = 'zz'
191 echo $x
192 }
193 }
194
195 proc do-setvar {
196 for x in a b {
197 echo $x
198 setvar x = 'zz'
199 echo $x
200 }
201 }
202
203 do-var
204 echo
205 do-setvar
206
207 ## STDOUT:
208 a
209 zz
210 b
211 zz
212 ## END