This commit is contained in:
2026-08-02 15:26:07 +02:00
parent 336b36c8e1
commit b2eb145a6f
415 changed files with 24264 additions and 36 deletions
+19
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
}
+62
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
}
+75
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
}
+351
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
}
@@ -0,0 +1,57 @@
#!/usr/bin/env bats
load test_helper
@test 'assert_not_equal() <actual> <unexpected>: returns 0 if <actual> does not equal <unexpected>' {
run assert_not_equal foo bar
assert_test_pass
run assert_not_equal "foo" "bar"
assert_test_pass
run assert_not_equal "foo" ""
assert_test_pass
run assert_not_equal "" "foo"
assert_test_pass
}
@test 'assert_not_equal() <actual> <unexpected>: returns 1 and displays details if <actual> equals <unexpected>' {
run assert_not_equal 'foobar' 'foobar'
assert_test_fail <<'ERR_MSG'
-- values should not be equal --
unexpected : foobar
actual : foobar
--
ERR_MSG
run assert_not_equal 1 1
assert_test_fail <<'ERR_MSG'
-- values should not be equal --
unexpected : 1
actual : 1
--
ERR_MSG
}
@test 'assert_not_equal() <actual> <unexpected>: displays details in multi-line format if <actual> and <unexpected> are longer than one line' {
run assert_not_equal $'foo\nbar' $'foo\nbar'
assert_test_fail <<'ERR_MSG'
-- values should not be equal --
unexpected (2 lines):
foo
bar
actual (2 lines):
foo
bar
--
ERR_MSG
}
@test 'assert_not_equal() <actual> <unexpected>: performs literal matching' {
run assert_not_equal 'a' '*'
assert_test_pass
}
+285
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
}
@@ -0,0 +1,87 @@
#!/usr/bin/env bats
load test_helper
#
# Literal matching
#
# Correctness
@test "assert_regex() <value> <pattern>: succeeds if a <value> substring matches extended regular expression <pattern>" {
run assert_regex 'abc' '^[a-z]b[c-z]+'
assert_test_pass
}
@test "assert_regex() <value> <pattern>: fails if no <value> substring matches extended regular expression <pattern>" {
run assert_regex 'bcd' '^[a-z]b[c-z]+'
assert_test_fail <<'ERR_MSG'
-- value does not match regular expression --
value : bcd
pattern : ^[a-z]b[c-z]+
case : sensitive
--
ERR_MSG
}
@test "assert_regex() <value> <pattern>: provides results in BASH_REMATCH" {
unset -v BASH_REMATCH
assert_regex 'abcd' 'b.d'
declare -p BASH_REMATCH
[ "${BASH_REMATCH[0]}" = 'bcd' ]
}
@test "assert_regex() <value> <pattern>: matches case-insensitively when 'nocasematch' is set" {
shopt -s nocasematch
assert_regex 'aBc' 'ABC'
}
@test "assert_regex() <value> <pattern>: outputs multi-line <value> nicely when it fails" {
run assert_regex $'bcd\n123' '^[a-z]b[c-z]+'
assert_test_fail <<'ERR_MSG'
-- value does not match regular expression --
value (2 lines):
bcd
123
pattern (1 lines):
^[a-z]b[c-z]+
case (1 lines):
sensitive
--
ERR_MSG
shopt -s nocasematch
run assert_regex $'bcd\n123' '^[a-z]b[c-z]+'
assert_test_fail <<'ERR_MSG'
-- value does not match regular expression --
value (2 lines):
bcd
123
pattern (1 lines):
^[a-z]b[c-z]+
case (1 lines):
insensitive
--
ERR_MSG
}
# Error handling
@test "assert_regex() <value> <pattern>: returns 1 and displays an error message if <pattern> is not a valid extended regular expression" {
run assert_regex value '[.*'
assert_test_fail <<'ERR_MSG'
-- ERROR: assert_regex --
Invalid extended regular expression: `[.*'
--
ERR_MSG
}
@test "assert_regex allows regex matching empty string (see #53)" {
run assert_regex any_value '.*'
assert_success
}
+40
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
}
+18
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
}
+344
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
}
+230
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
}
@@ -0,0 +1,98 @@
#!/usr/bin/env bats
load test_helper
#
# Literal matching
#
# Correctness
@test "refute_regex() <value> <pattern>: fails if a <value> substring matches extended regular expression <pattern>" {
run refute_regex 'abc' '^[a-z]b'
assert_test_fail <<'ERR_MSG'
-- value matches regular expression --
value : abc
pattern : ^[a-z]b
match : ab
case : sensitive
--
ERR_MSG
}
@test "refute_regex() <value> <pattern>: succeeds if no <value> substring matches extended regular expression <pattern>" {
run refute_regex 'bcd' '^[a-z]b[c-z]+'
assert_test_pass
}
@test "refute_regex() <value> <pattern>: provides results in BASH_REMATCH on failure" {
unset -v BASH_REMATCH
refute_regex 'abcd' 'b.d' \
|| {
declare -p BASH_REMATCH && \
[ "${BASH_REMATCH[0]}" = 'bcd' ]
}
}
@test "refute_regex() <value> <pattern>: matches case-insensitively when 'nocasematch' is set" {
shopt -s nocasematch
run refute_regex 'aBc' 'ABC'
assert_test_fail <<'ERR_MSG'
-- value matches regular expression --
value : aBc
pattern : ABC
match : aBc
case : insensitive
--
ERR_MSG
}
@test "refute_regex() <value> <pattern>: outputs multi-line <value> nicely when it fails" {
run refute_regex $'abc\n123' '^[a-z]b[c-z]+'
assert_test_fail <<'ERR_MSG'
-- value matches regular expression --
value (2 lines):
abc
123
pattern (1 lines):
^[a-z]b[c-z]+
match (1 lines):
abc
case (1 lines):
sensitive
--
ERR_MSG
shopt -s nocasematch
run refute_regex $'aBc\n123' '^[a-z]b[c-z]+'
assert_test_fail <<'ERR_MSG'
-- value matches regular expression --
value (2 lines):
aBc
123
pattern (1 lines):
^[a-z]b[c-z]+
match (1 lines):
aBc
case (1 lines):
insensitive
--
ERR_MSG
}
# Error handling
@test "refute_regex() <value> <pattern>: returns 1 and displays an error message if <pattern> is not a valid extended regular expression" {
run refute_regex value '[.*'
assert_test_fail <<'ERR_MSG'
-- ERROR: refute_regex --
Invalid extended regular expression: `[.*'
--
ERR_MSG
}
@@ -0,0 +1,28 @@
# Load dependencies.
BATS_LIB_PATH=$PWD/node_modules:${BATS_LIB_PATH-}
bats_load_library 'bats-support'
# 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"
}