#!/bin/bash

# Always run the tests from this script's folder
cd "$(dirname "$0")" || exit 1

# Read default configuration
# shellcheck source=test/_include.sh
source ../_include.sh

# Multiple tests from the BSD sed, adapted for GNU sed
test_message "Testing bsd.sh"
./bsd.sh "$sedsed --debug --_stdout-only" bsd.good || failed=1

# Test all the individual sed files
for ok_file in *.good
do
    base=${ok_file%.good}
    input_file=$base.inp
    script_file=$base.sed

    # Skip some files from testing
    case $ok_file in
        8bit.good)
            # UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc2 in
            # position 674: invalid continuation byte
            continue
        ;;
        badenc.good)
            # Bad encoding breaks sedsed --_stdout-only
            continue
        ;;
        bsd.good)
            # Already done
            continue
        ;;
        dc.good)
            # Travis CI always fails this test, skip it
            test -n "${TRAVIS:-}" && continue
        ;;
        eval.good)
            # Calls hardcoded "../sed/sed" command
            continue
        ;;
        insert.good)
            # Bug: sedsed can't handle i\ at EOF
            continue
        ;;
        mac-mf.good)
            # Non-UTF-8 file that breaks my GNU sed in Mac OS X
            # sed: file mac-mf.sed line 152: unterminated `s' command
            continue
        ;;
        recall.good)
            # Bug: sedsed can't maintain // state in this example (issue #15)
            continue
        ;;
        subwrt[12].good)
            # ERROR: Cannot read file: subwrt1.sed
            continue
        ;;
        noeol.good)
            # No EOL outputs can't be matched when using sedsed --_stdout-only
            continue
        ;;
        noeolw.good)
            # No EOL outputs can't be matched when using sedsed --_stdout-only
            input_file=noeol.inp
            continue
        ;;
        noeolw.[12]good)
            # Files created by the 'w' command
            continue
        ;;
        wrtout[12].good)
            # ERROR: Cannot read file: wrtout1.sed
            continue
        ;;
    esac

    test_message "Testing $script_file"
    $sedsed --debug --_stdout-only -f "$script_file" "$input_file" > "$ok_file" || failed=1
done

tests_git_status
tests_exit
