#!/usr/bin/env bats load test_helper ############################################################################### # Containing a line ############################################################################### # # Literal matching # # Correctness @test "refute_line() : returns 0 if is not a line in \`\${lines[@]}'" { run printf 'a\nb\nc' run refute_line 'd' assert_test_pass } @test "refute_line() : returns 1 and displays details if 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() : 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() : performs literal matching by default' { run echo 'a' run refute_line '*' assert_test_pass } # # Partial matching: `-p' and `--partial' # # Options @test 'refute_line() -p : enables partial matching' { run printf 'a\nb\nc' run refute_line -p 'd' assert_test_pass } @test 'refute_line() --partial : enables partial matching' { run printf 'a\nb\nc' run refute_line --partial 'd' assert_test_pass } # Correctness @test "refute_line() --partial : returns 0 if 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 : returns 1 and displays details if 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 : 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 : enables regular expression matching' { run printf 'a\nb\nc' run refute_line -e '^.d' assert_test_pass } @test 'refute_line() --regexp : enables regular expression matching' { run printf 'a\nb\nc' run refute_line --regexp '^.d' assert_test_pass } # Correctness @test "refute_line() --regexp : returns 0 if does not match any line in \`\${lines[@]}'" { run printf 'a\nb\nc' run refute_line --regexp '.*d.*' assert_test_pass } @test "refute_line() --regexp : returns 1 and displays details if 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 : 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 : matches against the -th line only' { run printf 'a\nb\nc' run refute_line -n 1 'd' assert_test_pass } @test 'refute_line() --index : matches against the -th line only' { run printf 'a\nb\nc' run refute_line --index 1 'd' assert_test_pass } @test 'refute_line() --index : returns 1 and displays an error message if 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 : returns 0 if does not equal \`\${lines[]}'" { run printf 'a\nb\nc' run refute_line --index 1 'd' assert_test_pass } @test "refute_line() --index : returns 1 and displays details if equals \`\${lines[]}'" { 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 : 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 -p : enables partial matching' { run printf 'a\nb\nc' run refute_line --index 1 -p 'd' assert_test_pass } @test 'refute_line() --index --partial : enables partial matching' { run printf 'a\nb\nc' run refute_line --index 1 --partial 'd' assert_test_pass } # Correctness @test "refute_line() --index --partial : returns 0 if is not a substring in \`\${lines[]}'" { run printf 'a\nabc\nc' run refute_line --index 1 --partial 'd' assert_test_pass } @test "refute_line() --index --partial : returns 1 and displays details if is a substring in \`\${lines[]}'" { 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 -e : enables regular expression matching' { run printf 'a\nb\nc' run refute_line --index 1 -e '^.b' assert_test_pass } @test 'refute_line() --index --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 --regexp : returns 0 if does not match \`\${lines[]}'" { run printf 'a\nabc\nc' run refute_line --index 1 --regexp '.*d.*' assert_test_pass } @test "refute_line() --index --regexp : returns 1 and displays details if matches \`\${lines[]}'" { 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 : returns 1 and displays an error message if 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 }