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

105 lines, 55 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#### declare -i with +=
12declare s
13s='1 '
14s+=' 2 ' # string append
15
16declare -i i
17i='1 '
18i+=' 2 ' # arith add
19
20declare -i j
21j=x # treated like zero
22j+=' 2 ' # arith add
23
24echo "[$s]"
25echo [$i]
26echo [$j]
27## STDOUT:
28[1 2 ]
29[3]
30[2]
31## END
32## N-I osh STDOUT:
33[1 2 ]
34[1 2 ]
35[x 2 ]
36## END
37
38#### declare -i with arithmetic inside strings (Nix, issue 864)
39
40# example
41# https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh#L379
42
43declare -i s
44s='1 + 2'
45echo s=$s
46
47declare -a array=(1 2 3)
48declare -i item
49item='array[1+1]'
50echo item=$item
51
52## STDOUT:
53s=3
54item=3
55## END
56## N-I osh STDOUT:
57s=1 + 2
58item=array[1+1]
59## END
60
61#### append in arith context
62declare s
63(( s='1 '))
64(( s+=' 2 ')) # arith add
65declare -i i
66(( i='1 ' ))
67(( i+=' 2 ' ))
68declare -i j
69(( j='x ' )) # treated like zero
70(( j+=' 2 ' ))
71echo "$s|$i|$j"
72## STDOUT:
733|3|2
74## END
75
76#### declare array vs. string: mixing -a +a and () ''
77# dynamic parsing of first argument.
78declare +a 'xyz1=1'
79declare +a 'xyz2=(2 3)'
80declare -a 'xyz3=4'
81declare -a 'xyz4=(5 6)'
82argv.py "${xyz1}" "${xyz2}" "${xyz3[@]}" "${xyz4[@]}"
83## STDOUT:
84['1', '(2 3)', '4', '5', '6']
85## END
86## N-I osh STDOUT:
87['', '']
88## END
89
90
91#### declare array vs. associative array
92# Hm I don't understand why the array only has one element. I guess because
93# index 0 is used twice?
94declare -a 'array=([a]=b [c]=d)'
95declare -A 'assoc=([a]=b [c]=d)'
96argv.py "${#array[@]}" "${!array[@]}" "${array[@]}"
97argv.py "${#assoc[@]}" "${!assoc[@]}" "${assoc[@]}"
98## STDOUT:
99['1', '0', 'd']
100['2', 'a', 'c', 'b', 'd']
101## END
102## N-I osh STDOUT:
103['0']
104['0']
105## END