|
| 1 | +/* |
| 2 | + * Sonar Delphi Plugin |
| 3 | + * Copyright (C) 2025 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 | +import static org.mockito.Mockito.mock; |
| 23 | + |
| 24 | +import au.com.integradev.delphi.symbol.occurrence.AttributeNameOccurrenceImpl; |
| 25 | +import au.com.integradev.delphi.symbol.occurrence.NameOccurrenceImpl; |
| 26 | +import au.com.integradev.delphi.utils.files.DelphiFileUtils; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.params.ParameterizedTest; |
| 29 | +import org.junit.jupiter.params.provider.ValueSource; |
| 30 | +import org.sonar.plugins.communitydelphi.api.ast.AttributeNode; |
| 31 | +import org.sonar.plugins.communitydelphi.api.symbol.NameOccurrence; |
| 32 | + |
| 33 | +class AttributeNodeImplTest { |
| 34 | + @Test |
| 35 | + void testAttribute() { |
| 36 | + AttributeNode node = parse("[Foo('Bar')]"); |
| 37 | + |
| 38 | + assertThat(node).isNotNull(); |
| 39 | + assertThat(node.isAssembly()).isFalse(); |
| 40 | + assertThat(node.getImage()).isEqualTo("Foo('Bar')"); |
| 41 | + assertThat(node.getNameReference()).isNotNull(); |
| 42 | + assertThat(node.getArgumentList()).isNotNull(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void testAttributeNameOccurrence() { |
| 47 | + AttributeNode node = parse("[Foo]"); |
| 48 | + |
| 49 | + assertThat(node.getNameReference()).isNotNull(); |
| 50 | + assertThat(node.getTypeNameOccurrence()).isNull(); |
| 51 | + assertThat(node.getConstructorNameOccurrence()).isNull(); |
| 52 | + |
| 53 | + var nameReference = (NameReferenceNodeImpl) node.getNameReference(); |
| 54 | + var constructorNameOccurrence = mock(NameOccurrence.class); |
| 55 | + var typeNameOccurrence = new AttributeNameOccurrenceImpl(nameReference); |
| 56 | + |
| 57 | + typeNameOccurrence.setImplicitConstructorNameOccurrence(constructorNameOccurrence); |
| 58 | + nameReference.setNameOccurrence(typeNameOccurrence); |
| 59 | + |
| 60 | + assertThat(node.getTypeNameOccurrence()).isEqualTo(typeNameOccurrence); |
| 61 | + assertThat(node.getConstructorNameOccurrence()).isEqualTo(constructorNameOccurrence); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testNonAttributeNameOccurrence() { |
| 66 | + AttributeNode node = parse("[Foo]"); |
| 67 | + |
| 68 | + assertThat(node.getNameReference()).isNotNull(); |
| 69 | + assertThat(node.getTypeNameOccurrence()).isNull(); |
| 70 | + assertThat(node.getConstructorNameOccurrence()).isNull(); |
| 71 | + |
| 72 | + var nameReference = (NameReferenceNodeImpl) node.getNameReference(); |
| 73 | + nameReference.setNameOccurrence(new NameOccurrenceImpl(nameReference)); |
| 74 | + |
| 75 | + assertThat(node.getTypeNameOccurrence()).isNull(); |
| 76 | + assertThat(node.getConstructorNameOccurrence()).isNull(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testAssemblyAttribute() { |
| 81 | + AttributeNode node = parse("[assembly : Foo]"); |
| 82 | + |
| 83 | + assertThat(node).isNotNull(); |
| 84 | + assertThat(node.isAssembly()).isTrue(); |
| 85 | + assertThat(node.getImage()).isEqualTo("assembly : Foo"); |
| 86 | + assertThat(node.getNameReference()).isNotNull(); |
| 87 | + assertThat(node.getArgumentList()).isNull(); |
| 88 | + } |
| 89 | + |
| 90 | + @ValueSource( |
| 91 | + strings = { |
| 92 | + "'{B5D90CF6-B2C9-473D-9DB9-1BB75EAFC517}'", |
| 93 | + "'{B5D90CF6-B2C9-473D-' + '9DB9-1BB75EAFC517}'" |
| 94 | + }) |
| 95 | + @ParameterizedTest |
| 96 | + void testExpressionAttribute(String expression) { |
| 97 | + AttributeNode node = parse("[" + expression + "]"); |
| 98 | + |
| 99 | + assertThat(node).isNotNull(); |
| 100 | + assertThat(node.isAssembly()).isFalse(); |
| 101 | + assertThat(node.getImage()).isEqualTo(expression); |
| 102 | + assertThat(node.getNameReference()).isNull(); |
| 103 | + assertThat(node.getArgumentList()).isNull(); |
| 104 | + assertThat(node.getTypeNameOccurrence()).isNull(); |
| 105 | + assertThat(node.getConstructorNameOccurrence()).isNull(); |
| 106 | + } |
| 107 | + |
| 108 | + private static AttributeNode parse(String attribute) { |
| 109 | + return DelphiFileUtils.parse( |
| 110 | + "unit Test;", |
| 111 | + "", |
| 112 | + "interface", |
| 113 | + "", |
| 114 | + "type", |
| 115 | + (" " + attribute), |
| 116 | + " TFoo = record", |
| 117 | + " end;", |
| 118 | + "", |
| 119 | + "implementation", |
| 120 | + "end.") |
| 121 | + .getAst() |
| 122 | + .getFirstDescendantOfType(AttributeNode.class); |
| 123 | + } |
| 124 | +} |
0 commit comments