|
22 | 22 | import java.util.List;
|
23 | 23 | import org.junit.jupiter.api.Test;
|
24 | 24 | import org.sonar.plugins.python.api.tree.ClassDef;
|
| 25 | +import org.sonar.plugins.python.api.tree.DictionaryLiteral; |
25 | 26 | import org.sonar.plugins.python.api.tree.ExpressionStatement;
|
26 | 27 | import org.sonar.plugins.python.api.tree.FileInput;
|
| 28 | +import org.sonar.plugins.python.api.tree.Name; |
| 29 | +import org.sonar.plugins.python.api.tree.NoneExpression; |
| 30 | +import org.sonar.plugins.python.api.tree.NumericLiteral; |
| 31 | +import org.sonar.plugins.python.api.tree.SetLiteral; |
| 32 | +import org.sonar.plugins.python.api.tree.StringLiteral; |
| 33 | +import org.sonar.plugins.python.api.tree.Tree; |
| 34 | +import org.sonar.plugins.python.api.tree.Tuple; |
27 | 35 | import org.sonar.python.semantic.ProjectLevelSymbolTable;
|
28 | 36 | import org.sonar.python.semantic.v2.ProjectLevelTypeTable;
|
29 | 37 | import org.sonar.python.semantic.v2.SymbolTableBuilderV2;
|
30 | 38 | import org.sonar.python.semantic.v2.TypeInferenceV2;
|
31 |
| - |
32 |
| -import static org.sonar.python.PythonTestUtils.parseWithoutSymbols; |
| 39 | +import org.sonar.python.tree.TreeUtils; |
33 | 40 |
|
34 | 41 | import static org.assertj.core.api.Assertions.assertThat;
|
| 42 | +import static org.sonar.python.PythonTestUtils.parseWithoutSymbols; |
35 | 43 |
|
36 | 44 |
|
37 | 45 | public class ObjectTypeTest {
|
@@ -87,6 +95,75 @@ def foo(self): ...
|
87 | 95 | assertThat(aType.hasMember("foo")).isEqualTo(TriBool.UNKNOWN);
|
88 | 96 | }
|
89 | 97 |
|
| 98 | + @Test |
| 99 | + void literalTypes() { |
| 100 | + FileInput fileInput = parseAndInferTypes("\"hello\""); |
| 101 | + StringLiteral stringLiteral = (StringLiteral) TreeUtils.firstChild(fileInput, t -> t.is(Tree.Kind.STRING_LITERAL)).get(); |
| 102 | + ObjectType stringLiteralType = (ObjectType) stringLiteral.typeV2(); |
| 103 | + assertThat(stringLiteralType.displayName()).isEqualTo("str"); |
| 104 | + |
| 105 | + fileInput = parseAndInferTypes("(1, 2, 3)"); |
| 106 | + Tuple intTuple = (Tuple) TreeUtils.firstChild(fileInput, t -> t.is(Tree.Kind.TUPLE)).get(); |
| 107 | + ObjectType intTupleType = (ObjectType) intTuple.typeV2(); |
| 108 | + assertThat(intTupleType.displayName()).isEqualTo("tuple"); |
| 109 | + assertThat(intTupleType.attributes()).extracting(PythonType::displayName).containsExactly("int"); |
| 110 | + |
| 111 | + fileInput = parseAndInferTypes("(1, \"hello\")"); |
| 112 | + Tuple intStrTuple = (Tuple) TreeUtils.firstChild(fileInput, t -> t.is(Tree.Kind.TUPLE)).get(); |
| 113 | + ObjectType intStrTupleType = (ObjectType) intStrTuple.typeV2(); |
| 114 | + assertThat(intStrTupleType.displayName()).isEqualTo("tuple"); |
| 115 | + assertThat(intStrTupleType.attributes()).isEmpty(); |
| 116 | + |
| 117 | + fileInput = parseAndInferTypes("(foo(),)"); |
| 118 | + Tuple unknownTuple = (Tuple) TreeUtils.firstChild(fileInput, t -> t.is(Tree.Kind.TUPLE)).get(); |
| 119 | + ObjectType unknownTupleType = (ObjectType) unknownTuple.typeV2(); |
| 120 | + assertThat(unknownTupleType.displayName()).isEqualTo("tuple"); |
| 121 | + assertThat(unknownTupleType.attributes()).isEmpty(); |
| 122 | + |
| 123 | + fileInput = parseAndInferTypes("{1, 2, 3}"); |
| 124 | + SetLiteral setLiteral = (SetLiteral) TreeUtils.firstChild(fileInput, t -> t.is(Tree.Kind.SET_LITERAL)).get(); |
| 125 | + ObjectType setLiteralType = (ObjectType) setLiteral.typeV2(); |
| 126 | + assertThat(setLiteralType.displayName()).isEqualTo("set"); |
| 127 | + |
| 128 | + fileInput = parseAndInferTypes("{\"my_key\": 42}"); |
| 129 | + DictionaryLiteral dictionaryLiteral = (DictionaryLiteral) TreeUtils.firstChild(fileInput, t -> t.is(Tree.Kind.DICTIONARY_LITERAL)).get(); |
| 130 | + ObjectType dictionaryLiteralType = (ObjectType) dictionaryLiteral.typeV2(); |
| 131 | + assertThat(dictionaryLiteralType.displayName()).isEqualTo("dict"); |
| 132 | + |
| 133 | + fileInput = parseAndInferTypes("None"); |
| 134 | + NoneExpression noneExpression = (NoneExpression) TreeUtils.firstChild(fileInput, t -> t.is(Tree.Kind.NONE)).get(); |
| 135 | + ObjectType noneExpressionType = (ObjectType) noneExpression.typeV2(); |
| 136 | + assertThat(noneExpressionType.displayName()).isEqualTo("NoneType"); |
| 137 | + |
| 138 | + fileInput = parseAndInferTypes("unknown"); |
| 139 | + Name name = (Name) TreeUtils.firstChild(fileInput, t -> t.is(Tree.Kind.NAME)).get(); |
| 140 | + UnknownType nameType = (UnknownType) name.typeV2(); |
| 141 | + assertThat(nameType.displayName()).isEqualTo("UnknownType"); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void numericLiteralTypes() { |
| 146 | + FileInput fileInput = parseAndInferTypes("42"); |
| 147 | + NumericLiteral intLiteral = (NumericLiteral) TreeUtils.firstChild(fileInput, t -> t.is(Tree.Kind.NUMERIC_LITERAL)).get(); |
| 148 | + ObjectType intLiteralType = (ObjectType) intLiteral.typeV2(); |
| 149 | + assertThat(intLiteralType.displayName()).isEqualTo("int"); |
| 150 | + |
| 151 | + fileInput = parseAndInferTypes("42.5"); |
| 152 | + NumericLiteral floatLiteral = (NumericLiteral) TreeUtils.firstChild(fileInput, t -> t.is(Tree.Kind.NUMERIC_LITERAL)).get(); |
| 153 | + ObjectType floatLiteralType = (ObjectType) floatLiteral.typeV2(); |
| 154 | + assertThat(floatLiteralType.displayName()).isEqualTo("float"); |
| 155 | + |
| 156 | + fileInput = parseAndInferTypes("4e2"); |
| 157 | + NumericLiteral exponentLiteral = (NumericLiteral) TreeUtils.firstChild(fileInput, t -> t.is(Tree.Kind.NUMERIC_LITERAL)).get(); |
| 158 | + ObjectType exponentLiteralType = (ObjectType) exponentLiteral.typeV2(); |
| 159 | + assertThat(exponentLiteralType.displayName()).isEqualTo("float"); |
| 160 | + |
| 161 | + fileInput = parseAndInferTypes("42j"); |
| 162 | + NumericLiteral complexLiteral = (NumericLiteral) TreeUtils.firstChild(fileInput, t -> t.is(Tree.Kind.NUMERIC_LITERAL)).get(); |
| 163 | + ObjectType complexLiteralType = (ObjectType) complexLiteral.typeV2(); |
| 164 | + assertThat(complexLiteralType.displayName()).isEqualTo("complex"); |
| 165 | + } |
| 166 | + |
90 | 167 | @Test
|
91 | 168 | void objectType_of_unknown() {
|
92 | 169 | // TODO: Ensure this is the behavior we want (do we even want it possible to have object of unknown? Maybe replace with UnionType when implemented
|
|
0 commit comments