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