OILS / spec / type-compat.test.sh View on Github | oilshell.org

159 lines, 79 significant
1## compare_shells: bash-4.4
2
3# Tests for bash's type flags on cells. Hopefully we don't have to implement
4# this, but it's good to know the behavior.
5#
6# OSH follows a Python-ish model of types carried with values/objects, not
7# locations.
8#
9# See https://github.com/oilshell/oil/issues/26
10
11
12#### declare -i -l -u errors can be silenced - ignore_flags_not_impl
13
14declare -i foo=2+3
15echo status=$?
16echo foo=$foo
17echo
18
19shopt -s ignore_flags_not_impl
20declare -i bar=2+3
21echo status=$?
22echo bar=$bar
23
24## STDOUT:
25status=2
26foo=
27
28status=0
29bar=2+3
30## END
31
32# bash doesn't need this
33
34## OK bash STDOUT:
35status=0
36foo=5
37
38status=0
39bar=5
40## END
41
42#### declare -i with +=
43declare s
44s='1 '
45s+=' 2 ' # string append
46
47declare -i i
48i='1 '
49i+=' 2 ' # arith add
50
51declare -i j
52j=x # treated like zero
53j+=' 2 ' # arith add
54
55echo "[$s]"
56echo [$i]
57echo [$j]
58## STDOUT:
59[1 2 ]
60[3]
61[2]
62## END
63## N-I osh STDOUT:
64[1 2 ]
65[1 2 ]
66[x 2 ]
67## END
68
69#### declare -i with arithmetic inside strings (Nix, issue 864)
70
71# example
72# https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh#L379
73
74declare -i s
75s='1 + 2'
76echo s=$s
77
78declare -a array=(1 2 3)
79declare -i item
80item='array[1+1]'
81echo item=$item
82
83## STDOUT:
84s=3
85item=3
86## END
87## N-I osh STDOUT:
88s=1 + 2
89item=array[1+1]
90## END
91
92#### append in arith context
93declare s
94(( s='1 '))
95(( s+=' 2 ')) # arith add
96declare -i i
97(( i='1 ' ))
98(( i+=' 2 ' ))
99declare -i j
100(( j='x ' )) # treated like zero
101(( j+=' 2 ' ))
102echo "$s|$i|$j"
103## STDOUT:
1043|3|2
105## END
106
107#### declare array vs. string: mixing -a +a and () ''
108# dynamic parsing of first argument.
109declare +a 'xyz1=1'
110declare +a 'xyz2=(2 3)'
111declare -a 'xyz3=4'
112declare -a 'xyz4=(5 6)'
113argv.py "${xyz1}" "${xyz2}" "${xyz3[@]}" "${xyz4[@]}"
114## STDOUT:
115['1', '(2 3)', '4', '5', '6']
116## END
117## N-I osh STDOUT:
118['', '']
119## END
120
121
122#### declare array vs. associative array
123# Hm I don't understand why the array only has one element. I guess because
124# index 0 is used twice?
125declare -a 'array=([a]=b [c]=d)'
126declare -A 'assoc=([a]=b [c]=d)'
127argv.py "${#array[@]}" "${!array[@]}" "${array[@]}"
128argv.py "${#assoc[@]}" "${!assoc[@]}" "${assoc[@]}"
129## STDOUT:
130['1', '0', 'd']
131['2', 'a', 'c', 'b', 'd']
132## END
133## N-I osh STDOUT:
134['0']
135['0']
136## END
137
138
139#### declare -l -u
140
141declare -l lower=FOO
142declare -u upper=foo
143
144echo $lower
145echo $upper
146
147# other:
148# -t trace
149# -I inherit attributes
150
151## STDOUT:
152foo
153FOO
154## END
155
156## N-I osh STDOUT:
157FOO
158foo
159## END