diff --git a/engine/src/flutter/flutter_frontend_server/test/to_string_test.dart b/engine/src/flutter/flutter_frontend_server/test/to_string_test.dart index 68f9a907de..3b88950c53 100644 --- a/engine/src/flutter/flutter_frontend_server/test/to_string_test.dart +++ b/engine/src/flutter/flutter_frontend_server/test/to_string_test.dart @@ -7,10 +7,9 @@ import 'dart:io'; import 'package:frontend_server/frontend_server.dart' as frontend show ProgramTransformer, ToStringTransformer, ToStringVisitor; import 'package:kernel/kernel.dart'; -import 'package:mockito/mockito.dart'; import 'package:path/path.dart' as path; -import 'package:test/test.dart'; +import 'utils.dart'; Future main(List args) async { if (args.length != 2) { @@ -26,38 +25,54 @@ Future main(List args) async { test('No packages', () { final frontend.ToStringTransformer transformer = frontend.ToStringTransformer(null, {}); - - final MockComponent component = MockComponent(); + final FakeComponent component = FakeComponent(); transformer.transform(component); - verifyNever(component.visitChildren(any)); + + expect( + !component.visitChildrenCalled, + 'Expected component.visitChildrenCalled to be false', + ); }); test('dart:ui package', () { final frontend.ToStringTransformer transformer = frontend.ToStringTransformer(null, uiAndFlutter); - - final MockComponent component = MockComponent(); + final FakeComponent component = FakeComponent(); transformer.transform(component); - verify(component.visitChildren(any)).called(1); + + expect( + component.visitChildrenCalled, + 'Expected component.visitChildrenCalled to be true', + ); }); test('Child transformer', () { - final MockTransformer childTransformer = MockTransformer(); + final FakeTransformer childTransformer = FakeTransformer(); final frontend.ToStringTransformer transformer = frontend.ToStringTransformer(childTransformer, {}); - - final MockComponent component = MockComponent(); + final FakeComponent component = FakeComponent(); transformer.transform(component); - verifyNever(component.visitChildren(any)); - verify(childTransformer.transform(component)).called(1); + + expect( + !component.visitChildrenCalled, + 'Expected component.visitChildrenCalled to be false', + ); + expect( + childTransformer.transformCalled, + 'Expected childTransformer.transformCalled to be true', + ); }); test('ToStringVisitor ignores non-toString procedures', () { final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter); - final MockProcedure procedure = MockProcedure(); - when(procedure.name).thenReturn(Name('main')); - when(procedure.annotations).thenReturn(const []); - + final FakeProcedure procedure = FakeProcedure( + name: Name('name'), + annotations: const [], + ); visitor.visitProcedure(procedure); - verifyNever(procedure.enclosingLibrary); + + expect( + !procedure.enclosingLibraryCalled, + 'Expected procedure.enclosingLibraryCalled to be false', + ); }); test('ToStringVisitor ignores top level toString', () { @@ -66,184 +81,217 @@ Future main(List args) async { // void main() {} // String toString() => 'why?'; final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter); - final MockProcedure procedure = MockProcedure(); - final MockFunctionNode function = MockFunctionNode(); - final MockStatement statement = MockStatement(); final Uri uri = Uri.parse('package:some_package/src/blah.dart'); final Library library = Library(uri, fileUri: uri); - when(procedure.function).thenReturn(function); - when(procedure.name).thenReturn(Name('toString')); - when(procedure.annotations).thenReturn(const []); - when(procedure.enclosingLibrary).thenReturn(library); - when(procedure.enclosingClass).thenReturn(null); - when(procedure.isAbstract).thenReturn(false); - when(procedure.isStatic).thenReturn(false); - when(function.body).thenReturn(statement); - + final FakeStatement statement = FakeStatement(); + final FakeFunctionNode function = FakeFunctionNode( + body: statement, + ); + final FakeProcedure procedure = FakeProcedure( + name: Name('tostring'), + function: function, + annotations: const [], + enclosingLibrary: library, + isAbstract: false, + isStatic: false, + ); visitor.visitProcedure(procedure); - verifyNever(statement.replaceWith(any)); + + expect( + !statement.replaceWithCalled, + 'Expected statement.replaceWithCalled to be false', + ); }); test('ToStringVisitor ignores abstract toString', () { final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter); - final MockProcedure procedure = MockProcedure(); - final MockFunctionNode function = MockFunctionNode(); - final MockStatement statement = MockStatement(); + final FakeStatement statement = FakeStatement(); + final FakeFunctionNode function = FakeFunctionNode( + body: statement, + ); final Uri uri = Uri.parse('package:some_package/src/blah.dart'); final Library library = Library(uri, fileUri: uri); - when(procedure.function).thenReturn(function); - when(procedure.name).thenReturn(Name('toString')); - when(procedure.annotations).thenReturn(const []); - when(procedure.enclosingLibrary).thenReturn(library); - when(procedure.enclosingClass).thenReturn(Class(name: 'foo', fileUri: uri)); - when(procedure.isAbstract).thenReturn(true); - when(procedure.isStatic).thenReturn(false); - when(function.body).thenReturn(statement); - + final FakeProcedure procedure = FakeProcedure( + name: Name('toString'), + function: function, + annotations: const [], + enclosingLibrary: library, + enclosingClass: Class(name: 'foo', fileUri: uri), + isAbstract: true, + isStatic: false, + ); visitor.visitProcedure(procedure); - verifyNever(statement.replaceWith(any)); + + expect( + !statement.replaceWithCalled, + 'Expected statement.replaceWithCalled to be false', + ); }); test('ToStringVisitor ignores static toString', () { final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter); - final MockProcedure procedure = MockProcedure(); - final MockFunctionNode function = MockFunctionNode(); - final MockStatement statement = MockStatement(); final Uri uri = Uri.parse('package:some_package/src/blah.dart'); + final FakeStatement statement = FakeStatement(); + final FakeFunctionNode function = FakeFunctionNode( + body: statement, + ); final Library library = Library(uri, fileUri: uri); - when(procedure.function).thenReturn(function); - when(procedure.name).thenReturn(Name('toString')); - when(procedure.annotations).thenReturn(const []); - when(procedure.enclosingLibrary).thenReturn(library); - when(procedure.enclosingClass).thenReturn(Class(name: 'foo', fileUri: uri)); - when(procedure.isAbstract).thenReturn(false); - when(procedure.isStatic).thenReturn(true); - when(function.body).thenReturn(statement); - + final FakeProcedure procedure = FakeProcedure( + name: Name('toString'), + function: function, + annotations: const [], + enclosingLibrary: library, + enclosingClass: Class(name: 'foo', fileUri: uri), + isAbstract: false, + isStatic: true, + ); visitor.visitProcedure(procedure); - verifyNever(statement.replaceWith(any)); + + expect( + !statement.replaceWithCalled, + 'Expected statement.replaceWithCalled jto be false', + ); }); test('ToStringVisitor ignores enum toString', () { final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter); - final MockProcedure procedure = MockProcedure(); - final MockFunctionNode function = MockFunctionNode(); - final MockStatement statement = MockStatement(); final Uri uri = Uri.parse('package:some_package/src/blah.dart'); final Library library = Library(uri, fileUri: uri); - when(procedure.function).thenReturn(function); - when(procedure.name).thenReturn(Name('toString')); - when(procedure.annotations).thenReturn(const []); - when(procedure.enclosingLibrary).thenReturn(library); - when(procedure.enclosingClass).thenReturn(Class(name: 'foo', fileUri: uri)..isEnum = true); - when(procedure.isAbstract).thenReturn(false); - when(procedure.isStatic).thenReturn(false); - when(function.body).thenReturn(statement); - + final FakeStatement statement = FakeStatement(); + final FakeFunctionNode function = FakeFunctionNode( + body: statement, + ); + final FakeProcedure procedure = FakeProcedure( + name: Name('toString'), + function: function, + annotations: const [], + enclosingLibrary: library, + enclosingClass: Class(name: 'foo', fileUri: uri)..isEnum = true, + isAbstract: false, + isStatic: false, + ); visitor.visitProcedure(procedure); - verifyNever(statement.replaceWith(any)); + + expect( + !statement.replaceWithCalled, + 'Expected statement.replaceWithCalled to be false', + ); }); test('ToStringVisitor ignores non-specified libraries', () { final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter); - final MockProcedure procedure = MockProcedure(); - final MockFunctionNode function = MockFunctionNode(); - final MockStatement statement = MockStatement(); final Uri uri = Uri.parse('package:some_package/src/blah.dart'); final Library library = Library(uri, fileUri: uri); - when(procedure.function).thenReturn(function); - when(procedure.name).thenReturn(Name('toString')); - when(procedure.annotations).thenReturn(const []); - when(procedure.enclosingLibrary).thenReturn(library); - when(procedure.enclosingClass).thenReturn(Class(name: 'foo', fileUri: uri)); - when(procedure.isAbstract).thenReturn(false); - when(procedure.isStatic).thenReturn(false); - when(function.body).thenReturn(statement); - + final FakeStatement statement = FakeStatement(); + final FakeFunctionNode function = FakeFunctionNode( + body: statement, + ); + final FakeProcedure procedure = FakeProcedure( + name: Name('toString'), + function: function, + annotations: const [], + enclosingLibrary: library, + enclosingClass: Class(name: 'foo', fileUri: uri), + isAbstract: false, + isStatic: false, + ); visitor.visitProcedure(procedure); - verifyNever(statement.replaceWith(any)); + + expect( + !statement.replaceWithCalled, + 'Expected statement.replaceWithCalled to be false', + ); }); test('ToStringVisitor ignores @keepToString', () { final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter); - final MockProcedure procedure = MockProcedure(); - final MockFunctionNode function = MockFunctionNode(); - final MockStatement statement = MockStatement(); final Uri uri = Uri.parse('dart:ui'); final Library library = Library(uri, fileUri: uri); final Name name = Name('toString'); final Class annotation = Class(name: '_KeepToString', fileUri: uri)..parent = library; - - when(procedure.function).thenReturn(function); - when(procedure.name).thenReturn(name); - when(procedure.annotations).thenReturn([ - ConstantExpression( + final FakeStatement statement = FakeStatement(); + final FakeFunctionNode function = FakeFunctionNode( + body: statement, + ); + final FakeProcedure procedure = FakeProcedure( + name: name, + function: function, + annotations: [ConstantExpression( InstanceConstant( Reference()..node = annotation, [], {}, ), - ), - ]); - - when(procedure.enclosingLibrary).thenReturn(library); - when(procedure.enclosingClass).thenReturn(Class(name: 'foo', fileUri: uri)); - when(procedure.isAbstract).thenReturn(false); - when(procedure.isStatic).thenReturn(false); - when(function.body).thenReturn(statement); - + )], + enclosingLibrary: library, + enclosingClass: Class(name: 'foo', fileUri: uri), + isAbstract: false, + isStatic: false, + ); visitor.visitProcedure(procedure); - verifyNever(statement.replaceWith(any)); + + expect( + !statement.replaceWithCalled, + 'Expected statement.replaceWithCalled to be false', + ); }); - void _validateReplacement(MockStatement body) { - final ReturnStatement replacement = verify(body.replaceWith(captureAny)).captured.single as ReturnStatement; - expect(replacement.expression, isA()); + void _validateReplacement(FakeStatement body) { + final ReturnStatement replacement = body.replacement as ReturnStatement; + expect( + replacement.expression is SuperMethodInvocation, + 'Expected replacement.expression to be a SuperMethodInvocation', + ); final SuperMethodInvocation superMethodInvocation = replacement.expression as SuperMethodInvocation; - expect(superMethodInvocation.name.text, 'toString'); + expect( + superMethodInvocation.name.text == 'toString', + 'Expected superMethodInvocation.name.text to be "toString"', + ); } test('ToStringVisitor replaces toString in specified libraries (dart:ui)', () { final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter); - final MockProcedure procedure = MockProcedure(); - final MockFunctionNode function = MockFunctionNode(); - final MockStatement statement = MockStatement(); final Uri uri = Uri.parse('dart:ui'); final Library library = Library(uri, fileUri: uri); final Name name = Name('toString'); - - when(procedure.function).thenReturn(function); - when(procedure.name).thenReturn(name); - when(procedure.annotations).thenReturn(const []); - when(procedure.enclosingLibrary).thenReturn(library); - when(procedure.enclosingClass).thenReturn(Class(name: 'foo', fileUri: uri)); - when(procedure.isAbstract).thenReturn(false); - when(procedure.isStatic).thenReturn(false); - when(function.body).thenReturn(statement); - + final FakeStatement statement = FakeStatement(); + final FakeFunctionNode function = FakeFunctionNode( + body: statement, + ); + final FakeProcedure procedure = FakeProcedure( + name: name, + function: function, + annotations: const [], + enclosingLibrary: library, + enclosingClass: Class(name: 'foo', fileUri: uri), + isAbstract: false, + isStatic: false, + ); visitor.visitProcedure(procedure); + _validateReplacement(statement); }); test('ToStringVisitor replaces toString in specified libraries (package:flutter)', () { final frontend.ToStringVisitor visitor = frontend.ToStringVisitor(uiAndFlutter); - final MockProcedure procedure = MockProcedure(); - final MockFunctionNode function = MockFunctionNode(); - final MockStatement statement = MockStatement(); final Uri uri = Uri.parse('package:flutter/src/foundation.dart'); final Library library = Library(uri, fileUri: uri); final Name name = Name('toString'); - - when(procedure.function).thenReturn(function); - when(procedure.name).thenReturn(name); - when(procedure.annotations).thenReturn(const []); - when(procedure.enclosingLibrary).thenReturn(library); - when(procedure.enclosingClass).thenReturn(Class(name: 'foo', fileUri: uri)); - when(procedure.isAbstract).thenReturn(false); - when(procedure.isStatic).thenReturn(false); - when(function.body).thenReturn(statement); - + final FakeStatement statement = FakeStatement(); + final FakeFunctionNode function = FakeFunctionNode( + body: statement, + ); + final FakeProcedure procedure = FakeProcedure( + name: name, + function: function, + annotations: const [], + enclosingLibrary: library, + enclosingClass: Class(name: 'foo', fileUri: uri), + isAbstract: false, + isStatic: false, + ); visitor.visitProcedure(procedure); + _validateReplacement(statement); }); @@ -264,10 +312,10 @@ Future main(List args) async { stdout.writeln(result.stdout); stderr.writeln(result.stderr); } - expect(result.exitCode, 0); + expect(result.exitCode == 0, 'Expected result.exitCode to be 0'); } - test('Without flag', () async { + test('Without flag', () { _checkProcessResult(Process.runSync(dart, [ frontendServer, '--sdk-root=$sdkRoot', @@ -282,16 +330,19 @@ Future main(List args) async { if (const bool.fromEnvironment('dart.vm.product', defaultValue: false)) { paintString = '"Paint.toString":"Instance of \'Paint\'"'; } + + final String expectedStdout = '{$paintString,' + '"Brightness.toString":"Brightness.dark",' + '"Foo.toString":"I am a Foo",' + '"Keep.toString":"I am a Keep"}'; + final String actualStdout = runResult.stdout.trim() as String; expect( - runResult.stdout.trim(), - '{$paintString,' - '"Brightness.toString":"Brightness.dark",' - '"Foo.toString":"I am a Foo",' - '"Keep.toString":"I am a Keep"}', + actualStdout == expectedStdout, + 'Expected "$expectedStdout" but got "$actualStdout"', ); }); - test('With flag', () async { + test('With flag', () { _checkProcessResult(Process.runSync(dart, [ frontendServer, '--sdk-root=$sdkRoot', @@ -304,19 +355,110 @@ Future main(List args) async { ])); final ProcessResult runResult = Process.runSync(dart, [transformedDill]); _checkProcessResult(runResult); + + const String expectedStdout = '{"Paint.toString":"Instance of \'Paint\'",' + '"Brightness.toString":"Brightness.dark",' + '"Foo.toString":"Instance of \'Foo\'",' + '"Keep.toString":"I am a Keep"}'; + final String actualStdout = runResult.stdout.trim() as String; expect( - runResult.stdout.trim(), - '{"Paint.toString":"Instance of \'Paint\'",' - '"Brightness.toString":"Brightness.dark",' - '"Foo.toString":"Instance of \'Foo\'",' - '"Keep.toString":"I am a Keep"}', + actualStdout == expectedStdout, + 'Expected "$expectedStdout" but got "$actualStdout"', ); }); }); + + if (TestFailure.testFailures == 0) { + print('All tests passed!'); + exit(0); + } else { + print('${TestFailure.testFailures} test expectations failed'); + exit(1); + } } -class MockComponent extends Mock implements Component {} -class MockTransformer extends Mock implements frontend.ProgramTransformer {} -class MockProcedure extends Mock implements Procedure {} -class MockFunctionNode extends Mock implements FunctionNode {} -class MockStatement extends Mock implements Statement {} +abstract class Fake { + @override + dynamic noSuchMethod(Invocation invocation) { + throw UnimplementedError(invocation.memberName.toString().split('"')[1]); + } +} + +class FakeComponent extends Fake implements Component { + bool visitChildrenCalled = false; + + @override + void visitChildren(Visitor v) { + visitChildrenCalled = true; + } +} + +class FakeTransformer extends Fake implements frontend.ProgramTransformer { + bool transformCalled = false; + + @override + void transform(Component component) { + transformCalled = true; + } +} + +class FakeProcedure extends Fake implements Procedure { + FakeProcedure({ + this.name, + this.function, + this.annotations, + Library enclosingLibrary, + this.enclosingClass, + this.isAbstract, + this.isStatic, + }) : _enclosingLibrary = enclosingLibrary; + + @override + final Name name; + + @override + final FunctionNode function; + + @override + final List annotations; + + @override + final bool isAbstract; + + @override + final bool isStatic; + + @override + final Class enclosingClass; + + final Library _enclosingLibrary; + + bool enclosingLibraryCalled = false; + + @override + Library get enclosingLibrary { + enclosingLibraryCalled = true; + return _enclosingLibrary; + } +} + +class FakeFunctionNode extends Fake implements FunctionNode { + FakeFunctionNode({ + this.body, + }); + + @override + final Statement body; +} + +class FakeStatement extends Fake implements Statement { + bool replaceWithCalled = false; + + TreeNode replacement; + + @override + void replaceWith(TreeNode r) { + replaceWithCalled = true; + replacement = r; + } +} diff --git a/engine/src/flutter/flutter_frontend_server/test/utils.dart b/engine/src/flutter/flutter_frontend_server/test/utils.dart new file mode 100644 index 0000000000..cb5fc743b4 --- /dev/null +++ b/engine/src/flutter/flutter_frontend_server/test/utils.dart @@ -0,0 +1,39 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:io'; + +class TestFailure implements Exception { + TestFailure(this.message) { + testFailures++; + } + + static int testFailures = 0; + + final String message; + + @override + String toString() => 'TestFailure("$message")'; +} + +void expect(bool b, String message) { + if (!b) { + throw TestFailure(message); + } +} + +void test(String name, void Function() fn) { + stdout.write('Running $name: '); + try { + fn(); + stdout.writeln('Passed'); + } on TestFailure catch (e, st) { + stdout.writeln('Failed\n$e\n$st'); + } +} + +void group(String name, void Function() fn) { + print('Running group $name'); + fn(); +}