#!/bin/sh

GREEN='\033[32m'
RED='\033[31m'
RESET='\033[0m'

trim() {
  printf '%s' "$1" | tr -d '\000-\037\177'
}

tests() {
  name=$1; shift
  expected=$1; shift

  out=$("$@" 2>&1)
  code=$?

  out=$(trim "$out")

  if [ $code -ne 0 ] || [ "$out" != "$expected" ]; then
    printf "$RED FAIL$RESET %s:\n" "$name"
    printf '  expected: %s\n  got: %s\n' "$expected" "$out" 
    return 1
  else
    printf "$GREEN PASS$RESET %s: %s\n" "$name" "$expected"
    return 0
  fi
}

tests "add.ul" "2" ./cli ./test/add.ul
tests "local.ul" "90" ./cli ./test/local.ul
tests "global.ul" "90" ./cli ./test/global.ul
tests "hello.ul" "nuqneH 'u'?" ./cli ./test/hello.ul
tests "bang.ul" "flag is false" ./cli ./test/bang.ul
tests "if.ul" "x is 20" ./cli ./test/if.ul
tests "while.ul" "0123456789" ./cli ./test/while.ul
tests "fib.ul" "28657" ./cli ./test/fib.ul
tests "attack.ul" "102 damage inflicted!" ./cli ./test/attack.ul
