Skip to content

Refactor class_test to use reflective_test_loader #3681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 96 additions & 100 deletions test/templates/class_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/file_system/memory_file_system.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import '../dartdoc_test_base.dart';
import '../src/test_descriptor_utils.dart' as d;
import '../src/utils.dart';
import 'template_test_base.dart';

void main() async {
defineReflectiveSuite(() {
Expand All @@ -18,57 +15,19 @@ void main() async {
}

@reflectiveTest
class ClassTest extends DartdocTestBase {
static const packageName = 'class_test';
class ClassTest extends TemplateTestBase {
@override
String get packageName => 'class_test';

@override
String get libraryName => 'class';

Future<void> createPackage({
List<d.Descriptor> libFiles = const [],
}) async {
packagePath = await d.createPackage(
packageName,
pubspec: '''
name: class_test
version: 0.0.1
environment:
sdk: '>=3.3.0-0 <4.0.0'
''',
dartdocOptions: '''
dartdoc:
linkToSource:
root: '.'
uriTemplate: 'https://github.com/dart-lang/TEST_PKG/%f%#L%l%'
''',
libFiles: libFiles,
resourceProvider: resourceProvider,
);
await writeDartdocResources(resourceProvider);
packageConfigProvider.addPackageToConfigFor(
packagePath, packageName, Uri.file('$packagePath/'));
}

Future<List<String>> createPackageAndReadLines({
required List<d.Descriptor> libFiles,
required List<String> filePath,
}) async {
await createPackage(libFiles: libFiles);
await (await buildDartdoc()).generateDocs();

return resourceProvider.readLines([packagePath, 'doc', ...filePath]);
}

void test_class_extends() async {
var baseLines = await createPackageAndReadLines(
libFiles: [
d.file('lib.dart', '''
void test_implementers_class_extends() async {
await createPackageWithLibrary('''
class Base {}
class Foo extends Base {}
'''),
],
filePath: ['lib', 'Base-class.html'],
);
''');
var baseLines = readLines(['lib', 'Base-class.html']);

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

void test_class_implements() async {
var baseLines = await createPackageAndReadLines(
libFiles: [
d.file('lib.dart', '''
void test_implementers_class_implements() async {
await createPackageWithLibrary('''
class Base {}
class Foo implements Base {}
'''),
],
filePath: ['lib', 'Base-class.html'],
);
''');
var baseLines = readLines(['lib', 'Base-class.html']);

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

void test_class_implements_withGenericType() async {
var baseLines = await createPackageAndReadLines(
libFiles: [
d.file('lib.dart', '''
void test_implementers_class_implements_withGenericType() async {
await createPackageWithLibrary('''
class Base<E> {}
class Foo<E> implements Base<E> {}
'''),
],
filePath: ['lib', 'Base-class.html'],
);
''');
var baseLines = readLines(['lib', 'Base-class.html']);

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

void test_class_implements_withInstantiatedType() async {
var baseLines = await createPackageAndReadLines(
libFiles: [
d.file('lib.dart', '''
void test_implementers_class_implements_withInstantiatedType() async {
await createPackageWithLibrary('''
class Base<E> {}
class Foo implements Base<int> {}
'''),
],
filePath: ['lib', 'Base-class.html'],
);
''');
var baseLines = readLines(['lib', 'Base-class.html']);

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

void test_extensionType_implements() async {
var base1Lines = await createPackageAndReadLines(
libFiles: [
d.file('lib.dart', '''
void test_implementers_extensionType_implements() async {
await createPackageWithLibrary('''
class Base1 {}
class Base2 extends Base1 {}
extension type ET(Base2 base) implements Base1 {}
'''),
],
filePath: ['lib', 'Base1-class.html'],
);
''');
var base1Lines = readLines(['lib', 'Base1-class.html']);

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

void test_mixin_implements() async {
var baseLines = await createPackageAndReadLines(
libFiles: [
d.file('lib.dart', '''
void test_implementers_mixin_implements() async {
await createPackageWithLibrary('''
class Base {}
mixin M implements Base {}
'''),
],
filePath: ['lib', 'Base-class.html'],
);
''');
var baseLines = readLines(['lib', 'Base-class.html']);

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

@FailingTest(reason: 'Not implemented yet; should be?')
void test_mixin_superclassConstraint() async {
var baseLines = await createPackageAndReadLines(
libFiles: [
d.file('lib.dart', '''
void test_implementers_mixin_superclassConstraint() async {
await createPackageWithLibrary('''
class Base {}
mixin M on Base {}
'''),
],
filePath: ['lib', 'Base-class.html'],
);
''');
var baseLines = readLines(['lib', 'Base-class.html']);

baseLines.expectMainContentContainsAllInOrder([
matches('<dt>Implementers</dt>'),
Expand All @@ -194,9 +129,70 @@ mixin M on Base {}
matches('</ul></dd>'),
]);
}

void test_constructor_named() async {
await createPackageWithLibrary('''
class C {
/// A named constructor.
C.named();
}
''');
var htmlLines = readLines(['lib', 'C-class.html']);

htmlLines.expectMainContentContainsAllInOrder([
matches('<h2>Constructors</h2>'),
matches('<a href="../lib/C/C.named.html">C.named</a>'),
matches('A named constructor.'),
]);
}

void test_constructor_unnamed() async {
await createPackageWithLibrary('''
class C {
/// An unnamed constructor.
C();
}
''');
var htmlLines = readLines(['lib', 'C-class.html']);

htmlLines.expectMainContentContainsAllInOrder([
matches('<h2>Constructors</h2>'),
matches('<a href="../lib/C/C.html">C</a>'),
matches('An unnamed constructor.'),
]);
}

extension on MemoryResourceProvider {
List<String> readLines(List<String> pathParts) =>
getFile(path.joinAll(pathParts)).readAsStringSync().split('\n');
void test_instanceMethod() async {
await createPackageWithLibrary('''
class C {
/// An instance method.
void m1() {}
}
''');
var htmlLines = readLines(['lib', 'C-class.html']);

htmlLines.expectMainContentContainsAllInOrder([
matches('<h2>Methods</h2>'),
matches('<dt id="m1" class="callable">'),
matches('<a href="../lib/C/m1.html">m1</a>'),
matches('An instance method.'),
]);
}

void test_instanceMethod_generic() async {
await createPackageWithLibrary('''
abstract class C {
/// An instance method.
T m1<T extends num>(T a);
}
''');
var htmlLines = readLines(['lib', 'C-class.html']);

htmlLines.expectMainContentContainsAllInOrder([
matches('<h2>Methods</h2>'),
matches('<dt id="m1" class="callable">'),
matches('<a href="../lib/C/m1.html">m1</a>'),
matches('An instance method.'),
]);
}
}
49 changes: 5 additions & 44 deletions test/templates/extension_type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/file_system/memory_file_system.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import '../dartdoc_test_base.dart';
import '../src/test_descriptor_utils.dart' as d;
import '../src/utils.dart';
import 'template_test_base.dart';

void main() async {
defineReflectiveSuite(() {
Expand All @@ -18,43 +15,12 @@ void main() async {
}

@reflectiveTest
class ExtensionTypeTest extends DartdocTestBase {
static const packageName = 'extension_type_test';

class ExtensionTypeTest extends TemplateTestBase {
@override
String get libraryName => 'class';

/// Creates a package on disk with the given singular library [content], and
/// generates the docs.
Future<void> createPackageWithLibrary(String content) async {
packagePath = await d.createPackage(
packageName,
pubspec: '''
name: extension_type_test
version: 0.0.1
environment:
sdk: '>=3.3.0-0 <4.0.0'
''',
dartdocOptions: '''
dartdoc:
linkToSource:
root: '.'
uriTemplate: 'https://github.com/dart-lang/TEST_PKG/%f%#L%l%'
''',
libFiles: [
d.file('lib.dart', content),
],
resourceProvider: resourceProvider,
);
await writeDartdocResources(resourceProvider);
packageConfigProvider.addPackageToConfigFor(
packagePath, packageName, Uri.file('$packagePath/'));

await (await buildDartdoc()).generateDocs();
}
String get packageName => 'extension_type_test';

List<String> readLines(List<String> filePath) =>
resourceProvider.readLines([packagePath, 'doc', ...filePath]);
@override
String get libraryName => 'extension_type';

void test_extensionNameWithGenerics() async {
await createPackageWithLibrary('''
Expand Down Expand Up @@ -434,8 +400,3 @@ extension type One(int it) {
);
}
}

extension on MemoryResourceProvider {
List<String> readLines(List<String> pathParts) =>
getFile(path.joinAll(pathParts)).readAsStringSync().split('\n');
}
Loading