Vendoring bats libraries.

Signed-off-by: Chad Metcalf <chad@docker.com>
This commit is contained in:
Chad Metcalf
2020-08-08 12:46:22 -07:00
committed by Chad Metcalf
parent 5185cedf4e
commit 6565a1f745
48 changed files with 4646 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
#!/usr/bin/env bats
load test_helper
@test 'assert() <expression>: returns 0 if <expression> evaluates to TRUE' {
run assert true
assert_test_pass
}
@test 'assert() <expression>: returns 1 and displays <expression> if it evaluates to FALSE' {
run assert false
assert_test_fail <<'ERR_MSG'
-- assertion failed --
expression : false
--
ERR_MSG
}

View File

@@ -0,0 +1,62 @@
#!/usr/bin/env bats
load test_helper
@test 'assert_equal() <actual> <expected>: returns 0 if <actual> equals <expected>' {
run assert_equal 'a' 'a'
assert_test_pass
}
@test 'assert_equal() <actual> <expected>: returns 1 and displays details if <actual> does not equal <expected>' {
run assert_equal 'a' 'b'
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'
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'
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' {
run assert_equal 'a' '*'
assert_test_fail <<'ERR_MSG'
-- values do not equal --
expected : *
actual : a
--
ERR_MSG
}

View File

@@ -0,0 +1,75 @@
#!/usr/bin/env bats
load test_helper
@test "assert_failure(): returns 0 if \`\$status' is not 0" {
run false
run assert_failure
assert_test_pass
}
@test "assert_failure(): returns 1 and displays details if \`\$status' is 0" {
run bash -c 'echo "a"
exit 0'
run assert_failure
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
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>" {
run bash -c 'exit 1'
run assert_failure 1
assert_test_pass
}
@test "assert_failure() <status>: returns 1 and displays details if \`\$status' does not equal <status>" {
run bash -c 'echo "a"
exit 1'
run assert_failure 2
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
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
}

View File

