#!/usr/bin/env bats load test_helper # # Literal matching # # Correctness @test "refute_output() : returns 0 if does not equal \`\$output'" { run echo 'b' run refute_output 'a' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test "refute_output() : returns 1 and displays details if 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]}" == '--' ] } @test 'refute_output(): reads from STDIN' { run echo 'a' run refute_output <: 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]}" == '--' ] } # Options @test 'refute_output() : performs literal matching by default' { run echo 'a' run refute_output '*' [ "$status" -eq 0 ] } # # Partial matching: `-p' and `--partial' # # Options test_p_partial () { run echo 'abc' run refute_output "$1" 'd' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test 'refute_output() -p : enables partial matching' { test_p_partial -p } @test 'refute_output() --partial : enables partial matching' { test_p_partial --partial } # Correctness @test "refute_output() --partial : returns 0 if is not a substring in \`\$output'" { run printf 'a\nb\nc' run refute_output --partial 'd' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test "refute_output() --partial : returns 1 and displays details if 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]}" == '--' ] } # Output formatting @test 'refute_output() --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]}" == '--' ] } # # Regular expression matching: `-e' and `--regexp' # # Options test_r_regexp () { run echo 'abc' run refute_output "$1" '^d' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test 'refute_output() -e : enables regular expression matching' { test_r_regexp -e } @test 'refute_output() --regexp : enables regular expression matching' { test_r_regexp --regexp } # Correctness @test "refute_output() --regexp : returns 0 if does not match \`\$output'" { run printf 'a\nb\nc' run refute_output --regexp '.*d.*' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] } @test "refute_output() --regexp : returns 1 and displays details if 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]}" == '--' ] } # Output formatting @test 'refute_output() --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]}" == '--' ] } # Error handling @test 'refute_output() --regexp : returns 1 and displays an error message if 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]}" == '--' ] } # # Common # @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]}" == '--' ] } @test "refute_output(): \`--' stops parsing options" { run echo '--' run refute_output -- '-p' [ "$status" -eq 0 ] [ "${#lines[@]}" -eq 0 ] }