|
| 1 | +/* |
| 2 | + * Sonar Delphi Plugin |
| 3 | + * Copyright (C) 2024 Integrated Application Development |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 3 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 |
| 18 | + */ |
| 19 | +package au.com.integradev.delphi.antlr.ast.node; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +import au.com.integradev.delphi.antlr.DelphiLexer; |
| 24 | +import org.antlr.runtime.CommonToken; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.sonar.plugins.communitydelphi.api.ast.DelphiNode; |
| 27 | + |
| 28 | +class TextLiteralNodeImplTest { |
| 29 | + @Test |
| 30 | + void testMultilineImage() { |
| 31 | + String image = |
| 32 | + "'''\n" // |
| 33 | + + " Foo\n" |
| 34 | + + " Bar\n" |
| 35 | + + " Baz\n" |
| 36 | + + " '''"; |
| 37 | + |
| 38 | + TextLiteralNodeImpl node = new TextLiteralNodeImpl(DelphiLexer.TkTextLiteral); |
| 39 | + node.addChild(createNode(DelphiLexer.TkMultilineString, image)); |
| 40 | + |
| 41 | + assertThat(node.getImage()).isEqualTo(image); |
| 42 | + assertThat(node.getValue()).isEqualTo(node.getImageWithoutQuotes()).isEqualTo("Foo\nBar\nBaz"); |
| 43 | + assertThat(node.isMultiline()).isTrue(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void testGetImageWithCharacterEscapes() { |
| 48 | + TextLiteralNodeImpl node = new TextLiteralNodeImpl(DelphiLexer.TkTextLiteral); |
| 49 | + node.addChild(createNode(DelphiLexer.TkQuotedString, "'F'")); |
| 50 | + node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#111")); |
| 51 | + node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#111")); |
| 52 | + node.addChild(createNode(DelphiLexer.TkQuotedString, "'B'")); |
| 53 | + node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#$61")); |
| 54 | + node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#$72")); |
| 55 | + node.addChild(createNode(DelphiLexer.TkQuotedString, "'B'")); |
| 56 | + node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#%01100001")); |
| 57 | + node.addChild(createNode(DelphiLexer.TkCharacterEscapeCode, "#%01111010")); |
| 58 | + |
| 59 | + assertThat(node.getImage()).isEqualTo("'F'#111#111'B'#$61#$72'B'#%01100001#%01111010"); |
| 60 | + assertThat(node.getValue()).isEqualTo(node.getImageWithoutQuotes()).isEqualTo("FooBarBaz"); |
| 61 | + assertThat(node.isMultiline()).isFalse(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testGetImageWithCaretNotation() { |
| 66 | + TextLiteralNodeImpl node = new TextLiteralNodeImpl(DelphiLexer.TkTextLiteral); |
| 67 | + node.addChild(createNode(DelphiLexer.TkQuotedString, "'F'")); |
| 68 | + node.addChild(createNode(DelphiLexer.TkEscapedCharacter, "/")); |
| 69 | + node.addChild(createNode(DelphiLexer.TkEscapedCharacter, "/")); |
| 70 | + node.addChild(createNode(DelphiLexer.TkQuotedString, "'B'")); |
| 71 | + node.addChild(createNode(DelphiLexer.TkEscapedCharacter, "!")); |
| 72 | + node.addChild(createNode(DelphiLexer.TkEscapedCharacter, "2")); |
| 73 | + node.addChild(createNode(DelphiLexer.TkQuotedString, "'B'")); |
| 74 | + node.addChild(createNode(DelphiLexer.TkEscapedCharacter, "!")); |
| 75 | + node.addChild(createNode(DelphiLexer.TkEscapedCharacter, ":")); |
| 76 | + |
| 77 | + assertThat(node.getImage()).isEqualTo("'F'^/^/'B'^!^2'B'^!^:"); |
| 78 | + assertThat(node.getValue()).isEqualTo(node.getImageWithoutQuotes()).isEqualTo("FooBarBaz"); |
| 79 | + assertThat(node.isMultiline()).isFalse(); |
| 80 | + } |
| 81 | + |
| 82 | + private static DelphiNode createNode(int tokenType, String image) { |
| 83 | + return new CommonDelphiNodeImpl(new CommonToken(tokenType, image)); |
| 84 | + } |
| 85 | +} |
0 commit comments