Skip to content

Commit 5ea87b2

Browse files
authored
Refactor class_test to use reflective_test_loader (#3681)
1 parent ec9c8e0 commit 5ea87b2

File tree

3 files changed

+146
-144
lines changed

3 files changed

+146
-144
lines changed

test/templates/class_test.dart

Lines changed: 96 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/file_system/memory_file_system.dart';
6-
import 'package:path/path.dart' as path;
75
import 'package:test/test.dart';
86
import 'package:test_reflective_loader/test_reflective_loader.dart';
97

10-
import '../dartdoc_test_base.dart';
11-
import '../src/test_descriptor_utils.dart' as d;
128
import '../src/utils.dart';
9+
import 'template_test_base.dart';
1310

1411
void main() async {
1512
defineReflectiveSuite(() {
@@ -18,57 +15,19 @@ void main() async {
1815
}
1916

2017
@reflectiveTest
21-
class ClassTest extends DartdocTestBase {
22-
static const packageName = 'class_test';
18+
class ClassTest extends TemplateTestBase {
19+
@override
20+
String get packageName => 'class_test';
2321

2422
@override
2523
String get libraryName => 'class';
2624

27-
Future<void> createPackage({
28-
List<d.Descriptor> libFiles = const [],
29-
}) async {
30-
packagePath = await d.createPackage(
31-
packageName,
32-
pubspec: '''
33-
name: class_test
34-
version: 0.0.1
35-
environment:
36-
sdk: '>=3.3.0-0 <4.0.0'
37-
''',
38-
dartdocOptions: '''
39-
dartdoc:
40-
linkToSource:
41-
root: '.'
42-
uriTemplate: 'https://github.com/dart-lang/TEST_PKG/%f%#L%l%'
43-
''',
44-
libFiles: libFiles,
45-
resourceProvider: resourceProvider,
46-
);
47-
await writeDartdocResources(resourceProvider);
48-
packageConfigProvider.addPackageToConfigFor(
49-
packagePath, packageName, Uri.file('$packagePath/'));
50-
}
51-
52-
Future<List<String>> createPackageAndReadLines({
53-
required List<d.Descriptor> libFiles,
54-
required List<String> filePath,
55-
}) async {
56-
await createPackage(libFiles: libFiles);
57-
await (await buildDartdoc()).generateDocs();
58-
59-
return resourceProvider.readLines([packagePath, 'doc', ...filePath]);
60-
}
61-
62-
void test_class_extends() async {
63-
var baseLines = await createPackageAndReadLines(
64-
libFiles: [
65-
d.file('lib.dart', '''
25+
void test_implementers_class_extends() async {
26+
await createPackageWithLibrary('''
6627
class Base {}
6728
class Foo extends Base {}
68-
'''),
69-
],
70-
filePath: ['lib', 'Base-class.html'],
71-
);
29+
''');
30+
var baseLines = readLines(['lib', 'Base-class.html']);
7231

7332
baseLines.expectMainContentContainsAllInOrder([
7433
matches('<dt>Implementers</dt>'),
@@ -78,16 +37,12 @@ class Foo extends Base {}
7837
]);
7938
}
8039

81-
void test_class_implements() async {
82-
var baseLines = await createPackageAndReadLines(
83-
libFiles: [
84-
d.file('lib.dart', '''
40+
void test_implementers_class_implements() async {
41+
await createPackageWithLibrary('''
8542
class Base {}
8643
class Foo implements Base {}
87-
'''),
88-
],
89-
filePath: ['lib', 'Base-class.html'],
90-
);
44+
''');
45+
var baseLines = readLines(['lib', 'Base-class.html']);
9146

9247
baseLines.expectMainContentContainsAllInOrder([
9348
matches('<dt>Implementers</dt>'),
@@ -97,16 +52,12 @@ class Foo implements Base {}
9752
]);
9853
}
9954

100-
void test_class_implements_withGenericType() async {
101-
var baseLines = await createPackageAndReadLines(
102-
libFiles: [
103-
d.file('lib.dart', '''
55+
void test_implementers_class_implements_withGenericType() async {
56+
await createPackageWithLibrary('''
10457
class Base<E> {}
10558
class Foo<E> implements Base<E> {}
106-
'''),
107-
],
108-
filePath: ['lib', 'Base-class.html'],
109-
);
59+
''');
60+
var baseLines = readLines(['lib', 'Base-class.html']);
11061

11162
baseLines.expectMainContentContainsAllInOrder([
11263
matches('<dt>Implementers</dt>'),
@@ -116,16 +67,12 @@ class Foo<E> implements Base<E> {}
11667
]);
11768
}
11869

119-
void test_class_implements_withInstantiatedType() async {
120-
var baseLines = await createPackageAndReadLines(
121-
libFiles: [
122-
d.file('lib.dart', '''
70+
void test_implementers_class_implements_withInstantiatedType() async {
71+
await createPackageWithLibrary('''
12372
class Base<E> {}
12473
class Foo implements Base<int> {}
125-
'''),
126-
],
127-
filePath: ['lib', 'Base-class.html'],
128-
);
74+
''');
75+
var baseLines = readLines(['lib', 'Base-class.html']);
12976

13077
baseLines.expectMainContentContainsAllInOrder([
13178
matches('<dt>Implementers</dt>'),
@@ -135,17 +82,13 @@ class Foo implements Base<int> {}
13582
]);
13683
}
13784

138-
void test_extensionType_implements() async {
139-
var base1Lines = await createPackageAndReadLines(
140-
libFiles: [
141-
d.file('lib.dart', '''
85+
void test_implementers_extensionType_implements() async {
86+
await createPackageWithLibrary('''
14287
class Base1 {}
14388
class Base2 extends Base1 {}
14489
extension type ET(Base2 base) implements Base1 {}
145-
'''),
146-
],
147-
filePath: ['lib', 'Base1-class.html'],
148-
);
90+
''');
91+
var base1Lines = readLines(['lib', 'Base1-class.html']);
14992

15093
base1Lines.expectMainContentContainsAllInOrder([
15194
matches('<dt>Implementers</dt>'),
@@ -156,16 +99,12 @@ extension type ET(Base2 base) implements Base1 {}
15699
]);
157100
}
158101

159-
void test_mixin_implements() async {
160-
var baseLines = await createPackageAndReadLines(
161-
libFiles: [
162-
d.file('lib.dart', '''
102+
void test_implementers_mixin_implements() async {
103+
await createPackageWithLibrary('''
163104
class Base {}
164105
mixin M implements Base {}
165-
'''),
166-
],
167-
filePath: ['lib', 'Base-class.html'],
168-
);
106+
''');
107+
var baseLines = readLines(['lib', 'Base-class.html']);
169108

170109
baseLines.expectMainContentContainsAllInOrder([
171110
matches('<dt>Implementers</dt>'),
@@ -176,16 +115,12 @@ mixin M implements Base {}
176115
}
177116

178117
@FailingTest(reason: 'Not implemented yet; should be?')
179-
void test_mixin_superclassConstraint() async {
180-
var baseLines = await createPackageAndReadLines(
181-
libFiles: [
182-
d.file('lib.dart', '''
118+
void test_implementers_mixin_superclassConstraint() async {
119+
await createPackageWithLibrary('''
183120
class Base {}
184121
mixin M on Base {}
185-
'''),
186-
],
187-
filePath: ['lib', 'Base-class.html'],
188-
);
122+
''');
123+
var baseLines = readLines(['lib', 'Base-class.html']);
189124

190125
baseLines.expectMainContentContainsAllInOrder([
191126
matches('<dt>Implementers</dt>'),
@@ -194,9 +129,70 @@ mixin M on Base {}
194129
matches('</ul></dd>'),
195130
]);
196131
}
132+
133+
void test_constructor_named() async {
134+
await createPackageWithLibrary('''
135+
class C {
136+
/// A named constructor.
137+
C.named();
138+
}
139+
''');
140+
var htmlLines = readLines(['lib', 'C-class.html']);
141+
142+
htmlLines.expectMainContentContainsAllInOrder([
143+
matches('<h2>Constructors</h2>'),
144+
matches('<a href="../lib/C/C.named.html">C.named</a>'),
145+
matches('A named constructor.'),
146+
]);
147+
}
148+
149+
void test_constructor_unnamed() async {
150+
await createPackageWithLibrary('''
151+
class C {
152+
/// An unnamed constructor.
153+
C();
197154
}
155+
''');
156+
var htmlLines = readLines(['lib', 'C-class.html']);
157+
158+
htmlLines.expectMainContentContainsAllInOrder([
159+
matches('<h2>Constructors</h2>'),
160+
matches('<a href="../lib/C/C.html">C</a>'),
161+
matches('An unnamed constructor.'),
162+
]);
163+
}
198164

199-
extension on MemoryResourceProvider {
200-
List<String> readLines(List<String> pathParts) =>
201-
getFile(path.joinAll(pathParts)).readAsStringSync().split('\n');
165+
void test_instanceMethod() async {
166+
await createPackageWithLibrary('''
167+
class C {
168+
/// An instance method.
169+
void m1() {}
170+
}
171+
''');
172+
var htmlLines = readLines(['lib', 'C-class.html']);
173+
174+
htmlLines.expectMainContentContainsAllInOrder([
175+
matches('<h2>Methods</h2>'),
176+
matches('<dt id="m1" class="callable">'),
177+
matches('<a href="../lib/C/m1.html">m1</a>'),
178+
matches('An instance method.'),
179+
]);
180+
}
181+
182+
void test_instanceMethod_generic() async {
183+
await createPackageWithLibrary('''
184+
abstract class C {
185+
/// An instance method.
186+
T m1<T extends num>(T a);
187+
}
188+
''');
189+
var htmlLines = readLines(['lib', 'C-class.html']);
190+
191+
htmlLines.expectMainContentContainsAllInOrder([
192+
matches('<h2>Methods</h2>'),
193+
matches('<dt id="m1" class="callable">'),
194+
matches('<a href="../lib/C/m1.html">m1</a>'),
195+
matches('An instance method.'),
196+
]);
197+
}
202198
}

test/templates/extension_type_test.dart

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/file_system/memory_file_system.dart';
6-
import 'package:path/path.dart' as path;
75
import 'package:test/test.dart';
86
import 'package:test_reflective_loader/test_reflective_loader.dart';
97

10-
import '../dartdoc_test_base.dart';
11-
import '../src/test_descriptor_utils.dart' as d;
128
import '../src/utils.dart';
9+
import 'template_test_base.dart';
1310

1411
void main() async {
1512
defineReflectiveSuite(() {
@@ -18,43 +15,12 @@ void main() async {
1815
}
1916

2017
@reflectiveTest
21-
class ExtensionTypeTest extends DartdocTestBase {
22-
static const packageName = 'extension_type_test';
23-
18+
class ExtensionTypeTest extends TemplateTestBase {
2419
@override
25-
String get libraryName => 'class';
26-
27-
/// Creates a package on disk with the given singular library [content], and
28-
/// generates the docs.
29-
Future<void> createPackageWithLibrary(String content) async {
30-
packagePath = await d.createPackage(
31-
packageName,
32-
pubspec: '''
33-
name: extension_type_test
34-
version: 0.0.1
35-
environment:
36-
sdk: '>=3.3.0-0 <4.0.0'
37-
''',
38-
dartdocOptions: '''
39-
dartdoc:
40-
linkToSource:
41-
root: '.'
42-
uriTemplate: 'https://github.com/dart-lang/TEST_PKG/%f%#L%l%'
43-
''',
44-
libFiles: [
45-
d.file('lib.dart', content),
46-
],
47-
resourceProvider: resourceProvider,
48-
);
49-
await writeDartdocResources(resourceProvider);
50-
packageConfigProvider.addPackageToConfigFor(
51-
packagePath, packageName, Uri.file('$packagePath/'));
52-
53-
await (await buildDartdoc()).generateDocs();
54-
}
20+
String get packageName => 'extension_type_test';
5521

56-
List<String> readLines(List<String> filePath) =>
57-
resourceProvider.readLines([packagePath, 'doc', ...filePath]);
22+
@override
23+
String get libraryName => 'extension_type';
5824

5925
void test_extensionNameWithGenerics() async {
6026
await createPackageWithLibrary('''
@@ -434,8 +400,3 @@ extension type One(int it) {
434400
);
435401
}
436402
}
437-
438-
extension on MemoryResourceProvider {
439-
List<String> readLines(List<String> pathParts) =>
440-
getFile(path.joinAll(pathParts)).readAsStringSync().split('\n');
441-
}

0 commit comments

Comments
 (0)