#!/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' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test "refute_line() : returns 1 and displays details if 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]}" == '--' ] } # 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' [ "$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]}" == '--' ] } # Options @test 'refute_line() : performs literal matching by default' { run echo 'a' run refute_line '*' [ "$status" -eq 0 ] } # # Partial matching: `-p' and `--partial' # # Options test_p_partial () { run printf 'a\nb\nc' run refute_line "$1" 'd' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test 'refute_line() -p : enables partial matching' { test_p_partial -p } @test 'refute_line() --partial : enables partial matching' { test_p_partial --partial } # 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' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @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' [ "$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]}" == '--' ] } # 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' [ "$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]}" == '--' ] } # # Regular expression matching: `-e' and `--regexp' # # Options test_r_regexp () { run printf 'a\nb\nc' run refute_line "$1" '^.d' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test 'refute_line() -e : enables regular expression matching' { test_r_regexp -e } @test 'refute_line() --regexp : enables regular expression matching' { test_r_regexp --regexp } # 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.*' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test "refute_line() --regexp : returns 1 and displays details if 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]}" == '--' ] } # 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.*' [ "$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]}" == '--' ] } ############################################################################### # Matching single line: `-n' and `--index' ############################################################################### # Options test_n_index () { run printf 'a\nb\nc' run refute_line "$1" 1 'd' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test 'refute_line() -n : matches against the -th line only' { test_n_index -n } @test 'refute_line() --index : matches against the -th line only' { test_n_index --index } @test 'refute_line() --index : returns 1 and displays an error message if 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]}" == '--' ] } # # 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' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test "refute_line() --index : returns 1 and displays details if equals \`\${lines[]}'" { 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]}" == '--' ] } # Options @test 'refute_line() --index : performs literal matching by default' { run printf 'a\nb\nc' run refute_line --index 1 '*' [ "$status" -eq 0 ] } # # Partial matching: `-p' and `--partial' # # Options test_index_p_partial () { run printf 'a\nb\nc' run refute_line --index 1 "$1" 'd' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test 'refute_line() --index -p : enables partial matching' { test_index_p_partial -p } @test 'refute_line() --index --partial : enables partial matching' { test_index_p_partial --partial } # 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' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @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' [ "$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]}" == '--' ] } # # Regular expression matching: `-e' and `--regexp' # # Options test_index_r_regexp () { run printf 'a\nb\nc' run refute_line --index 1 "$1" '^.b' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test 'refute_line() --index -e : enables regular expression matching' { test_index_r_regexp -e } @test 'refute_line() --index --regexp : enables regular expression matching' { test_index_r_regexp --regexp } # 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.*' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @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.*' [ "$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]}" == '--' ] } ############################################################################### # Common ############################################################################### @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]}" == '--' ] } @test 'refute_line() --regexp : returns 1 and displays an error message if 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]}" == '--' ] } @test "refute_line(): \`--' stops parsing options" { run printf 'a\n--\nc' run refute_line -- '-p' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] }