|
16 | 16 |
|
17 | 17 | package io.spring.format.formatter.intellij.formatting;
|
18 | 18 |
|
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
19 | 21 | import java.util.Collections;
|
20 | 22 |
|
21 | 23 | import com.intellij.formatting.FormattingContext;
|
|
25 | 27 | import com.intellij.openapi.fileTypes.FileTypeManager;
|
26 | 28 | import com.intellij.openapi.fileTypes.PlainTextFileType;
|
27 | 29 | import com.intellij.openapi.project.Project;
|
| 30 | +import com.intellij.openapi.vfs.VirtualFile; |
| 31 | +import com.intellij.openapi.vfs.local.CoreLocalFileSystem; |
| 32 | +import com.intellij.openapi.vfs.local.CoreLocalVirtualFile; |
28 | 33 | import com.intellij.psi.PsiFile;
|
| 34 | +import com.intellij.testFramework.LightVirtualFile; |
29 | 35 | import org.junit.jupiter.api.Test;
|
| 36 | +import org.junit.jupiter.api.io.TempDir; |
30 | 37 |
|
31 | 38 | import io.spring.format.formatter.intellij.state.State;
|
32 | 39 |
|
33 | 40 | import static org.assertj.core.api.Assertions.assertThat;
|
34 | 41 | import static org.mockito.ArgumentMatchers.any;
|
35 | 42 | import static org.mockito.BDDMockito.given;
|
| 43 | +import static org.mockito.BDDMockito.willAnswer; |
36 | 44 | import static org.mockito.Mockito.mock;
|
37 |
| -import static org.mockito.Mockito.verify; |
38 | 45 |
|
39 | 46 | /**
|
40 | 47 | * Tests for {@link SpringJavaFormatFormattingService}.
|
@@ -72,14 +79,49 @@ void canFormatWhenJavaFileAndActiveReturnsTrue() {
|
72 | 79 | assertThat(this.service.canFormat(file)).isTrue();
|
73 | 80 | }
|
74 | 81 |
|
| 82 | + @Test |
| 83 | + void formatDocumentAppliesFormatting(@TempDir Path projectDir) throws Exception { |
| 84 | + Files.writeString(projectDir.resolve(".springjavaformatconfig"), "indentation-style=spaces"); |
| 85 | + Document document = mockDocument("public class Hello{" |
| 86 | + + "\tpublic void hello() {" |
| 87 | + + "\tString value =\t\"Hello World\";}}"); |
| 88 | + FormattingContext formattingContext = mock(FormattingContext.class); |
| 89 | + VirtualFile virtualFile = new CoreLocalVirtualFile(new CoreLocalFileSystem(), projectDir.resolve("Hello.java")); |
| 90 | + given(formattingContext.getVirtualFile()).willReturn(virtualFile); |
| 91 | + this.service.formatDocument(document, Collections.emptyList(), formattingContext, false, false); |
| 92 | + assertThat(document.getText()).isEqualTo("public class Hello {\n\n" |
| 93 | + + " public void hello() {\n" |
| 94 | + + " String value = \"Hello World\";\n" |
| 95 | + + " }\n\n" |
| 96 | + + "}"); |
| 97 | + } |
| 98 | + |
75 | 99 | @Test
|
76 | 100 | void formatDocumentAppliesFormatting() {
|
77 |
| - Document document = mock(Document.class); |
78 |
| - String text = "public class Hello {}"; |
79 |
| - given(document.getText()).willReturn(text); |
| 101 | + Document document = mockDocument("public class Hello{" |
| 102 | + + "\tpublic void hello() {" |
| 103 | + + "\tString value =\t\"Hello World\";}}"); |
80 | 104 | FormattingContext formattingContext = mock(FormattingContext.class);
|
| 105 | + VirtualFile virtualFile = new LightVirtualFile("Hello.java", document.getText()); |
| 106 | + given(formattingContext.getVirtualFile()).willReturn(virtualFile); |
81 | 107 | this.service.formatDocument(document, Collections.emptyList(), formattingContext, false, false);
|
82 |
| - verify(document).replaceString(20, 20, "\n\n"); |
| 108 | + assertThat(document.getText()).isEqualTo("public class Hello {\n\n" |
| 109 | + + "\tpublic void hello() {\n" |
| 110 | + + "\t\tString value = \"Hello World\";\n" |
| 111 | + + "\t}\n\n" |
| 112 | + + "}"); |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + private Document mockDocument(String text) { |
| 117 | + Document document = mock(Document.class); |
| 118 | + StringBuilder documentText = new StringBuilder(text); |
| 119 | + willAnswer((invocation) -> { |
| 120 | + documentText.replace(invocation.getArgument(0), invocation.getArgument(1), invocation.getArgument(2)); |
| 121 | + return null; |
| 122 | + }).given(document).replaceString(any(Integer.class), any(Integer.class), any(CharSequence.class)); |
| 123 | + given(document.getText()).willAnswer((invocation) -> documentText.toString()); |
| 124 | + return document; |
83 | 125 | }
|
84 | 126 |
|
85 | 127 | private PsiFile mockFile(FileType fileType, State state) {
|
|
0 commit comments