Skip to content

Commit db236c1

Browse files
authored
Migrate most mustachio tests to null safety (#2810)
1 parent ada0600 commit db236c1

5 files changed

+88
-80
lines changed

test/mustachio/aot_compiler_builder_test.dart

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart=2.9
6-
75
@Timeout.factor(4)
86
import 'dart:convert';
97
import 'package:analyzer/dart/element/element.dart';
@@ -14,7 +12,7 @@ import 'package:test/test.dart';
1412
import 'builder_test_base.dart';
1513

1614
void main() {
17-
InMemoryAssetWriter writer;
15+
late InMemoryAssetWriter writer;
1816

1917
Future<LibraryElement> resolveGeneratedLibrary() async {
2018
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
@@ -65,7 +63,7 @@ library foo;
6563
import 'package:mustachio/annotations.dart';
6664
''');
6765
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
68-
var generatedContent = utf8.decode(writer.assets[rendererAsset]);
66+
var generatedContent = utf8.decode(writer.assets[rendererAsset]!);
6967
expect(
7068
generatedContent, contains('String renderFoo<T>(_i1.Foo<T> context0)'));
7169
});
@@ -87,7 +85,7 @@ import 'package:mustachio/annotations.dart';
8785
},
8886
);
8987
var rendererAsset = AssetId('foo', 'lib/foo.aot_renderers_for_html.dart');
90-
var generatedContent = utf8.decode(writer.assets[rendererAsset]);
88+
var generatedContent = utf8.decode(writer.assets[rendererAsset]!);
9189
expect(
9290
generatedContent,
9391
contains(
@@ -102,9 +100,9 @@ class Baz {}
102100
''');
103101
var renderersLibrary = await resolveGeneratedLibrary();
104102

105-
var fooRenderFunction = renderersLibrary.getTopLevelFunction('renderFoo');
103+
var fooRenderFunction = renderersLibrary.getTopLevelFunction('renderFoo')!;
106104
expect(fooRenderFunction.typeParameters, hasLength(1));
107-
var fBound = fooRenderFunction.typeParameters.single.bound;
105+
var fBound = fooRenderFunction.typeParameters.single.bound!;
108106
expect(fBound.getDisplayString(withNullability: false), equals('num'));
109107
});
110108
}

test/mustachio/builder_test_base.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
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-
// @dart=2.9
6-
75
import 'package:analyzer/dart/element/element.dart';
86
import 'package:build/build.dart';
97
import 'package:build_test/build_test.dart';
8+
import 'package:collection/collection.dart';
109

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

