OILS / spec / var-op-len.test.sh View on Github | oils.pub

221 lines, 155 significant
1## compare_shells: dash bash mksh zsh
2
3# Test the length oeprator, which dash supports. Dash doesn't support most
4# other ops.
5
6#### String length
7v=foo
8echo ${#v}
9## stdout: 3
10
11#### Unicode string length (UTF-8)
12v=$'_\u03bc_'
13echo ${#v}
14## stdout: 3
15## N-I dash stdout: 9
16## N-I mksh stdout: 4
17
18#### Unicode string length (spec/testdata/utf8-chars.txt)
19v=$(cat $REPO_ROOT/spec/testdata/utf8-chars.txt)
20echo ${#v}
21## stdout: 7
22## N-I dash stdout: 13
23## N-I mksh stdout: 13
24
25#### String length with incomplete utf-8
26for num_bytes in 0 1 2 3 4 5 6 7 8 9 10 11 12 13; do
27 s=$(head -c $num_bytes $REPO_ROOT/spec/testdata/utf8-chars.txt)
28 echo ${#s}
29done 2> $TMP/err.txt
30
31grep 'warning:' $TMP/err.txt
32true # exit 0
33
34## STDOUT:
350
361
372
38-1
393
404
41-1
42-1
435
446
45-1
46-1
47-1
487
49[ stdin ]:3: warning: UTF-8 decode: Truncated bytes at offset 2 in string of 3 bytes
50[ stdin ]:3: warning: UTF-8 decode: Truncated bytes at offset 5 in string of 6 bytes
51[ stdin ]:3: warning: UTF-8 decode: Truncated bytes at offset 5 in string of 7 bytes
52[ stdin ]:3: warning: UTF-8 decode: Truncated bytes at offset 9 in string of 10 bytes
53[ stdin ]:3: warning: UTF-8 decode: Truncated bytes at offset 9 in string of 11 bytes
54[ stdin ]:3: warning: UTF-8 decode: Truncated bytes at offset 9 in string of 12 bytes
55## END
56# zsh behavior actually matches bash!
57## BUG bash/zsh stderr-json: ""
58## BUG bash/zsh STDOUT:
590
601
612
623
633
644
655
666
675
686
697
708
719
727
73## END
74## N-I dash/mksh stderr-json: ""
75## N-I dash/mksh STDOUT:
760
771
782
793
804
815
826
837
848
859
8610
8711
8812
8913
90## END
91
92#### String length with invalid utf-8 continuation bytes
93for num_bytes in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14; do
94 s=$(head -c $num_bytes $REPO_ROOT/spec/testdata/utf8-chars.txt)$(echo -e "\xFF")
95 echo ${#s}
96done 2> $TMP/err.txt
97
98grep 'warning:' $TMP/err.txt
99true
100
101## STDOUT:
102-1
103-1
104-1
105-1
106-1
107-1
108-1
109-1
110-1
111-1
112-1
113-1
114-1
115-1
116-1
117[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 0 in string of 1 bytes
118[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 1 in string of 2 bytes
119[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 2 in string of 3 bytes
120[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 2 in string of 4 bytes
121[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 4 in string of 5 bytes
122[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 5 in string of 6 bytes
123[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 5 in string of 7 bytes
124[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 5 in string of 8 bytes
125[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 8 in string of 9 bytes
126[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 9 in string of 10 bytes
127[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 9 in string of 11 bytes
128[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 9 in string of 12 bytes
129[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 9 in string of 13 bytes
130[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 13 in string of 14 bytes
131[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 13 in string of 14 bytes
132## END
133## BUG bash/zsh stderr-json: ""
134## BUG bash/zsh STDOUT:
1351
1362
1373
1384
1394
1405
1416
1427
1436
1447
1458
1469
14710
1488
1498
150## N-I dash stderr-json: ""
151## N-I dash STDOUT:
1527
1538
1549
15510
15611
15712
15813
15914
16015
16116
16217
16318
16419
16520
16620
167## END
168## N-I mksh stderr-json: ""
169## N-I mksh STDOUT:
1701
1712
1723
1734
1745
1756
1767
1778
1789
17910
18011
18112
18213
18314
18414
185## END
186
187#### Length of undefined variable
188echo ${#undef}
189## stdout: 0
190
191#### Length of undefined variable with nounset
192set -o nounset
193echo ${#undef}
194## status: 1
195## OK dash status: 2
196
197#### Length operator can't be followed by test operator
198echo ${#x-default}
199
200x=''
201echo ${#x-default}
202
203x='foo'
204echo ${#x-default}
205
206## status: 2
207## OK bash/mksh status: 1
208## stdout-json: ""
209## BUG zsh status: 0
210## BUG zsh STDOUT:
2117
2120
2133
214## END
215## BUG dash status: 0
216## BUG dash STDOUT:
2170
2180
2193
220## END
221