Skip to content

Migrate most mustachio tests to null safety #2810

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 2 commits into from
Sep 28, 2021
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
12 changes: 5 additions & 7 deletions test/mustachio/aot_compiler_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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.

// @dart=2.9

@Timeout.factor(4)
import 'dart:convert';
import 'package:analyzer/dart/element/element.dart';
Expand All @@ -14,7 +12,7 @@ import 'package:test/test.dart';
import 'builder_test_base.dart';

void main() {
InMemoryAssetWriter writer;
late InMemoryAssetWriter writer;

Future<LibraryElement> resolveGeneratedLibrary() async {
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
Expand Down Expand Up @@ -65,7 +63,7 @@ library foo;
import 'package:mustachio/annotations.dart';
''');
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
var generatedContent = utf8.decode(writer.assets[rendererAsset]);
var generatedContent = utf8.decode(writer.assets[rendererAsset]!);
expect(
generatedContent, contains('String renderFoo<T>(_i1.Foo<T> context0)'));
});
Expand All @@ -87,7 +85,7 @@ import 'package:mustachio/annotations.dart';
},
);
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
var generatedContent = utf8.decode(writer.assets[rendererAsset]);
var generatedContent = utf8.decode(writer.assets[rendererAsset]!);
expect(
generatedContent,
contains(
Expand All @@ -102,9 +100,9 @@ class Baz {}
''');
var renderersLibrary = await resolveGeneratedLibrary();

var fooRenderFunction = renderersLibrary.getTopLevelFunction('renderFoo');
var fooRenderFunction = renderersLibrary.getTopLevelFunction('renderFoo')!;
expect(fooRenderFunction.typeParameters, hasLength(1));
var fBound = fooRenderFunction.typeParameters.single.bound;
var fBound = fooRenderFunction.typeParameters.single.bound!;
expect(fBound.getDisplayString(withNullability: false), equals('num'));
});
}
14 changes: 7 additions & 7 deletions test/mustachio/builder_test_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// 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.

// @dart=2.9

import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:build_test/build_test.dart';
import 'package:collection/collection.dart';

import '../../tool/mustachio/builder.dart';

