mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-30 18:27:49 +08:00
custom language test tutorial: include sample code, formatting, cleanup
This commit is contained in:
parent
bf6a7b958d
commit
3bd3040b3e
@ -1,16 +1,16 @@
|
||||
[//]: # (title: 4. Annotator Test)
|
||||
|
||||
<!-- Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
<!-- Copyright 2000-2021 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
|
||||
This test checks if the Simple Language annotator functionality, implemented in the [Annotator](annotator.md) section of the Custom Language Support Tutorial, works as expected.
|
||||
|
||||
## Define Input Test Data
|
||||
The `DefaultTestData.simple` properties file is reused for this test.
|
||||
The <path>DefaultTestData.simple</path> file is reused for this test.
|
||||
|
||||
Create an input test file `AnnotatorTestData.java` in the `testData` directory.
|
||||
Create an input test file <path>AnnotatorTestData.java</path> in the <path>testData</path> directory.
|
||||
This file contains two instances of Simple Language embedded in the Java code.
|
||||
The first instance is a valid use of the `simple:` prefix followed by the Simple Language key `website`.
|
||||
The second is a valid prefix but an invalid key, as noted by the test `<error>` [highlighting](testing_highlighting.md).
|
||||
The second is a valid prefix but an invalid key `websit`, as noted by the test `<error>` [highlighting](testing_highlighting.md).
|
||||
|
||||
```java
|
||||
public class Test {
|
||||
@ -27,11 +27,8 @@ Again, this method configures the test fixture by using the test files.
|
||||
It then calls the `checkHighlighting()` method to verify weak warnings.
|
||||
|
||||
```java
|
||||
public void testAnnotator() {
|
||||
myFixture.configureByFiles("AnnotatorTestData.java", "DefaultTestData.simple");
|
||||
myFixture.checkHighlighting(false, false, true, true);
|
||||
}
|
||||
```
|
||||
{src="simple_language_plugin/src/test/java/org/intellij/sdk/language/SimpleCodeInsightTest.java" include-symbol="testAnnotator"}
|
||||
|
||||
## Run the Test
|
||||
[Run](completion_test.md#run-the-test) the test and make sure it's green.
|
@ -1,6 +1,6 @@
|
||||
[//]: # (title: 9. Commenter Test)
|
||||
|
||||
<!-- Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
<!-- Copyright 2000-2021 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
|
||||
This test will check if the commenter, implemented in the [Commenter](commenter.md) section of the Custom Language Support Tutorial, works as expected.
|
||||
|
||||
@ -11,15 +11,8 @@ The test calls the commenter to insert a comment character at the caret, then ve
|
||||
It again calls the line comment action to remove the comment character and verifies the results.
|
||||
|
||||
```java
|
||||
public void testCommenter() {
|
||||
myFixture.configureByText(SimpleFileType.INSTANCE, "<caret>website = https://en.wikipedia.org/");
|
||||
CommentByLineCommentAction commentAction = new CommentByLineCommentAction();
|
||||
commentAction.actionPerformedImpl(getProject(), myFixture.getEditor());
|
||||
myFixture.checkResult("#website = https://en.wikipedia.org/");
|
||||
commentAction.actionPerformedImpl(getProject(), myFixture.getEditor());
|
||||
myFixture.checkResult("website = https://en.wikipedia.org/");
|
||||
}
|
||||
```
|
||||
{src="simple_language_plugin/src/test/java/org/intellij/sdk/language/SimpleCodeInsightTest.java" include-symbol="testCommenter"}
|
||||
|
||||
## Run the Test
|
||||
[Run](completion_test.md#run-the-test) the test and make sure it's green.
|
@ -5,13 +5,13 @@
|
||||
This test checks if the Simple Language code completion functionality, implemented in the [Reference Contributor](reference_contributor.md) section of the Custom Language Support Tutorial, works as expected.
|
||||
|
||||
## Define Test Data
|
||||
Create the `DefaultTestData.simple` properties file in the `testData` directory.
|
||||
Create the <path>DefaultTestData.simple</path> file in the <path>testData</path> directory.
|
||||
|
||||
```bash
|
||||
```
|
||||
{src="simple_language_plugin/src/test/testData/DefaultTestData.simple"}
|
||||
|
||||
Create a test input Java file `CompleteTestData.java` in the `testData` directory.
|
||||
Create a test input Java file <path>CompleteTestData.java</path> in the <path>testData</path> directory.
|
||||
This file contains a Simple Language reference within the Java code at `<caret>`.
|
||||
|
||||
```java
|
||||
@ -20,7 +20,12 @@ This file contains a Simple Language reference within the Java code at `<caret>`
|
||||
|
||||
## Define a Test
|
||||
Subclass [`LightJavaCodeInsightFixtureTestCase`](upsource:///java/testFramework/src/com/intellij/testFramework/fixtures/LightJavaCodeInsightFixtureTestCase.java) to create `SimpleCodeInsightTest`.
|
||||
Override `getTestDataPath()`, and return the path from the root of this plugin module to the `testData` directory.
|
||||
Override `getTestDataPath()`, and return the path from the root of this plugin module to the <path>testData</path> directory.
|
||||
|
||||
```java
|
||||
```
|
||||
{src="simple_language_plugin/src/test/java/org/intellij/sdk/language/SimpleCodeInsightTest.java" include-symbol="getTestDataPath"}
|
||||
|
||||
|
||||
At this point only one test is defined in `SimpleCodeInsightTest`: `testCompletion()`.
|
||||
This method:
|
||||
@ -30,35 +35,11 @@ This method:
|
||||
* Checks the list of returned lookup strings to ensure it matches the completion variants provided by the reference.
|
||||
|
||||
```java
|
||||
public class SimpleCodeInsightTest extends LightJavaCodeInsightFixtureTestCase {
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return "src/test/testData";
|
||||
}
|
||||
|
||||
public void testCompletion() {
|
||||
myFixture.configureByFiles("CompleteTestData.java", "DefaultTestData.simple");
|
||||
myFixture.complete(CompletionType.BASIC);
|
||||
List<String> lookupElementStrings = myFixture.getLookupElementStrings();
|
||||
assertNotNull(lookupElementStrings);
|
||||
assertSameElements(lookupElementStrings, "key with spaces", "language", "message", "tab", "website");
|
||||
}
|
||||
}
|
||||
```
|
||||
{src="simple_language_plugin/src/test/java/org/intellij/sdk/language/SimpleCodeInsightTest.java" include-symbol="testCompletion"}
|
||||
|
||||
|
||||
A number of related methods exist in `CodeInsightTestFixture` for testing completion and lookup elements, e.g., when testing completion variants and requiring only one testdata file `CodeInsightTestFixture.testCompletionVariants()`.
|
||||
|
||||
## Run the Test
|
||||
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** Tool Window, and also written to the `simple_language_plugin/build/test-results/test/` directory.
|
||||
|
||||
If the **Run** Tool Window displays the error *Test events were not received*, do the following:
|
||||
* In the **Gradle** Tool Window, drill down under `simple_language_plugin` to *Tasks*, *build*, *clean* task.
|
||||
* Run the *clean* task, which deletes the `simple_language_plugin/build/` directory.
|
||||
* Retry the test.
|
||||
[Run](completion_test.md#run-the-test) the test and make sure it's green.
|
@ -1,17 +1,17 @@
|
||||
[//]: # (title: 8. Find Usages Test)
|
||||
|
||||
<!-- Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
<!-- Copyright 2000-2021 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
|
||||
This test ensures the find usages provider, implemented in the [Find Usages Provider](find_usages_provider.md) section of the Custom Language Support Tutorial, works correctly.
|
||||
|
||||
## Define the Test Data
|
||||
Create the `FindUsagesTestData.simple` properties file in the `testData` directory.
|
||||
Create the <path>FindUsagesTestData.simple</path> file in the <path>testData</path> directory.
|
||||
|
||||
```bash
|
||||
```
|
||||
{src="simple_language_plugin/src/test/testData/FindUsagesTestData.simple"}
|
||||
|
||||
Create the test file `FindUsagesTestData.java`, which contains one embedded Simple Language prefix and key.
|
||||
Create the test file <path>FindUsagesTestData.java</path>, which contains one embedded Simple Language prefix and key.
|
||||
|
||||
```java
|
||||
```
|
||||
@ -22,11 +22,9 @@ Add the `testFindUsages()` method to the `SimpleCodeInsightTest` class [previous
|
||||
This test verifies the find usage functionality will identify the "key with spaces".
|
||||
|
||||
```java
|
||||
public void testFindUsages() {
|
||||
Collection<UsageInfo> usageInfos = myFixture.testFindUsages("FindUsagesTestData.simple", "FindUsagesTestData.java");
|
||||
assertEquals(1, usageInfos.size());
|
||||
}
|
||||
```
|
||||
{src="simple_language_plugin/src/test/java/org/intellij/sdk/language/SimpleCodeInsightTest.java" include-symbol="testFindUsages"}
|
||||
|
||||
|
||||
## Run the Test
|
||||
[Run](completion_test.md#run-the-test) the test and make sure it's green.
|
@ -1,6 +1,6 @@
|
||||
[//]: # (title: 7. Folding Test)
|
||||
|
||||
<!-- Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
<!-- Copyright 2000-2021 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
|
||||
This test verifies the Simple Language folding builder, implemented in the [Folding Builder](folding_builder.md) section of the Custom Language Support Tutorial, works as expected.
|
||||
|
||||
@ -9,7 +9,7 @@ This test verifies the Simple Language folding builder, implemented in the [Fold
|
||||
{type="note"}
|
||||
|
||||
## Define Test Data
|
||||
Create a file `FoldingTestData.java` in the `testData` directory.
|
||||
Create a file <path>FoldingTestData.java</path> in the <path>testData</path> directory.
|
||||
This java file contains markup instructions for three different cases of code folding.
|
||||
|
||||
```java
|
||||
@ -18,14 +18,12 @@ This java file contains markup instructions for three different cases of code fo
|
||||
|
||||
## Define a Test
|
||||
Add the `testFolding()` method to the `SimpleCodeInsightTest` class [previously defined](completion_test.md#define-a-test).
|
||||
This test method reuses the `DefaultTestData.simple` properties file.
|
||||
This test method reuses the <path>DefaultTestData.simple</path> Simple file.
|
||||
|
||||
```java
|
||||
public void testFolding() {
|
||||
myFixture.configureByFile("DefaultTestData.simple");
|
||||
myFixture.testFolding(getTestDataPath() + "/FoldingTestData.java");
|
||||
}
|
||||
```
|
||||
{src="simple_language_plugin/src/test/java/org/intellij/sdk/language/SimpleCodeInsightTest.java" include-symbol="testFolding"}
|
||||
|
||||
|
||||
## Run the Test
|
||||
[Run](completion_test.md#run-the-test) the test and make sure it's green.
|
@ -7,7 +7,7 @@ This test checks if the Simple Language formatter, implemented in the [Formatter
|
||||
> See also [`FormatterTestCase`](upsource:///platform/testFramework/src/com/intellij/psi/formatter/FormatterTestCase.java) as convenient base class.
|
||||
|
||||
## Define Test Data
|
||||
Create the `FormatterTestData.simple` properties file in the `testData` directory.
|
||||
Create the <path>FormatterTestData.simple</path> file in the <path>testData</path> directory.
|
||||
|
||||
```bash
|
||||
```
|
||||
@ -18,7 +18,7 @@ Add the `testFormatter()` method to the `SimpleCodeInsightTest` class [previousl
|
||||
* Again, this method configures the test fixture by using the test file.
|
||||
* The code style Simple Language settings for spaces and blank lines are set.
|
||||
* The file is then formatted according to the settings.
|
||||
* The formatted file is compared to the expected results in the benchmark file `DefaultTestData.simple`.
|
||||
* The formatted file is compared to the expected results in the benchmark file <path>DefaultTestData.simple</path>.
|
||||
|
||||
```java
|
||||
```
|
||||
|
@ -1,18 +1,18 @@
|
||||
[//]: # (title: 2. Parsing Test)
|
||||
|
||||
<!-- Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
<!-- Copyright 2000-2021 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
|
||||
The first test checks if the Simple Language parser, implemented in the [Lexer and Parser Definition](lexer_and_parser_definition.md) section of the Custom Language Support Tutorial, works as expected.
|
||||
|
||||
For more complex Lexers (e.g., having additional logic), it is advisable to add separate tests inheriting from [`LexerTestCase`](upsource:///platform/testFramework/src/com/intellij/testFramework/LexerTestCase.java).
|
||||
|
||||
## Update Grammar and Regenerate the Parser
|
||||
Before creating the parsing test, ensure the parser definition (`Simple.bnf`) includes the lines shown below.
|
||||
Before creating the parsing test, ensure the parser definition (<path>Simple.bnf</path>) 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.
|
||||
If the lines below are not present in <path>Simple.bnf</path>, 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**.
|
||||
Right-click on the <path>Simple.bnf</path> file and select <control>Generate Parser Code</control>.
|
||||
|
||||
```java
|
||||
property ::= (KEY? SEPARATOR VALUE?) | KEY {
|
||||
@ -26,7 +26,7 @@ private recover_property ::= !(KEY|SEPARATOR|COMMENT)
|
||||
```
|
||||
|
||||
## Define Input Test Data
|
||||
Create the *ParsingTestData.simple* properties file in the *testData* folder.
|
||||
Create the <path>ParsingTestData.simple</path> properties file in the <path>testData</path> folder.
|
||||
Note the last few lines define a purposely incorrect key.
|
||||
|
||||
```bash
|
||||
@ -35,14 +35,14 @@ Note the last few lines define a purposely incorrect key.
|
||||
|
||||
## Copy the Expected PSI Tree
|
||||
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**.
|
||||
Run the project and use <menupath>Tools | View PSI Structure</menupath>.
|
||||
|
||||

|
||||
|
||||
Use the `Copy PSI` button to copy the expected PSI structure to the clipboard.
|
||||
Use the <control>Copy PSI</control> button to copy the expected PSI structure to the clipboard.
|
||||
|
||||
## Define the Output Reference Test Data
|
||||
Create a file *ParsingTestData.txt* with the copied PSI tree.
|
||||
Create a file <path>ParsingTestData.txt</path> with the copied PSI tree.
|
||||
|
||||
```text
|
||||
```
|
||||
@ -50,18 +50,11 @@ Create a file *ParsingTestData.txt* with the copied PSI tree.
|
||||
|
||||
## Define a Parsing Test
|
||||
Subclass [`ParsingTestCase`](upsource:///platform/testFramework/src/com/intellij/testFramework/ParsingTestCase.java) to create `SimpleParsingTest`:
|
||||
Override `getTestDataPath()`, and return the path from the root of this plugin module to the `testData` directory.
|
||||
Override `getTestDataPath()`, and return the path from the root of this plugin module to the <path>testData</path> directory.
|
||||
|
||||
```java
|
||||
```
|
||||
{src="simple_language_plugin/src/test/java/org/intellij/sdk/language/SimpleParsingTest.java"}
|
||||
|
||||
## Run the Test
|
||||
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.
|
||||
[Run](completion_test.md#run-the-test) the test and make sure it's green.
|
||||
|
@ -1,13 +1,13 @@
|
||||
[//]: # (title: 10. Reference Test)
|
||||
|
||||
<!-- Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
<!-- Copyright 2000-2021 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
|
||||
This test checks if references functionality, implemented in the [Reference Contributor](reference_contributor.md) section of the Custom Language Support Tutorial, works as expected.
|
||||
|
||||
## Define Test Data
|
||||
This test reuses the Simple Language properties file `DefaultTestData.simple`.
|
||||
This test reuses the Simple Language file <path>DefaultTestData.simple</path>.
|
||||
|
||||
Create the test file `ReferenceTestData.java` in the `testData` directory.
|
||||
Create the test file <path>ReferenceTestData.java</path> in the <path>testData</path> directory.
|
||||
This file has one Simple Language prefix and key, with the caret placed after the key.
|
||||
|
||||
```java
|
||||
@ -15,18 +15,14 @@ This file has one Simple Language prefix and key, with the caret placed after th
|
||||
{src="simple_language_plugin/src/test/testData/ReferenceTestData.java"}
|
||||
|
||||
## Define a Test Method
|
||||
Add the ` testReference()` method to the `SimpleCodeInsightTest` class [previously defined](completion_test.md#define-a-test).
|
||||
Add the `testReference()` method to the `SimpleCodeInsightTest` class [previously defined](completion_test.md#define-a-test).
|
||||
This test is configured by the test files.
|
||||
The fixture gets the `PsiReference` at the caret position, and then asserts the resolved `SimpleProperty.value()` with the known value of that key.
|
||||
|
||||
```java
|
||||
public void testReference() {
|
||||
PsiReference referenceAtCaret =
|
||||
myFixture.getReferenceAtCaretPositionWithAssertion("ReferenceTestData.java", "DefaultTestData.simple");
|
||||
final SimpleProperty resolvedSimpleProperty = assertInstanceOf(referenceAtCaret.resolve(), SimpleProperty.class);
|
||||
assertEquals("https://en.wikipedia.org/", resolvedSimpleProperty.getValue());
|
||||
}
|
||||
```
|
||||
{src="simple_language_plugin/src/test/java/org/intellij/sdk/language/SimpleCodeInsightTest.java" include-symbol="testReference"}
|
||||
|
||||
|
||||
## Run the Test
|
||||
[Run](completion_test.md#run-the-test) the test and make sure it's green.
|
@ -1,17 +1,17 @@
|
||||
[//]: # (title: 6. Rename Test)
|
||||
|
||||
<!-- Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
<!-- Copyright 2000-2021 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -->
|
||||
|
||||
This test verifies the Simple Language in-place rename functionality, implemented in the [Reference Contributor](reference_contributor.md) section of the Custom Language Support Tutorial, works as expected.
|
||||
|
||||
## Define Input Test Data
|
||||
Create the `RenameTestData.simple` properties file in the `testData` directory.
|
||||
Create the <path>RenameTestData.simple</path> properties file in the <path>testData</path> directory.
|
||||
|
||||
```bash
|
||||
```
|
||||
{src="simple_language_plugin/src/test/testData/RenameTestData.simple"}
|
||||
|
||||
Create the file `RenameTestData.java` in the `testData` directory.
|
||||
Create the file <path>RenameTestData.java</path> in the <path>testData</path> directory.
|
||||
This file contains one Simple Language reference embedded in Java, with the [caret position](test_project_and_testdata_directories.md#special-markup) placed just after a Simple Language key.
|
||||
|
||||
```java
|
||||
@ -19,9 +19,9 @@ This file contains one Simple Language reference embedded in Java, with the [car
|
||||
{src="simple_language_plugin/src/test/testData/RenameTestData.java"}
|
||||
|
||||
## Create Output Test Data
|
||||
Create the `RenameTestDataAfter.simple` properties file in the `testData` directory.
|
||||
Create the <path>RenameTestDataAfter.simple</path> file in the <path>testData</path> directory.
|
||||
This file contains the expected outcome of the test.
|
||||
Note the `website =` in `RenameTestData.simple` should be renamed to `websiteUrl =` by the test.
|
||||
Note the `website =` in <path>RenameTestData.simple</path> should be renamed to `websiteUrl =` by the test.
|
||||
|
||||
```bash
|
||||
```
|
||||
@ -30,16 +30,13 @@ Note the `website =` in `RenameTestData.simple` should be renamed to `websiteUrl
|
||||
## Define a Test Method
|
||||
Add the `testRename()` method to the `SimpleCodeInsightTest` class [previously defined](completion_test.md#define-a-test).
|
||||
* Again, this method configures the test fixture by using the test files.
|
||||
* The fixture then renames the Simple Language element at the caret in `RenameTestData.java`.
|
||||
* It then compares the input and output property files, ignoring whitespace.
|
||||
* The fixture then renames the Simple Language element at the caret in <path>RenameTestData.java</path>.
|
||||
* It then compares the input and output files, ignoring whitespace.
|
||||
|
||||
```java
|
||||
public void testRename() {
|
||||
myFixture.configureByFiles("RenameTestData.java", "RenameTestData.simple");
|
||||
myFixture.renameElementAtCaret("websiteUrl");
|
||||
myFixture.checkResultByFile("RenameTestData.simple", "RenameTestDataAfter.simple", false);
|
||||
}
|
||||
```
|
||||
{src="simple_language_plugin/src/test/java/org/intellij/sdk/language/SimpleCodeInsightTest.java" include-symbol="testRename"}
|
||||
|
||||
|
||||
## Run the Test
|
||||
[Run](completion_test.md#run-the-test) the test and make sure it's green.
|
Loading…
x
Reference in New Issue
Block a user