Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: pkg/analyzer/test/generated/hint_code_test.dart

Issue 2100493002: Allow for library access to `@protected` protected members (linter#254). (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Merge branch 'master' into protected Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer/lib/src/generated/resolver.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library analyzer.test.generated.hint_code_test; 5 library analyzer.test.generated.hint_code_test;
6 6
7 import 'package:analyzer/src/generated/engine.dart'; 7 import 'package:analyzer/src/generated/engine.dart';
8 import 'package:analyzer/src/generated/error.dart'; 8 import 'package:analyzer/src/generated/error.dart';
9 import 'package:analyzer/src/generated/source_io.dart'; 9 import 'package:analyzer/src/generated/source_io.dart';
10 import 'package:unittest/unittest.dart'; 10 import 'package:unittest/unittest.dart';
(...skipping 1250 matching lines...) Expand 10 before | Expand all | Expand 10 after
1261 var p1 = new Point(0, 0); 1261 var p1 = new Point(0, 0);
1262 var p2 = new Point(10, 10); 1262 var p2 = new Point(10, 10);
1263 int n = p1 + p2; 1263 int n = p1 + p2;
1264 }'''); 1264 }''');
1265 computeLibrarySourceErrors(source); 1265 computeLibrarySourceErrors(source);
1266 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]); 1266 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]);
1267 verify([source]); 1267 verify([source]);
1268 } 1268 }
1269 1269
1270 void test_invalidUseOfProtectedMember_closure() { 1270 void test_invalidUseOfProtectedMember_closure() {
1271 Source source = addSource(r''' 1271 Source source = addNamedSource(
1272 '/lib1.dart',
1273 r'''
1272 import 'package:meta/meta.dart'; 1274 import 'package:meta/meta.dart';
1273 1275
1274 class A { 1276 class A {
1275 @protected 1277 @protected
1276 int a() => 42; 1278 int a() => 42;
1277 } 1279 }
1280 ''');
1281 Source source2 = addNamedSource(
1282 '/lib2.dart',
1283 r'''
1284 import 'lib1.dart';
1285
1278 void main() { 1286 void main() {
1279 var leak = new A().a; 1287 var leak = new A().a;
1280 print(leak); 1288 print(leak);
1281 }'''); 1289 }
1282 computeLibrarySourceErrors(source); 1290 ''');
1283 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); 1291 computeLibrarySourceErrors(source2);
1284 verify([source]); 1292 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
1293 assertNoErrors(source);
1294 verify([source, source2]);
1285 } 1295 }
1286 1296
1287 void test_invalidUseOfProtectedMember_field() { 1297 void test_invalidUseOfProtectedMember_field() {
1288 Source source = addSource(r''' 1298 Source source = addNamedSource(
1299 '/lib1.dart',
1300 r'''
1289 import 'package:meta/meta.dart'; 1301 import 'package:meta/meta.dart';
1290 class A { 1302 class A {
1291 @protected 1303 @protected
1292 int a; 1304 int a;
1293 } 1305 }
1306 ''');
1307 Source source2 = addNamedSource(
1308 '/lib2.dart',
1309 r'''
1310 import 'lib1.dart';
1311
1294 abstract class B { 1312 abstract class B {
1295 int b() => new A().a; 1313 int b() => new A().a;
1296 }'''); 1314 }
1297 computeLibrarySourceErrors(source); 1315 ''');
1298 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); 1316 computeLibrarySourceErrors(source2);
1299 verify([source]); 1317 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
1318 assertNoErrors(source);
1319 verify([source, source2]);
1300 } 1320 }
1301 1321
1302 void test_invalidUseOfProtectedMember_field_OK() { 1322 void test_invalidUseOfProtectedMember_field_OK() {
1303 Source source = addSource(r''' 1323 Source source = addSource(r'''
1304 import 'package:meta/meta.dart'; 1324 import 'package:meta/meta.dart';
1305 class A { 1325 class A {
1306 @protected 1326 @protected
1307 int a; 1327 int a;
1308 } 1328 }
1309 abstract class B implements A { 1329 abstract class B implements A {
1310 int b() => a; 1330 int b() => a;
1311 }'''); 1331 }''');
1312 computeLibrarySourceErrors(source); 1332 computeLibrarySourceErrors(source);
1313 assertNoErrors(source); 1333 assertNoErrors(source);
1314 verify([source]); 1334 verify([source]);
1315 } 1335 }
1316 1336
1317 void test_invalidUseOfProtectedMember_function() { 1337 void test_invalidUseOfProtectedMember_function() {
1338 Source source = addNamedSource(
1339 '/lib1.dart',
1340 r'''
1341 import 'package:meta/meta.dart';
1342 class A {
1343 @protected
1344 void a(){ }
1345 }
1346 ''');
1347 Source source2 = addNamedSource(
1348 '/lib2.dart',
1349 r'''
1350 import 'lib1.dart';
1351
1352 main() {
1353 new A().a();
1354 }
1355 ''');
1356 computeLibrarySourceErrors(source2);
1357 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
1358 assertNoErrors(source);
1359 verify([source, source2]);
1360 }
1361
1362 void test_invalidUseOfProtectedMember_function_OK2() {
1318 Source source = addSource(r''' 1363 Source source = addSource(r'''
1319 import 'package:meta/meta.dart'; 1364 import 'package:meta/meta.dart';
1320 class A { 1365 class A {
1321 @protected 1366 @protected
1322 void a(){ } 1367 void a(){ }
1323 } 1368 }
1324 main() { 1369 main() {
1325 new A().a(); 1370 new A().a();
1326 }'''); 1371 }''');
1327 computeLibrarySourceErrors(source); 1372 computeLibrarySourceErrors(source);
1328 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); 1373 assertNoErrors(source);
1329 verify([source]); 1374 verify([source]);
1330 } 1375 }
1331 1376
1332 void test_invalidUseOfProtectedMember_function_OK() { 1377 void test_invalidUseOfProtectedMember_function_OK() {
1333 Source source = addSource(r''' 1378 Source source = addSource(r'''
1334 import 'package:meta/meta.dart'; 1379 import 'package:meta/meta.dart';
1335 class A { 1380 class A {
1336 @protected 1381 @protected
1337 int a() => 0; 1382 int a() => 0;
1338 } 1383 }
1339 1384
1340 abstract class B implements A { 1385 abstract class B implements A {
1341 int b() => a(); 1386 int b() => a();
1342 }'''); 1387 }''');
1343 computeLibrarySourceErrors(source); 1388 computeLibrarySourceErrors(source);
1344 assertNoErrors(source); 1389 assertNoErrors(source);
1345 verify([source]); 1390 verify([source]);
1346 } 1391 }
1347 1392
1348 void test_invalidUseOfProtectedMember_getter() { 1393 void test_invalidUseOfProtectedMember_getter() {
1349 Source source = addSource(r''' 1394 Source source = addNamedSource(
1395 '/lib1.dart',
1396 r'''
1350 import 'package:meta/meta.dart'; 1397 import 'package:meta/meta.dart';
1351 class A { 1398 class A {
1352 @protected 1399 @protected
1353 int get a => 42; 1400 int get a => 42;
1354 } 1401 }
1402 ''');
1403 Source source2 = addNamedSource(
1404 '/lib2.dart',
1405 r'''
1406 import 'lib1.dart';
1407
1355 class B { 1408 class B {
1356 A a; 1409 A a;
1357 int b() => a.a; 1410 int b() => a.a;
1358 }'''); 1411 }
1359 computeLibrarySourceErrors(source); 1412 ''');
1360 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); 1413 computeLibrarySourceErrors(source2);
1361 verify([source]); 1414 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
1415 assertNoErrors(source);
1416 verify([source, source2]);
1362 } 1417 }
1363 1418
1364 void test_invalidUseOfProtectedMember_getter_OK() { 1419 void test_invalidUseOfProtectedMember_getter_OK() {
1365 Source source = addSource(r''' 1420 Source source = addSource(r'''
1366 import 'package:meta/meta.dart'; 1421 import 'package:meta/meta.dart';
1367 class A { 1422 class A {
1368 @protected 1423 @protected
1369 int get a => 42; 1424 int get a => 42;
1370 } 1425 }
1371 abstract class B implements A { 1426 abstract class B implements A {
(...skipping 19 matching lines...) Expand all
1391 1446
1392 /// OK: [A.a], [A.b], [A.c]. 1447 /// OK: [A.a], [A.b], [A.c].
1393 f() {} 1448 f() {}
1394 '''); 1449 ''');
1395 computeLibrarySourceErrors(source); 1450 computeLibrarySourceErrors(source);
1396 assertNoErrors(source); 1451 assertNoErrors(source);
1397 verify([source]); 1452 verify([source]);
1398 } 1453 }
1399 1454
1400 void test_invalidUseOfProtectedMember_message() { 1455 void test_invalidUseOfProtectedMember_message() {
1401 Source source = addSource(r''' 1456 Source source = addNamedSource(
1457 '/lib1.dart',
1458 r'''
1402 import 'package:meta/meta.dart'; 1459 import 'package:meta/meta.dart';
1403 class A { 1460 class A {
1404 @protected 1461 @protected
1405 void a(){ } 1462 void a(){ }
1406 } 1463 }
1464 ''');
1465 Source source2 = addNamedSource(
1466 '/lib2.dart',
1467 r'''
1468 import 'lib1.dart';
1469
1407 class B { 1470 class B {
1408 void b() => new A().a(); 1471 void b() => new A().a();
1409 }'''); 1472 }
1410 List<AnalysisError> errors = analysisContext2.computeErrors(source); 1473 ''');
1474 List<AnalysisError> errors = analysisContext2.computeErrors(source2);
1411 expect(errors, hasLength(1)); 1475 expect(errors, hasLength(1));
1412 expect(errors[0].message, 1476 expect(errors[0].message,
1413 "The member 'a' can only be used within instance members of subclasses o f 'A'"); 1477 "The member 'a' can only be used within instance members of subclasses o f 'A'");
1478 verify([source, source2]);
1414 } 1479 }
1415 1480
1416 void test_invalidUseOfProtectedMember_method_1() { 1481 void test_invalidUseOfProtectedMember_method_1() {
1417 Source source = addSource(r''' 1482 Source source = addNamedSource(
1483 '/lib1.dart',
1484 r'''
1418 import 'package:meta/meta.dart'; 1485 import 'package:meta/meta.dart';
1419 class A { 1486 class A {
1420 @protected 1487 @protected
1421 void a(){ } 1488 void a(){ }
1422 } 1489 }
1490 ''');
1491 Source source2 = addNamedSource(
1492 '/lib2.dart',
1493 r'''
1494 import 'lib1.dart';
1495
1423 class B { 1496 class B {
1424 void b() => new A().a(); 1497 void b() => new A().a();
1425 }'''); 1498 }
1426 computeLibrarySourceErrors(source); 1499 ''');
1427 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); 1500
1428 verify([source]); 1501 computeLibrarySourceErrors(source2);
1502 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
1503 assertNoErrors(source);
1504 verify([source, source2]);
1429 } 1505 }
1430 1506
1431 void test_invalidUseOfProtectedMember_method_OK() { 1507 void test_invalidUseOfProtectedMember_method_OK() {
1432 // https://github.com/dart-lang/linter/issues/257 1508 // https://github.com/dart-lang/linter/issues/257
1433 Source source = addSource(r''' 1509 Source source = addSource(r'''
1434 import 'package:meta/meta.dart'; 1510 import 'package:meta/meta.dart';
1435 1511
1436 typedef void VoidCallback(); 1512 typedef void VoidCallback();
1437 1513
1438 class State<E> { 1514 class State<E> {
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
1574 this.a = a; 1650 this.a = a;
1575 } 1651 }
1576 } 1652 }
1577 '''); 1653 ''');
1578 computeLibrarySourceErrors(source); 1654 computeLibrarySourceErrors(source);
1579 assertNoErrors(source); 1655 assertNoErrors(source);
1580 verify([source]); 1656 verify([source]);
1581 } 1657 }
1582 1658
1583 void test_invalidUseOfProtectedMember_setter() { 1659 void test_invalidUseOfProtectedMember_setter() {
1584 Source source = addSource(r''' 1660 Source source = addNamedSource(
1661 '/lib1.dart',
1662 r'''
1585 import 'package:meta/meta.dart'; 1663 import 'package:meta/meta.dart';
1586 class A { 1664 class A {
1587 @protected 1665 @protected
1588 void set a(int i) { } 1666 void set a(int i) { }
1589 } 1667 }
1668 ''');
1669 Source source2 = addNamedSource(
1670 '/lib2.dart',
1671 r'''
1672 import 'lib1.dart';
1673
1590 class B{ 1674 class B{
1591 A a; 1675 A a;
1592 b(int i) { 1676 b(int i) {
1593 a.a = i; 1677 a.a = i;
1594 } 1678 }
1595 }'''); 1679 }
1596 computeLibrarySourceErrors(source); 1680 ''');
1597 assertErrors(source, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]); 1681 computeLibrarySourceErrors(source2);
1598 verify([source]); 1682 assertErrors(source2, [HintCode.INVALID_USE_OF_PROTECTED_MEMBER]);
1683 assertNoErrors(source);
1684 verify([source, source2]);
1599 } 1685 }
1600 1686
1601 void test_invalidUseOfProtectedMember_setter_OK() { 1687 void test_invalidUseOfProtectedMember_setter_OK() {
1602 Source source = addSource(r''' 1688 Source source = addSource(r'''
1603 import 'package:meta/meta.dart'; 1689 import 'package:meta/meta.dart';
1604 class A { 1690 class A {
1605 @protected 1691 @protected
1606 void set a(int i) { } 1692 void set a(int i) { }
1607 } 1693 }
1608 abstract class B implements A { 1694 abstract class B implements A {
(...skipping 2184 matching lines...) Expand 10 before | Expand all | Expand 10 after
3793 n() { 3879 n() {
3794 var a = m(), b = m(); 3880 var a = m(), b = m();
3795 } 3881 }
3796 }'''); 3882 }''');
3797 computeLibrarySourceErrors(source); 3883 computeLibrarySourceErrors(source);
3798 assertErrors( 3884 assertErrors(
3799 source, [HintCode.USE_OF_VOID_RESULT, HintCode.USE_OF_VOID_RESULT]); 3885 source, [HintCode.USE_OF_VOID_RESULT, HintCode.USE_OF_VOID_RESULT]);
3800 verify([source]); 3886 verify([source]);
3801 } 3887 }
3802 } 3888 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/resolver.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698