Changeset View
Changeset View
Standalone View
Standalone View
duchain/tests/uses.cpp
Show All 16 Lines | |||||
17 | */ | 17 | */ | ||
18 | 18 | | |||
19 | #include "uses.h" | 19 | #include "uses.h" | ||
20 | 20 | | |||
21 | #include <QtTest/QtTest> | 21 | #include <QtTest/QtTest> | ||
22 | 22 | | |||
23 | #include <language/duchain/duchain.h> | 23 | #include <language/duchain/duchain.h> | ||
24 | #include <language/duchain/duchainlock.h> | 24 | #include <language/duchain/duchainlock.h> | ||
25 | #include <language/duchain/problem.h> | ||||
25 | 26 | | |||
26 | #include "../declarations/classdeclaration.h" | 27 | #include "../declarations/classdeclaration.h" | ||
27 | #include "../declarations/variabledeclaration.h" | 28 | #include "../declarations/variabledeclaration.h" | ||
28 | #include "../declarations/traitmethodaliasdeclaration.h" | 29 | #include "../declarations/traitmethodaliasdeclaration.h" | ||
29 | #include "../declarations/traitmemberaliasdeclaration.h" | 30 | #include "../declarations/traitmemberaliasdeclaration.h" | ||
30 | 31 | | |||
32 | #include "../types/structuretype.h" | ||||
33 | | ||||
31 | using namespace KDevelop; | 34 | using namespace KDevelop; | ||
32 | 35 | | |||
33 | QTEST_MAIN(Php::TestUses) | 36 | QTEST_MAIN(Php::TestUses) | ||
34 | 37 | | |||
35 | namespace Php | 38 | namespace Php | ||
36 | { | 39 | { | ||
37 | 40 | | |||
38 | void compareUses(Declaration* dec, QList<RangeInRevision> ranges) | 41 | void compareUses(Declaration* dec, QList<RangeInRevision> ranges) | ||
▲ Show 20 Lines • Show All 1097 Lines • ▼ Show 20 Line(s) | 1130 | { | |||
1136 | 1139 | | |||
1137 | QCOMPARE(top->localDeclarations().count(), 3); | 1140 | QCOMPARE(top->localDeclarations().count(), 3); | ||
1138 | 1141 | | |||
1139 | Declaration* a = top->localDeclarations().at(0); | 1142 | Declaration* a = top->localDeclarations().at(0); | ||
1140 | QCOMPARE(a->identifier().toString(), QString("a")); | 1143 | QCOMPARE(a->identifier().toString(), QString("a")); | ||
1141 | compareUses(a, QList<RangeInRevision>() | 1144 | compareUses(a, QList<RangeInRevision>() | ||
1142 | << RangeInRevision(1, 9, 1, 10) | 1145 | << RangeInRevision(1, 9, 1, 10) | ||
1143 | << RangeInRevision(2, 19, 2, 20)); | 1146 | << RangeInRevision(2, 19, 2, 20)); | ||
1147 | } | ||||
1148 | | ||||
1149 | void TestUses::instanceofClassProperty() | ||||
1150 | { | ||||
1151 | // 0 1 2 3 4 5 | ||||
1152 | // 012345678901234567890123456789012345678901234567890123456789 | ||||
1153 | TopDUContext* top = parse("<? class A { /** @var B **/ public $foo; }\n" | ||||
1154 | "class B { /** @var A **/ public $bar; }\n" | ||||
1155 | "$a = new A(); $a instanceof $a->foo->bar->foo;\n", DumpNone); | ||||
1156 | | ||||
1157 | QVERIFY(top); | ||||
1158 | DUChainReleaser releaseTop(top); | ||||
1159 | DUChainWriteLocker lock; | ||||
1160 | | ||||
1161 | QVERIFY(top->problems().isEmpty()); | ||||
1162 | | ||||
1163 | QVERIFY(!top->parentContext()); | ||||
1164 | QCOMPARE(top->childContexts().count(), 2); | ||||
1165 | QCOMPARE(top->localDeclarations().count(), 3); | ||||
1166 | | ||||
1167 | Declaration* dec = top->localDeclarations().at(0); | ||||
1168 | QCOMPARE(dec->identifier(), Identifier("a")); | ||||
1169 | compareUses(dec, QList<RangeInRevision>() | ||||
1170 | << RangeInRevision(2, 9, 2, 10)); | ||||
1171 | | ||||
1172 | dec = top->localDeclarations().at(2); | ||||
1173 | QCOMPARE(dec->identifier(), Identifier("a")); | ||||
1174 | StructureType::Ptr classType = dec->type<StructureType>(); | ||||
1175 | QVERIFY(classType); | ||||
1176 | QCOMPARE(classType->qualifiedIdentifier(), QualifiedIdentifier("a")); | ||||
1177 | QVERIFY(classType->equals(dec->abstractType().data())); | ||||
1178 | compareUses(dec, QList<RangeInRevision>() | ||||
1179 | << RangeInRevision(2, 14, 2, 16) | ||||
1180 | << RangeInRevision(2, 28, 2, 30)); | ||||
1181 | | ||||
1182 | dec = top->childContexts().at(0)->localDeclarations().at(0); | ||||
1183 | QVERIFY(dec); | ||||
1184 | QCOMPARE(dec->identifier(), Identifier("foo")); | ||||
1185 | compareUses(dec, QList<RangeInRevision>() | ||||
1186 | << RangeInRevision(2, 32, 2, 35) | ||||
1187 | << RangeInRevision(2, 42, 2, 45)); | ||||
1188 | | ||||
1189 | dec = top->childContexts().at(1)->localDeclarations().at(0); | ||||
1190 | QVERIFY(dec); | ||||
1191 | QCOMPARE(dec->identifier(), Identifier("bar")); | ||||
1192 | compareUses(dec, QList<RangeInRevision>() | ||||
1193 | << RangeInRevision(2, 37, 2, 40)); | ||||
1194 | } | ||||
1195 | | ||||
1196 | void TestUses::instanceofStaticProperty() | ||||
1197 | { | ||||
1198 | // 0 1 2 3 4 5 | ||||
1199 | // 012345678901234567890123456789012345678901234567890123456789 | ||||
1200 | TopDUContext* top = parse("<? class A { /** @var B **/ public static $foo; }\n" | ||||
1201 | "class B { /** @var A **/ public static $bar; }\n" | ||||
1202 | "$a = new A(); $a instanceof A::$foo::$bar::$foo;\n", DumpNone); | ||||
1203 | | ||||
1204 | QVERIFY(top); | ||||
1205 | DUChainReleaser releaseTop(top); | ||||
1206 | DUChainWriteLocker lock; | ||||
1207 | | ||||
1208 | QVERIFY(top->problems().isEmpty()); | ||||
1209 | | ||||
1210 | QVERIFY(!top->parentContext()); | ||||
1211 | QCOMPARE(top->childContexts().count(), 2); | ||||
1212 | QCOMPARE(top->localDeclarations().count(), 3); | ||||
1213 | | ||||
1214 | Declaration* dec = top->localDeclarations().at(0); | ||||
1215 | QCOMPARE(dec->identifier(), Identifier("a")); | ||||
1216 | compareUses(dec, QList<RangeInRevision>() | ||||
1217 | << RangeInRevision(2, 9, 2, 10) | ||||
1218 | << RangeInRevision(2, 28, 2, 29)); | ||||
1219 | | ||||
1220 | dec = top->localDeclarations().at(2); | ||||
1221 | QCOMPARE(dec->identifier(), Identifier("a")); | ||||
1222 | StructureType::Ptr classType = dec->type<StructureType>(); | ||||
1223 | QVERIFY(classType); | ||||
1224 | QCOMPARE(classType->qualifiedIdentifier(), QualifiedIdentifier("a")); | ||||
1225 | QVERIFY(classType->equals(dec->abstractType().data())); | ||||
1226 | compareUses(dec, QList<RangeInRevision>() | ||||
1227 | << RangeInRevision(2, 14, 2, 16)); | ||||
1228 | | ||||
1229 | dec = top->childContexts().at(0)->localDeclarations().at(0); | ||||
1230 | QVERIFY(dec); | ||||
1231 | QCOMPARE(dec->identifier(), Identifier("foo")); | ||||
1232 | compareUses(dec, QList<RangeInRevision>() | ||||
1233 | << RangeInRevision(2, 31, 2, 35) | ||||
1234 | << RangeInRevision(2, 43, 2, 47)); | ||||
1235 | | ||||
1236 | dec = top->childContexts().at(1)->localDeclarations().at(0); | ||||
1237 | QVERIFY(dec); | ||||
1238 | QCOMPARE(dec->identifier(), Identifier("bar")); | ||||
1239 | compareUses(dec, QList<RangeInRevision>() | ||||
1240 | << RangeInRevision(2, 37, 2, 41)); | ||||
1241 | } | ||||
1242 | | ||||
1243 | void TestUses::instanceofMixedProperty() | ||||
1244 | { | ||||
1245 | // 0 1 2 3 4 5 | ||||
1246 | // 012345678901234567890123456789012345678901234567890123456789 | ||||
1247 | TopDUContext* top = parse("<? class A { /** @var B **/ public static $foo; }\n" | ||||
1248 | "class B { /** @var A **/ public $bar; }\n" | ||||
1249 | "$a = new A(); $a instanceof A::$foo->bar::$foo;\n", DumpNone); | ||||
1250 | | ||||
1251 | QVERIFY(top); | ||||
1252 | DUChainReleaser releaseTop(top); | ||||
1253 | DUChainWriteLocker lock; | ||||
1254 | | ||||
1255 | QVERIFY(top->problems().isEmpty()); | ||||
1256 | | ||||
1257 | QVERIFY(!top->parentContext()); | ||||
1258 | QCOMPARE(top->childContexts().count(), 2); | ||||
1259 | QCOMPARE(top->localDeclarations().count(), 3); | ||||
1260 | | ||||
1261 | Declaration* dec = top->localDeclarations().at(0); | ||||
1262 | QCOMPARE(dec->identifier(), Identifier("a")); | ||||
1263 | compareUses(dec, QList<RangeInRevision>() | ||||
1264 | << RangeInRevision(2, 9, 2, 10) | ||||
1265 | << RangeInRevision(2, 28, 2, 29)); | ||||
1266 | | ||||
1267 | dec = top->localDeclarations().at(2); | ||||
1268 | QCOMPARE(dec->identifier(), Identifier("a")); | ||||
1269 | StructureType::Ptr classType = dec->type<StructureType>(); | ||||
1270 | QVERIFY(classType); | ||||
1271 | QCOMPARE(classType->qualifiedIdentifier(), QualifiedIdentifier("a")); | ||||
1272 | QVERIFY(classType->equals(dec->abstractType().data())); | ||||
1273 | compareUses(dec, QList<RangeInRevision>() | ||||
1274 | << RangeInRevision(2, 14, 2, 16)); | ||||
1275 | | ||||
1276 | dec = top->childContexts().at(0)->localDeclarations().at(0); | ||||
1277 | QVERIFY(dec); | ||||
1278 | QCOMPARE(dec->identifier(), Identifier("foo")); | ||||
1279 | compareUses(dec, QList<RangeInRevision>() | ||||
1280 | << RangeInRevision(2, 31, 2, 35) | ||||
1281 | << RangeInRevision(2, 42, 2, 46)); | ||||
1282 | | ||||
1283 | dec = top->childContexts().at(1)->localDeclarations().at(0); | ||||
1284 | QVERIFY(dec); | ||||
1285 | QCOMPARE(dec->identifier(), Identifier("bar")); | ||||
1286 | compareUses(dec, QList<RangeInRevision>() | ||||
1287 | << RangeInRevision(2, 37, 2, 40)); | ||||
1288 | } | ||||
1289 | | ||||
1290 | void TestUses::instanceofVariableProperty() | ||||
1291 | { | ||||
1292 | // 0 1 2 3 4 5 | ||||
1293 | // 012345678901234567890123456789012345678901234567890123456789 | ||||
1294 | TopDUContext* top = parse("<? class A { /** @var B **/ public $foo; }\n" | ||||
1295 | "class B { /** @var A **/ public $bar; }\n" | ||||
1296 | "$foo = 'foo'; $bar = 'bar';\n" | ||||
1297 | "$a = new A(); $a instanceof $a->$foo->$bar->$foo;\n", DumpNone); | ||||
1298 | | ||||
1299 | QVERIFY(top); | ||||
1300 | DUChainReleaser releaseTop(top); | ||||
1301 | DUChainWriteLocker lock; | ||||
1302 | | ||||
1303 | QVERIFY(top->problems().isEmpty()); | ||||
1304 | | ||||
1305 | QVERIFY(!top->parentContext()); | ||||
1306 | QCOMPARE(top->childContexts().count(), 2); | ||||
1307 | QCOMPARE(top->localDeclarations().count(), 5); | ||||
1308 | | ||||
1309 | Declaration* dec = top->localDeclarations().at(0); | ||||
1310 | QCOMPARE(dec->identifier(), Identifier("a")); | ||||
1311 | compareUses(dec, QList<RangeInRevision>() | ||||
1312 | << RangeInRevision(3, 9, 3, 10)); | ||||
1313 | | ||||
1314 | dec = top->localDeclarations().at(4); | ||||
1315 | QCOMPARE(dec->identifier(), Identifier("a")); | ||||
1316 | StructureType::Ptr classType = dec->type<StructureType>(); | ||||
1317 | QVERIFY(classType); | ||||
1318 | QCOMPARE(classType->qualifiedIdentifier(), QualifiedIdentifier("a")); | ||||
1319 | QVERIFY(classType->equals(dec->abstractType().data())); | ||||
1320 | compareUses(dec, QList<RangeInRevision>() | ||||
1321 | << RangeInRevision(3, 14, 3, 16) | ||||
1322 | << RangeInRevision(3, 28, 3, 30)); | ||||
1323 | | ||||
1324 | dec = top->localDeclarations().at(2); | ||||
1325 | QCOMPARE(dec->identifier(), Identifier("foo")); | ||||
1326 | compareUses(dec, QList<RangeInRevision>() | ||||
1327 | << RangeInRevision(3, 32, 3, 36) | ||||
1328 | << RangeInRevision(3, 44, 3, 48)); | ||||
1329 | | ||||
1330 | dec = top->localDeclarations().at(3); | ||||
1331 | QCOMPARE(dec->identifier(), Identifier("bar")); | ||||
1332 | compareUses(dec, QList<RangeInRevision>() | ||||
1333 | << RangeInRevision(3, 38, 3, 42)); | ||||
1334 | } | ||||
1335 | | ||||
1336 | void TestUses::instanceofPropertyArrayAccess() | ||||
1337 | { | ||||
1338 | // 0 1 2 3 4 5 | ||||
1339 | // 012345678901234567890123456789012345678901234567890123456789 | ||||
1340 | TopDUContext* top = parse("<? class A { /** @var array **/ public $foo; }\n" | ||||
1341 | "class B { /** @var array **/ public static $bar; }\n" | ||||
1342 | "$a = new A(); $a instanceof $a->foo[0]::$bar[0]->foo;\n", DumpNone); | ||||
1343 | | ||||
1344 | QVERIFY(top); | ||||
1345 | DUChainReleaser releaseTop(top); | ||||
1346 | DUChainWriteLocker lock; | ||||
1347 | | ||||
1348 | QVERIFY(top->problems().isEmpty()); | ||||
1349 | | ||||
1350 | QVERIFY(!top->parentContext()); | ||||
1351 | QCOMPARE(top->childContexts().count(), 2); | ||||
1352 | QCOMPARE(top->localDeclarations().count(), 3); | ||||
1353 | | ||||
1354 | Declaration* dec = top->localDeclarations().at(0); | ||||
1355 | QCOMPARE(dec->identifier(), Identifier("a")); | ||||
1356 | compareUses(dec, QList<RangeInRevision>() | ||||
1357 | << RangeInRevision(2, 9, 2, 10)); | ||||
1358 | | ||||
1359 | dec = top->localDeclarations().at(2); | ||||
1360 | QCOMPARE(dec->identifier(), Identifier("a")); | ||||
1361 | StructureType::Ptr classType = dec->type<StructureType>(); | ||||
1362 | QVERIFY(classType); | ||||
1363 | QCOMPARE(classType->qualifiedIdentifier(), QualifiedIdentifier("a")); | ||||
1364 | QVERIFY(classType->equals(dec->abstractType().data())); | ||||
1365 | compareUses(dec, QList<RangeInRevision>() | ||||
1366 | << RangeInRevision(2, 14, 2, 16) | ||||
1367 | << RangeInRevision(2, 28, 2, 30)); | ||||
1368 | | ||||
1369 | dec = top->childContexts().at(0)->localDeclarations().at(0); | ||||
1370 | QCOMPARE(dec->identifier(), Identifier("foo")); | ||||
1371 | compareUses(dec, QList<RangeInRevision>() | ||||
1372 | << RangeInRevision(2, 32, 2, 35)); | ||||
1144 | 1373 | | |||
1374 | dec = top->childContexts().at(1)->localDeclarations().at(0); | ||||
1375 | QCOMPARE(dec->identifier(), Identifier("bar")); | ||||
1376 | QVERIFY(dec->uses().isEmpty()); | ||||
1145 | } | 1377 | } | ||
1146 | 1378 | | |||
1147 | void TestUses::classNameString() | 1379 | void TestUses::classNameString() | ||
1148 | { | 1380 | { | ||
1149 | // 0 1 2 3 4 5 6 7 | 1381 | // 0 1 2 3 4 5 6 7 | ||
1150 | // 01234567890123456789012345678901234567890123456789012345678901234567890123456789 | 1382 | // 01234567890123456789012345678901234567890123456789012345678901234567890123456789 | ||
1151 | QByteArray method("<? class Foo { } echo 'Foo';"); | 1383 | QByteArray method("<? class Foo { } echo 'Foo';"); | ||
1152 | TopDUContext* top = parse(method, DumpNone); | 1384 | TopDUContext* top = parse(method, DumpNone); | ||
▲ Show 20 Lines • Show All 205 Lines • Show Last 20 Lines |