1 ## our_shell: ysh
2
3 #### !== operator
4 var a = 'bar'
5
6 if (a !== 'foo') {
7 echo 'not equal'
8 }
9
10 if (a !== 'bar') {
11 echo 'should not get here'
12 }
13
14 # NOTE: a !== foo is idiomatic)
15 if ("$a" !== 'bar') {
16 echo 'should not get here'
17 }
18
19 ## STDOUT:
20 not equal
21 ## END
22
23
24 #### elif bug
25 if (true) {
26 echo A
27 } elif (true) {
28 echo B
29 } elif (true) {
30 echo C
31 } else {
32 echo else
33 }
34 ## STDOUT:
35 A
36 ## END
37
38 #### global vars
39 builtin set -u
40
41 main() {
42 source $REPO_ROOT/spec/testdata/global-lib.sh
43 }
44
45 main
46 test_func
47
48 ## status: 1
49 ## STDOUT:
50 ## END
51
52 #### Julia port
53
54 # https://lobste.rs/s/ritbgc/what_glue_languages_do_you_use_like#c_nhikri
55 #
56 # See bash counterpart in spec/blog1.test.sh
57
58 git-branch-merged() {
59 cat <<EOF
60 foo
61 * bar
62 baz
63 master
64 EOF
65 }
66
67 # With bash-style readarray. The -t is annoying.
68 git-branch-merged | while read --line {
69 # Note: this can't be 'const' because const is dynamic like 'readonly'. And
70 # we don't have block scope.
71 var line = _reply->trim() # removing leading space
72
73 # with glob: line ~~ '\**' (awkward)
74 # with regex: line ~ / %start '*' / (not terrible, but somewhat complex)
75
76 if (line !== 'master' and not line->startsWith('*')) {
77 echo $line
78 }
79 } | readarray -t :branches
80
81 # TODO: I think we want read --lines :branches ? Then we don't need this
82 # conversion.
83 var branchList = :| "${branches[@]}" |
84
85 if (len(branchList) === 0) {
86 echo "No merged branches"
87 } else {
88 write git branch -D @branchList
89 }
90
91 # With "append". Hm read --lines isn't bad.
92 var branches2 = :| |
93 git-branch-merged | while read --line {
94 var line2 = _reply->trim() # removing leading space
95 if (line2 !== 'master' and not line2->startsWith('*')) {
96 append $line2 (branches2)
97 }
98 }
99
100 write -- ___ @branches2
101
102 ## STDOUT:
103 git
104 branch
105 -D
106 foo
107 baz
108 ___
109 foo
110 baz
111 ## END
112
113 #### readonly in loop: explains why const doesn't work
114
115 # TODO: Might want to change const in Oil...
116 # bash actually prevents assignment and prints a warning, DOH.
117
118 seq 3 | while read -r line; do
119 readonly stripped=${line//1/x}
120 #declare stripped=${line//1/x}
121 echo $stripped
122 done
123 ## status: 1
124 ## STDOUT:
125 x
126 ## END
127
128
129 #### Eggex bug in a loop
130
131 # https://oilshell.zulipchat.com/#narrow/stream/121540-oil-discuss/topic/A.20list.20of.20feedback
132 for i in @(seq 2) {
133 # BUG: This crashes here, but NOT when extracted! Bad.
134 var pat = / 'test' word+ /
135 if ("test$i" ~ pat) {
136 echo yes
137 }
138 }
139 ## STDOUT:
140 yes
141 yes
142 ## END
143
144
145 #### Append object onto Array
146 var e = []
147
148 # %() is also acceptable, but we prefer Python-like [] for objects.
149 # %() is more for an array of strings
150 # var e = %()
151
152 for i in @(seq 2) {
153 var o = {}
154 setvar o[i] = "Test $i"
155
156 # push builtin is only for strings
157
158 call e->append(o)
159 }
160
161 json write (e)
162
163 ## STDOUT:
164 [
165 {
166 "1": "Test 1"
167 },
168 {
169 "2": "Test 2"
170 }
171 ]
172 ## END
173
174 #### Invalid op on string
175 shopt -s oil:all
176
177 var clients = {'email': 'foo', 'e2': 'bar'}
178 for c in (clients) {
179 echo $c
180 # A user tickled this. I think this should make the whole 'const' line fail
181 # with code 1 or 2?
182 const e = c.email
183 }
184 ## status: 3
185 ## STDOUT:
186 email
187 ## END