| 1 | #!/bin/bash |
| 2 | |
| 3 | set -u |
| 4 | |
| 5 | step=all |
| 6 | mode=nob-swe |
| 7 | |
| 8 | cd "$(dirname "$0")"/.. || exit 1 |
| 9 | |
| 10 | difs () { |
| 11 | ref=$1 |
| 12 | declare -i errs=0 |
| 13 | for f in tests/*.input.txt; do |
| 14 | name="$(basename "${f%.input.txt}")" |
| 15 | [[ ${name} = *${namefilter}* ]] || continue |
| 16 | dif1 "${ref}" "${name}" || (( errs++ )) |
| 17 | done |
| 18 | return ${errs} |
| 19 | } |
| 20 | wdif () { |
| 21 | colorspec=$1 |
| 22 | if command -V dwdiff &>/dev/null; then |
| 23 | dwdiff --color="${colorspec}" --diff-input |
| 24 | else |
| 25 | cat |
| 26 | fi |
| 27 | } |
| 28 | dif1 () { |
| 29 | ref=$1 |
| 30 | name=$2 |
| 31 | case ${ref} in |
| 32 | expected) colorspec=green,bcyan |
| 33 | reffile=tests/out/"${name}.${step}".expected.txt ;; |
| 34 | gold) colorspec=brown,bcyan |
| 35 | reffile=tests/"${name}.${step}".gold.txt ;; |
| 36 | *) |
| 37 | echo "ERROR bug in call to dif1" >&2; exit 1 ;; |
| 38 | esac |
| 39 | ( |
| 40 | set -o pipefail |
| 41 | diff -U0 -I'^swe-nor\|AP_SRC' \ |
| 42 | "${reffile}" \ |
| 43 | tests/out/"${name}"."${step}".current.txt \ |
| 44 | | wdif "${colorspec}" |
| 45 | ) |
| 46 | } |
| 47 | |
| 48 | assert-translated () { |
| 49 | for f in tests/*.input.txt; do |
| 50 | name="$(basename "${f%.input.txt}")" |
| 51 | [[ ${name} = *${namefilter}* ]] || continue |
| 52 | cur=tests/out/"${name}"."${step}".current.txt |
| 53 | if [[ ! -f "${cur}" ]]; then |
| 54 | echo "${cur} not found" >&2 |
| 55 | echo >&2 |
| 56 | echo "Please run '$0 translate' first" >&2 |
| 57 | exit 1 |
| 58 | fi |
| 59 | done |
| 60 | } |
| 61 | |
| 62 | assert-usage () { |
| 63 | echo "USAGE: |
| 64 | |
| 65 | $0 translate # run this after your edits and compiles |
| 66 | $0 diff-expected # compare result of 'translate' with expected output |
| 67 | $0 diff-gold # compare result of 'translate' with gold set |
| 68 | $0 update # update the current expected output with result of 'translate' |
| 69 | $0 expect # add sentence from stdin as new test with current as expected |
| 70 | " |
| 71 | exit 1 |
| 72 | } |
| 73 | |
| 74 | |
| 75 | [[ $# -lt 1 || $# -gt 2 ]] && assert-usage |
| 76 | |
| 77 | namefilter='' |
| 78 | [[ $# -ge 2 ]] && namefilter=$2 |
| 79 | |
| 80 | case $1 in |
| 81 | translate) |
| 82 | for f in tests/*.input.txt; do |
| 83 | name="$(basename "${f%.input.txt}")" |
| 84 | [[ ${name} = *${namefilter}* ]] || continue |
| 85 | apertium -d . "${mode}" < tests/"${name}".input.txt >tests/out/"${name}.${step}".current.txt |
| 86 | done |
| 87 | ;; |
| 88 | update) |
| 89 | assert-translated "${namefilter}" |
| 90 | for cur in tests/out/*."${step}".current.txt; do |
| 91 | cp -v "${cur}" "${cur%current.txt}expected.txt" |
| 92 | done |
| 93 | ;; |
| 94 | diff-expected) |
| 95 | assert-translated "${namefilter}" |
| 96 | difs expected "${namefilter}" |
| 97 | ;; |
| 98 | diff-gold) |
| 99 | assert-translated "${namefilter}" |
| 100 | difs gold "${namefilter}" |
| 101 | ;; |
| 102 | expect) |
| 103 | if [[ -z "${namefilter}" ]]; then echo "Needs a test set, e.g. 'ymse'"; exit 1; fi |
| 104 | inputf=tests/"${namefilter}"-${mode}.input.txt |
| 105 | expf=tests/out/"${namefilter}"-${mode}.all.expected.txt |
| 106 | goldf=tests/"${namefilter}"-${mode}.all.gold.txt |
| 107 | if [[ ! -e "${inputf}" ]]; then echo "Unknown test set '${namefilter}'"; echo "(couldn't find file ${inputf} – try 'ymse' if unsure)"; exit 1; fi |
| 108 | input=$(cat) |
| 109 | [[ "${input}" = *[.?\!] ]] || input="${input}." # ensure final punctuation |
| 110 | echo "${input}" | tee -a "${inputf}" | apertium -d . "${mode}" | tee -a "${expf}" "${goldf}" |
| 111 | ;; |
| 112 | *) |
| 113 | assert-usage |
| 114 | ;; |
| 115 | esac |