Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 1 | Tests for syntax highlighting plugins |
| 2 | ===================================== |
| 3 | |
| 4 | Summary: Files in the "input" directory are edited by Vim with syntax |
| 5 | highlighting enabled. Screendumps are generated and compared with the |
| 6 | expected screendumps in the "dumps" directory. This will uncover any |
| 7 | character attributes that differ. |
| 8 | |
Aliaksei Budavei | b5a92d7 | 2024-07-07 20:51:14 +0200 | [diff] [blame] | 9 | The dumps are normally 20 screen lines tall. Without any further setup |
| 10 | a screendump is made at the top of the file (using _00.dump) and another |
| 11 | screendump is made if there are more lines (using _01.dump), and so on. |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 12 | |
| 13 | When the screendumps are OK an empty "done/{name}" file is created. This |
| 14 | avoids running the test again until "make clean" is used. Thus you can run |
| 15 | "make test", see one test fail, try to fix the problem, then run "make test" |
| 16 | again to only repeat the failing test. |
| 17 | |
| 18 | When a screendump differs it is stored in the "failed" directory. This allows |
| 19 | for comparing it with the expected screendump, using a command like: |
| 20 | |
Aliaksei Budavei | b5a92d7 | 2024-07-07 20:51:14 +0200 | [diff] [blame] | 21 | let fname = '{name}_00.dump' |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 22 | call term_dumpdiff('failed/' .. fname, 'dumps/' .. fname) |
| 23 | |
| 24 | |
| 25 | Creating a syntax plugin test |
| 26 | ----------------------------- |
| 27 | |
| 28 | Create a source file in the language you want to test in the "input" |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 29 | directory. Use the filetype name as the base and a file name extension |
Aliaksei Budavei | 8418446 | 2024-05-21 01:10:26 +0300 | [diff] [blame] | 30 | matching the filetype. Let's use Java as an example. The file would then be |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 31 | "input/java.java". |
| 32 | |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 33 | Make sure to include some interesting constructs with plenty of complicated |
| 34 | highlighting. Optionally, pre-configure the testing environment by including |
Aliaksei Budavei | 8418446 | 2024-05-21 01:10:26 +0300 | [diff] [blame] | 35 | setup commands at the top of the input file. The format for these lines is: |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 36 | |
Aliaksei Budavei | a2addeb | 2024-03-18 20:39:32 +0100 | [diff] [blame] | 37 | VIM_TEST_SETUP {command} |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 38 | |
| 39 | where {command} is any valid Ex command, which extends to the end of the line. |
| 40 | The first 20 lines of the input file are ALWAYS scanned for setup commands and |
| 41 | these will be executed before the syntax highlighting is enabled. Typically, |
| 42 | these lines would be included as comments so as not to introduce any syntax |
| 43 | errors in the input file but this is not required. |
| 44 | |
| 45 | Continuing the Java example: |
| 46 | |
Aliaksei Budavei | a2addeb | 2024-03-18 20:39:32 +0100 | [diff] [blame] | 47 | // VIM_TEST_SETUP let g:java_space_errors = 1 |
| 48 | // VIM_TEST_SETUP let g:java_minlines = 5 |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 49 | class Test { } |
| 50 | |
Aliaksei Budavei | f6069a7 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 51 | As an alternative, setup commands can be included in an external Vim script |
| 52 | file in the "input/setup" directory. This script file must have the same base |
| 53 | name as the input file. |
| 54 | |
| 55 | So, the equivalent example configuration using this method would be to create |
| 56 | an "input/setup/java.vim" script file with the following lines: |
| 57 | |
| 58 | let g:java_space_errors = 1 |
| 59 | let g:java_minlines = 5 |
| 60 | |
| 61 | Both inline setup commands and setup scripts may be used at the same time, the |
Aliaksei Budavei | a2addeb | 2024-03-18 20:39:32 +0100 | [diff] [blame] | 62 | script file will be sourced before any VIM_TEST_SETUP commands are executed. |
Aliaksei Budavei | f6069a7 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 63 | |
Aliaksei Budavei | 8418446 | 2024-05-21 01:10:26 +0300 | [diff] [blame] | 64 | Every line of a source file must not be longer than 1425 (19 x 75) characters. |
| 65 | |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 66 | If there is no further setup required, you can now run the tests: |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 67 | |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 68 | make test |
| 69 | |
| 70 | The first time this will fail with an error for a missing screendump. The |
| 71 | newly created screendumps will be "failed/java_00.dump", |
| 72 | "failed/java_01.dump", etc. You can inspect each with: |
| 73 | |
| 74 | call term_dumpload('failed/java_00.dump') |
| 75 | call term_dumpload('failed/java_01.dump') |
| 76 | ... |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 77 | |
| 78 | If they look OK, move them to the "dumps" directory: |
| 79 | |
| 80 | :!mv failed/java_00.dump dumps |
| 81 | :!mv failed/java_01.dump dumps |
| 82 | ... |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 83 | |
| 84 | If you now run the test again, it will succeed. |
| 85 | |
| 86 | |
| 87 | Adjusting a syntax plugin test |
| 88 | ------------------------------ |
| 89 | |
| 90 | If you make changes to the syntax plugin, you should add code to the input |
| 91 | file to see the effect of these changes. So that the effect of the changes |
| 92 | are covered by the test. You can follow these steps: |
| 93 | |
| 94 | 1. Edit the syntax plugin somewhere in your personal setup. Use a file |
| 95 | somewhere to try out the changes. |
| 96 | 2. Go to the directory where you have the Vim code checked out and replace the |
| 97 | syntax plugin. Run the tests: "make test". Usually the tests will still |
| 98 | pass, but if you fixed syntax highlighting that was already visible in the |
| 99 | input file, carefully check that the changes in the screendump are |
| 100 | intentional: |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 101 | |
Aliaksei Budavei | b5a92d7 | 2024-07-07 20:51:14 +0200 | [diff] [blame] | 102 | let fname = '{name}_00.dump' |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 103 | call term_dumpdiff('failed/' .. fname, 'dumps/' .. fname) |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 104 | |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 105 | Fix the syntax plugin until the result is good. |
| 106 | 2. Edit the input file for your language to add the items you have improved. |
| 107 | (TODO: how to add another screendump?). |
| 108 | Run the tests and you should get failures. Like with the previous step, |
| 109 | carefully check that the new screendumps in the "failed" directory are |
| 110 | good. Update the syntax plugin and the input file until the highlighting |
| 111 | is good and you can see the effect of the syntax plugin improvements. Then |
| 112 | move the screendumps from the "failed" to the "dumps" directory. Now "make |
| 113 | test" should succeed. |
| 114 | 3. Prepare a pull request with the modified files: |
| 115 | - syntax plugin: syntax/{name}.vim |
Aliaksei Budavei | f6069a7 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 116 | - Vim setup file: syntax/testdir/input/setup/{name}.vim (if any) |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 117 | - test input file: syntax/testdir/input/{name}.{ext} |
Aliaksei Budavei | b5a92d7 | 2024-07-07 20:51:14 +0200 | [diff] [blame] | 118 | - test dump files: syntax/testdir/dumps/{name}_00.dump |
| 119 | syntax/testdir/dumps/{name}_01.dump (if any) |
| 120 | ... |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 121 | |
| 122 | As an extra check you can temporarily put back the old syntax plugin and |
| 123 | verify that the tests fail. Then you know your changes are covered by the |
| 124 | test. |
| 125 | |
| 126 | |
| 127 | |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 128 | |
Aliaksei Budavei | 93edd25 | 2024-03-05 22:34:36 +0300 | [diff] [blame] | 129 | TODO: run test for one specific filetype |
Bram Moolenaar | 46acad7 | 2023-06-11 19:04:18 +0100 | [diff] [blame] | 130 | TODO: test syncing by jumping around |