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.

124 lines
3.8 KiB

3 years ago
  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 `$fail_test` to a non-empty string, the test will be skipped
  23. with the provided string as the reason.
  24. 4.
  25. If a test sets `unsorted=1` the order of highlights in `$expected_region_highlight`
  26. need not match the order in `$region_highlight`.
  27. 5.
  28. Normally, tests fail if `$expected_region_highlight` and `$region_highlight`
  29. have different numbers of elements. To mark this check as expected to fail,
  30. tests may set `$expected_mismatch` to an explanation string (like `$todo`);
  31. this is useful when the only difference between actual and expected is that actual
  32. has some additional, superfluous elements. This check is skipped if the
  33. `$todo` component is present in any regular test point.
  34. **Note**: `$region_highlight` uses the same `"$i $j $style"` syntax but
  35. interprets the indexes differently.
  36. **Note**: Tests are run with `setopt NOUNSET WARN_CREATE_GLOBAL`, so any
  37. variables the test creates must be declared local.
  38. **Isolation**: Each test is run in a separate subshell, so any variables,
  39. aliases, functions, etc., it defines will be visible to the tested code (that
  40. computes `$region_highlight`), but will not affect subsequent tests. The
  41. current working directory of tests is set to a newly-created empty directory,
  42. which is automatically cleaned up after the test exits. For example:
  43. ```zsh
  44. setopt PATH_DIRS
  45. mkdir -p foo/bar
  46. touch foo/bar/testing-issue-228
  47. chmod +x foo/bar/testing-issue-228
  48. path+=( "$PWD"/foo )
  49. BUFFER='bar/testing-issue-228'
  50. expected_region_highlight=(
  51. "1 21 command" # bar/testing-issue-228
  52. )
  53. ```
  54. Writing new tests
  55. -----------------
  56. An experimental tool is available to generate test files:
  57. ```zsh
  58. zsh -f tests/generate.zsh 'ls -x' acme newfile
  59. ```
  60. This generates a `highlighters/acme/test-data/newfile.zsh` test file based on
  61. the current highlighting of the given `$BUFFER` (in this case, `ls -x`).
  62. _This tool is experimental._ Its interface may change. In particular it may
  63. grow ways to set `$PREBUFFER` to inject free-form code into the generated file.
  64. Highlighting test
  65. -----------------
  66. [`test-highlighting.zsh`](tests/test-highlighting.zsh) tests the correctness of
  67. the highlighting. Usage:
  68. ```zsh
  69. zsh test-highlighting.zsh <HIGHLIGHTER NAME>
  70. ```
  71. All tests may be run with
  72. ```zsh
  73. make test
  74. ```
  75. which will run all highlighting tests and report results in [TAP format][TAP].
  76. By default, the results of all tests will be printed; to show only "interesting"
  77. results (tests that failed but were expected to succeed, or vice-versa), run
  78. `make quiet-test` (or `make test QUIET=y`).
  79. [TAP]: http://testanything.org/
  80. Performance test
  81. ----------------
  82. [`test-perfs.zsh`](tests/test-perfs.zsh) measures the time spent doing the
  83. highlighting. Usage:
  84. ```zsh
  85. zsh test-perfs.zsh <HIGHLIGHTER NAME>
  86. ```
  87. All tests may be run with
  88. ```zsh
  89. make perf
  90. ```