| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 /// Unittest test of the CPS ir generated by the analyzer2dart compiler. | 5 /// Unittest test of the CPS ir generated by the analyzer2dart compiler. |
| 6 | 6 |
| 7 import 'mock_sdk.dart'; | 7 import 'mock_sdk.dart'; |
| 8 import 'package:analyzer/file_system/memory_file_system.dart'; | 8 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 9 import 'package:analyzer/src/generated/element.dart'; | 9 import 'package:analyzer/src/generated/element.dart'; |
| 10 import 'package:analyzer/src/generated/sdk.dart'; | 10 import 'package:analyzer/src/generated/sdk.dart'; |
| 11 import 'package:analyzer/src/generated/source.dart'; | 11 import 'package:analyzer/src/generated/source.dart'; |
| 12 import 'package:compiler/src/cps_ir/cps_ir_nodes.dart'; |
| 12 import 'package:compiler/src/cps_ir/cps_ir_nodes_sexpr.dart'; | 13 import 'package:compiler/src/cps_ir/cps_ir_nodes_sexpr.dart'; |
| 13 import 'package:compiler/src/elements/elements.dart' as dart2js; | 14 import 'package:compiler/src/elements/elements.dart' as dart2js; |
| 14 import 'package:unittest/unittest.dart'; | 15 import 'package:unittest/unittest.dart'; |
| 15 | 16 |
| 16 import '../lib/src/closed_world.dart'; | 17 import '../lib/src/closed_world.dart'; |
| 17 import '../lib/src/driver.dart'; | 18 import '../lib/src/driver.dart'; |
| 18 import '../lib/src/converted_world.dart'; | 19 import '../lib/src/converted_world.dart'; |
| 19 import 'output_helper.dart'; | 20 import 'output_helper.dart'; |
| 20 import 'test_helper.dart'; | 21 import 'test_helper.dart'; |
| 21 import 'sexpr_data.dart'; | 22 import 'sexpr_data.dart'; |
| 22 | 23 |
| 23 main() { | 24 main(List<String> args) { |
| 24 performTests(TEST_DATA, unittester, checkResult); | 25 performTests(TEST_DATA, unittester, checkResult, args); |
| 25 } | 26 } |
| 26 | 27 |
| 27 checkResult(TestSpec result) { | 28 checkResult(TestSpec result) { |
| 28 if (result.skipInAnalyzerFrontend) return; | 29 if (result.skipInAnalyzerFrontend) return; |
| 29 String input = result.input.trim(); | 30 String input = result.input.trim(); |
| 30 CollectingOutputProvider outputProvider = new CollectingOutputProvider(); | 31 CollectingOutputProvider outputProvider = new CollectingOutputProvider(); |
| 31 MemoryResourceProvider provider = new MemoryResourceProvider(); | 32 MemoryResourceProvider provider = new MemoryResourceProvider(); |
| 32 DartSdk sdk = new MockSdk(); | 33 DartSdk sdk = new MockSdk(); |
| 33 Driver driver = new Driver(provider, sdk, outputProvider); | 34 Driver driver = new Driver(provider, sdk, outputProvider); |
| 34 String rootFile = '/root.dart'; | 35 String rootFile = '/root.dart'; |
| 35 provider.newFile(rootFile, input); | 36 provider.newFile(rootFile, input); |
| 36 Source rootSource = driver.setRoot(rootFile); | 37 Source rootSource = driver.setRoot(rootFile); |
| 37 FunctionElement entryPoint = driver.resolveEntryPoint(rootSource); | 38 FunctionElement entryPoint = driver.resolveEntryPoint(rootSource); |
| 38 ClosedWorld world = driver.computeWorld(entryPoint); | 39 ClosedWorld world = driver.computeWorld(entryPoint); |
| 39 ConvertedWorld convertedWorld = convertWorld(world); | 40 ConvertedWorld convertedWorld = convertWorld(world); |
| 40 | 41 |
| 41 void checkOutput(String elementName, | 42 void checkOutput(String elementName, |
| 42 dart2js.Element element, | 43 dart2js.Element element, |
| 43 String expectedOutput) { | 44 String expectedOutput) { |
| 44 expectedOutput = expectedOutput.trim(); | 45 ExecutableDefinition ir = convertedWorld.getIr(element); |
| 45 String output = convertedWorld.getIr(element) | 46 if (expectedOutput == null) { |
| 46 .accept(new SExpressionStringifier()); | 47 expect(ir, isNull, |
| 47 expect(output, equals(expectedOutput), | 48 reason: "\nInput:\n${result.input}\n" |
| 48 reason: "\nInput:\n${result.input}\n" | 49 "No CPS IR expected for $element"); |
| 49 "Expected for '$elementName':\n$expectedOutput\n" | 50 } else { |
| 50 "Actual for '$elementName':\n$output\n"); | 51 expect(ir, isNotNull, |
| 52 reason: "\nInput:\n${result.input}\n" |
| 53 "No CPS IR for $element"); |
| 54 expectedOutput = expectedOutput.trim(); |
| 55 String output = ir.accept(new SExpressionStringifier()); |
| 56 expect(output, equals(expectedOutput), |
| 57 reason: "\nInput:\n${result.input}\n" |
| 58 "Expected for '$elementName':\n$expectedOutput\n" |
| 59 "Actual for '$elementName':\n$output\n"); |
| 60 } |
| 51 } | 61 } |
| 52 | 62 |
| 53 if (result.output is String) { | 63 if (result.output is String) { |
| 54 checkOutput('main', convertedWorld.mainFunction, result.output); | 64 checkOutput('main', convertedWorld.mainFunction, result.output); |
| 55 } else { | 65 } else { |
| 56 assert(result.output is Map<String, String>); | 66 assert(result.output is Map<String, String>); |
| 57 dart2js.LibraryElement mainLibrary = convertedWorld.mainFunction.library; | 67 dart2js.LibraryElement mainLibrary = convertedWorld.mainFunction.library; |
| 58 result.output.forEach((String elementName, String output) { | 68 result.output.forEach((String elementName, String output) { |
| 59 bool found = false; | 69 bool found = false; |
| 60 List<String> names = <String>[]; | 70 List<String> names = <String>[]; |
| 61 convertedWorld.resolvedElements.forEach((dart2js.Element element) { | 71 convertedWorld.resolvedElements.forEach((dart2js.Element element) { |
| 62 if (element.library == mainLibrary) { | 72 if (element.library == mainLibrary) { |
| 63 String name = element.name; | 73 String name = element.name; |
| 64 if (element.enclosingClass != null) { | 74 if (element.enclosingClass != null) { |
| 65 name = '${element.enclosingClass.name}.$name'; | 75 name = '${element.enclosingClass.name}.$name'; |
| 66 } | 76 } |
| 67 if (name == elementName) { | 77 if (name == elementName) { |
| 68 checkOutput(elementName, element, output); | 78 checkOutput(elementName, element, output); |
| 69 found = true; | 79 found = true; |
| 70 } | 80 } |
| 71 names.add(name); | 81 names.add(name); |
| 72 } | 82 } |
| 73 }); | 83 }); |
| 74 expect(found, isTrue, reason: "'$elementName' not found in $names."); | 84 expect(found, isTrue, reason: "'$elementName' not found in $names."); |
| 75 }); | 85 }); |
| 76 } | 86 } |
| 77 } | 87 } |
| 78 | 88 |
| OLD | NEW |