|
20 | 20 |
|
21 | 21 | import static org.assertj.core.api.Assertions.assertThat; |
22 | 22 | import static org.mockito.ArgumentMatchers.anyInt; |
| 23 | +import static org.mockito.ArgumentMatchers.eq; |
23 | 24 | import static org.mockito.Mockito.mock; |
| 25 | +import static org.mockito.Mockito.spy; |
24 | 26 | import static org.mockito.Mockito.when; |
25 | 27 |
|
26 | 28 | import au.com.integradev.delphi.antlr.DelphiLexer; |
27 | 29 | import au.com.integradev.delphi.antlr.ast.DelphiAstImpl; |
28 | 30 | import au.com.integradev.delphi.file.DelphiFile; |
| 31 | +import au.com.integradev.delphi.preprocessor.CompilerSwitchRegistry; |
29 | 32 | import au.com.integradev.delphi.preprocessor.TextBlockLineEndingMode; |
30 | 33 | import au.com.integradev.delphi.preprocessor.TextBlockLineEndingModeRegistry; |
| 34 | +import java.nio.charset.Charset; |
31 | 35 | import org.antlr.runtime.CommonToken; |
32 | 36 | import org.junit.jupiter.api.Test; |
| 37 | +import org.junit.jupiter.params.ParameterizedTest; |
| 38 | +import org.junit.jupiter.params.provider.ValueSource; |
33 | 39 | import org.sonar.plugins.communitydelphi.api.ast.DelphiNode; |
| 40 | +import org.sonar.plugins.communitydelphi.api.directive.SwitchDirective.SwitchKind; |
34 | 41 |
|
35 | 42 | class TextLiteralNodeImplTest { |
36 | 43 | @Test |
@@ -59,22 +66,45 @@ void testMultilineImage() { |
59 | 66 | assertThat(node.isMultiline()).isTrue(); |
60 | 67 | } |
61 | 68 |
|
62 | | - @Test |
63 | | - void testGetImageWithCharacterEscapes() { |
64 | | - TextLiteralNodeImpl node = new TextLiteralNodeImpl(DelphiLexer.TkTextLiteral); |
| 69 | + @ParameterizedTest(name = "HIGHCHARUNICODE = {0}") |
| 70 | + @ValueSource(booleans = {true, false}) |
| 71 | + void testGetImageWithCharacterEscapes(boolean highCharUnicode) { |
| 72 | + var registry = mock(CompilerSwitchRegistry.class); |
| 73 | + when(registry.isActiveSwitch(eq(SwitchKind.HIGHCHARUNICODE), anyInt())) |
| 74 | + .thenReturn(highCharUnicode); |
| 75 | + var file = mock(DelphiFile.class); |
| 76 | + when(file.getCompilerSwitchRegistry()).thenReturn(registry); |
| 77 | + var ast = mock(DelphiAstImpl.class); |
| 78 | + when(ast.getDelphiFile()).thenReturn(file); |
| 79 | + |
| 80 | + TextLiteralNodeImpl node = spy(new TextLiteralNodeImpl(DelphiLexer.TkTextLiteral)); |
| 81 | + when(node.getAnsiCharset()).thenReturn(Charset.forName("windows-1252")); |
| 82 | + node.setParent(ast); |
| 83 | + |
65 | 84 | node.addChild(createNode(DelphiLexer.TkQuotedString, "'F'")); |
66 | 85 | node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#111")); |
67 | 86 | node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#111")); |
68 | 87 | node.addChild(createNode(DelphiLexer.TkQuotedString, "'B'")); |
69 | 88 | node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#$61")); |
70 | 89 | node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#$72")); |
71 | 90 | node.addChild(createNode(DelphiLexer.TkQuotedString, "'B'")); |
72 | | - node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#%01100001")); |
73 | | - node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#%01111010")); |
| 91 | + node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#$80")); |
| 92 | + node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#$98")); |
| 93 | + node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#$A3")); |
| 94 | + node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#$20AC")); |
| 95 | + node.addChild(createNode(DelphiLexer.TkQuotedString, "'az'")); |
74 | 96 |
|
75 | | - assertThat(node.getImage()).isEqualTo("'F'#111#111'B'#$61#$72'B'#%01100001#%01111010"); |
76 | | - assertThat(node.getValue()).isEqualTo(node.getImageWithoutQuotes()).isEqualTo("FooBarBaz"); |
77 | 97 | assertThat(node.isMultiline()).isFalse(); |
| 98 | + assertThat(node.getImage()).isEqualTo("'F'#111#111'B'#$61#$72'B'#$80#$98#$A3#$20AC'az'"); |
| 99 | + if (highCharUnicode) { |
| 100 | + assertThat(node.getValue()) |
| 101 | + .isEqualTo(node.getImageWithoutQuotes()) |
| 102 | + .isEqualTo("FooBarB\u0080\u0098£€az"); |
| 103 | + } else { |
| 104 | + assertThat(node.getValue()) |
| 105 | + .isEqualTo(node.getImageWithoutQuotes()) |
| 106 | + .isEqualTo("FooBarB€˜£€az"); |
| 107 | + } |
78 | 108 | } |
79 | 109 |
|
80 | 110 | @Test |
|
0 commit comments