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