Skip to content

Commit

Permalink
point out more corner cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Jul 11, 2019
1 parent 9fd9d7a commit 6181733
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
6 changes: 4 additions & 2 deletions osh/word_eval.py
Expand Up @@ -465,8 +465,10 @@ def _ApplyPrefixOp(self, val, op_id, token):
elif val.tag == value_e.StrArray:
indices = [str(i) for i, s in enumerate(val.strs) if s is not None]
return value.StrArray(indices)
else:
raise AssertionError

elif val.tag == value_e.AssocArray:
indices = [str(k) for k in val.d]
return value.StrArray(indices)

else:
raise AssertionError(op_id)
Expand Down
33 changes: 31 additions & 2 deletions spec/assoc.test.sh
Expand Up @@ -26,9 +26,11 @@ echo ${d['foo']}

#### retrieve indices with !
declare -A a
a=([aa]=b [foo]=bar ['a+1']=c)
#a=([aa]=b [foo]=bar ['a+1']=c)
a[aa]=b
a[foo]=bar
a['a+1']=c
argv.py "${!a[@]}"
# Is this invalid on associative arrays? Makes no sense.
## stdout: ['foo', 'aa', 'a+1']

#### $a gives nothing
Expand Down Expand Up @@ -94,6 +96,33 @@ d[a]="${array[@]}"
argv.py "${d[a]}"
## stdout: ['1 2 3']

#### Using indexed array as key of associative array coerces to string
declare -a array=(1 2 3)
declare -A assoc
assoc[42]=43
assoc["${array[@]}"]=foo
echo "${assoc["${array[@]}"]}"
argv "${!assoc[@]}"
# TODO: This should fail!
## status: 1
## BUG bash status: 0
## BUG bash STDOUT:
foo
['1 2 3', '42']
##
#### Using indexed array as key of associative array coerces to string
declare -a array=(1 2 3)
declare -A assoc
assoc[42]=43
assoc[array]=foo
echo "${assoc[array]}"
argv "${!assoc[@]}"
## STDOUT:
foo
['array', '42']
##
#### Can't initialize assoc array with indexed array
declare -A A=(1 2 3)
## status: 1
Expand Down
2 changes: 1 addition & 1 deletion test/spec.sh
Expand Up @@ -564,7 +564,7 @@ append() {

# associative array -- mksh and zsh implement different associative arrays.
assoc() {
sh-spec spec/assoc.test.sh --osh-failures-allowed 11 \
sh-spec spec/assoc.test.sh --osh-failures-allowed 13 \
$BASH $OSH_LIST "$@"
}

Expand Down

0 comments on commit 6181733

Please sign in to comment.