framework.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/sh
  2. # Simple integration test framework
  3. set -e
  4. cleanup() {
  5. rm -f test.output test.c test.h test.tree
  6. }
  7. dumpone() {
  8. if [ -e "$@" ]; then
  9. echo "Content of $@:"
  10. cat "$@" | sed "s#^#\t#g"
  11. fi
  12. }
  13. dump() {
  14. dumpone test.output
  15. dumpone test.c
  16. dumpone test.h
  17. dumpone test.tree
  18. return 1
  19. }
  20. testsuccess() {
  21. [ "$INNER" ] || cleanup
  22. [ "$INNER" ] || echo "Testing success of $@"
  23. if ! "$@" > test.output 2>&1; then
  24. echo "ERROR: Running $@ failed with error $?, messages were:" >&2
  25. dump
  26. return 1
  27. fi
  28. }
  29. testfailure() {
  30. [ "$INNER" ] || cleanup
  31. [ "$INNER" ] || echo "Testing failure of $@"
  32. if "$@" > test.output 2>&1; then
  33. echo "ERROR: Running $@ unexpectedly succeeded, messages were:" >&2
  34. dump
  35. return 1
  36. fi
  37. }
  38. testfileequal() {
  39. [ "$INNER" ] || echo "Testing output of $2"
  40. printf "%b\n" "$1" > expected
  41. if ! diff -u "expected" "$2" > test.diff; then
  42. echo "ERROR: Differences between expected output and and $2:" >&2
  43. cat test.diff | sed "s#^#\t#g"
  44. dump
  45. return 1
  46. fi
  47. }
  48. testgrep() {
  49. [ "$INNER" ] || echo "Testing grep $@"
  50. INNER=1 testsuccess grep "$@"
  51. unset INNER
  52. }
  53. testsuccessequal() {
  54. expect="$1"
  55. shift
  56. cleanup
  57. echo "Testing success and output of $@"
  58. INNER=1 testsuccess "$@"
  59. INNER=1 testfileequal "$expect" test.output
  60. unset INNER
  61. }
  62. WORDS="Word-_0
  63. Word = 42
  64. VeryLongWord
  65. Label ~ Word2
  66. = -9"
  67. triehash() {
  68. printf "%b\n" "$WORDS" | perl -MDevel::Cover=-summary,0,-silent,1 $(dirname $(dirname $(readlink -f $0)))/triehash.pl "$@" || return $?
  69. return $?
  70. }