Skip to content

Commit

Permalink
Add contains and does_not_contain assertion methods
Browse files Browse the repository at this point in the history
  • Loading branch information
molovo committed Jul 12, 2017
1 parent 7677348 commit 83103bd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/assertions.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
################################

###
# Assert one string is a substring if another
# Assert one string is a substring of another
###
function _zunit_assert_is_substring_of() {
local value=$1 comparison=$2
Expand All @@ -15,7 +15,7 @@ function _zunit_assert_is_substring_of() {
}

###
# Assert one string is not a substring if another
# Assert one string is not a substring of another
###
function _zunit_assert_is_not_substring_of() {
local value=$1 comparison=$2
Expand All @@ -26,6 +26,30 @@ function _zunit_assert_is_not_substring_of() {
exit 1
}

###
# Assert one string contains another
###
function _zunit_assert_contains() {
local value=$1 comparison=$2

[[ "$value" = *"$comparison"* ]] && return 0

echo "'$value' does not contain '$comparison'"
exit 1
}

###
# Assert one string does not contain another
###
function _zunit_assert_does_not_contain() {
local value=$1 comparison=$2

[[ "$value" != *"$comparison"* ]] && return 0

echo "'$value' contains '$comparison'"
exit 1
}

###
# Assert that two integers are equal
###
Expand Down
36 changes: 36 additions & 0 deletions tests/assertions/contains.zunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env zunit

@test 'Test _zunit_assert_contains match' {
run assert 'hello world' contains 'lo wo'

assert "$output" is_empty
assert $state equals 0
}

@test 'Test _zunit_assert_contains suffix match' {
run assert 'hello world' contains 'world'

assert "$output" is_empty
assert $state equals 0
}

@test 'Test _zunit_assert_contains prefix match' {
run assert 'hello world' contains 'hello'

assert "$output" is_empty
assert $state equals 0
}

@test 'Test _zunit_assert_contains whole word matches' {
run assert 'red blue green' contains 'red blue green'

assert "$output" is_empty
assert $state equals 0
}

@test 'Test _zunit_assert_contains match failure' {
run assert 'elephants' contains 'foo'

assert "$output" same_as "'elephants' does not contain 'foo'"
assert $state equals 1
}
15 changes: 15 additions & 0 deletions tests/assertions/does_not_contain.zunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env zunit

@test 'Test _zunit_assert_does_not_contain success' {
run assert 'yellow' does_not_contain 'foo'

assert "$output" is_empty
assert $state equals 0
}

@test 'Test _zunit_assert_does_not_contain failure' {
run assert 'foobar' does_not_contain 'foo'

assert "$output" same_as "'foobar' contains 'foo'"
assert $state equals 1
}

0 comments on commit 83103bd

Please sign in to comment.