@@ -0,0 +1,351 @@
#!/usr/bin/env bats
load test_helper
###############################################################################
# Containing a line
###############################################################################
#
# Literal matching
#
# Correctness
@test "assert_line() <expected>: returns 0 if <expected> is a line in \`\${lines[@]}'" {
run printf 'a\nb\nc'
run assert_line 'b'
assert_test_pass
}
@test "assert_line() <expected>: returns 1 and displays details if <expected> is not a line in \`\${lines[@]}'" {
run echo 'b'
run assert_line 'a'
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'
assert_test_fail <<'ERR_MSG'
-- output does not contain line --
line : a
output (2 lines):
b 0
b 1
--
ERR_MSG
}
# Options
@test 'assert_line() <expected>: performs literal matching by default' {
run echo 'a'
run assert_line '*'
assert_test_fail <<'ERR_MSG'
-- output does not contain line --
line : *
output : a
--
ERR_MSG
}
#
# Partial matching: `-p' and `--partial'
#
# Options
@test 'assert_line() -p <partial>: enables partial matching' {
run printf 'a\n_b_\nc'
run assert_line -p 'b'
assert_test_pass
}
@test 'assert_line() --partial <partial>: enables partial matching' {
run printf 'a\n_b_\nc'
run assert_line --partial 'b'
assert_test_pass
}
# Correctness
@test "assert_line() --partial <partial>: returns 0 if <partial> is a substring in any line in \`\${lines[@]}'" {
run printf 'a\n_b_\nc'
run assert_line --partial 'b'
assert_test_pass
}
@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'
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'
assert_test_fail <<'ERR_MSG'
-- no output line contains substring --
substring : a
output (2 lines):
b 0
b 1
--
ERR_MSG
}
#
# Regular expression matching: `-e' and `--regexp'
#
# Options
@test 'assert_line() -e <regexp>: enables regular expression matching' {
run printf 'a\n_b_\nc'
run assert_line -e '^.b'
assert_test_pass
}
@test 'assert_line() --regexp <regexp>: enables regular expression matching' {
run printf 'a\n_b_\nc'
run assert_line --regexp '^.b'
assert_test_pass
}
# Correctness
@test "assert_line() --regexp <regexp>: returns 0 if <regexp> matches any line in \`\${lines[@]}'" {
run printf 'a\n_b_\nc'
run assert_line --regexp '^.b'
assert_test_pass
}
@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'
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'
assert_test_fail <<'ERR_MSG'
-- no output line matches regular expression --
regexp : ^.a
output (2 lines):
b 0
b 1
--
ERR_MSG
}
###############################################################################
# Matching single line: `-n' and `--index'
###############################################################################
# Options
@test 'assert_line() -n <idx> <expected>: matches against the <idx>-th line only' {
run printf 'a\nb\nc'
run assert_line -n 1 'b'
assert_test_pass
}
@test 'assert_line() --index <idx> <expected>: matches against the <idx>-th line only' {
run printf 'a\nb\nc'
run assert_line --index 1 'b'
assert_test_pass
}
@test 'assert_line() --index <idx>: returns 1 and displays an error message if <idx> is not an integer' {
run assert_line --index 1a
assert_test_fail <<'ERR_MSG'
-- ERROR: assert_line --
`--index' requires an integer argument: `1a'
--
ERR_MSG
}
#
# Literal matching
#
# Correctness
@test "assert_line() --index <idx> <expected>: returns 0 if <expected> equals \`\${lines[<idx>]}'" {
run printf 'a\nb\nc'
run assert_line --index 1 'b'
assert_test_pass
}
@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'
assert_test_fail <<'ERR_MSG'
-- line differs --
index : 1
expected : a
actual : b
--
ERR_MSG
}
# Options
@test 'assert_line() --index <idx> <expected>: performs literal matching by default' {
run printf 'a\nb\nc'
run assert_line --index 1 '*'
assert_test_fail <<'ERR_MSG'
-- line differs --
index : 1
expected : *
actual : b
--
ERR_MSG
}
#
# Partial matching: `-p' and `--partial'
#
# Options
@test 'assert_line() --index <idx> -p <partial>: enables partial matching' {
run printf 'a\n_b_\nc'
run assert_line --index 1 -p 'b'
assert_test_pass
}
@test 'assert_line() --index <idx> --partial <partial>: enables partial matching' {
run printf 'a\n_b_\nc'
run assert_line --index 1 --partial 'b'
assert_test_pass
}
# Correctness
@test "assert_line() --index <idx> --partial <partial>: returns 0 if <partial> is a substring in \`\${lines[<idx>]}'" {
run printf 'a\n_b_\nc'
run assert_line --index 1 --partial 'b'
assert_test_pass
}
@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'
assert_test_fail <<'ERR_MSG'
-- line does not contain substring --
index : 1
substring : a
line : b 1
--
ERR_MSG
}
#
# Regular expression matching: `-e' and `--regexp'
#
# Options
@test 'assert_line() --index <idx> -e <regexp>: enables regular expression matching' {
run printf 'a\n_b_\nc'
run assert_line --index 1 -e '^.b'
assert_test_pass
}
@test 'assert_line() --index <idx> --regexp <regexp>: enables regular expression matching' {
run printf 'a\n_b_\nc'
run assert_line --index 1 --regexp '^.b'
assert_test_pass
}
# Correctness
@test "assert_line() --index <idx> --regexp <regexp>: returns 0 if <regexp> matches \`\${lines[<idx>]}'" {
run printf 'a\n_b_\nc'
run assert_line --index 1 --regexp '^.b'
assert_test_pass
}
@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'
assert_test_fail <<'ERR_MSG'
-- regular expression does not match line --
index : 1
regexp : ^.a
line : b
--
ERR_MSG
}
###############################################################################
# Common
###############################################################################
@test "assert_line(): \`--partial' and \`--regexp' are mutually exclusive" {
run assert_line --partial --regexp
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 '[.*'
assert_test_fail <<'ERR_MSG'
-- ERROR: assert_line --
Invalid extended regular expression: `[.*'
--
ERR_MSG
}
@test "assert_line(): \`--' stops parsing options" {
run printf 'a\n-p\nc'
run assert_line -- '-p'
assert_test_pass
}

View File

@@ -0,0 +1,285 @@
#!/usr/bin/env bats
load test_helper
#
# Literal matching
#
# Correctness
@test "assert_output() <expected>: returns 0 if <expected> equals \`\$output'" {
run echo 'a'
run assert_output 'a'
assert_test_pass
}
@test "assert_output() <expected>: returns 1 and displays details if <expected> does not equal \`\$output'" {
run echo 'b'
run assert_output 'a'
assert_test_fail <<'ERR_MSG'
-- output differs --
expected : a
actual : b
--
ERR_MSG
}
@test 'assert_output(): succeeds if output is non-empty' {
run echo 'a'
run assert_output
assert_test_pass
}
@test 'assert_output(): fails if output is empty' {
run echo ''
run assert_output
assert_test_fail <<'ERR_MSG'
-- no output --
expected non-empty output, but output was empty
--
ERR_MSG
}
@test 'assert_output() - : reads <expected> from STDIN' {
run echo 'a'
run assert_output - <<STDIN
a
STDIN
assert_test_pass
}
@test 'assert_output() --stdin : reads <expected> from STDIN' {
run echo 'a'
run assert_output --stdin <<STDIN
a
STDIN
assert_test_pass
}
# Output formatting
@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'
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'
assert_test_fail <<'ERR_MSG'
-- output differs --
expected (2 lines):
a 0
a 1
actual (1 lines):
b
--
ERR_MSG
}
# Options
@test 'assert_output() <expected>: performs literal matching by default' {
run echo 'a'
run assert_output '*'
assert_test_fail <<'ERR_MSG'
-- output differs --
expected : *
actual : a
--
ERR_MSG
}
#
# Partial matching: `-p' and `--partial'
#
@test 'assert_output() -p <partial>: enables partial matching' {
run echo 'abc'
run assert_output -p 'b'
assert_test_pass
}
@test 'assert_output() --partial <partial>: enables partial matching' {
run echo 'abc'
run assert_output --partial 'b'
assert_test_pass
}
# Correctness
@test "assert_output() --partial <partial>: returns 0 if <partial> is a substring in \`\$output'" {
run printf 'a\nb\nc'
run assert_output --partial 'b'
assert_test_pass
}
@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'
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'
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'
assert_test_fail <<'ERR_MSG'
-- output does not contain substring --
substring (2 lines):
a 0
a 1
output (1 lines):
b
--
ERR_MSG
}
#
# Regular expression matching: `-e' and `--regexp'
#
@test 'assert_output() -e <regexp>: enables regular expression matching' {
run echo 'abc'
run assert_output -e '^a'
assert_test_pass
}
@test 'assert_output() --regexp <regexp>: enables regular expression matching' {
run echo 'abc'
run assert_output --regexp '^a'
assert_test_pass
}
# Correctness
@test "assert_output() --regexp <regexp>: returns 0 if <regexp> matches \`\$output'" {
run printf 'a\nb\nc'
run assert_output --regexp '.*b.*'
assert_test_pass
}
@test "assert_output() --regexp <regexp>: returns 1 and displays details if <regexp> does not match \`\$output'" {
run echo 'b'
run assert_output --regexp '.*a.*'
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.*'
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.*'
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 '[.*'
assert_test_fail <<'ERR_MSG'
-- ERROR: assert_output --
Invalid extended regular expression: `[.*'
--
ERR_MSG
}
#
# Common
#
@test "assert_output(): \`--partial' and \`--regexp' are mutually exclusive" {
run assert_output --partial --regexp
assert_test_fail <<'ERR_MSG'
-- ERROR: assert_output --
`--partial' and `--regexp' are mutually exclusive
--
ERR_MSG
}
@test "assert_output(): \`--' stops parsing options" {
run echo '-p'
run assert_output -- '-p'
assert_test_pass
}

