| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 #library("stub_generator_test_config"); | 5 #library("stub_generator_test_config"); |
| 6 | 6 |
| 7 #import("../../tools/testing/dart/test_suite.dart"); | 7 #import("../../tools/testing/dart/test_suite.dart"); |
| 8 | 8 |
| 9 class StubGeneratorTestSuite extends StandardTestSuite { | 9 class StubGeneratorTestSuite extends StandardTestSuite { |
| 10 String dartcPath; |
| 11 |
| 10 StubGeneratorTestSuite(Map configuration) | 12 StubGeneratorTestSuite(Map configuration) |
| 11 : super(configuration, | 13 : super(configuration, |
| 12 "stub-generator", | 14 "stub-generator", |
| 13 "tests/stub-generator/src", | 15 "tests/stub-generator/src", |
| 14 ["tests/stub-generator/stub-generator.status"]) { | 16 ["tests/stub-generator/stub-generator.status"]) { |
| 15 // TODO(ager): Support the stub generation part of this test on | 17 try { |
| 16 // dartc. | 18 dartcPath = TestUtils.dartcCompilationShellPath(configuration); |
| 17 if (configuration["component"] == "dartc") { | 19 } catch (var e) { |
| 18 print("Warning: stub-generator tests on dartc do not test generation"); | 20 // ignore |
| 19 } | 21 } |
| 20 } | 22 } |
| 21 | 23 |
| 22 bool isTestFile(String filename) => filename.contains("-generatedTest.dart"); | 24 void combineFiles(String filename, |
| 25 String stubsFile, |
| 26 Function onGenerated) { |
| 27 File orig = new File(filename); |
| 28 File stubs = new File(stubsFile); |
| 29 Expect.isTrue(filename.endsWith(".dart")); |
| 30 String baseName = filename.substring(0, filename.length - 5); |
| 31 String resultPath = '${baseName}-generatedTest.dart'; |
| 32 File result = new File(resultPath); |
| 33 StringInputStream origStream = |
| 34 new StringInputStream(orig.openInputStream()); |
| 35 FileOutputStream resultStream = result.openOutputStream(); |
| 36 |
| 37 // First copy first comments and imports from original file. |
| 38 var origLine = origStream.readLine(); |
| 39 while (origLine != null) { |
| 40 origLine = origLine.trim(); |
| 41 if (origLine.isEmpty() || |
| 42 origLine.startsWith('//') || |
| 43 origLine.startsWith('#')) { |
| 44 resultStream.write(origLine.charCodes()); |
| 45 resultStream.write('\n'.charCodes()); |
| 46 origLine = origStream.readLine(); |
| 47 } else { |
| 48 break; |
| 49 } |
| 50 } |
| 51 |
| 52 // Then copy in the generated stubs code. |
| 53 StringInputStream stubsStream = |
| 54 new StringInputStream(stubs.openInputStream()); |
| 55 var stubsLine = stubsStream.readLine(); |
| 56 while (stubsLine != null) { |
| 57 resultStream.write(stubsLine.charCodes()); |
| 58 resultStream.write('\n'.charCodes()); |
| 59 stubsLine = stubsStream.readLine(); |
| 60 } |
| 61 |
| 62 // Then copy in the rest of the original file. |
| 63 while (origLine != null) { |
| 64 resultStream.write(origLine.charCodes()); |
| 65 resultStream.write('\n'.charCodes()); |
| 66 origLine = origStream.readLine(); |
| 67 } |
| 68 |
| 69 // Done. |
| 70 resultStream.close(); |
| 71 onGenerated(resultPath); |
| 72 } |
| 73 |
| 74 void generateTestCase(String filename, |
| 75 String interfaceFile, |
| 76 String classes, |
| 77 Function onGenerated) { |
| 78 testGeneratorStarted(); |
| 79 Directory temp = new Directory(''); |
| 80 temp.createTempSync(); |
| 81 File stubsOutFile = new File("${temp.path}/${interfaceFile}"); |
| 82 stubsOutFile.createSync(); |
| 83 String stubsPath = stubsOutFile.fullPathSync(); |
| 84 Process dartcProcess = new Process(dartcPath, |
| 85 [filename, |
| 86 '-noincremental', |
| 87 '-out', temp.path, |
| 88 '-isolate-stub-out', stubsPath, |
| 89 '-generate-isolate-stubs', classes ]); |
| 90 dartcProcess.exitHandler = (int exitCode) { |
| 91 combineFiles(filename, stubsPath, onGenerated); |
| 92 }; |
| 93 dartcProcess.start(); |
| 94 } |
| 95 |
| 96 void processFile(String filename) { |
| 97 // Only run the tests that match the pattern. |
| 98 RegExp pattern = configuration['selectors'][suiteName]; |
| 99 if (!pattern.hasMatch(filename)) return; |
| 100 var optionsFromFile = optionsFromFile(filename); |
| 101 var timeout = configuration['timeout']; |
| 102 Function createTestCase = makeTestCaseCreator(optionsFromFile, timeout); |
| 103 |
| 104 if (filename.endsWith("-generatedTest.dart")) { |
| 105 if (dartcPath == null) { |
| 106 createTestCase(filename, optionsFromFile['isNegative']); |
| 107 } |
| 108 } else if (filename.endsWith("Test.dart")) { |
| 109 if (dartcPath != null) { |
| 110 String isolateStubsOptions = optionsFromFile['isolateStubs']; |
| 111 List<String> splitIsolateStubsOptions = isolateStubsOptions.split(':'); |
| 112 Expect.equals(2, splitIsolateStubsOptions.length); |
| 113 String interfaceFile = splitIsolateStubsOptions[0]; |
| 114 String classes = splitIsolateStubsOptions[1]; |
| 115 generateTestCase(filename, interfaceFile, classes, (String filename) { |
| 116 createTestCase(filename, optionsFromFile['isNegative']); |
| 117 testGeneratorDone(); |
| 118 }); |
| 119 } |
| 120 } |
| 121 } |
| 23 } | 122 } |
| OLD | NEW |