IntelliJ Platform Plugin SDK Help

5. Formatter Test

This test checks if the Simple Language formatter, implemented in the Formatter section of the Custom Language Support Tutorial, works as expected.

Define Test Data

Create the FormatterTestData.simple file in the testData directory.

# You are reading the ".properties" entry. ! The exclamation mark can also mark text as comments. website= language= English # The backslash below tells the application to continue reading # the value onto the next line. message = Welcome to \ Wikipedia! # Add spaces to the key key\ with\ spaces = This is the value that could be looked up with the key "key with spaces". # Unicode tab :\u0009

Define a Test Method

Add the testFormatter() method to the SimpleCodeInsightTest class previously defined.

  • 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.

public void testFormatter() { myFixture.configureByFile("FormatterTestData.simple"); CodeStyle.getLanguageSettings(myFixture.getFile()).SPACE_AROUND_ASSIGNMENT_OPERATORS = true; CodeStyle.getLanguageSettings(myFixture.getFile()).KEEP_BLANK_LINES_IN_CODE = 2; WriteCommandAction.writeCommandAction(getProject()).run(() -> CodeStyleManager.getInstance(getProject()).reformatText( myFixture.getFile(), ContainerUtil.newArrayList(myFixture.getFile().getTextRange()) ) ); myFixture.checkResultByFile("DefaultTestData.simple"); }

Run the Test

Run the test and make sure it's green.

Last modified: 07 March 2022