Skip to content

Commit

Permalink
[spec/assoc] Cases for "cell sublanguage" and assoc arrays.
Browse files Browse the repository at this point in the history
- unset -v
- declare -n
- ${!ref}

Not done: print -v

Addresses issue #651.

Unrelated: add some ideas in stdlib/README.md
  • Loading branch information
Andy Chu committed Mar 31, 2020
1 parent 9fa3fa8 commit 0a331a5
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
79 changes: 79 additions & 0 deletions spec/assoc.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,82 @@ len=1
unset len=0
## END
#### unset -v and assoc array
show-len() {
echo len=${#A[@]}
}
declare -A A=(['K']=val)
show-len
unset -v 'A["K"]'
show-len
declare -A A=(['K']=val)
show-len
key=K
unset -v 'A[$key]'
show-len
## STDOUT:
len=1
len=0
len=1
len=0
## END
#### nameref and assoc array
show-values() {
echo values: ${A[@]}
}
declare -A A=(['K']=val)
show-values
declare -n ref='A["K"]'
echo before $ref
ref='val2'
echo after $ref
show-values
echo ---
key=K
declare -n ref='A[$key]'
echo before $ref
ref='val3'
echo after $ref
show-values
## STDOUT:
values: val
before val
after val2
values: val2
---
before val2
after val3
values: val3
## END
#### ${!ref} and assoc array
show-values() {
echo values: ${A[@]}
}
declare -A A=(['K']=val)
show-values
declare ref='A["K"]'
echo ref ${!ref}
key=K
declare ref='A[$key]'
echo ref ${!ref}
## STDOUT:
values: val
ref val
ref val
## END
19 changes: 19 additions & 0 deletions stdlib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
stdlib/
=======

Ideas for shell functions that could be in here:

- Version comparison: https://github.com/oilshell/oil/issues/683
- An automated way to download the latest version of Oil: https://github.com/oilshell/oil/issues/463
- Maybe functions to lazily download documentation too?
- `errexit` utilities: https://github.com/oilshell/oil/issues/474

Already have:

- `log` and `die`

Note: The file `oil-polyfill.sh` is POSIX shell for functions that should be
portable, e.g. like updating Oil, which may be done from the system shell.

Other functions can be written in Oil.

2 changes: 1 addition & 1 deletion test/spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ append() {

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

Expand Down

0 comments on commit 0a331a5

Please sign in to comment.