View File

@@ -0,0 +1,40 @@
#!/usr/bin/env bats
load test_helper
@test "assert_success(): returns 0 if \`\$status' is 0" {
run true
run assert_success
assert_test_pass
}
@test "assert_success(): returns 1 and displays details if \`\$status' is not 0" {
run bash -c 'echo "a"
exit 1'
run assert_success
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
assert_test_fail <<'ERR_MSG'
-- command failed --
status : 1
output (2 lines):
a 0
a 1
--
ERR_MSG
}

View File

@@ -0,0 +1,18 @@
#!/usr/bin/env bats
load test_helper
@test 'refute() <expression>: returns 0 if <expression> evaluates to FALSE' {
run refute false
assert_test_pass
}
@test 'refute() <expression>: returns 1 and displays <expression> if it evaluates to TRUE' {
run refute true
assert_test_fail <<'ERR_MSG'
-- assertion succeeded, but it was expected to fail --
expression : true
--
ERR_MSG
}

View File

@@ -0,0 +1,344 @@
#!/usr/bin/env bats
load test_helper
###############################################################################
# Containing a line
###############################################################################
#
# Literal matching
#
# Correctness
@test "refute_line() <unexpected>: returns 0 if <unexpected> is not a line in \`\${lines[@]}'" {
run printf 'a\nb\nc'
run refute_line 'd'
assert_test_pass
}
@test "refute_line() <unexpected>: returns 1 and displays details if <unexpected> is not a line in \`\${lines[@]}'" {
run echo 'a'
run refute_line 'a'
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'
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
@test 'refute_line() <unexpected>: performs literal matching by default' {
run echo 'a'
run refute_line '*'
assert_test_pass
}
#
# Partial matching: `-p' and `--partial'
#
# Options
@test 'refute_line() -p <partial>: enables partial matching' {
run printf 'a\nb\nc'
run refute_line -p 'd'
assert_test_pass
}
@test 'refute_line() --partial <partial>: enables partial matching' {
run printf 'a\nb\nc'
run refute_line --partial 'd'
assert_test_pass
}
# Correctness
@test "refute_line() --partial <partial>: returns 0 if <partial> is not a substring in any line in \`\${lines[@]}'" {
run printf 'a\nb\nc'
run refute_line --partial 'd'
assert_test_pass
}
@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'
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'
assert_test_fail <<'ERR_MSG'
-- no line should contain substring --
substring : b
index : 1
output (3 lines):
a
> abc
c
--
ERR_MSG
}
#
# Regular expression matching: `-e' and `--regexp'
#
# Options
@test 'refute_line() -e <regexp>: enables regular expression matching' {
run printf 'a\nb\nc'
run refute_line -e '^.d'
assert_test_pass
}
@test 'refute_line() --regexp <regexp>: enables regular expression matching' {
run printf 'a\nb\nc'
run refute_line --regexp '^.d'
assert_test_pass
}
# Correctness
@test "refute_line() --regexp <regexp>: returns 0 if <regexp> does not match any line in \`\${lines[@]}'" {
run printf 'a\nb\nc'
run refute_line --regexp '.*d.*'
assert_test_pass
}
@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.*'
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.*'
assert_test_fail <<'ERR_MSG'
-- no line should match the regular expression --
regexp : .*b.*
index : 1
output (3 lines):
a
> abc
c
--
ERR_MSG
}
###############################################################################
# Matching single line: `-n' and `--index'
###############################################################################
# Options
@test 'refute_line() -n <idx> <expected>: matches against the <idx>-th line only' {
run printf 'a\nb\nc'
run refute_line -n 1 'd'
assert_test_pass
}
@test 'refute_line() --index <idx> <expected>: matches against the <idx>-th line only' {
run printf 'a\nb\nc'
run refute_line --index 1 'd'
assert_test_pass
}
@test 'refute_line() --index <idx>: returns 1 and displays an error message if <idx> is not an integer' {
run refute_line --index 1a
assert_test_fail <<'ERR_MSG'
-- ERROR: refute_line --
`--index' requires an integer argument: `1a'
--
ERR_MSG
}
#
# Literal matching
#
# Correctness
@test "refute_line() --index <idx> <unexpected>: returns 0 if <unexpected> does not equal \`\${lines[<idx>]}'" {
run printf 'a\nb\nc'
run refute_line --index 1 'd'
assert_test_pass
}
@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'
assert_test_fail <<'ERR_MSG'
-- line should differ --
index : 1
line : b
--
ERR_MSG
}
# Options
@test 'refute_line() --index <idx> <unexpected>: performs literal matching by default' {
run printf 'a\nb\nc'
run refute_line --index 1 '*'
assert_test_pass
}
#
# Partial matching: `-p' and `--partial'
#
# Options
@test 'refute_line() --index <idx> -p <partial>: enables partial matching' {
run printf 'a\nb\nc'
run refute_line --index 1 -p 'd'
assert_test_pass
}
@test 'refute_line() --index <idx> --partial <partial>: enables partial matching' {
run printf 'a\nb\nc'
run refute_line --index 1 --partial 'd'
assert_test_pass
}
# Correctness
@test "refute_line() --index <idx> --partial <partial>: returns 0 if <partial> is not a substring in \`\${lines[<idx>]}'" {
run printf 'a\nabc\nc'
run refute_line --index 1 --partial 'd'
assert_test_pass
}
@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'
assert_test_fail <<'ERR_MSG'
-- line should not contain substring --
index : 1
substring : b
line : abc
--
ERR_MSG
}
#
# Regular expression matching: `-e' and `--regexp'
#
# Options
@test 'refute_line() --index <idx> -e <regexp>: enables regular expression matching' {
run printf 'a\nb\nc'
run refute_line --index 1 -e '^.b'
assert_test_pass
}
@test 'refute_line() --index <idx> --regexp <regexp>: enables regular expression matching' {
run printf 'a\nb\nc'
run refute_line --index 1 --regexp '^.b'
assert_test_pass
}
# Correctness
@test "refute_line() --index <idx> --regexp <regexp>: returns 0 if <regexp> does not match \`\${lines[<idx>]}'" {
run printf 'a\nabc\nc'
run refute_line --index 1 --regexp '.*d.*'
assert_test_pass
}
@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.*'
assert_test_fail <<'ERR_MSG'
-- regular expression should not match line --
index : 1
regexp : .*b.*
line : abc
--
ERR_MSG
}
###############################################################################
# Common
###############################################################################
@test "refute_line(): \`--partial' and \`--regexp' are mutually exclusive" {
run refute_line --partial --regexp
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 '[.*'
assert_test_fail <<'ERR_MSG'
-- ERROR: refute_line --
Invalid extended regular expression: `[.*'
--
ERR_MSG
}
@test "refute_line(): \`--' stops parsing options" {
run printf 'a\n--\nc'
run refute_line -- '-p'
assert_test_pass
}

View File

@@ -0,0 +1,230 @@
#!/usr/bin/env bats
load test_helper
#
# Literal matching
#
# Correctness
@test "refute_output() <unexpected>: returns 0 if <unexpected> does not equal \`\$output'" {
run echo 'b'
run refute_output 'a'
assert_test_pass
}
@test "refute_output() <unexpected>: returns 1 and displays details if <unexpected> equals \`\$output'" {
run echo 'a'
run refute_output 'a'
assert_test_fail <<'ERR_MSG'
-- output equals, but it was expected to differ --
output : a
--
ERR_MSG
}
@test 'refute_output(): succeeds if output is empty' {
run echo ''
run refute_output
assert_test_pass
}
@test 'refute_output(): fails if output is non-empty' {
run echo 'a'
run refute_output
assert_test_fail <<'ERR_MSG'
-- output non-empty, but expected no output --
output : a
--
ERR_MSG
}
@test 'refute_output() - : reads <unexpected> from STDIN' {
run echo '-'
run refute_output - <<INPUT
b
INPUT
assert_test_pass
}
@test 'refute_output() --stdin : reads <unexpected> from STDIN' {
run echo '--stdin'
run refute_output --stdin <<INPUT
b
INPUT
assert_test_pass
}
# Output formatting
@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'
assert_test_fail <<'ERR_MSG'
-- output equals, but it was expected to differ --
output (2 lines):
a 0
a 1
--
ERR_MSG
}
# Options
@test 'refute_output() <unexpected>: performs literal matching by default' {
run echo 'a'
run refute_output '*'
assert_test_pass
}
#
# Partial matching: `-p' and `--partial'
#
# Options
@test 'refute_output() -p <partial>: enables partial matching' {
run echo 'abc'
run refute_output -p 'd'
assert_test_pass
}
@test 'refute_output() --partial <partial>: enables partial matching' {
run echo 'abc'
run refute_output --partial 'd'
assert_test_pass
}
# Correctness
@test "refute_output() --partial <partial>: returns 0 if <partial> is not a substring in \`\$output'" {
run printf 'a\nb\nc'
run refute_output --partial 'd'
assert_test_pass
}
@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'
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'
assert_test_fail <<'ERR_MSG'
-- output should not contain substring --
substring (1 lines):
a
output (2 lines):
a 0
a 1
--
ERR_MSG
}
#
# Regular expression matching: `-e' and `--regexp'
#
# Options
@test 'refute_output() -e <regexp>: enables regular expression matching' {
run echo 'abc'
run refute_output -e '^d'
assert_test_pass
}
@test 'refute_output() --regexp <regexp>: enables regular expression matching' {
run echo 'abc'
run refute_output --regexp '^d'
assert_test_pass
}
# Correctness
@test "refute_output() --regexp <regexp>: returns 0 if <regexp> does not match \`\$output'" {
run printf 'a\nb\nc'
run refute_output --regexp '.*d.*'
assert_test_pass
}
@test "refute_output() --regexp <regexp>: returns 1 and displays details if <regexp> matches \`\$output'" {
run echo 'a'
run refute_output --regexp '.*a.*'
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.*'
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 '[.*'
assert_test_fail <<'ERR_MSG'
-- ERROR: refute_output --
Invalid extended regular expression: `[.*'
--
ERR_MSG
}
#
# Common
#
@test "refute_output(): \`--partial' and \`--regexp' are mutually exclusive" {
run refute_output --partial --regexp
assert_test_fail <<'ERR_MSG'
-- ERROR: refute_output --
`--partial' and `--regexp' are mutually exclusive
--
ERR_MSG
}
@test "refute_output(): \`--' stops parsing options" {
run echo '--'
run refute_output -- '-p'
assert_test_pass
}

View File

@@ -0,0 +1,27 @@
# Load dependencies.
load "${BATS_TEST_DIRNAME}/../node_modules/bats-support/load.bash"
# Load library.
load '../load'
# validate that bats-assert is safe to use under -u
set -u
: "${status:=}"
: "${lines:=}"
: "${output:=}"
assert_test_pass() {
test "$status" -eq 0
test "${#lines[@]}" -eq 0
}
assert_test_fail() {
local err_msg="${1-$(cat -)}"
local num_lines
num_lines="$(printf '%s' "$err_msg" | wc -l)"
test "$status" -eq 1
test "${#lines[@]}" -eq "$num_lines"
test "$output" == "$err_msg"
}