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

213 lines, 116 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
69shopt --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
74seq 3 | while read -r line; do
75 readonly stripped=${line//1/x}
76 #declare stripped=${line//1/x}
77 echo $stripped
78done
79## status: 1
80## STDOUT:
81x
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
88for 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:
96yes
97yes
98## END
99
100
101#### Append object onto Array
102var 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
108for 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
117json 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
131shopt -s ysh:all
132
133var clients = {'email': 'foo', 'e2': 'bar'}
134for 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:
142email
143## END
144
145
146#### var in both branches of if (Julian)
147
148# remove static check?
149
150proc 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
160scopetest (1)
161scopetest (2)
162
163## STDOUT:
164## END
165
166#### var in branches of case (Aidan)
167shopt -s ysh:all
168
169# at top level, there is no static check
170case (1) {
171 (1) { var name = "one" }
172 (2) { var name = "two" }
173}
174echo name=$name
175
176proc 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
188proc do-var {
189 for x in a b {
190 echo $x
191 var x = 'zz'
192 echo $x
193 }
194}
195
196proc do-setvar {
197 for x in a b {
198 echo $x
199 setvar x = 'zz'
200 echo $x
201 }
202}
203
204do-var
205echo
206do-setvar
207
208## STDOUT:
209a
210zz
211b
212zz
213## END