Skip to content

Commit

Permalink
Merge pull request #45 from molovo/version-0.5.x
Browse files Browse the repository at this point in the history
v0.5.2
  • Loading branch information
James Dinsdale committed Feb 22, 2017
2 parents 1152d77 + d0908f5 commit 431ae6a
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 8 deletions.
70 changes: 69 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,58 @@ 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}
```

#### is_not_key_in

Asserts that a value is not a key in a hash.

```sh
typeset -A hash; hash=(
'a' 1
'b' 2
'c' 3
)
assert 'x' is_not_key_in ${(@kv)hash}
```

#### is_value_in

Asserts that a value is a value in a hash.

```sh
typeset -A hash; hash=(
'a' 1
'b' 2
'c' 3
)
assert 1 is_value_in ${(@kv)hash}
```

#### is_not_value_in

Asserts that a value is not a value in a hash.

```sh
typeset -A hash; hash=(
'a' 1
'b' 2
'c' 3
)
assert 4 is_not_value_in ${(@kv)hash}
```

#### exists

Asserts that the given path exists
Expand Down Expand Up @@ -171,12 +223,28 @@ assert /path/to/link is_link

#### is_readable

Asserts that the given path exists and is a symbolic readable
Asserts that the given path exists and is readable

```sh
assert /path/to/file is_readable
```

#### is_writable

Asserts that the given path exists and is writable

```sh
assert /path/to/file is_writable
```

#### is_executable

Asserts that the given path exists and is executable

```sh
assert /path/to/file is_executable
```

### Loading scripts

Each of your tests is run in isolation, meaning that there is no variable or function leakage between tests. The `load` helper function will source a script into the test environment for you, allowing you to set up variables and functions etc.
Expand Down
Empty file.
Empty file.
27 changes: 27 additions & 0 deletions tests/assertions/is_executable.zunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env zunit

@test 'Test _zunit_assert_is_executable success' {
# Make file executable before checking it
chmod 744 "$testdir/is_executable.zunit"

run assert './is_executable.zunit' is_executable

# Change permissions back before the test makes its assertions
chmod 644 "$testdir/is_executable.zunit"

assert $state equals 0
assert $output is_empty
}

@test 'Test _zunit_assert_is_executable failure' {
# Make file unexecutable before checking it
chmod 000 "$testdir/../_support/non-executable-file"

run assert '../_support/non-executable-file' is_executable

# Change permissions back before the test makes its assertions
chmod 644 "$testdir/../_support/non-executable-file"

assert $state equals 1
assert $output same_as "'../_support/non-executable-file' does not exist or is not executable"
}
23 changes: 23 additions & 0 deletions tests/assertions/is_key_in.zunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env zunit

@test 'Test _zunit_assert_is_key_in success' {
typeset -A hash; hash=(
'a' 1
'b' 2
'c' 3
)
run assert 'a' is_key_in ${(@kv)hash}
assert $state equals 0
assert "$output" is_empty
}

@test 'Test _zunit_assert_is_key_in failure' {
typeset -A hash; hash=(
'x' 1
'y' 2
'z' 3
)
run assert 'a' is_key_in ${(@kv)hash}
assert $state equals 1
assert "$output" same_as "'a' is not a key in (x 1 y 2 z 3)"
}
23 changes: 23 additions & 0 deletions tests/assertions/is_not_key_in.zunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env zunit

@test 'Test _zunit_assert_is_not_key_in success' {
typeset -A assoc; assoc=(
'x' 1
'y' 2
'z' 3
)
run assert 'a' is_not_key_in ${(@kv)assoc}
assert $state equals 0
assert "$output" is_empty
}

@test 'Test _zunit_assert_is_not_key_in failure' {
typeset -A assoc; assoc=(
'a' 1
'b' 2
'c' 3
)
run assert 'a' is_not_key_in ${(@kv)assoc}
assert $state equals 1
assert "$output" same_as "'a' is a key in (a 1 b 2 c 3)"
}
23 changes: 23 additions & 0 deletions tests/assertions/is_not_value_in.zunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env zunit

@test 'Test _zunit_assert_is_not_value_in success' {
typeset -A hash; hash=(
'x' 7
'y' 8
'z' 9
)
run assert 1 is_not_value_in ${(@kv)hash}
assert $state equals 0
assert "$output" is_empty
}

@test 'Test _zunit_assert_is_not_value_in failure' {
typeset -A hash; hash=(
'a' 1
'b' 2
'c' 3
)
run assert 1 is_not_value_in ${(@kv)hash}
assert $state equals 1
assert "$output" same_as "'1' is a value in (a 1 b 2 c 3)"
}
23 changes: 23 additions & 0 deletions tests/assertions/is_value_in.zunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env zunit

@test 'Test _zunit_assert_is_value_in success' {
typeset -A hash; hash=(
'a' 1
'b' 2
'c' 3
)
run assert 1 is_value_in ${(@kv)hash}
assert $state equals 0
assert "$output" is_empty
}

@test 'Test _zunit_assert_is_value_in failure' {
typeset -A hash; hash=(
'x' 1
'y' 2
'z' 3
)
run assert 4 is_value_in ${(@kv)hash}
assert $state equals 1
assert "$output" same_as "'4' is not a value in (x 1 y 2 z 3)"
}
20 changes: 20 additions & 0 deletions tests/assertions/is_writable.zunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env zunit

@test 'Test _zunit_assert_is_writable success' {
run assert './is_writable.zunit' is_writable
assert $state equals 0
assert $output is_empty
}

@test 'Test _zunit_assert_is_writable failure' {
# Make file unwritable before checking it
chmod 000 "$testdir/../_support/non-writable-file"

run assert '../_support/non-writable-file' is_writable

# Change permissions back before the test makes its assertions
chmod 644 "$testdir/../_support/non-writable-file"

assert $state equals 1
assert $output same_as "'../_support/non-writable-file' does not exist or is not writable"
}
126 changes: 119 additions & 7 deletions zunit
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,78 @@ 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 that a value is not a key in a hash
###
function _zunit_assert_is_not_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 0 ]] && return 0

echo "'$value' is a key in (${(@kv)hash})"
exit 1
}

###
# Assert that a value is a value in a hash
###
function _zunit_assert_is_value_in() {
local i found=0 value=$1
local -A hash
hash=(${(@)@:2})

for k v in ${(@kv)hash}; do
[[ $v = $value ]] && found=1
done

[[ $found -eq 1 ]] && return 0

echo "'$value' is not a value in (${(@kv)hash})"
exit 1
}

###
# Assert that a value is not a value in a hash
###
function _zunit_assert_is_not_value_in() {
local i found=0 value=$1
local -A hash
hash=(${(@)@:2})

for k v in ${(@kv)hash}; do
[[ $v = $value ]] && found=1
done

[[ $found -eq 0 ]] && return 0

echo "'$value' is a value in (${(@kv)hash})"
exit 1
}

###
# Assert the a path exists
###
Expand Down Expand Up @@ -242,6 +314,44 @@ function _zunit_assert_is_readable() {
exit 1
}

###
# Assert the a path exists and is writable
###
function _zunit_assert_is_writable() {
local pathname=$1 filepath

# If filepath is relative, prepend the test directory
if [[ "${pathname:0:1}" != "/" ]]; then
filepath="$testdir/${pathname}"
else
filepath="$pathname"
fi

[[ -w "$filepath" ]] && return 0

echo "'$pathname' does not exist or is not writable"
exit 1
}

###
# Assert the a path exists and is executable
###
function _zunit_assert_is_executable() {
local pathname=$1 filepath

# If filepath is relative, prepend the test directory
if [[ "${pathname:0:1}" != "/" ]]; then
filepath="$testdir/${pathname}"
else
filepath="$pathname"
fi

[[ -x "$filepath" ]] && return 0

echo "'$pathname' does not exist or is not executable"
exit 1
}

################################
# Helpers for use within tests #
################################
Expand Down Expand Up @@ -1112,12 +1222,6 @@ function _zunit_run() {
local fail_fast tap allow_risky
local output_text logfile_text output_html logfile_html

# Print version information
echo $(color yellow 'Launching ZUnit')
echo "ZUnit: $(_zunit_version)"
echo "ZSH: $(zsh --version)"
echo

zmodload zsh/datetime
local start_time=$((EPOCHREALTIME*1000)) end_time

Expand All @@ -1135,6 +1239,14 @@ function _zunit_run() {
echo 'TAP version 13'
fi

if [[ -z $tap ]]; then
# Print version information
echo $(color yellow 'Launching ZUnit')
echo "ZUnit: $(_zunit_version)"
echo "ZSH: $(zsh --version)"
echo
fi

if [[ -n $output_text ]]; then
if [[ $missing_config -eq 1 ]]; then
echo $(color red '.zunit.yml could not be found. Run `zulu init`')
Expand Down Expand Up @@ -1214,7 +1326,7 @@ function _zunit_run() {
# Output the version number
###
function _zunit_version() {
echo '0.5.1'
echo '0.5.2'
}

###
Expand Down

0 comments on commit 431ae6a

Please sign in to comment.