diff options
author | Jason Karns <jason@karns.name> | 2018-11-27 20:12:31 +0300 |
---|---|---|
committer | Jason Karns <jason@karns.name> | 2018-11-27 20:12:31 +0300 |
commit | 0a2cf181b011112164e2f8d31e981001643e6daf (patch) | |
tree | 71af97e7c6ec928ae71a7026590fcbf3c4c6e984 | |
parent | 4dfe724a2e65102261697b29d3e6a7345ee6e85d (diff) |
Extract helper for asserting test failures
-rwxr-xr-x | test/50-assert-11-assert.bats | 12 | ||||
-rwxr-xr-x | test/50-assert-12-assert_equal.bats | 54 | ||||
-rwxr-xr-x | test/50-assert-13-assert_success.bats | 33 | ||||
-rwxr-xr-x | test/50-assert-14-assert_failure.bats | 64 | ||||
-rwxr-xr-x | test/50-assert-15-assert_output.bats | 188 | ||||
-rwxr-xr-x | test/50-assert-16-refute_output.bats | 120 | ||||
-rwxr-xr-x | test/50-assert-17-assert_line.bats | 180 | ||||
-rwxr-xr-x | test/50-assert-18-refute_line.bats | 196 | ||||
-rwxr-xr-x | test/50-assert-19-refute.bats | 11 | ||||
-rw-r--r-- | test/test_helper.bash | 10 |
10 files changed, 494 insertions, 374 deletions
diff --git a/test/50-assert-11-assert.bats b/test/50-assert-11-assert.bats index 2ed7d5d..d1a34a2 100755 --- a/test/50-assert-11-assert.bats +++ b/test/50-assert-11-assert.bats @@ -9,9 +9,11 @@ load test_helper @test 'assert() <expression>: returns 1 and displays <expression> if it evaluates to FALSE' { run assert false - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- assertion failed --' ] - [ "${lines[1]}" == 'expression : false' ] - [ "${lines[2]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- assertion failed -- +expression : false +-- +ERR_MSG } diff --git a/test/50-assert-12-assert_equal.bats b/test/50-assert-12-assert_equal.bats index af10bfa..3198584 100755 --- a/test/50-assert-12-assert_equal.bats +++ b/test/50-assert-12-assert_equal.bats @@ -9,38 +9,44 @@ load test_helper @test 'assert_equal() <actual> <expected>: returns 1 and displays details if <actual> does not equal <expected>' { run assert_equal 'a' 'b' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 4 ] - [ "${lines[0]}" == '-- values do not equal --' ] - [ "${lines[1]}" == 'expected : b' ] - [ "${lines[2]}" == 'actual : a' ] - [ "${lines[3]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- values do not equal -- +expected : b +actual : a +-- +ERR_MSG } @test 'assert_equal() <actual> <expected>: displays details in multi-line format if <actual> is longer than one line' { run assert_equal $'a 0\na 1' 'b' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 7 ] - [ "${lines[0]}" == '-- values do not equal --' ] - [ "${lines[1]}" == 'expected (1 lines):' ] - [ "${lines[2]}" == ' b' ] - [ "${lines[3]}" == 'actual (2 lines):' ] - [ "${lines[4]}" == ' a 0' ] - [ "${lines[5]}" == ' a 1' ] - [ "${lines[6]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- values do not equal -- +expected (1 lines): + b +actual (2 lines): + a 0 + a 1 +-- +ERR_MSG } @test 'assert_equal() <actual> <expected>: displays details in multi-line format if <expected> is longer than one line' { run assert_equal 'a' $'b 0\nb 1' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 7 ] - [ "${lines[0]}" == '-- values do not equal --' ] - [ "${lines[1]}" == 'expected (2 lines):' ] - [ "${lines[2]}" == ' b 0' ] - [ "${lines[3]}" == ' b 1' ] - [ "${lines[4]}" == 'actual (1 lines):' ] - [ "${lines[5]}" == ' a' ] - [ "${lines[6]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- values do not equal -- +expected (2 lines): + b 0 + b 1 +actual (1 lines): + a +-- +ERR_MSG } @test 'assert_equal() <actual> <expected>: performs literal matching' { diff --git a/test/50-assert-13-assert_success.bats b/test/50-assert-13-assert_success.bats index 65c23f5..66f5aa7 100755 --- a/test/50-assert-13-assert_success.bats +++ b/test/50-assert-13-assert_success.bats @@ -5,6 +5,7 @@ load test_helper @test "assert_success(): returns 0 if \`\$status' is 0" { run true run assert_success + assert_test_pass } @@ -12,24 +13,28 @@ load test_helper run bash -c 'echo "a" exit 1' run assert_success - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 4 ] - [ "${lines[0]}" == '-- command failed --' ] - [ "${lines[1]}" == 'status : 1' ] - [ "${lines[2]}" == 'output : a' ] - [ "${lines[3]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- command failed -- +status : 1 +output : a +-- +ERR_MSG } @test "assert_success(): displays \`\$output' in multi-line format if it is longer than one line" { run bash -c 'printf "a 0\na 1" exit 1' run assert_success - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 6 ] - [ "${lines[0]}" == '-- command failed --' ] - [ "${lines[1]}" == 'status : 1' ] - [ "${lines[2]}" == 'output (2 lines):' ] - [ "${lines[3]}" == ' a 0' ] - [ "${lines[4]}" == ' a 1' ] - [ "${lines[5]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- command failed -- +status : 1 +output (2 lines): + a 0 + a 1 +-- +ERR_MSG } diff --git a/test/50-assert-14-assert_failure.bats b/test/50-assert-14-assert_failure.bats index 21b9999..6291afe 100755 --- a/test/50-assert-14-assert_failure.bats +++ b/test/50-assert-14-assert_failure.bats @@ -12,24 +12,28 @@ load test_helper run bash -c 'echo "a" exit 0' run assert_failure - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- command succeeded, but it was expected to fail --' ] - [ "${lines[1]}" == 'output : a' ] - [ "${lines[2]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- command succeeded, but it was expected to fail -- +output : a +-- +ERR_MSG } @test "assert_failure(): displays \`\$output' in multi-line format if it is longer then one line" { run bash -c 'printf "a 0\na 1" exit 0' run assert_failure - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 5 ] - [ "${lines[0]}" == '-- command succeeded, but it was expected to fail --' ] - [ "${lines[1]}" == 'output (2 lines):' ] - [ "${lines[2]}" == ' a 0' ] - [ "${lines[3]}" == ' a 1' ] - [ "${lines[4]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- command succeeded, but it was expected to fail -- +output (2 lines): + a 0 + a 1 +-- +ERR_MSG } @test "assert_failure() <status>: returns 0 if \`\$status' equals <status>" { @@ -42,26 +46,30 @@ load test_helper run bash -c 'echo "a" exit 1' run assert_failure 2 - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 5 ] - [ "${lines[0]}" == '-- command failed as expected, but status differs --' ] - [ "${lines[1]}" == 'expected : 2' ] - [ "${lines[2]}" == 'actual : 1' ] - [ "${lines[3]}" == 'output : a' ] - [ "${lines[4]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- command failed as expected, but status differs -- +expected : 2 +actual : 1 +output : a +-- +ERR_MSG } @test "assert_failure() <status>: displays \`\$output' in multi-line format if it is longer then one line" { run bash -c 'printf "a 0\na 1" exit 1' run assert_failure 2 - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 7 ] - [ "${lines[0]}" == '-- command failed as expected, but status differs --' ] - [ "${lines[1]}" == 'expected : 2' ] - [ "${lines[2]}" == 'actual : 1' ] - [ "${lines[3]}" == 'output (2 lines):' ] - [ "${lines[4]}" == ' a 0' ] - [ "${lines[5]}" == ' a 1' ] - [ "${lines[6]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- command failed as expected, but status differs -- +expected : 2 +actual : 1 +output (2 lines): + a 0 + a 1 +-- +ERR_MSG } diff --git a/test/50-assert-15-assert_output.bats b/test/50-assert-15-assert_output.bats index 19ad6a5..6cf1b9d 100755 --- a/test/50-assert-15-assert_output.bats +++ b/test/50-assert-15-assert_output.bats @@ -16,12 +16,14 @@ load test_helper @test "assert_output() <expected>: returns 1 and displays details if <expected> does not equal \`\$output'" { run echo 'b' run assert_output 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 4 ] - [ "${lines[0]}" == '-- output differs --' ] - [ "${lines[1]}" == 'expected : a' ] - [ "${lines[2]}" == 'actual : b' ] - [ "${lines[3]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- output differs -- +expected : a +actual : b +-- +ERR_MSG } @test 'assert_output() - : reads <expected> from STDIN' { @@ -29,6 +31,7 @@ load test_helper run assert_output - <<STDIN a STDIN + assert_test_pass } @@ -37,6 +40,7 @@ STDIN run assert_output --stdin <<STDIN a STDIN + assert_test_pass } @@ -44,29 +48,33 @@ STDIN @test "assert_output() <expected>: displays details in multi-line format if \`\$output' is longer than one line" { run printf 'b 0\nb 1' run assert_output 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 7 ] - [ "${lines[0]}" == '-- output differs --' ] - [ "${lines[1]}" == 'expected (1 lines):' ] - [ "${lines[2]}" == ' a' ] - [ "${lines[3]}" == 'actual (2 lines):' ] - [ "${lines[4]}" == ' b 0' ] - [ "${lines[5]}" == ' b 1' ] - [ "${lines[6]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- output differs -- +expected (1 lines): + a +actual (2 lines): + b 0 + b 1 +-- +ERR_MSG } @test 'assert_output() <expected>: displays details in multi-line format if <expected> is longer than one line' { run echo 'b' run assert_output $'a 0\na 1' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 7 ] - [ "${lines[0]}" == '-- output differs --' ] - [ "${lines[1]}" == 'expected (2 lines):' ] - [ "${lines[2]}" == ' a 0' ] - [ "${lines[3]}" == ' a 1' ] - [ "${lines[4]}" == 'actual (1 lines):' ] - [ "${lines[5]}" == ' b' ] - [ "${lines[6]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- output differs -- +expected (2 lines): + a 0 + a 1 +actual (1 lines): + b +-- +ERR_MSG } # Options @@ -103,41 +111,47 @@ STDIN @test "assert_output() --partial <partial>: returns 1 and displays details if <partial> is not a substring in \`\$output'" { run echo 'b' run assert_output --partial 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 4 ] - [ "${lines[0]}" == '-- output does not contain substring --' ] - [ "${lines[1]}" == 'substring : a' ] - [ "${lines[2]}" == 'output : b' ] - [ "${lines[3]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- output does not contain substring -- +substring : a +output : b +-- +ERR_MSG } # Output formatting @test "assert_output() --partial <partial>: displays details in multi-line format if \`\$output' is longer than one line" { run printf 'b 0\nb 1' run assert_output --partial 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 7 ] - [ "${lines[0]}" == '-- output does not contain substring --' ] - [ "${lines[1]}" == 'substring (1 lines):' ] - [ "${lines[2]}" == ' a' ] - [ "${lines[3]}" == 'output (2 lines):' ] - [ "${lines[4]}" == ' b 0' ] - [ "${lines[5]}" == ' b 1' ] - [ "${lines[6]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- output does not contain substring -- +substring (1 lines): + a +output (2 lines): + b 0 + b 1 +-- +ERR_MSG } @test 'assert_output() --partial <partial>: displays details in multi-line format if <partial> is longer than one line' { run echo 'b' run assert_output --partial $'a 0\na 1' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 7 ] - [ "${lines[0]}" == '-- output does not contain substring --' ] - [ "${lines[1]}" == 'substring (2 lines):' ] - [ "${lines[2]}" == ' a 0' ] - [ "${lines[3]}" == ' a 1' ] - [ "${lines[4]}" == 'output (1 lines):' ] - [ "${lines[5]}" == ' b' ] - [ "${lines[6]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- output does not contain substring -- +substring (2 lines): + a 0 + a 1 +output (1 lines): + b +-- +ERR_MSG } @@ -167,51 +181,59 @@ STDIN @test "assert_output() --regexp <regexp>: returns 1 and displays details if <regexp> does not match \`\$output'" { run echo 'b' run assert_output --regexp '.*a.*' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 4 ] - [ "${lines[0]}" == '-- regular expression does not match output --' ] - [ "${lines[1]}" == 'regexp : .*a.*' ] - [ "${lines[2]}" == 'output : b' ] - [ "${lines[3]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- regular expression does not match output -- +regexp : .*a.* +output : b +-- +ERR_MSG } # Output formatting @test "assert_output() --regexp <regexp>: displays details in multi-line format if \`\$output' is longer than one line" { run printf 'b 0\nb 1' run assert_output --regexp '.*a.*' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 7 ] - [ "${lines[0]}" == '-- regular expression does not match output --' ] - [ "${lines[1]}" == 'regexp (1 lines):' ] - [ "${lines[2]}" == ' .*a.*' ] - [ "${lines[3]}" == 'output (2 lines):' ] - [ "${lines[4]}" == ' b 0' ] - [ "${lines[5]}" == ' b 1' ] - [ "${lines[6]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- regular expression does not match output -- +regexp (1 lines): + .*a.* +output (2 lines): + b 0 + b 1 +-- +ERR_MSG } @test 'assert_output() --regexp <regexp>: displays details in multi-line format if <regexp> is longer than one line' { run echo 'b' run assert_output --regexp $'.*a\nb.*' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 7 ] - [ "${lines[0]}" == '-- regular expression does not match output --' ] - [ "${lines[1]}" == 'regexp (2 lines):' ] - [ "${lines[2]}" == ' .*a' ] - [ "${lines[3]}" == ' b.*' ] - [ "${lines[4]}" == 'output (1 lines):' ] - [ "${lines[5]}" == ' b' ] - [ "${lines[6]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- regular expression does not match output -- +regexp (2 lines): + .*a + b.* +output (1 lines): + b +-- +ERR_MSG } # Error handling @test 'assert_output() --regexp <regexp>: returns 1 and displays an error message if <regexp> is not a valid extended regular expression' { run assert_output --regexp '[.*' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- ERROR: assert_output --' ] - [ "${lines[1]}" == "Invalid extended regular expression: \`[.*'" ] - [ "${lines[2]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- ERROR: assert_output -- +Invalid extended regular expression: `[.*' +-- +ERR_MSG } @@ -221,11 +243,13 @@ STDIN @test "assert_output(): \`--partial' and \`--regexp' are mutually exclusive" { run assert_output --partial --regexp - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- ERROR: assert_output --' ] - [ "${lines[1]}" == "\`--partial' and \`--regexp' are mutually exclusive" ] - [ "${lines[2]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- ERROR: assert_output -- +`--partial' and `--regexp' are mutually exclusive +-- +ERR_MSG } @test "assert_output(): \`--' stops parsing options" { diff --git a/test/50-assert-16-refute_output.bats b/test/50-assert-16-refute_output.bats index 6b29ff7..b0b6559 100755 --- a/test/50-assert-16-refute_output.bats +++ b/test/50-assert-16-refute_output.bats @@ -17,11 +17,13 @@ load test_helper @test "refute_output() <unexpected>: returns 1 and displays details if <unexpected> equals \`\$output'" { run echo 'a' run refute_output 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- output equals, but it was expected to differ --' ] - [ "${lines[1]}" == 'output : a' ] - [ "${lines[2]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- output equals, but it was expected to differ -- +output : a +-- +ERR_MSG } @test 'refute_output() - : reads <unexpected> from STDIN' { @@ -46,13 +48,15 @@ INPUT @test 'refute_output() <unexpected>: displays details in multi-line format if necessary' { run printf 'a 0\na 1' run refute_output $'a 0\na 1' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 5 ] - [ "${lines[0]}" == '-- output equals, but it was expected to differ --' ] - [ "${lines[1]}" == 'output (2 lines):' ] - [ "${lines[2]}" == ' a 0' ] - [ "${lines[3]}" == ' a 1' ] - [ "${lines[4]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- output equals, but it was expected to differ -- +output (2 lines): + a 0 + a 1 +-- +ERR_MSG } # Options @@ -92,27 +96,31 @@ test_p_partial () { @test "refute_output() --partial <partial>: returns 1 and displays details if <partial> is a substring in \`\$output'" { run echo 'a' run refute_output --partial 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 4 ] - [ "${lines[0]}" == '-- output should not contain substring --' ] - [ "${lines[1]}" == 'substring : a' ] - [ "${lines[2]}" == 'output : a' ] - [ "${lines[3]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- output should not contain substring -- +substring : a +output : a +-- +ERR_MSG } # Output formatting @test 'refute_output() --partial <partial>: displays details in multi-line format if necessary' { run printf 'a 0\na 1' run refute_output --partial 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 7 ] - [ "${lines[0]}" == '-- output should not contain substring --' ] - [ "${lines[1]}" == 'substring (1 lines):' ] - [ "${lines[2]}" == ' a' ] - [ "${lines[3]}" == 'output (2 lines):' ] - [ "${lines[4]}" == ' a 0' ] - [ "${lines[5]}" == ' a 1' ] - [ "${lines[6]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- output should not contain substring -- +substring (1 lines): + a +output (2 lines): + a 0 + a 1 +-- +ERR_MSG } @@ -145,37 +153,43 @@ test_r_regexp () { @test "refute_output() --regexp <regexp>: returns 1 and displays details if <regexp> matches \`\$output'" { run echo 'a' run refute_output --regexp '.*a.*' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 4 ] - [ "${lines[0]}" == '-- regular expression should not match output --' ] - [ "${lines[1]}" == 'regexp : .*a.*' ] - [ "${lines[2]}" == 'output : a' ] - [ "${lines[3]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- regular expression should not match output -- +regexp : .*a.* +output : a +-- +ERR_MSG } # Output formatting @test 'refute_output() --regexp <regexp>: displays details in multi-line format if necessary' { run printf 'a 0\na 1' run refute_output --regexp '.*a.*' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 7 ] - [ "${lines[0]}" == '-- regular expression should not match output --' ] - [ "${lines[1]}" == 'regexp (1 lines):' ] - [ "${lines[2]}" == ' .*a.*' ] - [ "${lines[3]}" == 'output (2 lines):' ] - [ "${lines[4]}" == ' a 0' ] - [ "${lines[5]}" == ' a 1' ] - [ "${lines[6]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- regular expression should not match output -- +regexp (1 lines): + .*a.* +output (2 lines): + a 0 + a 1 +-- +ERR_MSG } # Error handling @test 'refute_output() --regexp <regexp>: returns 1 and displays an error message if <regexp> is not a valid extended regular expression' { run refute_output --regexp '[.*' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- ERROR: refute_output --' ] - [ "${lines[1]}" == "Invalid extended regular expression: \`[.*'" ] - [ "${lines[2]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- ERROR: refute_output -- +Invalid extended regular expression: `[.*' +-- +ERR_MSG } @@ -185,11 +199,13 @@ test_r_regexp () { @test "refute_output(): \`--partial' and \`--regexp' are mutually exclusive" { run refute_output --partial --regexp - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- ERROR: refute_output --' ] - [ "${lines[1]}" == "\`--partial' and \`--regexp' are mutually exclusive" ] - [ "${lines[2]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- ERROR: refute_output -- +`--partial' and `--regexp' are mutually exclusive +-- +ERR_MSG } @test "refute_output(): \`--' stops parsing options" { diff --git a/test/50-assert-17-assert_line.bats b/test/50-assert-17-assert_line.bats index 0d178dd..d0f7103 100755 --- a/test/50-assert-17-assert_line.bats +++ b/test/50-assert-17-assert_line.bats @@ -21,26 +21,30 @@ load test_helper @test "assert_line() <expected>: returns 1 and displays details if <expected> is not a line in \`\${lines[@]}'" { run echo 'b' run assert_line 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 4 ] - [ "${lines[0]}" == '-- output does not contain line --' ] - [ "${lines[1]}" == 'line : a' ] - [ "${lines[2]}" == 'output : b' ] - [ "${lines[3]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- output does not contain line -- +line : a +output : b +-- +ERR_MSG } # Output formatting @test "assert_line() <expected>: displays \`\$output' in multi-line format if it is longer than one line" { run printf 'b 0\nb 1' run assert_line 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 6 ] - [ "${lines[0]}" == '-- output does not contain line --' ] - [ "${lines[1]}" == 'line : a' ] - [ "${lines[2]}" == 'output (2 lines):' ] - [ "${lines[3]}" == ' b 0' ] - [ "${lines[4]}" == ' b 1' ] - [ "${lines[5]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- output does not contain line -- +line : a +output (2 lines): + b 0 + b 1 +-- +ERR_MSG } # Options @@ -80,26 +84,30 @@ test_p_partial () { @test "assert_line() --partial <partial>: returns 1 and displays details if <partial> is not a substring in any lines in \`\${lines[@]}'" { run echo 'b' run assert_line --partial 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 4 ] - [ "${lines[0]}" == '-- no output line contains substring --' ] - [ "${lines[1]}" == 'substring : a' ] - [ "${lines[2]}" == 'output : b' ] - [ "${lines[3]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- no output line contains substring -- +substring : a +output : b +-- +ERR_MSG } # Output formatting @test "assert_line() --partial <partial>: displays \`\$output' in multi-line format if it is longer than one line" { run printf 'b 0\nb 1' run assert_line --partial 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 6 ] - [ "${lines[0]}" == '-- no output line contains substring --' ] - [ "${lines[1]}" == 'substring : a' ] - [ "${lines[2]}" == 'output (2 lines):' ] - [ "${lines[3]}" == ' b 0' ] - [ "${lines[4]}" == ' b 1' ] - [ "${lines[5]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- no output line contains substring -- +substring : a +output (2 lines): + b 0 + b 1 +-- +ERR_MSG } @@ -132,26 +140,30 @@ test_r_regexp () { @test "assert_line() --regexp <regexp>: returns 1 and displays details if <regexp> does not match any lines in \`\${lines[@]}'" { run echo 'b' run assert_line --regexp '^.a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 4 ] - [ "${lines[0]}" == '-- no output line matches regular expression --' ] - [ "${lines[1]}" == 'regexp : ^.a' ] - [ "${lines[2]}" == 'output : b' ] - [ "${lines[3]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- no output line matches regular expression -- +regexp : ^.a +output : b +-- +ERR_MSG } # Output formatting @test "assert_line() --regexp <regexp>: displays \`\$output' in multi-line format if longer than one line" { run printf 'b 0\nb 1' run assert_line --regexp '^.a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 6 ] - [ "${lines[0]}" == '-- no output line matches regular expression --' ] - [ "${lines[1]}" == 'regexp : ^.a' ] - [ "${lines[2]}" == 'output (2 lines):' ] - [ "${lines[3]}" == ' b 0' ] - [ "${lines[4]}" == ' b 1' ] - [ "${lines[5]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- no output line matches regular expression -- +regexp : ^.a +output (2 lines): + b 0 + b 1 +-- +ERR_MSG } @@ -176,11 +188,13 @@ test_n_index () { @test 'assert_line() --index <idx>: returns 1 and displays an error message if <idx> is not an integer' { run assert_line --index 1a - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- ERROR: assert_line --' ] - [ "${lines[1]}" == "\`--index' requires an integer argument: \`1a'" ] - [ "${lines[2]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- ERROR: assert_line -- +`--index' requires an integer argument: `1a' +-- +ERR_MSG } @@ -198,13 +212,15 @@ test_n_index () { @test "assert_line() --index <idx> <expected>: returns 1 and displays details if <expected> does not equal \`\${lines[<idx>]}'" { run printf 'a\nb\nc' run assert_line --index 1 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 5 ] - [ "${lines[0]}" == '-- line differs --' ] - [ "${lines[1]}" == 'index : 1' ] - [ "${lines[2]}" == 'expected : a' ] - [ "${lines[3]}" == 'actual : b' ] - [ "${lines[4]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- line differs -- +index : 1 +expected : a +actual : b +-- +ERR_MSG } # Options @@ -244,13 +260,15 @@ test_index_p_partial () { @test "assert_line() --index <idx> --partial <partial>: returns 1 and displays details if <partial> is not a substring in \`\${lines[<idx>]}'" { run printf 'b 0\nb 1' run assert_line --index 1 --partial 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 5 ] - [ "${lines[0]}" == '-- line does not contain substring --' ] - [ "${lines[1]}" == 'index : 1' ] - [ "${lines[2]}" == 'substring : a' ] - [ "${lines[3]}" == 'line : b 1' ] - [ "${lines[4]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- line does not contain substring -- +index : 1 +substring : a +line : b 1 +-- +ERR_MSG } @@ -283,13 +301,15 @@ test_index_r_regexp () { @test "assert_line() --index <idx> --regexp <regexp>: returns 1 and displays details if <regexp> does not match \`\${lines[<idx>]}'" { run printf 'a\nb\nc' run assert_line --index 1 --regexp '^.a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 5 ] - [ "${lines[0]}" == '-- regular expression does not match line --' ] - [ "${lines[1]}" == 'index : 1' ] - [ "${lines[2]}" == 'regexp : ^.a' ] - [ "${lines[3]}" == 'line : b' ] - [ "${lines[4]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- regular expression does not match line -- +index : 1 +regexp : ^.a +line : b +-- +ERR_MSG } @@ -299,20 +319,24 @@ test_index_r_regexp () { @test "assert_line(): \`--partial' and \`--regexp' are mutually exclusive" { run assert_line --partial --regexp - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- ERROR: assert_line --' ] - [ "${lines[1]}" == "\`--partial' and \`--regexp' are mutually exclusive" ] - [ "${lines[2]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- ERROR: assert_line -- +`--partial' and `--regexp' are mutually exclusive +-- +ERR_MSG } @test 'assert_line() --regexp <regexp>: returns 1 and displays an error message if <regexp> is not a valid extended regular expression' { run assert_line --regexp '[.*' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- ERROR: assert_line --' ] - [ "${lines[1]}" == "Invalid extended regular expression: \`[.*'" ] - [ "${lines[2]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- ERROR: assert_line -- +Invalid extended regular expression: `[.*' +-- +ERR_MSG } @test "assert_line(): \`--' stops parsing options" { diff --git a/test/50-assert-18-refute_line.bats b/test/50-assert-18-refute_line.bats index 5d77066..a26b0ba 100755 --- a/test/50-assert-18-refute_line.bats +++ b/test/50-assert-18-refute_line.bats @@ -21,29 +21,33 @@ load test_helper @test "refute_line() <unexpected>: returns 1 and displays details if <unexpected> is not a line in \`\${lines[@]}'" { run echo 'a' run refute_line 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 5 ] - [ "${lines[0]}" == '-- line should not be in output --' ] - [ "${lines[1]}" == 'line : a' ] - [ "${lines[2]}" == 'index : 0' ] - [ "${lines[3]}" == 'output : a' ] - [ "${lines[4]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- line should not be in output -- +line : a +index : 0 +output : a +-- +ERR_MSG } # Output formatting @test "refute_line() <unexpected>: displays \`\$output' in multi-line format if it is longer than one line" { run printf 'a 0\na 1\na 2' run refute_line 'a 1' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 8 ] - [ "${lines[0]}" == '-- line should not be in output --' ] - [ "${lines[1]}" == 'line : a 1' ] - [ "${lines[2]}" == 'index : 1' ] - [ "${lines[3]}" == 'output (3 lines):' ] - [ "${lines[4]}" == ' a 0' ] - [ "${lines[5]}" == '> a 1' ] - [ "${lines[6]}" == ' a 2' ] - [ "${lines[7]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- line should not be in output -- +line : a 1 +index : 1 +output (3 lines): + a 0 +> a 1 + a 2 +-- +ERR_MSG } # Options @@ -83,29 +87,33 @@ test_p_partial () { @test "refute_line() --partial <partial>: returns 1 and displays details if <partial> is a substring in any line in \`\${lines[@]}'" { run echo 'a' run refute_line --partial 'a' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 5 ] - [ "${lines[0]}" == '-- no line should contain substring --' ] - [ "${lines[1]}" == 'substring : a' ] - [ "${lines[2]}" == 'index : 0' ] - [ "${lines[3]}" == 'output : a' ] - [ "${lines[4]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- no line should contain substring -- +substring : a +index : 0 +output : a +-- +ERR_MSG } # Output formatting @test "refute_line() --partial <partial>: displays \`\$output' in multi-line format if it is longer than one line" { run printf 'a\nabc\nc' run refute_line --partial 'b' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 8 ] - [ "${lines[0]}" == '-- no line should contain substring --' ] - [ "${lines[1]}" == 'substring : b' ] - [ "${lines[2]}" == 'index : 1' ] - [ "${lines[3]}" == 'output (3 lines):' ] - [ "${lines[4]}" == ' a' ] - [ "${lines[5]}" == '> abc' ] - [ "${lines[6]}" == ' c' ] - [ "${lines[7]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- no line should contain substring -- +substring : b +index : 1 +output (3 lines): + a +> abc + c +-- +ERR_MSG } @@ -138,29 +146,33 @@ test_r_regexp () { @test "refute_line() --regexp <regexp>: returns 1 and displays details if <regexp> matches any lines in \`\${lines[@]}'" { run echo 'a' run refute_line --regexp '.*a.*' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 5 ] - [ "${lines[0]}" == '-- no line should match the regular expression --' ] - [ "${lines[1]}" == 'regexp : .*a.*' ] - [ "${lines[2]}" == 'index : 0' ] - [ "${lines[3]}" == 'output : a' ] - [ "${lines[4]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- no line should match the regular expression -- +regexp : .*a.* +index : 0 +output : a +-- +ERR_MSG } # Output formatting @test "refute_line() --regexp <regexp>: displays \`\$output' in multi-line format if longer than one line" { run printf 'a\nabc\nc' run refute_line --regexp '.*b.*' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 8 ] - [ "${lines[0]}" == '-- no line should match the regular expression --' ] - [ "${lines[1]}" == 'regexp : .*b.*' ] - [ "${lines[2]}" == 'index : 1' ] - [ "${lines[3]}" == 'output (3 lines):' ] - [ "${lines[4]}" == ' a' ] - [ "${lines[5]}" == '> abc' ] - [ "${lines[6]}" == ' c' ] - [ "${lines[7]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- no line should match the regular expression -- +regexp : .*b.* +index : 1 +output (3 lines): + a +> abc + c +-- +ERR_MSG } @@ -185,11 +197,13 @@ test_n_index () { @test 'refute_line() --index <idx>: returns 1 and displays an error message if <idx> is not an integer' { run refute_line --index 1a - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- ERROR: refute_line --' ] - [ "${lines[1]}" == "\`--index' requires an integer argument: \`1a'" ] - [ "${lines[2]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- ERROR: refute_line -- +`--index' requires an integer argument: `1a' +-- +ERR_MSG } @@ -207,12 +221,14 @@ test_n_index () { @test "refute_line() --index <idx> <unexpected>: returns 1 and displays details if <unexpected> equals \`\${lines[<idx>]}'" { run printf 'a\nb\nc' run refute_line --index 1 'b' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 4 ] - [ "${lines[0]}" == '-- line should differ --' ] - [ "${lines[1]}" == 'index : 1' ] - [ "${lines[2]}" == 'line : b' ] - [ "${lines[3]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- line should differ -- +index : 1 +line : b +-- +ERR_MSG } # Options @@ -252,13 +268,15 @@ test_index_p_partial () { @test "refute_line() --index <idx> --partial <partial>: returns 1 and displays details if <partial> is a substring in \`\${lines[<idx>]}'" { run printf 'a\nabc\nc' run refute_line --index 1 --partial 'b' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 5 ] - [ "${lines[0]}" == '-- line should not contain substring --' ] - [ "${lines[1]}" == 'index : 1' ] - [ "${lines[2]}" == 'substring : b' ] - [ "${lines[3]}" == 'line : abc' ] - [ "${lines[4]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- line should not contain substring -- +index : 1 +substring : b +line : abc +-- +ERR_MSG } @@ -291,13 +309,15 @@ test_index_r_regexp () { @test "refute_line() --index <idx> --regexp <regexp>: returns 1 and displays details if <regexp> matches \`\${lines[<idx>]}'" { run printf 'a\nabc\nc' run refute_line --index 1 --regexp '.*b.*' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 5 ] - [ "${lines[0]}" == '-- regular expression should not match line --' ] - [ "${lines[1]}" == 'index : 1' ] - [ "${lines[2]}" == 'regexp : .*b.*' ] - [ "${lines[3]}" == 'line : abc' ] - [ "${lines[4]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- regular expression should not match line -- +index : 1 +regexp : .*b.* +line : abc +-- +ERR_MSG } @@ -307,20 +327,24 @@ test_index_r_regexp () { @test "refute_line(): \`--partial' and \`--regexp' are mutually exclusive" { run refute_line --partial --regexp - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- ERROR: refute_line --' ] - [ "${lines[1]}" == "\`--partial' and \`--regexp' are mutually exclusive" ] - [ "${lines[2]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- ERROR: refute_line -- +`--partial' and `--regexp' are mutually exclusive +-- +ERR_MSG } @test 'refute_line() --regexp <regexp>: returns 1 and displays an error message if <regexp> is not a valid extended regular expression' { run refute_line --regexp '[.*' - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- ERROR: refute_line --' ] - [ "${lines[1]}" == "Invalid extended regular expression: \`[.*'" ] - [ "${lines[2]}" == '--' ] + + assert_test_fail <<'ERR_MSG' + +-- ERROR: refute_line -- +Invalid extended regular expression: `[.*' +-- +ERR_MSG } @test "refute_line(): \`--' stops parsing options" { diff --git a/test/50-assert-19-refute.bats b/test/50-assert-19-refute.bats index 31524fa..f9e2737 100755 --- a/test/50-assert-19-refute.bats +++ b/test/50-assert-19-refute.bats @@ -9,9 +9,10 @@ load test_helper @test 'refute() <expression>: returns 1 and displays <expression> if it evaluates to TRUE' { run refute true - [ "$status" -eq 1 ] - [ "${#lines[@]}" -eq 3 ] - [ "${lines[0]}" == '-- assertion succeeded, but it was expected to fail --' ] - [ "${lines[1]}" == 'expression : true' ] - [ "${lines[2]}" == '--' ] + assert_test_fail <<'ERR_MSG' + +-- assertion succeeded, but it was expected to fail -- +expression : true +-- +ERR_MSG } diff --git a/test/test_helper.bash b/test/test_helper.bash index 77927fe..1461696 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -12,4 +12,14 @@ setup() { test "$status" -eq 0 test "${#lines[@]}" -eq 0 } + + assert_test_fail() { + local err_msg="${1-$(cat -)}" + local num_lines="$(printf '%s' "$err_msg" | wc -l)" + + + test "$status" -eq 1 + test "${#lines[@]}" -eq "$num_lines" + test "$output" == "$err_msg" + } } |