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 shopt --unset no_osh_builtins
70
71 # TODO: Might want to change const in YSH
72 # bash actually prevents assignment and prints a warning, DOH.
73
74 seq 3 | while read -r line; do
75 readonly stripped=${line//1/x}
76 #declare stripped=${line//1/x}
77 echo $stripped
78 done
79 ## status: 1
80 ## STDOUT:
81 x
82 ## END
83
84
85 #### Eggex bug in a loop
86
87 # https://oilshell.zulipchat.com/#narrow/stream/121540-oil-discuss/topic/A.20list.20of.20feedback
88 for i in @(seq 2) {
89 # BUG: This crashes here, but NOT when extracted! Bad.
90 var pat = / 'test' word+ /
91 if ("test$i" ~ pat) {
92 echo yes
93 }
94 }
95 ## STDOUT:
96 yes
97 yes
98 ## END
99
100
101 #### Append object onto Array
102 var e = []
103
104 # %() is also acceptable, but we prefer Python-like [] for objects.
105 # %() is more for an array of strings
106 # var e = %()
107
108 for i in @(seq 2) {
109 var o = {}
110 setvar o[i] = "Test $i"
111
112 # push builtin is only for strings
113
114 call e->append(o)
115 }
116
117 json write (e)
118
119 ## STDOUT:
120 [
121 {
122 "1": "Test 1"
123 },
124 {
125 "2": "Test 2"
126 }
127 ]
128 ## END
129
130 #### Invalid op on string
131 shopt -s ysh:all
132
133 var clients = {'email': 'foo', 'e2': 'bar'}
134 for c in (clients) {
135 echo $c
136 # A user tickled this. I think this should make the whole 'const' line fail
137 # with code 1 or 2?
138 const e = c.email
139 }
140 ## status: 3
141 ## STDOUT:
142 email
143 ## END
144
145
146 #### var in both branches of if (Julian)
147
148 # remove static check?
149
150 proc scopetest(; var) {
151 if (var === 2) {
152 var tmp = "hello"
153 write $tmp
154 } else {
155 var tmp = "world"
156 write $tmp
157 }
158 }
159
160 scopetest (1)
161 scopetest (2)
162
163 ## STDOUT:
164 ## END
165
166 #### var in branches of case (Aidan)
167 shopt -s ysh:all
168
169 # at top level, there is no static check
170 case (1) {
171 (1) { var name = "one" }
172 (2) { var name = "two" }
173 }
174 echo name=$name
175
176 proc my-proc {
177 case (1) {
178 (1) { var name = "one" }
179 (2) { var name = "two" }
180 }
181 }
182
183 ## STDOUT:
184 ## END
185
186 #### Modify for loop variable with var or setvar? (Machine Stops)
187
188 proc do-var {
189 for x in a b {
190 echo $x
191 var x = 'zz'
192 echo $x
193 }
194 }
195
196 proc do-setvar {
197 for x in a b {
198 echo $x
199 setvar x = 'zz'
200 echo $x
201 }
202 }
203
204 do-var
205 echo
206 do-setvar
207
208 ## STDOUT:
209 a
210 zz
211 b
212 zz
213 ## END