|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2021 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.python.semantic; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | +import org.sonar.plugins.python.api.symbols.Symbol; |
| 24 | +import org.sonar.plugins.python.api.symbols.Usage; |
| 25 | +import org.sonar.plugins.python.api.tree.AsPattern; |
| 26 | +import org.sonar.plugins.python.api.tree.CapturePattern; |
| 27 | +import org.sonar.plugins.python.api.tree.CaseBlock; |
| 28 | +import org.sonar.plugins.python.api.tree.ClassPattern; |
| 29 | +import org.sonar.plugins.python.api.tree.DoubleStarPattern; |
| 30 | +import org.sonar.plugins.python.api.tree.GroupPattern; |
| 31 | +import org.sonar.plugins.python.api.tree.KeyValuePattern; |
| 32 | +import org.sonar.plugins.python.api.tree.KeywordPattern; |
| 33 | +import org.sonar.plugins.python.api.tree.MappingPattern; |
| 34 | +import org.sonar.plugins.python.api.tree.Name; |
| 35 | +import org.sonar.plugins.python.api.tree.OrPattern; |
| 36 | +import org.sonar.plugins.python.api.tree.Pattern; |
| 37 | +import org.sonar.plugins.python.api.tree.QualifiedExpression; |
| 38 | +import org.sonar.plugins.python.api.tree.SequencePattern; |
| 39 | +import org.sonar.plugins.python.api.tree.StarPattern; |
| 40 | +import org.sonar.plugins.python.api.tree.ValuePattern; |
| 41 | +import org.sonar.python.PythonTestUtils; |
| 42 | + |
| 43 | +import static org.assertj.core.api.Assertions.assertThat; |
| 44 | +import static org.sonar.plugins.python.api.symbols.Usage.Kind.CLASS_DECLARATION; |
| 45 | +import static org.sonar.plugins.python.api.symbols.Usage.Kind.OTHER; |
| 46 | +import static org.sonar.plugins.python.api.symbols.Usage.Kind.PATTERN_DECLARATION; |
| 47 | +import static org.sonar.plugins.python.api.tree.Tree.Kind.CASE_BLOCK; |
| 48 | +import static org.sonar.plugins.python.api.tree.Tree.Kind.NAME; |
| 49 | +import static org.sonar.python.PythonTestUtils.getLastDescendant; |
| 50 | + |
| 51 | +public class MatchStatementSymbolsTest { |
| 52 | + |
| 53 | + @Test |
| 54 | + public void capture_pattern() { |
| 55 | + CapturePattern capturePattern = patternFromCase("case others: print(others)"); |
| 56 | + Symbol others = capturePattern.name().symbol(); |
| 57 | + assertThat(others.name()).isEqualTo("others"); |
| 58 | + assertThat(others.fullyQualifiedName()).isNull(); |
| 59 | + assertThat(others.usages()).extracting(Usage::kind).containsExactly(PATTERN_DECLARATION, OTHER); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void wildcard_pattern() { |
| 64 | + Name wildcard = getLastDescendant(PythonTestUtils.parse( |
| 65 | + "def foo(value):", |
| 66 | + " match(value):", |
| 67 | + " case _: print(_)" |
| 68 | + ), t -> t.is(NAME) && ((Name) t).name().equals("_")); |
| 69 | + assertThat(wildcard.symbol()).isNull(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void class_pattern() { |
| 74 | + ClassPattern classPattern = pattern( |
| 75 | + "class A:", |
| 76 | + " foo = 42", |
| 77 | + "def foo(value):", |
| 78 | + " match(value):", |
| 79 | + " case A(foo=x):", |
| 80 | + " print(x)"); |
| 81 | + Name name = (Name) classPattern.targetClass(); |
| 82 | + assertThat(name.symbol().kind()).isEqualTo(Symbol.Kind.CLASS); |
| 83 | + KeywordPattern keywordPattern = ((KeywordPattern) classPattern.arguments().get(0)); |
| 84 | + Symbol x = ((CapturePattern) keywordPattern.pattern()).name().symbol(); |
| 85 | + assertThat(x.usages()).extracting(Usage::kind).containsExactly(PATTERN_DECLARATION, OTHER); |
| 86 | + |
| 87 | + classPattern = pattern( |
| 88 | + "import mod", |
| 89 | + "def foo(value):", |
| 90 | + " match(value):", |
| 91 | + " case mod.A(foo=x):", |
| 92 | + " print(x)"); |
| 93 | + QualifiedExpression qualifiedExpression = (QualifiedExpression) classPattern.targetClass(); |
| 94 | + assertThat(qualifiedExpression.symbol().kind()).isEqualTo(Symbol.Kind.OTHER); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void value_pattern() { |
| 99 | + ValuePattern valuePattern = pattern( |
| 100 | + "import command", |
| 101 | + "def foo(value):", |
| 102 | + " match(value):", |
| 103 | + " case command.QUIT:", |
| 104 | + " print(x)"); |
| 105 | + assertThat(valuePattern.qualifiedExpression().symbol().kind()).isEqualTo(Symbol.Kind.OTHER); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void as_pattern() { |
| 110 | + AsPattern asPattern = patternFromCase("case 42 as x: ..."); |
| 111 | + Symbol symbol = asPattern.alias().name().symbol(); |
| 112 | + assertThat(symbol.name()).isEqualTo("x"); |
| 113 | + assertThat(symbol.fullyQualifiedName()).isNull(); |
| 114 | + assertThat(symbol.usages()).extracting(Usage::kind).containsExactly(PATTERN_DECLARATION); |
| 115 | + |
| 116 | + asPattern = patternFromCase("case z as x: return x + z"); |
| 117 | + Symbol z = ((CapturePattern) asPattern.pattern()).name().symbol(); |
| 118 | + Symbol x = asPattern.alias().name().symbol(); |
| 119 | + assertThat(z.name()).isEqualTo("z"); |
| 120 | + assertThat(x.name()).isEqualTo("x"); |
| 121 | + assertThat(z.usages()).extracting(Usage::kind).containsExactly(PATTERN_DECLARATION, OTHER); |
| 122 | + assertThat(x.usages()).extracting(Usage::kind).containsExactly(PATTERN_DECLARATION, OTHER); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void or_pattern() { |
| 127 | + OrPattern orPattern = pattern( |
| 128 | + "class A: ...", |
| 129 | + "class B: ...", |
| 130 | + "def foo(value):", |
| 131 | + " match(value):", |
| 132 | + " case A() | B(): ..."); |
| 133 | + Symbol symbolA = ((Name) ((ClassPattern) orPattern.patterns().get(0)).targetClass()).symbol(); |
| 134 | + Symbol symbolB = ((Name) ((ClassPattern) orPattern.patterns().get(1)).targetClass()).symbol(); |
| 135 | + assertThat(symbolA.usages()).extracting(Usage::kind).containsExactly(CLASS_DECLARATION, OTHER); |
| 136 | + assertThat(symbolB.usages()).extracting(Usage::kind).containsExactly(CLASS_DECLARATION, OTHER); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void sequence_pattern() { |
| 141 | + SequencePattern sequencePattern = patternFromCase("case [42, x, *others]: ..."); |
| 142 | + CapturePattern capturePattern = (CapturePattern) sequencePattern.elements().get(1); |
| 143 | + StarPattern starPattern = (StarPattern) sequencePattern.elements().get(2); |
| 144 | + assertThat(capturePattern.name().symbol()).isNotNull(); |
| 145 | + assertThat(((CapturePattern) starPattern.pattern()).name().symbol()).isNotNull(); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void mapping_pattern() { |
| 150 | + MappingPattern mappingPattern = patternFromCase("case {'x': 'foo', 'y': val, **others}: ..."); |
| 151 | + KeyValuePattern keyValuePattern = (KeyValuePattern) mappingPattern.elements().get(1); |
| 152 | + CapturePattern capturePattern = (CapturePattern) keyValuePattern.value(); |
| 153 | + DoubleStarPattern doubleStarPattern = (DoubleStarPattern) mappingPattern.elements().get(2); |
| 154 | + assertThat(capturePattern.name().symbol()).isNotNull(); |
| 155 | + assertThat(doubleStarPattern.capturePattern().name().symbol()).isNotNull(); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void group_pattern() { |
| 160 | + GroupPattern groupPattern = patternFromCase("case (x): ..."); |
| 161 | + CapturePattern capturePattern = (CapturePattern) groupPattern.pattern(); |
| 162 | + assertThat(capturePattern.name().symbol()).isNotNull(); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void guard() { |
| 167 | + CapturePattern capturePattern = patternFromCase("case x if x > 42: ..."); |
| 168 | + assertThat(capturePattern.name().symbol().usages()).extracting(Usage::kind).containsExactly(PATTERN_DECLARATION, OTHER); |
| 169 | + } |
| 170 | + |
| 171 | + @SuppressWarnings("unchecked") |
| 172 | + private static <T extends Pattern> T pattern(String... lines) { |
| 173 | + String code = String.join(System.getProperty("line.separator"), lines); |
| 174 | + CaseBlock caseBlock = getLastDescendant(PythonTestUtils.parse(code), t -> t.is(CASE_BLOCK)); |
| 175 | + return ((T) caseBlock.pattern()); |
| 176 | + } |
| 177 | + |
| 178 | + @SuppressWarnings("unchecked") |
| 179 | + private static <T extends Pattern> T patternFromCase(String code) { |
| 180 | + CaseBlock caseBlock = getLastDescendant(PythonTestUtils.parse( |
| 181 | + "def foo(value):", |
| 182 | + " match(value):", |
| 183 | + " " + code |
| 184 | + ), t -> t.is(CASE_BLOCK)); |
| 185 | + return (T) caseBlock.pattern(); |
| 186 | + } |
| 187 | +} |
0 commit comments