Skip to content

Commit 4729417

Browse files
authored
#2825. Add tests for resolving extensions (#3360)
Add tests for resolving extensions
1 parent 82f8ef1 commit 4729417

12 files changed

+387
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
/// @assertion With this feature, imports are not global to the entire library,
6+
/// and neither is extension availability.
7+
///
8+
/// Extension availability is defined per file, and an extension is available in
9+
/// a Dart file if any of:
10+
/// - The extension is declared by the library of the Dart file.
11+
/// - The extension is available by import in the Dart file.
12+
/// where an extension is available by import in a Dart file if any of:
13+
/// - That file contains an import directive which imports the extension.
14+
/// - That file is a part file and the extension is (recursively) available by
15+
/// import in its parent file.
16+
///
17+
/// @description Check that an extension declared in a part file is available in
18+
/// its parent file and vice versa.
19+
/// @author [email protected]
20+
21+
// SharedOptions=--enable-experiment=enhanced-parts
22+
23+
import '../../Utils/expect.dart';
24+
25+
part 'resolving_extensions_A01_t01_part1.dart';
26+
27+
class A {}
28+
29+
extension on A {
30+
String get id => "extension from root";
31+
}
32+
33+
class C {}
34+
35+
main() {
36+
Expect.equals("extension from part1", C().getId());
37+
Expect.equals("extension from part2", C().getId2());
38+
testPart1();
39+
testPart2();
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
/// @assertion With this feature, imports are not global to the entire library,
6+
/// and neither is extension availability.
7+
///
8+
/// Extension availability is defined per file, and an extension is available in
9+
/// a Dart file if any of:
10+
/// - The extension is declared by the library of the Dart file.
11+
/// - The extension is available by import in the Dart file.
12+
/// where an extension is available by import in a Dart file if any of:
13+
/// - That file contains an import directive which imports the extension.
14+
/// - That file is a part file and the extension is (recursively) available by
15+
/// import in its parent file.
16+
///
17+
/// @description Check that an extension declared in a part file is available in
18+
/// its parent file and vice versa.
19+
/// @author [email protected]
20+
21+
// SharedOptions=--enable-experiment=enhanced-parts
22+
23+
part of 'resolving_extensions_A01_t01.dart';
24+
part 'resolving_extensions_A01_t01_part2.dart';
25+
26+
extension on C {
27+
String getId() => "extension from part1";
28+
}
29+
30+
testPart1() {
31+
Expect.equals("extension from root", A().id);
32+
Expect.equals("extension from part2", C().getId2());
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
/// @assertion With this feature, imports are not global to the entire library,
6+
/// and neither is extension availability.
7+
///
8+
/// Extension availability is defined per file, and an extension is available in
9+
/// a Dart file if any of:
10+
/// - The extension is declared by the library of the Dart file.
11+
/// - The extension is available by import in the Dart file.
12+
/// where an extension is available by import in a Dart file if any of:
13+
/// - That file contains an import directive which imports the extension.
14+
/// - That file is a part file and the extension is (recursively) available by
15+
/// import in its parent file.
16+
///
17+
/// @description Check that an extension declared in a part file is available in
18+
/// its parent file and vice versa.
19+
/// @author [email protected]
20+
21+
// SharedOptions=--enable-experiment=enhanced-parts
22+
23+
part of 'resolving_extensions_A01_t01_part1.dart';
24+
25+
extension on A {
26+
String getId2() => "extension from part2";
27+
}
28+
29+
testPart2() {
30+
Expect.equals("extension from root", A().id);
31+
Expect.equals("extension from part1", C().getId());
32+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
/// @assertion With this feature, imports are not global to the entire library,
6+
/// and neither is extension availability.
7+
///
8+
/// Extension availability is defined per file, and an extension is available in
9+
/// a Dart file if any of:
10+
/// - The extension is declared by the library of the Dart file.
11+
/// - The extension is available by import in the Dart file.
12+
/// where an extension is available by import in a Dart file if any of:
13+
/// - That file contains an import directive which imports the extension.
14+
/// - That file is a part file and the extension is (recursively) available by
15+
/// import in its parent file.
16+
///
17+
/// @description Check that an extension declared in a part file is available in
18+
/// its parent file and it is a compile-time error if extensions conflict.
19+
/// @author [email protected]
20+
21+
// SharedOptions=--enable-experiment=enhanced-parts
22+
23+
part 'resolving_extensions_A01_t02_part1.dart';
24+
25+
class C {}
26+
27+
extension on C {
28+
String getId() => "extension defined in this file";
29+
String get id => "extension defined in this file";
30+
}
31+
32+
main() {
33+
C().getId();
34+
// ^^^^^
35+
// [analyzer] unspecified
36+
// [cfe] unspecified
37+
38+
C().id; // Ok
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
/// @assertion With this feature, imports are not global to the entire library,
6+
/// and neither is extension availability.
7+
///
8+
/// Extension availability is defined per file, and an extension is available in
9+
/// a Dart file if any of:
10+
/// - The extension is declared by the library of the Dart file.
11+
/// - The extension is available by import in the Dart file.
12+
/// where an extension is available by import in a Dart file if any of:
13+
/// - That file contains an import directive which imports the extension.
14+
/// - That file is a part file and the extension is (recursively) available by
15+
/// import in its parent file.
16+
///
17+
/// @description Check that an extension declared in a part file is available in
18+
/// its parent file and it is a compile-time error if extensions conflict.
19+
/// @author [email protected]
20+
21+
// SharedOptions=--enable-experiment=enhanced-parts
22+
23+
part of 'resolving_extensions_A01_t02.dart';
24+
25+
extension on C {
26+
String getId() => "extension from part";
27+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
/// @assertion With this feature, imports are not global to the entire library,
6+
/// and neither is extension availability.
7+
///
8+
/// Extension availability is defined per file, and an extension is available in
9+
/// a Dart file if any of:
10+
/// - The extension is declared by the library of the Dart file.
11+
/// - The extension is available by import in the Dart file.
12+
/// where an extension is available by import in a Dart file if any of:
13+
/// - That file contains an import directive which imports the extension.
14+
/// - That file is a part file and the extension is (recursively) available by
15+
/// import in its parent file.
16+
///
17+
/// @description Check that an extension available by import in a part file is
18+
/// available in its parts but not available in the parent.
19+
/// @author [email protected]
20+
21+
// SharedOptions=--enable-experiment=enhanced-parts
22+
23+
import '../../Utils/expect.dart';
24+
25+
part 'resolving_extensions_A01_t03_part1.dart';
26+
27+
class C {}
28+
29+
extension Ext on Object {
30+
String get id => "Extension Ext";
31+
}
32+
33+
main() {
34+
testPart1();
35+
testPart2();
36+
// If there is no conflict with the LibExt extension imported in _part1, that
37+
// means LibExt is not available here.
38+
Expect.equals("Extension Ext", C().id);
39+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
/// @assertion With this feature, imports are not global to the entire library,
6+
/// and neither is extension availability.
7+
///
8+
/// Extension availability is defined per file, and an extension is available in
9+
/// a Dart file if any of:
10+
/// - The extension is declared by the library of the Dart file.
11+
/// - The extension is available by import in the Dart file.
12+
/// where an extension is available by import in a Dart file if any of:
13+
/// - That file contains an import directive which imports the extension.
14+
/// - That file is a part file and the extension is (recursively) available by
15+
/// import in its parent file.
16+
///
17+
/// @description Check that an extension available by import in a part file is
18+
/// available in its parts.
19+
/// @author [email protected]
20+
21+
// SharedOptions=--enable-experiment=enhanced-parts
22+
23+
part of 'resolving_extensions_A01_t03.dart';
24+
25+
import 'resolving_extensions_lib.dart';
26+
27+
part 'resolving_extensions_A01_t03_part2.dart';
28+
29+
testPart1() {
30+
Expect.equals("extension LibExt", C().id);
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
/// @assertion With this feature, imports are not global to the entire library,
6+
/// and neither is extension availability.
7+
///
8+
/// Extension availability is defined per file, and an extension is available in
9+
/// a Dart file if any of:
10+
/// - The extension is declared by the library of the Dart file.
11+
/// - The extension is available by import in the Dart file.
12+
/// where an extension is available by import in a Dart file if any of:
13+
/// - That file contains an import directive which imports the extension.
14+
/// - That file is a part file and the extension is (recursively) available by
15+
/// import in its parent file.
16+
///
17+
/// @description Check that an extension available by import in a part file is
18+
/// available in its parts.
19+
/// @author [email protected]
20+
21+
// SharedOptions=--enable-experiment=enhanced-parts
22+
23+
part of 'resolving_extensions_A01_t03_part1.dart';
24+
25+
testPart2() {
26+
Expect.equals("extension LibExt", C().id);
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
/// @assertion With this feature, imports are not global to the entire library,
6+
/// and neither is extension availability.
7+
///
8+
/// Extension availability is defined per file, and an extension is available in
9+
/// a Dart file if any of:
10+
/// - The extension is declared by the library of the Dart file.
11+
/// - The extension is available by import in the Dart file.
12+
/// where an extension is available by import in a Dart file if any of:
13+
/// - That file contains an import directive which imports the extension.
14+
/// - That file is a part file and the extension is (recursively) available by
15+
/// import in its parent file.
16+
///
17+
/// @description Check that an extension available by import in a part file is
18+
/// not available in its parent.
19+
/// @author [email protected]
20+
21+
// SharedOptions=--enable-experiment=enhanced-parts
22+
23+
part 'resolving_extensions_A01_t04_part1.dart';
24+
25+
class C {}
26+
27+
main() {
28+
C().id;
29+
// ^^
30+
// [analyzer] unspecified
31+
// [cfe] unspecified
32+
testPart1();
33+
testPart2();
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
/// @assertion With this feature, imports are not global to the entire library,
6+
/// and neither is extension availability.
7+
///
8+
/// Extension availability is defined per file, and an extension is available in
9+
/// a Dart file if any of:
10+
/// - The extension is declared by the library of the Dart file.
11+
/// - The extension is available by import in the Dart file.
12+
/// where an extension is available by import in a Dart file if any of:
13+
/// - That file contains an import directive which imports the extension.
14+
/// - That file is a part file and the extension is (recursively) available by
15+
/// import in its parent file.
16+
///
17+
/// @description Check that an extension available by import in a part file is
18+
/// not available in its parent.
19+
/// @author [email protected]
20+
21+
// SharedOptions=--enable-experiment=enhanced-parts
22+
23+
part of 'resolving_extensions_A01_t04.dart';
24+
25+
import 'resolving_extensions_lib.dart';
26+
27+
part 'resolving_extensions_A01_t04_part2.dart';
28+
29+
testPart1() {
30+
C().id; // Ok
31+
}

0 commit comments

Comments
 (0)