Expand Down Expand Up @@ -57,7 +56,7 @@ Future<void> testMustachioBuilder(
InMemoryAssetWriter writer,
String sourceLibraryContent, {
String libraryFrontMatter = libraryFrontMatter,
Map<String, String> additionalAssets,
Map<String, String> additionalAssets = const {},
}) async {
sourceLibraryContent = '''
$libraryFrontMatter
Expand All @@ -74,15 +73,16 @@ $sourceLibraryContent
'foo|lib/templates/md/bar.md': 'EMPTY',
'foo|lib/templates/html/baz.html': 'EMPTY',
'foo|lib/templates/md/baz.md': 'EMPTY',
...?additionalAssets,
...additionalAssets,
},
writer: writer,
);
}

extension LibraryExtensions on LibraryElement {
/// Returns the top-level function in [this] library, named [name].
FunctionElement getTopLevelFunction(String name) => topLevelElements
/// Returns the top-level function in [this] library, named [name], or `null`
/// if no function is found.
FunctionElement? getTopLevelFunction(String name) => topLevelElements
.whereType<FunctionElement>()
.firstWhere((element) => element.name == name, orElse: () => null);
.firstWhereOrNull((element) => element.name == name);
}
110 changes: 62 additions & 48 deletions test/mustachio/parser_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @dart=2.9
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// 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:dartdoc/src/mustachio/parser.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -354,11 +356,11 @@ void main() {
expect(ast, hasLength(2));
_expectText(ast[0], equals('Text '));
var section = ast[1] as Section;
_expectSection(section, equals(['key']));
_expectSection(section, equals(['key']), spanStart: 5, spanEnd: 49);
expect(section.children, hasLength(3));
_expectText(section.children[0], equals(' AA '));
var innerSection = section.children[1] as Section;
_expectSection(innerSection, equals(['key']));
_expectSection(innerSection, equals(['key']), spanStart: 17, spanEnd: 37);
expect(innerSection.children, hasLength(1));
_expectText(innerSection.children[0], equals(' BB '));
});
Expand Down Expand Up @@ -407,7 +409,7 @@ void main() {
}

void _expectText(MustachioNode node, Object matcher,
{int spanStart, int spanEnd}) {
{int? spanStart, int? spanEnd}) {
expect(node, isA<Text>().having((e) => e.content, 'content', matcher));
if (spanStart != null) {
expect(
Expand All @@ -421,82 +423,94 @@ void _expectText(MustachioNode node, Object matcher,
}
}

void _expectVariable(MustachioNode node, Object matcher,
{bool escape = true,
int spanStart,
int spanEnd,
int keySpanStart,
int keySpanEnd}) {
void _expectVariable(
MustachioNode node,
Object matcher, {
bool escape = true,
required int spanStart,
required int spanEnd,
int? keySpanStart,
int? keySpanEnd,
}) {
expect(
node,
isA<Variable>()
.having((e) => e.key, 'key', matcher)
.having((e) => e.escape, 'escape', escape));
if (spanStart != null) {
var actualSpanStart = (node as Variable).span.start.offset;
_expectSpanOffset('Variable', 'start', actualSpanStart, spanStart);
}
if (spanEnd != null) {
var actualSpanEnd = (node as Variable).span.end.offset;
_expectSpanOffset('Variable', 'end', actualSpanEnd, spanEnd);
}
node as Variable;

var actualSpanStart = node.span.start.offset;
_expectSpanOffset('Variable', 'start', actualSpanStart, spanStart);

var actualSpanEnd = node.span.end.offset;
_expectSpanOffset('Variable', 'end', actualSpanEnd, spanEnd);

if (keySpanStart != null) {
var actualKeySpanStart = (node as Variable).keySpan.start.offset;
var actualKeySpanStart = node.keySpan.start.offset;
_expectSpanOffset(
'Variable key', 'start', actualKeySpanStart, keySpanStart);
}
if (keySpanEnd != null) {
var actualKeySpanEnd = (node as Variable).keySpan.end.offset;
var actualKeySpanEnd = node.keySpan.end.offset;
_expectSpanOffset('Variable key', 'end', actualKeySpanEnd, keySpanEnd);
}
}

void _expectSection(MustachioNode node, Object matcher,
{bool invert = false,
int spanStart,
int spanEnd,
int keySpanStart,
int keySpanEnd}) {
void _expectSection(
MustachioNode node,
Object matcher, {
bool invert = false,
required int spanStart,
required int spanEnd,
int? keySpanStart,
int? keySpanEnd,
}) {
expect(
node,
isA<Section>()
.having((e) => e.key, 'key', matcher)
.having((e) => e.invert, 'invert', invert));
if (spanStart != null) {
var actualSpanStart = (node as Section).span.start.offset;
_expectSpanOffset('Section', 'start', actualSpanStart, spanStart);
}
if (spanEnd != null) {
var actualSpanEnd = (node as Section).span.end.offset;
_expectSpanOffset('Section', 'end', actualSpanEnd, spanEnd);
}
node as Section;

var actualSpanStart = node.span.start.offset;
_expectSpanOffset('Section', 'start', actualSpanStart, spanStart);

var actualSpanEnd = node.span.end.offset;
_expectSpanOffset('Section', 'end', actualSpanEnd, spanEnd);

if (keySpanStart != null) {
var actualKeySpanStart = (node as Section).keySpan.start.offset;
var actualKeySpanStart = node.keySpan.start.offset;
_expectSpanOffset('Section key', 'start', actualKeySpanStart, keySpanStart);
}
if (keySpanEnd != null) {
var actualKeySpanEnd = (node as Section).keySpan.end.offset;
var actualKeySpanEnd = node.keySpan.end.offset;
_expectSpanOffset('Section key', 'end', actualKeySpanEnd, keySpanEnd);
}
}

void _expectPartial(MustachioNode node, Object matcher,
{int spanStart, int spanEnd, int keySpanStart, int keySpanEnd}) {
void _expectPartial(
MustachioNode node,
Object matcher, {
required int spanStart,
required int spanEnd,
int? keySpanStart,
int? keySpanEnd,
}) {
expect(node, isA<Partial>().having((e) => e.key, 'key', matcher));
if (spanStart != null) {
var actualSpanStart = (node as Partial).span.start.offset;
_expectSpanOffset('Partial', 'start', actualSpanStart, spanStart);
}
if (spanEnd != null) {
var actualSpanEnd = (node as Partial).span.end.offset;
_expectSpanOffset('Partial', 'end', actualSpanEnd, spanEnd);
}
node as Partial;

var actualSpanStart = node.span.start.offset;
_expectSpanOffset('Partial', 'start', actualSpanStart, spanStart);

var actualSpanEnd = node.span.end.offset;
_expectSpanOffset('Partial', 'end', actualSpanEnd, spanEnd);

if (keySpanStart != null) {
var actualKeySpanStart = (node as Partial).keySpan.start.offset;
var actualKeySpanStart = node.keySpan.start.offset;
_expectSpanOffset('Partial key', 'start', actualKeySpanStart, keySpanStart);
}
if (keySpanEnd != null) {
var actualKeySpanEnd = (node as Partial).keySpan.end.offset;
var actualKeySpanEnd = node.keySpan.end.offset;
_expectSpanOffset('Partial key', 'end', actualKeySpanEnd, keySpanEnd);
}
}
Expand Down
8 changes: 3 additions & 5 deletions test/mustachio/render_tests_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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.

// @dart=2.9

import 'dart:io';
import 'dart:isolate' show Isolate;

Expand All @@ -17,7 +15,7 @@ void main() {
var testCasePattern = RegExp(r'test\(.*,');
var dartdocLibUri = await Isolate.resolvePackageUri(
Uri.parse('package:dartdoc/dartdoc.dart'));
var dartdocPath = p.dirname(p.dirname(dartdocLibUri.path));
var dartdocPath = p.dirname(p.dirname(dartdocLibUri!.path));
// Correct Windows issue path coming out of [Isolate.resolvePackageUri].
if (p.separator == p.windows.separator && dartdocPath.startsWith('/')) {
dartdocPath = dartdocPath.substring(1).replaceAll('/', p.separator);
Expand All @@ -35,15 +33,15 @@ void main() {
.where((line) => !line.contains('Parser '))
// Ignore tests about the SimpleRenderer.
.where((line) => !line.contains('SimpleRenderer'))
.map((line) => testCasePattern.firstMatch(line).group(0))
.map((line) => testCasePattern.firstMatch(line)!.group(0))
.toSet();

var aotCompilerRenderTest = File(p.join(
dartdocPath, 'test', 'mustachio', 'aot_compiler_render_test.dart'))
.readAsLinesSync();
var aotCompilerTestCases = aotCompilerRenderTest
.where((line) => testCasePattern.hasMatch(line))
.map((line) => testCasePattern.firstMatch(line).group(0))
.map((line) => testCasePattern.firstMatch(line)!.group(0))
.toSet();

var difference = runtimeRendererTestCases.difference(aotCompilerTestCases);
Expand Down
24 changes: 11 additions & 13 deletions test/mustachio/runtime_renderer_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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.

// @dart=2.9

@Timeout.factor(2)
import 'dart:convert';
import 'package:analyzer/dart/element/element.dart';
Expand All @@ -14,7 +12,7 @@ import 'package:test/test.dart';
import 'builder_test_base.dart';

void main() {
InMemoryAssetWriter writer;
late InMemoryAssetWriter writer;

Future<LibraryElement> resolveGeneratedLibrary() async {
var rendererAsset = AssetId('foo', 'lib/foo.runtime_renderers.dart');
Expand All @@ -29,8 +27,8 @@ void main() {
});

group('builds a renderer class', () {
LibraryElement renderersLibrary;
String generatedContent;
late final LibraryElement renderersLibrary;
late final String generatedContent;

// Builders are fairly expensive (about 4 seconds per `testBuilder` call),
// so this [setUpAll] saves significant time over [setUp].
Expand All @@ -56,7 +54,7 @@ class Baz {}
''');
renderersLibrary = await resolveGeneratedLibrary();
var rendererAsset = AssetId('foo', 'lib/foo.runtime_renderers.dart');
generatedContent = utf8.decode(writer.assets[rendererAsset]);
generatedContent = utf8.decode(writer.assets[rendererAsset]!);
});

