#!/usr/bin/env bash

set -e

function usage {
  cat <<-EOF
USAGE

  $0  [-dgGhv] [-f focus-string] [-s seed]

OPTIONS

  -h  print this message

  -a  Compile C code with the Clang static analyzer

  -l  run only the corpus tests for the given language

  -e  run only the corpus tests whose name contain the given string

  -s  set the seed used to control random behavior

  -d  print parsing log to stderr

  -D  pipe tests' stderr to \`dot(1)\` to render an SVG log

EOF
}

export TREE_SITTER_TEST=1
export RUST_TEST_THREADS=1
export RUST_BACKTRACE=full

while getopts "bdl:e:s:gGhpvD" option; do
  case ${option} in
    h)
      usage
      exit
      ;;
    l)
      export TREE_SITTER_TEST_LANGUAGE_FILTER=${OPTARG}
      ;;
    e)
      export TREE_SITTER_TEST_EXAMPLE_FILTER=${OPTARG}
      ;;
    s)
      export TREE_SITTER_SEED=${OPTARG}
      ;;
    d)
      export TREE_SITTER_ENABLE_LOG=1
      ;;
    D)
      export TREE_SITTER_ENABLE_LOG_GRAPHS=1
      ;;
  esac
done

if [[ -n $TREE_SITTER_TEST_LANGUAGE_FILTER || -n $TREE_SITTER_TEST_EXAMPLE_FILTER ]]; then
  top_level_filter=corpus
else
  top_level_filter=$1
fi

cargo test --jobs 1 $top_level_filter -- --nocapture
