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.

118 regels
3.5 KiB

8 jaren geleden
8 jaren geleden
  1. zsh-syntax-highlighting / tests
  2. ===============================
  3. Utility scripts for testing zsh-syntax-highlighting highlighters.
  4. The tests harness expects the highlighter directory to contain a `test-data`
  5. directory with test data files.
  6. See the [main highlighter](../highlighters/main/test-data) for examples.
  7. Tests should set the following variables:
  8. 1.
  9. Each test should define the string `$BUFFER` that is to be highlighted and the
  10. array parameter `$expected_region_highlight`.
  11. The value of that parameter is a list of strings of the form `"$i $j $style"`.
  12. or `"$i $j $style $todo"`.
  13. Each string specifies the highlighting that `$BUFFER[$i,$j]` should have;
  14. that is, `$i` and `$j` specify a range, 1-indexed, inclusive of both endpoints.
  15. `$style` is a key of `$ZSH_HIGHLIGHT_STYLES`.
  16. If `$todo` exists, the test point is marked as TODO (the failure of that test
  17. point will not fail the test), and `$todo` is used as the explanation.
  18. 2.
  19. If a test sets `$skip_test` to a non-empty string, the test will be skipped
  20. with the provided string as the reason.
  21. 3.
  22. If a test sets `unsorted=1` the order of highlights in `$expected_region_highlight`
  23. need not match the order in `$region_highlight`.
  24. 4.
  25. Normally, tests fail if `$expected_region_highlight` and `$region_highlight`
  26. have different numbers of elements. Tests may set `$expected_mismatch` to an
  27. explanation string (like `$todo`) to avoid this and mark the cardinality check
  28. as todo.
  29. **Note**: `$region_highlight` uses the same `"$i $j $style"` syntax but
  30. interprets the indexes differently.
  31. **Note**: Tests are run with `setopt NOUNSET WARN_CREATE_GLOBAL`, so any
  32. variables the test creates must be declared local.
  33. **Isolation**: Each test is run in a separate subshell, so any variables,
  34. aliases, functions, etc., it defines will be visible to the tested code (that
  35. computes `$region_highlight`), but will not affect subsequent tests. The
  36. current working directory of tests is set to a newly-created empty directory,
  37. which is automatically cleaned up after the test exits. For example:
  38. ```zsh
  39. setopt PATH_DIRS
  40. mkdir -p foo/bar
  41. touch foo/bar/testing-issue-228
  42. chmod +x foo/bar/testing-issue-228
  43. path+=( "$PWD"/foo )
  44. BUFFER='bar/testing-issue-228'
  45. expected_region_highlight=(
  46. "1 21 command" # bar/testing-issue-228
  47. )
  48. ```
  49. Writing new tests
  50. -----------------
  51. An experimental tool is available to generate test files:
  52. ```zsh
  53. zsh -f tests/generate.zsh 'ls -x' acme newfile
  54. ```
  55. This generates a `highlighters/acme/test-data/newfile.zsh` test file based on
  56. the current highlighting of the given `$BUFFER` (in this case, `ls -x`).
  57. _This tool is experimental._ Its interface may change. In particular it may
  58. grow ways to set `$PREBUFFER` to inject free-form code into the generated file.
  59. Highlighting test
  60. -----------------
  61. [`test-highlighting.zsh`](tests/test-highlighting.zsh) tests the correctness of
  62. the highlighting. Usage:
  63. ```zsh
  64. zsh test-highlighting.zsh <HIGHLIGHTER NAME>
  65. ```
  66. All tests may be run with
  67. ```zsh
  68. make test
  69. ```
  70. which will run all highlighting tests and report results in [TAP format][TAP].
  71. By default, the results of all tests will be printed; to show only "interesting"
  72. results (tests that failed but were expected to succeed, or vice-versa), run
  73. `make quiet-test` (or `make test QUIET=y`).
  74. [TAP]: http://testanything.org/
  75. Performance test
  76. ----------------
  77. [`test-perfs.zsh`](tests/test-perfs.zsh) measures the time spent doing the
  78. highlighting. Usage:
  79. ```zsh
  80. zsh test-perfs.zsh <HIGHLIGHTER NAME>
  81. ```
  82. All tests may be run with
  83. ```zsh
  84. make perf
  85. ```