Unverified Commit 4881b335 authored by Alexandre Ardhuin's avatar Alexandre Ardhuin Committed by GitHub

use throwsA matcher instead of try-catch-fail (#82290)

parent e621ebfb
......@@ -274,14 +274,14 @@ void main() {
);
await creator.initializeRepo();
try {
await creator.createArchive();
fail('failed to throw');
} on Exception catch (e) {
expect(e is PreparePackageException, true);
final PreparePackageException exception = e as PreparePackageException;
expect(exception.message, contains('The binary $binPath was not codesigned!'));
}
await expectLater(
() => creator.createArchive(),
throwsA(isA<PreparePackageException>().having(
(PreparePackageException exception) => exception.message,
'message',
contains('The binary $binPath was not codesigned!'),
)),
);
}, skip: !platform.isMacOS);
});
......
......@@ -183,28 +183,30 @@ void main() {
test('and generates correct output in the correct base location', () async {
comparator = LocalFileComparator(Uri.parse('local_test.dart'), pathStyle: fs.path.style);
await fs.file(fix('/golden.png')).writeAsBytes(_kColorFailurePngBytes);
try {
await doComparison();
fail('TestFailure expected but not thrown.');
} on FlutterError catch (error) {
expect(error.message, contains('% diff detected'));
final io.File master = fs.file(
fix('/failures/golden_masterImage.png')
);
final io.File test = fs.file(
fix('/failures/golden_testImage.png')
);
final io.File isolated = fs.file(
fix('/failures/golden_isolatedDiff.png')
);
final io.File masked = fs.file(
fix('/failures/golden_maskedDiff.png')
);
expect(master.existsSync(), isTrue);
expect(test.existsSync(), isTrue);
expect(isolated.existsSync(), isTrue);
expect(masked.existsSync(), isTrue);
}
await expectLater(
() => doComparison(),
throwsA(isA<FlutterError>().having(
(FlutterError error) => error.message,
'message',
contains('% diff detected'),
)),
);
final io.File master = fs.file(
fix('/failures/golden_masterImage.png')
);
final io.File test = fs.file(
fix('/failures/golden_testImage.png')
);
final io.File isolated = fs.file(
fix('/failures/golden_isolatedDiff.png')
);
final io.File masked = fs.file(
fix('/failures/golden_maskedDiff.png')
);
expect(master.existsSync(), isTrue);
expect(test.existsSync(), isTrue);
expect(isolated.existsSync(), isTrue);
expect(masked.existsSync(), isTrue);
});
test('and generates correct output when files are in a subdirectory', () async {
......@@ -212,67 +214,77 @@ void main() {
fs.file(fix('subdir/golden.png'))
..createSync(recursive:true)
..writeAsBytesSync(_kColorFailurePngBytes);
try {
await doComparison('subdir/golden.png');
fail('TestFailure expected but not thrown.');
} on FlutterError catch (error) {
expect(error.message, contains('% diff detected'));
final io.File master = fs.file(
fix('/failures/golden_masterImage.png')
);
final io.File test = fs.file(
fix('/failures/golden_testImage.png')
);
final io.File isolated = fs.file(
fix('/failures/golden_isolatedDiff.png')
);
final io.File masked = fs.file(
fix('/failures/golden_maskedDiff.png')
);
expect(master.existsSync(), isTrue);
expect(test.existsSync(), isTrue);
expect(isolated.existsSync(), isTrue);
expect(masked.existsSync(), isTrue);
}
await expectLater(
() => doComparison('subdir/golden.png'),
throwsA(isA<FlutterError>().having(
(FlutterError error) => error.message,
'message',
contains('% diff detected'),
)),
);
final io.File master = fs.file(
fix('/failures/golden_masterImage.png')
);
final io.File test = fs.file(
fix('/failures/golden_testImage.png')
);
final io.File isolated = fs.file(
fix('/failures/golden_isolatedDiff.png')
);
final io.File masked = fs.file(
fix('/failures/golden_maskedDiff.png')
);
expect(master.existsSync(), isTrue);
expect(test.existsSync(), isTrue);
expect(isolated.existsSync(), isTrue);
expect(masked.existsSync(), isTrue);
});
test('when golden file does not exist', () async {
try {
await doComparison();
fail('TestFailure expected but not thrown.');
} on TestFailure catch (error) {
expect(error.message, contains('Could not be compared against non-existent file'));
}
await expectLater(
() => doComparison(),
throwsA(isA<TestFailure>().having(
(TestFailure error) => error.message,
'message',
contains('Could not be compared against non-existent file'),
)),
);
});
test('when images are not the same size', () async{
await fs.file(fix('/golden.png')).writeAsBytes(_kSizeFailurePngBytes);
try {
await doComparison();
fail('TestFailure expected but not thrown.');
} on FlutterError catch (error) {
expect(error.message, contains('image sizes do not match'));
}
await expectLater(
() => doComparison(),
throwsA(isA<FlutterError>().having(
(FlutterError error) => error.message,
'message',
contains('image sizes do not match'),
)),
);
});
test('when pixels do not match', () async{
await fs.file(fix('/golden.png')).writeAsBytes(_kColorFailurePngBytes);
try {
await doComparison();
fail('TestFailure expected but not thrown.');
} on FlutterError catch (error) {
expect(error.message, contains('% diff detected'));
}
await expectLater(
() => doComparison(),
throwsA(isA<FlutterError>().having(
(FlutterError error) => error.message,
'message',
contains('% diff detected'),
)),
);
});
test('when golden bytes are empty', () async {
await fs.file(fix('/golden.png')).writeAsBytes(<int>[]);
try {
await doComparison();
fail('TestFailure expected but not thrown.');
} on FlutterError catch (error) {
expect(error.message, contains('null image provided'));
}
await expectLater(
() => doComparison(),
throwsA(isA<FlutterError>().having(
(FlutterError error) => error.message,
'message',
contains('null image provided'),
)),
);
});
});
});
......
......@@ -368,38 +368,44 @@ void main() {
comparator.behavior = _ComparatorBehavior.returnFalse;
await tester.pumpWidget(boilerplate(const Text('hello')));
final Finder finder = find.byType(Text);
try {
await expectLater(finder, matchesGoldenFile('foo.png'));
fail('TestFailure expected but not thrown');
} on TestFailure catch (error) {
expect(comparator.invocation, _ComparatorInvocation.compare);
expect(error.message, contains('does not match'));
}
await expectLater(
() => expectLater(finder, matchesGoldenFile('foo.png')),
throwsA(isA<TestFailure>().having(
(TestFailure error) => error.message,
'message',
contains('does not match'),
)),
);
expect(comparator.invocation, _ComparatorInvocation.compare);
});
testWidgets('if comparator throws', (WidgetTester tester) async {
comparator.behavior = _ComparatorBehavior.throwTestFailure;
await tester.pumpWidget(boilerplate(const Text('hello')));
final Finder finder = find.byType(Text);
try {
await expectLater(finder, matchesGoldenFile('foo.png'));
fail('TestFailure expected but not thrown');
} on TestFailure catch (error) {
expect(comparator.invocation, _ComparatorInvocation.compare);
expect(error.message, contains('fake message'));
}
await expectLater(
() => expectLater(finder, matchesGoldenFile('foo.png')),
throwsA(isA<TestFailure>().having(
(TestFailure error) => error.message,
'message',
contains('fake message'),
)),
);
expect(comparator.invocation, _ComparatorInvocation.compare);
});
testWidgets('if finder finds no widgets', (WidgetTester tester) async {
await tester.pumpWidget(boilerplate(Container()));
final Finder finder = find.byType(Text);
try {
await expectLater(finder, matchesGoldenFile('foo.png'));
fail('TestFailure expected but not thrown');
} on TestFailure catch (error) {
expect(comparator.invocation, isNull);
expect(error.message, contains('no widget was found'));
}
await expectLater(
() => expectLater(finder, matchesGoldenFile('foo.png')),
throwsA(isA<TestFailure>().having(
(TestFailure error) => error.message,
'message',
contains('no widget was found'),
)),
);
expect(comparator.invocation, isNull);
});
testWidgets('if finder finds multiple widgets', (WidgetTester tester) async {
......@@ -407,13 +413,15 @@ void main() {
children: const <Widget>[Text('hello'), Text('world')],
)));
final Finder finder = find.byType(Text);
try {
await expectLater(finder, matchesGoldenFile('foo.png'));
fail('TestFailure expected but not thrown');
} on TestFailure catch (error) {
expect(comparator.invocation, isNull);
expect(error.message, contains('too many widgets'));
}
await expectLater(
() => expectLater(finder, matchesGoldenFile('foo.png')),
throwsA(isA<TestFailure>().having(
(TestFailure error) => error.message,
'message',
contains('too many widgets'),
)),
);
expect(comparator.invocation, isNull);
});
});
......
......@@ -43,11 +43,9 @@ void main() {
throw FlutterError('A fake error occurred during action processing.');
});
try {
await tester.testTextInput.receiveAction(TextInputAction.done);
fail('Expected a PlatformException, but it was not thrown.');
} catch (e) {
expect(e, isA<PlatformException>());
}
await expectLater(
() => tester.testTextInput.receiveAction(TextInputAction.done),
throwsA(isA<PlatformException>()),
);
});
}
......@@ -288,20 +288,17 @@ void main() {
processManager: FakeProcessManager.any(),
logger: logger,
);
try {
await cache.updateAll(<DevelopmentArtifact>{
null,
});
fail('Mock thrown exception expected');
} on Exception {
verify(artifact1.update(any, any, any, any));
// Don't continue when retrieval fails.
verifyNever(artifact2.update(any, any, any, any));
expect(
logger.errorText,
contains('https://flutter.dev/community/china'),
);
}
await expectLater(
() => cache.updateAll(<DevelopmentArtifact>{null}),
throwsA(isA<Exception>()),
);
verify(artifact1.update(any, any, any, any));
// Don't continue when retrieval fails.
verifyNever(artifact2.update(any, any, any, any));
expect(
logger.errorText,
contains('https://flutter.dev/community/china'),
);
});
testWithoutContext('Invalid URI for FLUTTER_STORAGE_BASE_URL throws ToolExit', () async {
......
......@@ -210,24 +210,23 @@ void main() {
throwToolExit('fail');
}
);
try {
await flutterCommand.run();
fail('Mock should make this fail');
} on ToolExit {
expect(usage.events, <TestUsageEvent>[
const TestUsageEvent(
'tool-command-result',
'dummy',
label: 'fail',
),
const TestUsageEvent(
'tool-command-max-rss',
'dummy',
label: 'fail',
value: 10,
),
]);
}
await expectLater(
() => flutterCommand.run(),
throwsA(isA<ToolExit>()),
);
expect(usage.events, <TestUsageEvent>[
const TestUsageEvent(
'tool-command-result',
'dummy',
label: 'fail',
),
const TestUsageEvent(
'tool-command-max-rss',
'dummy',
label: 'fail',
value: 10,
),
]);
});
test('FlutterCommandResult.success()', () async {
......@@ -435,18 +434,18 @@ void main() {
},
);
try {
await flutterCommand.run();
fail('Mock should make this fail');
} on ToolExit {
expect(usage.timings, contains(
const TestTimingEvent(
'flutter',
'dummy',
Duration(milliseconds: 1000),
label: 'fail',
)));
}
await expectLater(
() => flutterCommand.run(),
throwsA(isA<ToolExit>()),
);
expect(usage.timings, contains(
const TestTimingEvent(
'flutter',
'dummy',
Duration(milliseconds: 1000),
label: 'fail',
),
));
});
testUsingContext('reports null safety analytics when reportNullSafety is true', () async {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment