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.

92 lines
3.1 KiB

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