@@ -57,7 +56,7 @@ Future<void> testMustachioBuilder(
5756
InMemoryAssetWriter writer,
5857
String sourceLibraryContent, {
5958
String libraryFrontMatter = libraryFrontMatter,
60-
Map<String, String> additionalAssets,
59+
Map<String, String> additionalAssets = const {},
6160
}) async {
6261
sourceLibraryContent = '''
6362
$libraryFrontMatter
@@ -74,15 +73,16 @@ $sourceLibraryContent
7473
'foo|lib/templates/md/bar.md': 'EMPTY',
7574
'foo|lib/templates/html/baz.html': 'EMPTY',
7675
'foo|lib/templates/md/baz.md': 'EMPTY',
77-
...?additionalAssets,
76+
...additionalAssets,
7877
},
7978
writer: writer,
8079
);
8180
}
8281

8382
extension LibraryExtensions on LibraryElement {
84-
/// Returns the top-level function in [this] library, named [name].
85-
FunctionElement getTopLevelFunction(String name) => topLevelElements
83+
/// Returns the top-level function in [this] library, named [name], or `null`
84+
/// if no function is found.
85+
FunctionElement? getTopLevelFunction(String name) => topLevelElements
8686
.whereType<FunctionElement>()
87-
.firstWhere((element) => element.name == name, orElse: () => null);
87+
.firstWhereOrNull((element) => element.name == name);
8888
}

test/mustachio/parser_test.dart

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// @dart=2.9
1+
// Copyright (c) 2021, 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.
24

35
import 'package:dartdoc/src/mustachio/parser.dart';
46
import 'package:test/test.dart';
@@ -354,11 +356,11 @@ void main() {
354356
expect(ast, hasLength(2));
355357
_expectText(ast[0], equals('Text '));
356358
var section = ast[1] as Section;
357-
_expectSection(section, equals(['key']));
359+
_expectSection(section, equals(['key']), spanStart: 5, spanEnd: 49);
358360
expect(section.children, hasLength(3));
359361
_expectText(section.children[0], equals(' AA '));
360362
var innerSection = section.children[1] as Section;
361-
_expectSection(innerSection, equals(['key']));
363+
_expectSection(innerSection, equals(['key']), spanStart: 17, spanEnd: 37);
362364
expect(innerSection.children, hasLength(1));
363365
_expectText(innerSection.children[0], equals(' BB '));
364366
});
@@ -407,7 +409,7 @@ void main() {
407409
}
408410

409411
void _expectText(MustachioNode node, Object matcher,
410-
{int spanStart, int spanEnd}) {
412+
{int? spanStart, int? spanEnd}) {
411413
expect(node, isA<Text>().having((e) => e.content, 'content', matcher));
412414
if (spanStart != null) {
413415
expect(
@@ -421,82 +423,94 @@ void _expectText(MustachioNode node, Object matcher,
421423
}
422424
}
423425

424-
void _expectVariable(MustachioNode node, Object matcher,
425-
{bool escape = true,
426-
int spanStart,
427-
int spanEnd,
428-
int keySpanStart,
429-
int keySpanEnd}) {
426+
void _expectVariable(
427+
MustachioNode node,
428+
Object matcher, {
429+
bool escape = true,
430+
required int spanStart,
431+
required int spanEnd,
432+
int? keySpanStart,
433+
int? keySpanEnd,
434+
}) {
430435
expect(
431436
node,
432437
isA<Variable>()
433438
.having((e) => e.key, 'key', matcher)
434439
.having((e) => e.escape, 'escape', escape));
435-
if (spanStart != null) {
436-
var actualSpanStart = (node as Variable).span.start.offset;
437-
_expectSpanOffset('Variable', 'start', actualSpanStart, spanStart);
438-
}
439-
if (spanEnd != null) {
440-
var actualSpanEnd = (node as Variable).span.end.offset;
441-
_expectSpanOffset('Variable', 'end', actualSpanEnd, spanEnd);
442-
}
440+
node as Variable;
441+
442+
var actualSpanStart = node.span.start.offset;
443+
_expectSpanOffset('Variable', 'start', actualSpanStart, spanStart);
444+
445+
var actualSpanEnd = node.span.end.offset;
446+
_expectSpanOffset('Variable', 'end', actualSpanEnd, spanEnd);
447+
443448
if (keySpanStart != null) {
444-
var actualKeySpanStart = (node as Variable).keySpan.start.offset;
449+
var actualKeySpanStart = node.keySpan.start.offset;
445450
_expectSpanOffset(
446451
'Variable key', 'start', actualKeySpanStart, keySpanStart);
447452
}
448453
if (keySpanEnd != null) {
449-
var actualKeySpanEnd = (node as Variable).keySpan.end.offset;
454+
var actualKeySpanEnd = node.keySpan.end.offset;
450455
_expectSpanOffset('Variable key', 'end', actualKeySpanEnd, keySpanEnd);
451456
}
452457
}
453458

454-
void _expectSection(MustachioNode node, Object matcher,
455-
{bool invert = false,
456-
int spanStart,
457-
int spanEnd,
458-
int keySpanStart,
459-
int keySpanEnd}) {
459+
void _expectSection(
460+
MustachioNode node,
461+
Object matcher, {
462+
bool invert = false,
463+
required int spanStart,
464+
required int spanEnd,
465+
int? keySpanStart,
466+
int? keySpanEnd,
467+
}) {
460468
expect(
461469
node,
462470
isA<Section>()
463471
.having((e) => e.key, 'key', matcher)
464472
.having((e) => e.invert, 'invert', invert));
465-
if (spanStart != null) {
466-
var actualSpanStart = (node as Section).span.start.offset;
467-
_expectSpanOffset('Section', 'start', actualSpanStart, spanStart);
468-
}
469-
if (spanEnd != null) {
470-
var actualSpanEnd = (node as Section).span.end.offset;
471-
_expectSpanOffset('Section', 'end', actualSpanEnd, spanEnd);
472-
}
473+
node as Section;
474+
475+
var actualSpanStart = node.span.start.offset;
476+
_expectSpanOffset('Section', 'start', actualSpanStart, spanStart);
477+
478+
var actualSpanEnd = node.span.end.offset;
479+
_expectSpanOffset('Section', 'end', actualSpanEnd, spanEnd);
480+
473481
if (keySpanStart != null) {
474-
var actualKeySpanStart = (node as Section).keySpan.start.offset;
482+
var actualKeySpanStart = node.keySpan.start.offset;
475483
_expectSpanOffset('Section key', 'start', actualKeySpanStart, keySpanStart);
476484
}
477485
if (keySpanEnd != null) {
478-
var actualKeySpanEnd = (node as Section).keySpan.end.offset;
486+
var actualKeySpanEnd = node.keySpan.end.offset;
479487
_expectSpanOffset('Section key', 'end', actualKeySpanEnd, keySpanEnd);
480488
}
481489
}
482490

483-
void _expectPartial(MustachioNode node, Object matcher,
484-
{int spanStart, int spanEnd, int keySpanStart, int keySpanEnd}) {
491+
void _expectPartial(
492+
MustachioNode node,
493+
Object matcher, {
494+
required int spanStart,
495+
required int spanEnd,
496+
int? keySpanStart,
497+
int? keySpanEnd,
498+
}) {
485499
expect(node, isA<Partial>().having((e) => e.key, 'key', matcher));
486-
if (spanStart != null) {
487-
var actualSpanStart = (node as Partial).span.start.offset;
488-
_expectSpanOffset('Partial', 'start', actualSpanStart, spanStart);
489-
}
490-
if (spanEnd != null) {
491-
var actualSpanEnd = (node as Partial).span.end.offset;
492-
_expectSpanOffset('Partial', 'end', actualSpanEnd, spanEnd);
493-
}
500+
node as Partial;
501+
502+
var actualSpanStart = node.span.start.offset;
503+
_expectSpanOffset('Partial', 'start', actualSpanStart, spanStart);
504+
505+
var actualSpanEnd = node.span.end.offset;
506+
_expectSpanOffset('Partial', 'end', actualSpanEnd, spanEnd);
507+
494508
if (keySpanStart != null) {
495-
var actualKeySpanStart = (node as Partial).keySpan.start.offset;
509+
var actualKeySpanStart = node.keySpan.start.offset;
496510
_expectSpanOffset('Partial key', 'start', actualKeySpanStart, keySpanStart);
497511
}
498512
if (keySpanEnd != null) {
499-
var actualKeySpanEnd = (node as Partial).keySpan.end.offset;
513+
var actualKeySpanEnd = node.keySpan.end.offset;
500514
_expectSpanOffset('Partial key', 'end', actualKeySpanEnd, keySpanEnd);
501515
}
502516
}

test/mustachio/render_tests_test.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart=2.9
6-
75
import 'dart:io';
86
import 'dart:isolate' show Isolate;
97

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

4139
var aotCompilerRenderTest = File(p.join(
4240
dartdocPath, 'test', 'mustachio', 'aot_compiler_render_test.dart'))
4341
.readAsLinesSync();
4442
var aotCompilerTestCases = aotCompilerRenderTest
4543
.where((line) => testCasePattern.hasMatch(line))
46-
.map((line) => testCasePattern.firstMatch(line).group(0))
44+
.map((line) => testCasePattern.firstMatch(line)!.group(0))
4745
.toSet();
4846

4947
var difference = runtimeRendererTestCases.difference(aotCompilerTestCases);

test/mustachio/runtime_renderer_builder_test.dart

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart=2.9
6-
75
@Timeout.factor(2)
86
import 'dart:convert';
97
import 'package:analyzer/dart/element/element.dart';
@@ -14,7 +12,7 @@ import 'package:test/test.dart';
1412
import 'builder_test_base.dart';
1513

1614
void main() {
17-
InMemoryAssetWriter writer;
15+
late InMemoryAssetWriter writer;
1816

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

3129
group('builds a renderer class', () {
32-
LibraryElement renderersLibrary;
33-
String generatedContent;
30+
late final LibraryElement renderersLibrary;
31+
late final String generatedContent;
3432

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

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

181179
group('builds a renderer class for a generic type', () {
182-
String generatedContent;
180+
late final String generatedContent;
183181

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

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

244-
var fooRenderFunction = renderersLibrary.getTopLevelFunction('renderFoo');
242+
var fooRenderFunction = renderersLibrary.getTopLevelFunction('renderFoo')!;
245243
expect(fooRenderFunction.typeParameters, hasLength(1));
246-
var fBound = fooRenderFunction.typeParameters.single.bound;
244+
var fBound = fooRenderFunction.typeParameters.single.bound!;
247245
expect(fBound.getDisplayString(withNullability: false), equals('num'));
248246

249-
var fooRendererClass = renderersLibrary.getType('_Renderer_Foo');
247+
var fooRendererClass = renderersLibrary.getType('_Renderer_Foo')!;
250248
expect(fooRendererClass.typeParameters, hasLength(1));
251-
var cBound = fooRenderFunction.typeParameters.single.bound;
249+
var cBound = fooRenderFunction.typeParameters.single.bound!;
252250
expect(cBound.getDisplayString(withNullability: false), equals('num'));
253251
});
254252

255253
group('does not generate a renderer', () {
256-
LibraryElement renderersLibrary;
254+
late final LibraryElement renderersLibrary;
257255

258256
setUpAll(() async {
259257
writer = InMemoryAssetWriter();

0 commit comments

Comments
 (0)