| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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("dart:io"); | |
| 8 | |
| 9 #import("../../tools/testing/dart/test_suite.dart"); | 7 #import("../../tools/testing/dart/test_suite.dart"); |
| 10 | 8 |
| 11 class StubGeneratorTestSuite extends StandardTestSuite { | 9 class StubGeneratorTestSuite extends StandardTestSuite { |
| 12 String dartcPath; | |
| 13 | |
| 14 StubGeneratorTestSuite(Map configuration) | 10 StubGeneratorTestSuite(Map configuration) |
| 15 : super(configuration, | 11 : super(configuration, |
| 16 "stub-generator", | 12 "stub-generator", |
| 17 "tests/stub-generator/src", | 13 "tests/stub-generator/src", |
| 18 ["tests/stub-generator/stub-generator.status"]) { | 14 ["tests/stub-generator/stub-generator.status"]); |
| 19 try { | |
| 20 dartcPath = TestUtils.compilerPath(configuration); | |
| 21 } catch (var e) { | |
| 22 // ignore | |
| 23 } | |
| 24 } | |
| 25 | 15 |
| 26 bool get requiresCleanTemporaryDirectory() => true; | 16 bool isTestFile(String filename) => filename.endsWith("-generatedTest.dart"); |
| 27 | |
| 28 void combineFiles(String filename, | |
| 29 String stubsFile, | |
| 30 Function onGenerated) { | |
| 31 File orig = new File(filename); | |
| 32 File stubs = new File(stubsFile); | |
| 33 Expect.isTrue(filename.endsWith(".dart")); | |
| 34 String baseName = filename.substring(0, filename.length - 5); | |
| 35 // Find place in the buildDir at the same relative level as the test. | |
| 36 String resultPath = TestUtils.buildDir(configuration); | |
| 37 String relativeImportPath = '../../../tests/isolate/src/TestFramework.dart'; | |
| 38 if (!new File('$resultPath/$relativeImportPath').existsSync()) { | |
| 39 resultPath = '$resultPath/generated_tests'; | |
| 40 if (!new Directory(resultPath).existsSync()) { | |
| 41 new Directory(resultPath).createSync(); | |
| 42 } | |
| 43 if (!new File('$resultPath/$relativeImportPath').existsSync()) { | |
| 44 throw new Exception('Cannot find $relativeImportPath from $resultPath'); | |
| 45 } | |
| 46 } | |
| 47 int testNamePos = filename.lastIndexOf('/'); | |
| 48 resultPath = '$resultPath/${filename.substring(testNamePos + 1)}'; | |
| 49 File result = new File(resultPath); | |
| 50 StringInputStream origStream = | |
| 51 new StringInputStream(orig.openInputStream()); | |
| 52 StringInputStream stubsStream = | |
| 53 new StringInputStream(stubs.openInputStream()); | |
| 54 OutputStream resultStream = result.openOutputStream(); | |
| 55 var origLine; | |
| 56 | |
| 57 void writeResultLine(String line) { | |
| 58 resultStream.write(line.charCodes()); | |
| 59 resultStream.write('\n'.charCodes()); | |
| 60 } | |
| 61 | |
| 62 // Step 3: Copy in the rest of the original file. | |
| 63 void copyThirdPart() { | |
| 64 origLine = origStream.readLine(); | |
| 65 while (origLine != null) { | |
| 66 writeResultLine(origLine); | |
| 67 origLine = origStream.readLine(); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 // Step 2: Copy in the generated stubs code. | |
| 72 void copySecondPart() { | |
| 73 var stubsLine = stubsStream.readLine(); | |
| 74 while (stubsLine != null) { | |
| 75 writeResultLine(stubsLine); | |
| 76 stubsLine = stubsStream.readLine(); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 // Step 1: Copy first comments and imports from original file. | |
| 81 void copyFirstPart() { | |
| 82 origLine = origStream.readLine(); | |
| 83 while (origLine != null) { | |
| 84 origLine = origLine.trim(); | |
| 85 if (origLine.isEmpty() || | |
| 86 origLine.startsWith('//') || | |
| 87 origLine.startsWith('#')) { | |
| 88 writeResultLine(origLine); | |
| 89 origLine = origStream.readLine(); | |
| 90 } else { | |
| 91 origStream.lineHandler = null; | |
| 92 // Start copying from the stubs file. | |
| 93 stubsStream.lineHandler = copySecondPart; | |
| 94 stubsStream.closeHandler = () { | |
| 95 // When the stubs file is all read continue with the rest | |
| 96 // of the original file including the pending line. | |
| 97 writeResultLine(origLine); | |
| 98 origStream.lineHandler = copyThirdPart; | |
| 99 }; | |
| 100 return; | |
| 101 } | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 origStream.lineHandler = copyFirstPart; | |
| 106 origStream.closeHandler = () { | |
| 107 // Done generating file when all data has been read from the | |
| 108 // original file. | |
| 109 resultStream.close(); | |
| 110 onGenerated(resultPath); | |
| 111 }; | |
| 112 } | |
| 113 | |
| 114 void generateTestCase(String filename, | |
| 115 String interfaceFile, | |
| 116 String classes, | |
| 117 Function onGenerated) { | |
| 118 testGeneratorStarted(); | |
| 119 Directory temp = createOutputDirectory(filename, 'stub_generator'); | |
| 120 | |
| 121 File stubsOutFile = new File("${temp.path}/${interfaceFile}"); | |
| 122 stubsOutFile.createSync(); | |
| 123 String stubsPath = stubsOutFile.fullPathSync(); | |
| 124 List<String> args = [filename, | |
| 125 '-noincremental', | |
| 126 '-out', temp.path, | |
| 127 '-isolate-stub-out', stubsPath, | |
| 128 '-generate-isolate-stubs', classes ]; | |
| 129 if (configuration['verbose']) { | |
| 130 print("# $dartcPath ${Strings.join(args, ' ')}"); | |
| 131 } | |
| 132 Process dartcProcess = new Process.start(dartcPath, args); | |
| 133 dartcProcess.exitHandler = (int exitCode) { | |
| 134 combineFiles(filename, stubsPath, onGenerated); | |
| 135 }; | |
| 136 } | |
| 137 | |
| 138 void processFile(String filename) { | |
| 139 filename = filename.replaceAll('\\', '/') ; | |
| 140 // Only run the tests that match the pattern. | |
| 141 RegExp pattern = configuration['selectors'][suiteName]; | |
| 142 if (!pattern.hasMatch(filename)) return; | |
| 143 var optionsFromFile = optionsFromFile(filename); | |
| 144 Function createTestCase = makeTestCaseCreator(optionsFromFile); | |
| 145 | |
| 146 if (filename.endsWith("-generatedTest.dart")) { | |
| 147 if (dartcPath == null) { | |
| 148 createTestCase(filename, optionsFromFile['isNegative']); | |
| 149 } | |
| 150 } else if (filename.endsWith("Test.dart")) { | |
| 151 if (dartcPath != null) { | |
| 152 String isolateStubsOptions = optionsFromFile['isolateStubs']; | |
| 153 List<String> splitIsolateStubsOptions = isolateStubsOptions.split(':'); | |
| 154 Expect.equals(2, splitIsolateStubsOptions.length); | |
| 155 String interfaceFile = splitIsolateStubsOptions[0]; | |
| 156 String classes = splitIsolateStubsOptions[1]; | |
| 157 generateTestCase(filename, interfaceFile, classes, (String filename) { | |
| 158 createTestCase(filename, optionsFromFile['isNegative']); | |
| 159 testGeneratorDone(); | |
| 160 }); | |
| 161 } | |
| 162 } | |
| 163 } | |
| 164 } | 17 } |
| OLD | NEW |