Add is_key_in assertion method

This commit is contained in:
James Dinsdale 2017-02-22 21:59:37 +00:00
parent c0b8b46ca3
commit c46c149ebf
No known key found for this signature in database
GPG key ID: DBCB56BD98007031
2 changed files with 31 additions and 0 deletions

View file

@ -137,6 +137,19 @@ Asserts that a value is not included in the comparison array.
assert 'a' not_in 'x' 'y' 'z'
```
#### is_key_in
Asserts that a value is a key in a hash.
```sh
typeset -A hash; hash=(
'a' 1
'b' 2
'c' 3
)
assert 'a' is_key_in ${(@kv)hash}
```
#### exists
Asserts that the given path exists

18
zunit
View file

@ -147,6 +147,24 @@ function _zunit_assert_not_in() {
exit 1
}
###
# Assert that a value is a key in a hash
###
function _zunit_assert_is_key_in() {
local i found=0 value=$1
local -A hash
hash=(${(@)@:2})
for k v in ${(@kv)hash}; do
[[ $k = $value ]] && found=1
done
[[ $found -eq 1 ]] && return 0
echo "'$value' is not a key in (${(@kv)hash})"
exit 1
}
###
# Assert the a path exists
###