Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/commands/run.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ function _zunit_execute_test() {
# Add an exit handler which calls the teardown function if it is
# defined and the test exits early
if (( \$+functions[__zunit_test_teardown] )); then
zshexit() {
TRAPEXIT() {
__zunit_test_teardown 2>&1
}
TRAPZERR() {
[[ -o ERR_EXIT ]] && __zunit_test_teardown 2>&1
}
fi

# Create some local variables to store test state in
Expand All @@ -102,14 +105,6 @@ function _zunit_execute_test() {
# function it will be read as part of the body of this function
${body}

# If a teardown function is defined, run it now
if (( \$+functions[__zunit_test_teardown] )); then
__zunit_test_teardown 2>&1
fi

# Remove the error handler
zshexit() {}

# Check the assertion count, and if it is 0, return
# the warning exit code
[[ \$_zunit_assertion_count -gt 0 ]] || return 248
Expand Down
43 changes: 33 additions & 10 deletions tests/setup-and-teardown.zunit
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
#!/usr/bin/env zunit

@setup {
SOME_VAR='rainbows'
SOME_VAR='rainbows'
}

@teardown {
unset SOME_VAR
unset SOME_VAR
unset SOME_VAR_TEARDOWN
}

@test 'Check value of SOME_VAR' {
assert $SOME_VAR same_as 'rainbows'
assert $SOME_VAR same_as 'rainbows'
}

@test 'Change value of SOME_VAR' {
SOME_VAR='unicorns'
assert $SOME_VAR same_as 'unicorns'
SOME_VAR='unicorns'
assert $SOME_VAR same_as 'unicorns'
}

@test 'Check value of SOME_VAR again' {
# Check will fail, because the variable was unset in
# the @teardown method, and then reset to 'rainbows'
# when @setup was run again.
run assert $SOME_VAR same_as 'unicorns'
# Check will fail, because the variable was unset in
# the @teardown method, and then reset to 'rainbows'
# when @setup was run again.
run assert $SOME_VAR same_as 'unicorns'

assert $state equals 1
assert $state equals 1
}

@test 'Change value of SOME_VAR_TEARDOWN' {
SOME_VAR_TEARDOWN='rainbows'
run true

assert $state equals 0
}

@test 'Check value of SOME_VAR_TEARDOWN' {
assert $SOME_VAR_TEARDOWN is_empty
}

@test 'Change value of SOME_VAR_TEARDOWN again' {
SOME_VAR_TEARDOWN='rainbows'
run false

assert $state equals 1
}

@test 'Check value of SOME_VAR_TEARDOWN again' {
assert $SOME_VAR_TEARDOWN is_empty
}