test('for a class which implicitly extends Object', () {
Expand Down Expand Up @@ -179,7 +177,7 @@ import 'package:mustachio/annotations.dart';
});

group('builds a renderer class for a generic type', () {
String generatedContent;
late final String generatedContent;

// Builders are fairly expensive (about 4 seconds per `testBuilder` call),
// so this [setUpAll] saves significant time over [setUp].
Expand All @@ -198,7 +196,7 @@ library foo;
import 'package:mustachio/annotations.dart';
''');
var rendererAsset = AssetId('foo', 'lib/foo.runtime_renderers.dart');
generatedContent = utf8.decode(writer.assets[rendererAsset]);
generatedContent = utf8.decode(writer.assets[rendererAsset]!);
});

test('with a corresponding public API function', () async {
Expand Down Expand Up @@ -241,19 +239,19 @@ class Baz {}
''');
var renderersLibrary = await resolveGeneratedLibrary();

var fooRenderFunction = renderersLibrary.getTopLevelFunction('renderFoo');
var fooRenderFunction = renderersLibrary.getTopLevelFunction('renderFoo')!;
expect(fooRenderFunction.typeParameters, hasLength(1));
var fBound = fooRenderFunction.typeParameters.single.bound;
var fBound = fooRenderFunction.typeParameters.single.bound!;
expect(fBound.getDisplayString(withNullability: false), equals('num'));

var fooRendererClass = renderersLibrary.getType('_Renderer_Foo');
var fooRendererClass = renderersLibrary.getType('_Renderer_Foo')!;
expect(fooRendererClass.typeParameters, hasLength(1));
var cBound = fooRenderFunction.typeParameters.single.bound;
var cBound = fooRenderFunction.typeParameters.single.bound!;
expect(cBound.getDisplayString(withNullability: false), equals('num'));
});

group('does not generate a renderer', () {
LibraryElement renderersLibrary;
late final LibraryElement renderersLibrary;

setUpAll(() async {
writer = InMemoryAssetWriter();
Expand Down