OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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. |
| 4 |
| 5 import "package:expect/expect.dart"; |
| 6 import "package:kernel/ast.dart"; |
| 7 import "package:kernel/core_types.dart"; |
| 8 import "package:kernel/testing/mock_sdk_program.dart"; |
| 9 import "package:kernel/text/ast_to_text.dart"; |
| 10 import "package:kernel/transformations/erasure.dart"; |
| 11 |
| 12 void main(List<String> arguments) { |
| 13 new Tester().testLocalFunction(); |
| 14 } |
| 15 |
| 16 class Tester { |
| 17 final Program program; |
| 18 |
| 19 final Library library; |
| 20 |
| 21 final Procedure mainMethod; |
| 22 |
| 23 final CoreTypes coreTypes; |
| 24 |
| 25 static final Uri base = |
| 26 new Uri(scheme: "org.dartlang.kernel", host: "", path: "/"); |
| 27 |
| 28 Tester.internal(this.program, this.library, this.mainMethod) |
| 29 : coreTypes = new CoreTypes(program); |
| 30 |
| 31 factory Tester() { |
| 32 Program program = createMockSdkProgram(); |
| 33 Library library = new Library(base.resolve("main.dart"))..parent = program; |
| 34 Procedure mainMethod = buildProcedure("main"); |
| 35 library.addMember(mainMethod); |
| 36 program.libraries.add(library); |
| 37 program.mainMethod = mainMethod; |
| 38 return new Tester.internal(program, library, mainMethod); |
| 39 } |
| 40 |
| 41 void addStatement(Statement statement) { |
| 42 Block body = mainMethod.function.body; |
| 43 body.statements.add(statement); |
| 44 statement.parent = body; |
| 45 } |
| 46 |
| 47 void testLocalFunction() { |
| 48 FunctionDeclaration fDeclaration = buildFunctionDeclaration( |
| 49 "f", buildFunctionNodeWithTypesAndParameters()); |
| 50 FunctionExpression functionExpression = |
| 51 new FunctionExpression(buildFunctionNodeWithTypesAndParameters()); |
| 52 |
| 53 addStatement(fDeclaration); |
| 54 addStatement(new ExpressionStatement(functionExpression)); |
| 55 Expect.stringEquals( |
| 56 "<T extends dynamic, S extends dart.core::List<T>>(S) → T", |
| 57 debugNodeToString(fDeclaration.variable.type).trim()); |
| 58 Expect.stringEquals( |
| 59 "<T extends dynamic, S extends dart.core::List<T>>(S) → T", |
| 60 debugNodeToString(functionExpression.getStaticType(null)).trim()); |
| 61 transformProgram(coreTypes, program); |
| 62 Expect.stringEquals("(dart.core::List<dynamic>) → dynamic", |
| 63 debugNodeToString(fDeclaration.variable.type).trim()); |
| 64 Expect.stringEquals("(dart.core::List<dynamic>) → dynamic", |
| 65 debugNodeToString(functionExpression.getStaticType(null)).trim()); |
| 66 } |
| 67 |
| 68 /// Builds this function: `f<T, S extends List<T>>(S argument) → T {}`. |
| 69 FunctionNode buildFunctionNodeWithTypesAndParameters() { |
| 70 TypeParameter tVariable = new TypeParameter("T", const DynamicType()); |
| 71 TypeParameter sVariable = new TypeParameter("S", const DynamicType()); |
| 72 TypeParameterType tType = new TypeParameterType(tVariable); |
| 73 TypeParameterType sType = new TypeParameterType(sVariable); |
| 74 sVariable.bound = new InterfaceType(coreTypes.listClass, <DartType>[tType]); |
| 75 return new FunctionNode(buildBlock(), |
| 76 positionalParameters: <VariableDeclaration>[ |
| 77 new VariableDeclaration("argument", type: sType), |
| 78 ], |
| 79 typeParameters: <TypeParameter>[tVariable, sVariable], |
| 80 returnType: tType); |
| 81 } |
| 82 |
| 83 static Block buildBlock([List<Statement> statements]) { |
| 84 return new Block(statements ?? <Statement>[]); |
| 85 } |
| 86 |
| 87 static FunctionNode buildFunction([Statement body]) { |
| 88 return new FunctionNode(body ?? buildBlock()); |
| 89 } |
| 90 |
| 91 static Procedure buildProcedure(String name, [FunctionNode function]) { |
| 92 return new Procedure( |
| 93 new Name(name), ProcedureKind.Method, function ?? buildFunction()); |
| 94 } |
| 95 |
| 96 static FunctionDeclaration buildFunctionDeclaration( |
| 97 String name, FunctionNode function) { |
| 98 return new FunctionDeclaration( |
| 99 new VariableDeclaration(name, |
| 100 type: function.functionType, isFinal: true), |
| 101 function); |
| 102 } |
| 103 } |
OLD | NEW |