OILS / spec / var-op-len.test.sh View on Github | oilshell.org

219 lines, 155 significant
1#
2# Test the length oeprator, which dash supports. Dash doesn't support most
3# other ops.
4
5#### String length
6v=foo
7echo ${#v}
8## stdout: 3
9
10#### Unicode string length (UTF-8)
11v=$'_\u03bc_'
12echo ${#v}
13## stdout: 3
14## N-I dash stdout: 9
15## N-I mksh stdout: 4
16
17#### Unicode string length (spec/testdata/utf8-chars.txt)
18v=$(cat $REPO_ROOT/spec/testdata/utf8-chars.txt)
19echo ${#v}
20## stdout: 7
21## N-I dash stdout: 13
22## N-I mksh stdout: 13
23
24#### String length with incomplete utf-8
25for num_bytes in 0 1 2 3 4 5 6 7 8 9 10 11 12 13; do
26 s=$(head -c $num_bytes $REPO_ROOT/spec/testdata/utf8-chars.txt)
27 echo ${#s}
28done 2> $TMP/err.txt
29
30grep 'warning:' $TMP/err.txt
31true # exit 0
32
33## STDOUT:
340
351
362
37-1
383
394
40-1
41-1
425
436
44-1
45-1
46-1
477
48[ stdin ]:3: warning: UTF-8 decode: Truncated bytes at offset 2 in string of 3 bytes
49[ stdin ]:3: warning: UTF-8 decode: Truncated bytes at offset 5 in string of 6 bytes
50[ stdin ]:3: warning: UTF-8 decode: Truncated bytes at offset 5 in string of 7 bytes
51[ stdin ]:3: warning: UTF-8 decode: Truncated bytes at offset 9 in string of 10 bytes
52[ stdin ]:3: warning: UTF-8 decode: Truncated bytes at offset 9 in string of 11 bytes
53[ stdin ]:3: warning: UTF-8 decode: Truncated bytes at offset 9 in string of 12 bytes
54## END
55# zsh behavior actually matches bash!
56## BUG bash/zsh stderr-json: ""
57## BUG bash/zsh STDOUT:
580
591
602
613
623
634
645
656
665
676
687
698
709
717
72## END
73## BUG dash/mksh stderr-json: ""
74## N-I dash/mksh STDOUT:
750
761
772
783
794
805
816
827
838
849
8510
8611
8712
8813
89## END
90
91#### String length with invalid utf-8 continuation bytes
92for num_bytes in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14; do
93 s=$(head -c $num_bytes $REPO_ROOT/spec/testdata/utf8-chars.txt)$(echo -e "\xFF")
94 echo ${#s}
95done 2> $TMP/err.txt
96
97grep 'warning:' $TMP/err.txt
98true
99
100## STDOUT:
101-1
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[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 0 in string of 1 bytes
117[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 1 in string of 2 bytes
118[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 2 in string of 3 bytes
119[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 2 in string of 4 bytes
120[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 4 in string of 5 bytes
121[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 5 in string of 6 bytes
122[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 5 in string of 7 bytes
123[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 5 in string of 8 bytes
124[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 8 in string of 9 bytes
125[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 9 in string of 10 bytes
126[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 9 in string of 11 bytes
127[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 9 in string of 12 bytes
128[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 9 in string of 13 bytes
129[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 13 in string of 14 bytes
130[ stdin ]:3: warning: UTF-8 decode: Bad encoding at offset 13 in string of 14 bytes
131## END
132## BUG bash/zsh stderr-json: ""
133## BUG bash/zsh STDOUT:
1341
1352
1363
1374
1384
1395
1406
1417
1426
1437
1448
1459
14610
1478
1488
149## N-I dash stderr-json: ""
150## N-I dash STDOUT:
1517
1528
1539
15410
15511
15612
15713
15814
15915
16016
16117
16218
16319
16420
16520
166## END
167## N-I mksh stderr-json: ""
168## N-I mksh STDOUT:
1691
1702
1713
1724
1735
1746
1757
1768
1779
17810
17911
18012
18113
18214
18314
184## END
185
186#### Length of undefined variable
187echo ${#undef}
188## stdout: 0
189
190#### Length of undefined variable with nounset
191set -o nounset
192echo ${#undef}
193## status: 1
194## OK dash status: 2
195
196#### Length operator can't be followed by test operator
197echo ${#x-default}
198
199x=''
200echo ${#x-default}
201
202x='foo'
203echo ${#x-default}
204
205## status: 2
206## OK bash/mksh status: 1
207## stdout-json: ""
208## BUG zsh status: 0
209## BUG zsh STDOUT:
2107
2110
2123
213## END
214## BUG dash status: 0
215## BUG dash STDOUT:
2160
2170
2183
219## END