title |
---|
2. Parsing Test |
The first test checks if the Simple Language parser, implemented in the Lexer and Parser Definition section of the Custom Language Support Tutorial, works as expected.
Before creating the parsing test, ensure the parser definition (Simple.bnf
) includes the lines shown below.
These additional lines facilitate testing incorrect keys.
If the lines below are not present in Simple.bnf
, replace the existing property
definition with the lines below.
Don't forget to regenerate the parser after updating the file!
Right-click on the Simple.bnf
file and select Generate Parser Code.
property ::= (KEY? SEPARATOR VALUE?) | KEY {
pin=3
recoverWhile="recover_property"
mixin="org.intellij.sdk.language.psi.impl.SimpleNamedElementImpl"
implements="org.intellij.sdk.language.psi.SimpleNamedElement"
methods=[getKey getValue getName setName getNameIdentifier getPresentation]
}
private recover_property ::= !(KEY|SEPARATOR|COMMENT)
Create the ParsingTestData.simple properties file in the testData folder. Note the last few lines define a purposely incorrect key.
{% include /code_samples/simple_language_plugin/src/test/testData/ParsingTestData.simple %}
The easiest way to get the expected PSI structure for any file is to use the PSI Viewer. Run the project and use Tools | View PSI Structure.
Use the Copy PSI
button to copy the expected PSI structure to the clipboard.
Create a file ParsingTestData.txt with the copied PSI tree.
{% include /code_samples/simple_language_plugin/src/test/testData/ParsingTestData.txt %}
Subclass ParsingTestCase
to create SimpleParsingTest
:
Override getTestDataPath()
, and return the path from the root of this plugin module to the testData
directory.
{% include /code_samples/simple_language_plugin/src/test/java/org/intellij/sdk/language/SimpleParsingTest.java %}
Run the test by:
- Opening the Gradle Tool Window.
- Select the
simple_language_plugin
. You may need to reimport it as a Gradle project. - Drill down under
simple_language_plugin
to Tasks, verification, test task. - Run the test task.
The results are displayed in the Run Window, and also written to the simple_language_plugin/build/test-results/test/
directory.