Skip to content

Commit 0f97767

Browse files
authored
Merge pull request #93 from pgrange/time_delta
assert number equality within delta
2 parents df57504 + 4411bc6 commit 0f97767

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.adoc

+35
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ Running tests in tests/test_core.sh
106106
Running test_assert_status_code_fails ... SUCCESS
107107
Running test_assert_status_code_succeeds ... SUCCESS
108108
Running test_assert_succeeds ... SUCCESS
109+
Running test_assert_within_delta_fails ... SUCCESS
110+
Running test_assert_within_delta_succeeds ... SUCCESS
109111
Running test_fail_fails ... SUCCESS
110112
Running test_fail_prints_failure_message ... SUCCESS
111113
Running test_fail_prints_where_is_error ... SUCCESS
@@ -147,6 +149,8 @@ Running tests in tests/test_core.sh
147149
Running test_assert_status_code_fails ... SUCCESS
148150
Running test_assert_status_code_succeeds ... SUCCESS
149151
Running test_assert_succeeds ... SUCCESS
152+
Running test_assert_within_delta_fails ... SUCCESS
153+
Running test_assert_within_delta_succeeds ... SUCCESS
150154
Running test_fail_fails ... SUCCESS
151155
Overall result: SUCCESS
152156
```
@@ -178,6 +182,8 @@ ok - test_assert_shows_stdout_on_failure
178182
ok - test_assert_status_code_fails
179183
ok - test_assert_status_code_succeeds
180184
ok - test_assert_succeeds
185+
ok - test_assert_within_delta_fails
186+
ok - test_assert_within_delta_succeeds
181187
ok - test_fail_fails
182188
ok - test_fail_prints_failure_message
183189
ok - test_fail_prints_where_is_error
@@ -473,6 +479,35 @@ doc:2:test_obvious_matching_with_assert_not_matches()
473479
Running test_obvious_notmatching_with_assert_not_matches ... SUCCESS
474480
```
475481

482+
=== *assert_within_delta*
483+
484+
assert_within_delta <expected num> <actual num> <max delta> [message]
485+
486+
Asserts that the expected num matches the actual num up to a given max delta.
487+
This function only support integers.
488+
Given an expectation of 5 and a delta of 2 this would match 3, 4, 5, 6, and 7:
489+
490+
```test
491+
test_matches_within_delta(){
492+
assert_within_delta 5 3 2
493+
assert_within_delta 5 4 2
494+
assert_within_delta 5 5 2
495+
assert_within_delta 5 6 2
496+
assert_within_delta 5 7 2
497+
}
498+
test_does_not_match_within_delta(){
499+
assert_within_delta 5 2 2
500+
}
501+
502+
```
503+
504+
```output
505+
Running test_does_not_match_within_delta ... FAILURE
506+
expected value [5] to match [2] with a maximum delta of [2]
507+
doc:9:test_does_not_match_within_delta()
508+
Running test_matches_within_delta ... SUCCESS
509+
```
510+
476511
=== *assert_no_diff*
477512

478513
assert_no_diff <expected> <actual> [message]

bash_unit

+26
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,32 @@ assert_not_matches() {
143143
fi
144144
}
145145

146+
assert_within_delta() {
147+
function abs() {
148+
local value=$1
149+
local sign=$(( value < 0 ? -1 : 1 ))
150+
echo $((value * sign))
151+
}
152+
function is_number() {
153+
local value=$1
154+
test $value -eq $value 2>/dev/null
155+
}
156+
local expected=$1
157+
local actual=$2
158+
local max_delta=$3
159+
assert "is_number $expected" "$message expected value [$expected] is not a number"
160+
assert "is_number $actual" "$message actual value [$actual] is not a number"
161+
assert "is_number $max_delta" "$message max_delta [$max_delta] is not a number"
162+
local message=${4:-}
163+
[[ -z $message ]] || message="$message\n"
164+
165+
local actual_delta="$(abs $(($expected - $actual)))"
166+
167+
if (( $actual_delta > $max_delta )); then
168+
fail "$message expected value [$expected] to match [$actual] with a maximum delta of [$max_delta]"
169+
fi
170+
}
171+
146172
assert_no_diff() {
147173
local expected=$1
148174
local actual=$2

tests/test_core.sh

+30
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,36 @@ test_assert_not_equals_succeeds_when_not_equal() {
8181
'assert_not_equals should succeed'
8282
}
8383

84+
test_assert_within_delta_succeeds() {
85+
assert \
86+
"assert_within_delta 12 10 3"\
87+
'assert_within_delta should succeed'
88+
assert \
89+
"assert_within_delta 10 12 3"\
90+
'assert_within_delta should succeed'
91+
assert \
92+
"assert_within_delta 10 12 2"\
93+
'assert_within_delta should succeed'
94+
assert \
95+
"assert_within_delta 10 10 0"\
96+
'assert_within_delta should succeed'
97+
}
98+
99+
test_assert_within_delta_fails() {
100+
assert_fails \
101+
"assert_within_delta 13 10 2"\
102+
'assert_within_delta should fail'
103+
assert_fails \
104+
"assert_within_delta 10 13 2"\
105+
'assert_within_delta should fail'
106+
assert_fails \
107+
"assert_within_delta 11 10 0"\
108+
'assert_within_delta should fail'
109+
assert_fails \
110+
"assert_within_delta eleven 10 0"\
111+
'assert_within_delta should fail'
112+
}
113+
84114
test_assert_no_diff_succeeds_when_no_diff() {
85115
assert \
86116
"assert_no_diff <(echo foo) <(echo foo)" \

0 commit comments

Comments
 (0)