Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

170 rindas
6.7 KiB

  1. #!/usr/bin/env zsh
  2. # -------------------------------------------------------------------------------------------------
  3. # Copyright (c) 2010-2016 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. # Check an highlighter was given as argument.
  31. [[ -n "$1" ]] || {
  32. echo >&2 "Bail out! You must provide the name of a valid highlighter as argument."
  33. exit 2
  34. }
  35. # Check the highlighter is valid.
  36. [[ -f ${0:h:h}/highlighters/$1/$1-highlighter.zsh ]] || {
  37. echo >&2 "Bail out! Could not find highlighter ${(qq)1}."
  38. exit 2
  39. }
  40. # Check the highlighter has test data.
  41. [[ -d ${0:h:h}/highlighters/$1/test-data ]] || {
  42. echo >&2 "Bail out! Highlighter ${(qq)1} has no test data."
  43. exit 2
  44. }
  45. # Load the main script.
  46. . ${0:h:h}/zsh-syntax-highlighting.zsh
  47. # Overwrite _zsh_highlight_add_highlight so we get the key itself instead of the style
  48. _zsh_highlight_add_highlight()
  49. {
  50. region_highlight+=("$1 $2 $3")
  51. }
  52. # Activate the highlighter.
  53. ZSH_HIGHLIGHT_HIGHLIGHTERS=($1)
  54. # Runs a highlighting test
  55. # $1: data file
  56. run_test_internal() {
  57. local tests_tempdir="$1"; shift
  58. local srcdir="$PWD"
  59. builtin cd -q -- "$tests_tempdir" || { echo >&2 "Bail out! cd failed: $?"; return 1 }
  60. echo "# ${1:t:r}"
  61. # Load the data and prepare checking it.
  62. PREBUFFER= BUFFER= ;
  63. . "$srcdir"/"$1"
  64. # Check the data declares $PREBUFFER or $BUFFER.
  65. [[ -z $PREBUFFER && -z $BUFFER ]] && { echo >&2 "Bail out! Either 'PREBUFFER' or 'BUFFER' must be declared and non-blank"; return 1; }
  66. # Check the data declares $expected_region_highlight.
  67. (( ${#expected_region_highlight} == 0 )) && { echo >&2 "Bail out! 'expected_region_highlight' is not declared or empty."; return 1; }
  68. # Process the data.
  69. region_highlight=()
  70. _zsh_highlight
  71. # Overlapping regions can be declared in region_highlight, so we first build an array of the
  72. # observed highlighting.
  73. local -A observed_result
  74. for ((i=1; i<=${#region_highlight}; i++)); do
  75. local -a highlight_zone; highlight_zone=( ${(z)region_highlight[$i]} )
  76. integer start=$highlight_zone[1] end=$highlight_zone[2]
  77. if (( start < end )) # region_highlight ranges are half-open
  78. then
  79. (( --end )) # convert to closed range, like expected_region_highlight
  80. (( ++start, ++end )) # region_highlight is 0-indexed; expected_region_highlight is 1-indexed
  81. for j in {$start..$end}; do
  82. observed_result[$j]=$highlight_zone[3]
  83. done
  84. else
  85. # noop range; ignore.
  86. fi
  87. unset start end
  88. unset highlight_zone
  89. done
  90. # Then we compare the observed result with the expected one.
  91. echo "1..${#expected_region_highlight}"
  92. for ((i=1; i<=${#expected_region_highlight}; i++)); do
  93. local -a highlight_zone; highlight_zone=( ${(z)expected_region_highlight[$i]} )
  94. local todo=
  95. integer start=$highlight_zone[1] end=$highlight_zone[2]
  96. # Escape # as ♯ since the former is illegal in the 'description' part of TAP output
  97. local desc="[$start,$end] «${BUFFER[$start,$end]//'#'/♯}»"
  98. [[ $highlight_zone[3] == NONE ]] && highlight_zone[3]=
  99. [[ -n "$highlight_zone[4]" ]] && todo=" # TODO $highlight_zone[4]"
  100. for j in {$start..$end}; do
  101. if [[ "$observed_result[$j]" != "$highlight_zone[3]" ]]; then
  102. print -r -- "not ok $i - $desc - expected ${(qqq)highlight_zone[3]}, observed ${(qqq)observed_result[$j]}.$todo"
  103. continue 2
  104. fi
  105. done
  106. print -r -- "ok $i - $desc${todo:+' - '}$todo"
  107. unset desc
  108. unset start end
  109. unset todo
  110. unset highlight_zone
  111. done
  112. }
  113. # Run a single test file. The exit status is 1 if the test harness had
  114. # an error and 0 otherwise. The exit status does not depend on whether
  115. # test points succeeded or failed.
  116. run_test() {
  117. # Do not combine the declaration and initialization: «local x="$(false)"» does not set $?.
  118. local __tests_tempdir
  119. __tests_tempdir="$(mktemp -d)" && [[ -d $__tests_tempdir ]] || {
  120. echo >&2 "Bail out! mktemp failed"; return 1
  121. }
  122. typeset -r __tests_tempdir # don't allow tests to override the variable that we will 'rm -rf' later on
  123. {
  124. # Use a subshell to isolate tests from each other.
  125. # (So tests can alter global shell state using 'cd', 'hash', etc)
  126. (run_test_internal "$__tests_tempdir" "$@")
  127. } always {
  128. rm -rf -- "$__tests_tempdir"
  129. }
  130. }
  131. # Set up results_filter
  132. local results_filter
  133. if [[ $QUIET == y ]]; then
  134. if type -w perl >/dev/null; then
  135. results_filter=${0:A:h}/tap-filter
  136. else
  137. echo >&2 "Bail out! quiet mode not supported: perl not found"; exit 2
  138. fi
  139. else
  140. results_filter=cat
  141. fi
  142. [[ -n $results_filter ]] || { echo >&2 "Bail out! BUG setting \$results_filter"; exit 2 }
  143. # Process each test data file in test data directory.
  144. integer something_failed=0
  145. ZSH_HIGHLIGHT_STYLES=()
  146. for data_file in ${0:h:h}/highlighters/$1/test-data/*.zsh; do
  147. 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 ))
  148. (( $pipestatus[1] )) && exit 2
  149. done
  150. exit $something_failed