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.

199 lines
7.9 KiB

преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 9 години
преди 9 години
  1. #!/usr/bin/env zsh
  2. # -------------------------------------------------------------------------------------------------
  3. # Copyright (c) 2010-2017 zsh-syntax-highlighting contributors
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without modification, are permitted
  7. # provided that the following conditions are met:
  8. #
  9. # * Redistributions of source code must retain the above copyright notice, this list of conditions
  10. # and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above copyright notice, this list of
  12. # conditions and the following disclaimer in the documentation and/or other materials provided
  13. # with the distribution.
  14. # * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
  15. # may be used to endorse or promote products derived from this software without specific prior
  16. # written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  19. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  21. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  24. # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  25. # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. # -------------------------------------------------------------------------------------------------
  27. # -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
  28. # vim: ft=zsh sw=2 ts=2 et
  29. # -------------------------------------------------------------------------------------------------
  30. setopt NO_UNSET WARN_CREATE_GLOBAL
  31. # Check an highlighter was given as argument.
  32. [[ -n "$1" ]] || {
  33. echo >&2 "Bail out! You must provide the name of a valid highlighter as argument."
  34. exit 2
  35. }
  36. # Check the highlighter is valid.
  37. [[ -f ${0:h:h}/highlighters/$1/$1-highlighter.zsh ]] || {
  38. echo >&2 "Bail out! Could not find highlighter ${(qq)1}."
  39. exit 2
  40. }
  41. # Check the highlighter has test data.
  42. [[ -d ${0:h:h}/highlighters/$1/test-data ]] || {
  43. echo >&2 "Bail out! Highlighter ${(qq)1} has no test data."
  44. exit 2
  45. }
  46. # Set up results_filter
  47. local results_filter
  48. if [[ ${QUIET-} == y ]]; then
  49. if type -w perl >/dev/null; then
  50. results_filter=${0:A:h}/tap-filter
  51. else
  52. echo >&2 "Bail out! quiet mode not supported: perl not found"; exit 2
  53. fi
  54. else
  55. results_filter=cat
  56. fi
  57. [[ -n $results_filter ]] || { echo >&2 "Bail out! BUG setting \$results_filter"; exit 2 }
  58. # Load the main script.
  59. # While here, test that it doesn't eat aliases.
  60. print > >($results_filter | ${0:A:h}/tap-colorizer.zsh) -r -- "# global (driver) tests"
  61. print > >($results_filter | ${0:A:h}/tap-colorizer.zsh) -r -- "1..1"
  62. alias -- +plus=plus
  63. alias -- _other=other
  64. original_alias_dash_L_output="$(alias -L)"
  65. . ${0:h:h}/zsh-syntax-highlighting.zsh
  66. if [[ $original_alias_dash_L_output == $(alias -L) ]]; then
  67. print -r -- "ok 1 # 'alias -- +foo=bar' is preserved"
  68. else
  69. print -r -- "not ok 1 # 'alias -- +foo=bar' is preserved"
  70. exit 1
  71. fi > >($results_filter | ${0:A:h}/tap-colorizer.zsh)
  72. # Overwrite _zsh_highlight_add_highlight so we get the key itself instead of the style
  73. _zsh_highlight_add_highlight()
  74. {
  75. region_highlight+=("$1 $2 $3")
  76. }
  77. # Activate the highlighter.
  78. ZSH_HIGHLIGHT_HIGHLIGHTERS=($1)
  79. # Runs a highlighting test
  80. # $1: data file
  81. run_test_internal() {
  82. local tests_tempdir="$1"; shift
  83. local srcdir="$PWD"
  84. builtin cd -q -- "$tests_tempdir" || { echo >&2 "Bail out! On ${(qq)1}: cd failed: $?"; return 1 }
  85. echo "# ${1:t:r}"
  86. # Load the data and prepare checking it.
  87. local BUFFER CURSOR MARK PENDING PREBUFFER REGION_ACTIVE WIDGET skip_test unsorted=0
  88. local -a expected_region_highlight region_highlight
  89. . "$srcdir"/"$1"
  90. (( $#skip_test )) && { print -r -- "1..0 # SKIP $skip_test"; return; }
  91. # Check the data declares $PREBUFFER or $BUFFER.
  92. [[ -z $PREBUFFER && -z $BUFFER ]] && { echo >&2 "Bail out! On ${(qq)1}: Either 'PREBUFFER' or 'BUFFER' must be declared and non-blank"; return 1; }
  93. # Check the data declares $expected_region_highlight.
  94. (( $+expected_region_highlight == 0 )) && { echo >&2 "Bail out! On ${(qq)1}: 'expected_region_highlight' is not declared."; return 1; }
  95. # Set sane defaults for ZLE variables
  96. : ${CURSOR=$#BUFFER} ${PENDING=0} ${WIDGET=z-sy-h-test-harness-test-widget}
  97. # Process the data.
  98. _zsh_highlight
  99. if (( unsorted )); then
  100. region_highlight=("${(@n)region_highlight}")
  101. expected_region_highlight=("${(@n)expected_region_highlight}")
  102. fi
  103. echo "1..$(( $#expected_region_highlight + 1))"
  104. local i
  105. for ((i=1; i<=$#expected_region_highlight; i++)); do
  106. local -a expected_highlight_zone; expected_highlight_zone=( ${(z)expected_region_highlight[i]} )
  107. integer exp_start=$expected_highlight_zone[1] exp_end=$expected_highlight_zone[2]
  108. local todo=
  109. (( $+expected_highlight_zone[4] )) && todo="# TODO $expected_highlight_zone[4]"
  110. if ! (( $+region_highlight[i] )); then
  111. print -r -- "not ok $i - unmatched expectation ($exp_start $exp_end $expected_highlight_zone[3])"
  112. continue
  113. fi
  114. local -a highlight_zone; highlight_zone=( ${(z)region_highlight[i]} )
  115. integer start=$(( highlight_zone[1] + 1 )) end=$highlight_zone[2]
  116. # Escape # as ♯ and newline as ↵ they are illegal in the 'description' part of TAP output
  117. local desc="[$start,$end] «${${BUFFER[$start,$end]//'#'/♯}//$'\n'/↵}»"
  118. if
  119. [[ $start != $exp_start ]] ||
  120. [[ $end != $exp_end ]] ||
  121. [[ $highlight_zone[3] != $expected_highlight_zone[3] ]]
  122. then
  123. print -r -- "not ok $i - $desc - expected ($exp_start $exp_end ${(qqq)expected_highlight_zone[3]}), observed ($start $end ${(qqq)highlight_zone[3]}). $todo"
  124. else
  125. print -r -- "ok $i - $desc${todo:+ - }$todo"
  126. fi
  127. unset expected_highlight_zone
  128. unset exp_start exp_end
  129. unset todo
  130. unset highlight_zone
  131. unset start end
  132. unset desc
  133. done
  134. if (( $#expected_region_highlight == $#region_highlight )); then
  135. print -r -- "ok $i - cardinality check"
  136. else
  137. print -r -- "not ok $i - have $#expected_region_highlight expectations and $#region_highlight region_highlight entries"
  138. fi
  139. }
  140. # Run a single test file. The exit status is 1 if the test harness had
  141. # an error and 0 otherwise. The exit status does not depend on whether
  142. # test points succeeded or failed.
  143. run_test() {
  144. # Do not combine the declaration and initialization: «local x="$(false)"» does not set $?.
  145. local __tests_tempdir
  146. __tests_tempdir="$(mktemp -d)" && [[ -d $__tests_tempdir ]] || {
  147. echo >&2 "Bail out! mktemp failed"; return 1
  148. }
  149. typeset -r __tests_tempdir # don't allow tests to override the variable that we will 'rm -rf' later on
  150. {
  151. # Use a subshell to isolate tests from each other.
  152. # (So tests can alter global shell state using 'cd', 'hash', etc)
  153. {
  154. # These braces are so multios don't come into play.
  155. { (run_test_internal "$__tests_tempdir" "$@") 3>&1 >&2 2>&3 } | grep \^
  156. local ret=$pipestatus[1] stderr=$pipestatus[2]
  157. if (( ! stderr )); then
  158. # stdout will become stderr
  159. echo "Bail out! On ${(qq)1}: output on stderr"; return 1
  160. else
  161. return $ret
  162. fi
  163. } 3>&1 >&2 2>&3
  164. } always {
  165. rm -rf -- "$__tests_tempdir"
  166. }
  167. }
  168. # Process each test data file in test data directory.
  169. integer something_failed=0
  170. ZSH_HIGHLIGHT_STYLES=()
  171. for data_file in ${0:h:h}/highlighters/$1/test-data/*.zsh; do
  172. run_test "$data_file" | tee >($results_filter | ${0:A:h}/tap-colorizer.zsh) | grep -v '^not ok.*# TODO' | grep -Eq '^not ok|^ok.*# TODO' && (( something_failed=1 ))
  173. (( $pipestatus[1] )) && exit 2
  174. done
  175. exit $something_failed