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