| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; | 8 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; |
| 9 import 'package:analyzer/dart/element/element.dart'; |
| 9 import 'package:analyzer/file_system/physical_file_system.dart'; | 10 import 'package:analyzer/file_system/physical_file_system.dart'; |
| 10 import 'package:analyzer/src/dart/sdk/sdk.dart'; | 11 import 'package:analyzer/src/dart/sdk/sdk.dart'; |
| 11 import 'package:front_end/compiler_options.dart'; | 12 import 'package:front_end/compiler_options.dart'; |
| 12 import 'package:front_end/incremental_resolved_ast_generator.dart'; | 13 import 'package:front_end/incremental_resolved_ast_generator.dart'; |
| 13 import 'package:front_end/memory_file_system.dart'; | 14 import 'package:front_end/memory_file_system.dart'; |
| 14 import 'package:path/path.dart' as pathos; | 15 import 'package:path/path.dart' as pathos; |
| 15 import 'package:test/test.dart'; | 16 import 'package:test/test.dart'; |
| 16 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 17 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 17 | 18 |
| 18 main() { | 19 main() { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 49 startingUri, | 50 startingUri, |
| 50 new CompilerOptions() | 51 new CompilerOptions() |
| 51 ..fileSystem = fileSystem | 52 ..fileSystem = fileSystem |
| 52 ..chaseDependencies = true | 53 ..chaseDependencies = true |
| 53 ..sdkSummary = sdkSummaryUri | 54 ..sdkSummary = sdkSummaryUri |
| 54 ..packagesFileUri = new Uri()); | 55 ..packagesFileUri = new Uri()); |
| 55 return (await incrementalResolvedAstGenerator.computeDelta()).newState; | 56 return (await incrementalResolvedAstGenerator.computeDelta()).newState; |
| 56 } | 57 } |
| 57 | 58 |
| 58 test_incrementalUpdate_referenceToCore() async { | 59 test_incrementalUpdate_referenceToCore() async { |
| 59 // TODO(paulberry): test parts. | |
| 60 writeFiles({'/foo.dart': 'main() { print(1); }'}); | 60 writeFiles({'/foo.dart': 'main() { print(1); }'}); |
| 61 var fooUri = Uri.parse('file:///foo.dart'); | 61 var fooUri = Uri.parse('file:///foo.dart'); |
| 62 var initialProgram = await getInitialProgram(fooUri); | 62 var initialProgram = await getInitialProgram(fooUri); |
| 63 expect(initialProgram.keys, unorderedEquals([fooUri])); | 63 expect(initialProgram.keys, unorderedEquals([fooUri])); |
| 64 | 64 |
| 65 void _checkMain(CompilationUnit unit, int expectedArgument) { | 65 void _checkMain(CompilationUnit unit, int expectedArgument) { |
| 66 expect(unit.declarations, hasLength(1)); | 66 var mainStatements = _getFunctionStatements(_getFunction(unit, 'main')); |
| 67 expect(unit.declarations[0], new isInstanceOf<FunctionDeclaration>()); | 67 expect(mainStatements, hasLength(1)); |
| 68 var main = unit.declarations[0] as FunctionDeclaration; | 68 _checkPrintLiteralInt(mainStatements[0], expectedArgument); |
| 69 expect(main.name.name, 'main'); | |
| 70 expect( | |
| 71 main.functionExpression.body, new isInstanceOf<BlockFunctionBody>()); | |
| 72 var blockFunctionBody = main.functionExpression.body as BlockFunctionBody; | |
| 73 expect(blockFunctionBody.block.statements, hasLength(1)); | |
| 74 expect(blockFunctionBody.block.statements[0], | |
| 75 new isInstanceOf<ExpressionStatement>()); | |
| 76 var expressionStatement = | |
| 77 blockFunctionBody.block.statements[0] as ExpressionStatement; | |
| 78 expect( | |
| 79 expressionStatement.expression, new isInstanceOf<MethodInvocation>()); | |
| 80 var methodInvocation = expressionStatement.expression as MethodInvocation; | |
| 81 expect(methodInvocation.methodName.name, 'print'); | |
| 82 var printElement = | |
| 83 resolutionMap.staticElementForIdentifier(methodInvocation.methodName); | |
| 84 expect(printElement, isNotNull); | |
| 85 expect(printElement.library.source.uri, Uri.parse('dart:core')); | |
| 86 expect(methodInvocation.argumentList.arguments, hasLength(1)); | |
| 87 expect(methodInvocation.argumentList.arguments[0], | |
| 88 new isInstanceOf<IntegerLiteral>()); | |
| 89 var integerLiteral = | |
| 90 methodInvocation.argumentList.arguments[0] as IntegerLiteral; | |
| 91 expect(integerLiteral.value, expectedArgument); | |
| 92 } | 69 } |
| 93 | 70 |
| 94 _checkMain(initialProgram[fooUri].definingCompilationUnit, 1); | 71 _checkMain(initialProgram[fooUri].definingCompilationUnit, 1); |
| 95 writeFiles({'/foo.dart': 'main() { print(2); }'}); | 72 writeFiles({'/foo.dart': 'main() { print(2); }'}); |
| 96 // TODO(paulberry): verify that the file isn't actually reread until | 73 // TODO(paulberry): verify that the file isn't actually reread until |
| 97 // invalidate is called. | 74 // invalidate is called. |
| 98 // var deltaProgram1 = await incrementalResolvedAstGenerator.computeDelta(); | 75 // var deltaProgram1 = await incrementalResolvedAstGenerator.computeDelta(); |
| 99 // expect(deltaProgram1.newState, isEmpty); | 76 // expect(deltaProgram1.newState, isEmpty); |
| 100 incrementalResolvedAstGenerator.invalidateAll(); | 77 incrementalResolvedAstGenerator.invalidateAll(); |
| 101 var deltaProgram2 = await incrementalResolvedAstGenerator.computeDelta(); | 78 var deltaProgram2 = await incrementalResolvedAstGenerator.computeDelta(); |
| 102 expect(deltaProgram2.newState.keys, unorderedEquals([fooUri])); | 79 expect(deltaProgram2.newState.keys, unorderedEquals([fooUri])); |
| 103 _checkMain(deltaProgram2.newState[fooUri].definingCompilationUnit, 2); | 80 _checkMain(deltaProgram2.newState[fooUri].definingCompilationUnit, 2); |
| 104 } | 81 } |
| 105 | 82 |
| 106 test_invalidateAllBeforeInitialProgram() async { | 83 test_invalidateAllBeforeInitialProgram() async { |
| 107 incrementalResolvedAstGenerator = new IncrementalResolvedAstGenerator( | 84 incrementalResolvedAstGenerator = new IncrementalResolvedAstGenerator( |
| 108 Uri.parse('file:///foo.dart'), | 85 Uri.parse('file:///foo.dart'), |
| 109 new CompilerOptions() | 86 new CompilerOptions() |
| 110 ..fileSystem = fileSystem | 87 ..fileSystem = fileSystem |
| 111 ..chaseDependencies = true | 88 ..chaseDependencies = true |
| 112 ..packagesFileUri = new Uri()); | 89 ..packagesFileUri = new Uri()); |
| 113 incrementalResolvedAstGenerator.invalidateAll(); | 90 incrementalResolvedAstGenerator.invalidateAll(); |
| 114 } | 91 } |
| 115 | 92 |
| 93 test_part() async { |
| 94 writeFiles({ |
| 95 '/foo.dart': 'library foo; part "bar.dart"; main() { print(1); f(); }', |
| 96 '/bar.dart': 'part of foo; f() { print(2); }' |
| 97 }); |
| 98 var fooUri = Uri.parse('file:///foo.dart'); |
| 99 var barUri = Uri.parse('file:///bar.dart'); |
| 100 var initialProgram = await getInitialProgram(fooUri); |
| 101 expect(initialProgram.keys, unorderedEquals([fooUri])); |
| 102 expect(initialProgram[fooUri].partUnits.keys, unorderedEquals([barUri])); |
| 103 var mainStatements = _getFunctionStatements( |
| 104 _getFunction(initialProgram[fooUri].definingCompilationUnit, 'main')); |
| 105 var fDeclaration = |
| 106 _getFunction(initialProgram[fooUri].partUnits[barUri], 'f'); |
| 107 var fStatements = _getFunctionStatements(fDeclaration); |
| 108 expect(mainStatements, hasLength(2)); |
| 109 _checkPrintLiteralInt(mainStatements[0], 1); |
| 110 _checkFunctionCall(mainStatements[1], |
| 111 resolutionMap.elementDeclaredByFunctionDeclaration(fDeclaration)); |
| 112 expect(fStatements, hasLength(1)); |
| 113 _checkPrintLiteralInt(fStatements[0], 2); |
| 114 // TODO(paulberry): now test incremental updates |
| 115 } |
| 116 |
| 116 /// Write the given file contents to the virtual filesystem. | 117 /// Write the given file contents to the virtual filesystem. |
| 117 void writeFiles(Map<String, String> contents) { | 118 void writeFiles(Map<String, String> contents) { |
| 118 contents.forEach((path, text) { | 119 contents.forEach((path, text) { |
| 119 fileSystem | 120 fileSystem |
| 120 .entityForUri(Uri.parse('file://$path')) | 121 .entityForUri(Uri.parse('file://$path')) |
| 121 .writeAsStringSync(text); | 122 .writeAsStringSync(text); |
| 122 }); | 123 }); |
| 123 } | 124 } |
| 125 |
| 126 void _checkFunctionCall(Statement statement, Element expectedTarget) { |
| 127 expect(statement, new isInstanceOf<ExpressionStatement>()); |
| 128 var expressionStatement = statement as ExpressionStatement; |
| 129 expect( |
| 130 expressionStatement.expression, new isInstanceOf<MethodInvocation>()); |
| 131 var methodInvocation = expressionStatement.expression as MethodInvocation; |
| 132 expect( |
| 133 resolutionMap.staticElementForIdentifier(methodInvocation.methodName), |
| 134 expectedTarget); |
| 135 } |
| 136 |
| 137 void _checkPrintLiteralInt(Statement statement, int expectedArgument) { |
| 138 expect(statement, new isInstanceOf<ExpressionStatement>()); |
| 139 var expressionStatement = statement as ExpressionStatement; |
| 140 expect( |
| 141 expressionStatement.expression, new isInstanceOf<MethodInvocation>()); |
| 142 var methodInvocation = expressionStatement.expression as MethodInvocation; |
| 143 expect(methodInvocation.methodName.name, 'print'); |
| 144 var printElement = |
| 145 resolutionMap.staticElementForIdentifier(methodInvocation.methodName); |
| 146 expect(printElement, isNotNull); |
| 147 expect(printElement.library.source.uri, Uri.parse('dart:core')); |
| 148 expect(methodInvocation.argumentList.arguments, hasLength(1)); |
| 149 expect(methodInvocation.argumentList.arguments[0], |
| 150 new isInstanceOf<IntegerLiteral>()); |
| 151 var integerLiteral = |
| 152 methodInvocation.argumentList.arguments[0] as IntegerLiteral; |
| 153 expect(integerLiteral.value, expectedArgument); |
| 154 } |
| 155 |
| 156 FunctionDeclaration _getFunction(CompilationUnit unit, String name) { |
| 157 for (var declaration in unit.declarations) { |
| 158 if (declaration is FunctionDeclaration && declaration.name.name == name) { |
| 159 return declaration; |
| 160 } |
| 161 } |
| 162 throw fail('No function declaration found with name "$name"'); |
| 163 } |
| 164 |
| 165 NodeList<Statement> _getFunctionStatements(FunctionDeclaration function) { |
| 166 var body = function.functionExpression.body; |
| 167 expect(body, new isInstanceOf<BlockFunctionBody>()); |
| 168 return (body as BlockFunctionBody).block.statements; |
| 169 } |
| 124 } | 170 } |
| OLD | NEW |