OILS / spec / ysh-user-feedback.test.sh View on Github | oils.pub

212 lines, 115 significant
1## our_shell: ysh
2## oils_failures_allowed: 3
3
4#### !== operator
5var a = 'bar'
6
7if (a !== 'foo') {
8 echo 'not equal'
9}
10
11if (a !== 'bar') {
12 echo 'should not get here'
13}
14
15# NOTE: a !== foo is idiomatic)
16if ("$a" !== 'bar') {
17 echo 'should not get here'
18}
19
20## STDOUT:
21not equal
22## END
23
24
25#### elif bug
26if (true) {
27 echo A
28} elif (true) {
29 echo B
30} elif (true) {
31 echo C
32} else {
33 echo else
34}
35## STDOUT:
36A
37## END
38
39#### global vars
40builtin set -u
41
42main() {
43 source $[ENV.REPO_ROOT]/spec/testdata/global-lib.sh
44}
45
46main
47test_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:
58git
59branch
60-D
61foo
62baz
63___
64foo
65baz
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
73seq 3 | while read -r line; do
74 readonly stripped=${line//1/x}
75 #declare stripped=${line//1/x}
76 echo $stripped
77done
78## status: 1
79## STDOUT:
80x
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
87for 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:
95yes
96yes
97## END
98
99
100#### Append object onto Array
101var 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
107for 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
116json 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
130shopt -s ysh:all
131
132var clients = {'email': 'foo', 'e2': 'bar'}
133for 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:
141email
142## END
143
144
145#### var in both branches of if (Julian)
146
147# remove static check?
148
149proc 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
159scopetest (1)
160scopetest (2)
161
162## STDOUT:
163## END
164
165#### var in branches of case (Aidan)
166shopt -s ysh:all
167
168# at top level, there is no static check
169case (1) {
170 (1) { var name = "one" }
171 (2) { var name = "two" }
172}
173echo name=$name
174
175proc 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
187proc do-var {
188 for x in a b {
189 echo $x
190 var x = 'zz'
191 echo $x
192 }
193}
194
195proc do-setvar {
196 for x in a b {
197 echo $x
198 setvar x = 'zz'
199 echo $x
200 }
201}
202
203do-var
204echo
205do-setvar
206
207## STDOUT:
208a
209zz
210b
211zz
212## END