Skip to content

Commit

Permalink
Merge pull request #377 from smeghead/fix/issue-372
Browse files Browse the repository at this point in the history
Add `assert_not_same` function
  • Loading branch information
Chemaclass authored Oct 13, 2024
2 parents d8dfb23 + b5dc56c commit beb3799
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- Removed git dependency for stable installations
- Rename option `--verbose` to `--detailed`
- which is the default display behaviour, the opposite as `--simple`
- Added `assert_not_same`

## [0.17.0](https://github.com/TypedDevs/bashunit/compare/0.16.0...0.17.0) - 2024-10-01

Expand Down
27 changes: 26 additions & 1 deletion src/assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@ function assert_equals() {
state::add_assertions_passed
}

function assert_not_equals() {
local expected="$1"
local actual="$2"

# Remove ANSI escape sequences (color codes)
local actual_cleaned
actual_cleaned=$(echo -e "$actual" | sed -r "s/\x1B\[[0-9;]*[mK]//g")
local expected_cleaned
expected_cleaned=$(echo -e "$expected" | sed -r "s/\x1B\[[0-9;]*[mK]//g")

# Remove all control characters and whitespace (optional, depending on your needs)
actual_cleaned=$(echo "$actual_cleaned" | tr -d '[:cntrl:]')
expected_cleaned=$(echo "$expected_cleaned" | tr -d '[:cntrl:]')

if [[ "$expected_cleaned" == "$actual_cleaned" ]]; then
local label
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
state::add_assertions_failed
console_results::print_failed_test "${label}" "${expected_cleaned}" "but got " "${actual_cleaned}"
return
fi

state::add_assertions_passed
}

function assert_empty() {
local expected="$1"

Expand Down Expand Up @@ -140,7 +165,7 @@ function assert_not_empty() {
state::add_assertions_passed
}

function assert_not_equals() {
function assert_not_same() {
local expected="$1"
local actual="$2"

Expand Down
21 changes: 21 additions & 0 deletions tests/unit/assert_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ function test_unsuccessful_assert_not_empty() {
"$(assert_not_empty "")"
}

function test_successful_assert_not_same() {
assert_empty "$(assert_not_same "1" "2")"
}

function test_unsuccessful_assert_not_same() {
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert not same" "1" "but got " "1")"\
"$(assert_not_same "1" "1")"
}

function test_successful_assert_not_equals() {
assert_empty "$(assert_not_equals "1" "2")"
}
Expand All @@ -119,6 +129,17 @@ function test_unsuccessful_assert_not_equals() {
"$(assert_not_equals "1" "1")"
}

function test_unsuccessful_assert_not_equals_with_special_chars() {
local str1="${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT} foo"
local str2="✗ Failed foo"

assert_equals\
"$(console_results::print_failed_test \
"Unsuccessful assert not equals with special chars" \
"$str1" "but got " "$str2")"\
"$(assert_not_equals "$str1" "$str2")"
}

function test_successful_assert_contains_ignore_case() {
assert_empty "$(assert_contains_ignore_case "Linux" "GNU/LINUX")"
}
Expand Down

0 comments on commit beb3799

Please sign in to comment.