You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
3.7 KiB

  1. # -*- mode: shell-script -*-
  2. exname="$(basename $0)"
  3. if [ -t 1 ]; then
  4. GRAY=$(echo -en "\e[1;30m")
  5. RED=$(echo -en "\e[1;31m")
  6. GREEN=$(echo -en "\e[1;32m")
  7. YELLOW=$(echo -en "\e[1;33m")
  8. BLUE=$(echo -en "\e[1;34m")
  9. PINK=$(echo -en "\e[1;35m")
  10. CYAN=$(echo -en "\e[1;36m")
  11. WHITE=$(echo -en "\e[1;37m")
  12. DARKGRAY=$(echo -en "\e[0;30m")
  13. DARKRED=$(echo -en "\e[0;31m")
  14. DARKGREEN=$(echo -en "\e[0;32m")
  15. DARKYELLOW=$(echo -en "\e[0;33m")
  16. DARKBLUE=$(echo -en "\e[0;34m")
  17. DARKPINK=$(echo -en "\e[0;35m")
  18. DARKCYAN=$(echo -en "\e[0;36m")
  19. NORMAL=$(echo -en "\e[0m")
  20. fi
  21. function out() { cat "$tmp_out"; }
  22. function err() { cat "$tmp_err"; }
  23. function errlvl() { cat "$tmp_errlvl"; }
  24. function var() { echo "${$1}"; }
  25. function time_note() {
  26. echo "scale=1 ; l($1 - $empty_try_time) / l(10)" | bc -l
  27. }
  28. function swallow_last_time() {
  29. if test "$sum_time" == "0" -a -z "$cmd"; then ## catches first empty try ''
  30. empty_try_time="$(echo "scale=0 ; $time_diff / 2" | bc -l )"
  31. return 0
  32. fi
  33. test -z "$test_counter" && test_counter=0 || test_counter=$[$test_counter + 1]
  34. test -z "$sum_time" && sum_time=0
  35. test_name=${exname}_${test_counter}
  36. if test "$time_diff"; then
  37. test_time_note=$(time_note $time_diff)
  38. profiler_info="$(echo -en "$profiler_info\n- $test_name\t$test_time_note")"
  39. sum_time=$(echo "scale=3; $sum_time + $time_diff" | bc -l )
  40. fi
  41. }
  42. function time_exec() {
  43. beg_exec=$(date +%s.%N)
  44. ( echo "$*" | bash )
  45. errorlevel=$?
  46. end_exec=$(date +%s.%N)
  47. time_diff="$(echo "scale=3; ($end_exec - $beg_exec)*1000000" | bc | cut -f 1 -d ".")"
  48. return $errorlevel
  49. }
  50. function try() {
  51. swallow_last_time
  52. cmd="$*"
  53. desc=$(echo ; echo "$ $cmd" )
  54. time_exec "$prefix_cmd$cmd" 1> "$tmp_out" 2> "$tmp_err"
  55. echo $? > "$tmp_errlvl"
  56. }
  57. function apply_opt() {
  58. code=$(cat -)
  59. for opt in $*; do
  60. code=$(echo "$code" | $opt)
  61. done
  62. echo "$code"
  63. }
  64. function NOCOLOR() {
  65. esc_char=$(echo -en "\e")
  66. cat - | sed -r "s/$esc_char\[[0-9]+(;[0-9]+)*m//g"
  67. }
  68. function NOPOS() {
  69. esc_char=$(echo -en "\e\\[[0-9]\\+[GA]")
  70. cat - | sed "s/$esc_char//g"
  71. }
  72. function TRIM() {
  73. cat - | sed -r "s/^ +//g" | sed -r "s/ +\$//g"
  74. }
  75. function RTRIM() {
  76. cat - | sed -r "s/ +\$//g"
  77. }
  78. function SIZE() {
  79. cat - | wc -c
  80. }
  81. ## usage:
  82. ## is ACTION [reg] CODE [OPTION ...]
  83. is() {
  84. local act="$1" type code msg
  85. test -z "$total" && total=0
  86. shift
  87. case "$1" in
  88. reg|part)
  89. type="$1"
  90. shift
  91. ;;
  92. *)
  93. type=""
  94. ;;
  95. esac
  96. code="$1"
  97. shift
  98. #code=$(echo "$code" | apply_opt $*)
  99. msg=$(echo "$type $code" | cut -c -30)
  100. output=$($act | apply_opt $*)
  101. case "$type" in
  102. "")
  103. test "$code" == "$output"
  104. ;;
  105. "part")
  106. [[ "$output" == *"$code"* ]]
  107. ;;
  108. ("reg")
  109. echo -n "$output" | egrep -- "$code" >/dev/null 2>&1
  110. ;;
  111. esac && total=$[$total + 1] &&
  112. echo "[v] is $act $msg" >/dev/null && return 0
  113. echo "$desc"
  114. echo "[ ] is $act $msg"
  115. echo "--- $*"
  116. echo -n "$output"
  117. echo
  118. echo "--- DIFF"
  119. diff -u <(echo "$code") <(echo "$output") | egrep -v '^(---|\+\+\+) /'
  120. exit 1
  121. }
  122. function summary() {
  123. swallow_last_time
  124. echo "$profiler_info"
  125. echo
  126. echo "$total tests conducted in $(echo "scale=3;$sum_time/1000000" | bc) s ($(time_note $sum_time))"
  127. }
  128. function noerror() {
  129. is err ''
  130. is errlvl 0
  131. }
  132. pid=$$
  133. tmp_dir="/tmp"
  134. tmp_out="$tmp_dir/test.$pid.out.tmp"
  135. tmp_err="$tmp_dir/test.$pid.err.tmp"
  136. tmp_errlvl="$tmp_dir/test.$pid.errlvl.tmp"
  137. try ''
  138. try ''
  139. try ''