Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016, 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.md file. | |
| 4 | |
| 5 library test.kernel.closures.suite; | |
| 6 | |
| 7 import 'dart:async' show | |
| 8 Future; | |
| 9 | |
| 10 import 'dart:io' show | |
| 11 Directory, | |
| 12 File, | |
| 13 IOSink; | |
| 14 | |
| 15 import 'dart:typed_data' show | |
| 16 Uint8List; | |
| 17 | |
| 18 import 'package:kernel/kernel.dart' show | |
| 19 Repository, | |
| 20 loadProgramFromBinary; | |
| 21 | |
| 22 import 'package:kernel/text/ast_to_text.dart' show | |
| 23 Printer; | |
| 24 | |
| 25 import 'package:testing/testing.dart' show | |
|
asgerf
2016/12/09 10:57:58
This should not be imported from something under t
karlklose
2016/12/15 07:54:19
Done.
| |
| 26 Result, | |
| 27 StdioProcess, | |
| 28 Step; | |
| 29 | |
| 30 import 'package:kernel/ast.dart' show | |
| 31 Library, | |
| 32 Program; | |
| 33 | |
| 34 import 'package:kernel/verifier.dart' show | |
| 35 verifyProgram; | |
| 36 | |
| 37 import 'package:kernel/binary/ast_to_binary.dart' show | |
| 38 BinaryPrinter; | |
| 39 | |
| 40 import 'package:kernel/binary/ast_from_binary.dart' show | |
| 41 BinaryBuilder; | |
| 42 | |
| 43 import 'package:kernel/binary/loader.dart' show | |
| 44 BinaryLoader; | |
| 45 | |
| 46 Future<bool> fileExists(Uri base, String path) async { | |
| 47 return await new File.fromUri(base.resolve(path)).exists(); | |
| 48 } | |
| 49 | |
| 50 class Print extends Step<Program, Program, dynamic> { | |
| 51 const Print(); | |
| 52 | |
| 53 String get name => "print"; | |
| 54 | |
| 55 Future<Result<Program>> run(Program program, _) async { | |
| 56 StringBuffer sb = new StringBuffer(); | |
| 57 for (Library library in program.libraries) { | |
| 58 Printer printer = new Printer(sb); | |
| 59 if (library.importUri.scheme != "dart") { | |
| 60 printer.writeLibraryFile(library); | |
| 61 } | |
| 62 } | |
| 63 print("$sb"); | |
| 64 return pass(program); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 class SanityCheck extends Step<Program, Program, dynamic> { | |
| 69 const SanityCheck(); | |
| 70 | |
| 71 String get name => "sanity check"; | |
| 72 | |
| 73 Future<Result<Program>> run(Program program, _) async { | |
| 74 try { | |
| 75 verifyProgram(program); | |
| 76 return pass(program); | |
| 77 } catch (e, s) { | |
| 78 return crash(e, s); | |
| 79 } | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 class MatchExpectation extends Step<Program, Program, dynamic> { | |
| 84 final String suffix; | |
| 85 | |
| 86 final bool updateExpectations; | |
| 87 | |
| 88 const MatchExpectation(this.suffix, {this.updateExpectations: true}); | |
| 89 | |
| 90 String get name => "match expectations"; | |
| 91 | |
| 92 Future<Result<Program>> run(Program program, _) async { | |
| 93 Library library = program.libraries.firstWhere( | |
| 94 (Library library) =>library.importUri.scheme != "dart"); | |
| 95 Uri uri = library.importUri; | |
| 96 StringBuffer buffer = new StringBuffer(); | |
| 97 new Printer(buffer).writeLibraryFile(library); | |
| 98 | |
| 99 File expectedFile = new File("${uri.toFilePath()}$suffix"); | |
| 100 if (await expectedFile.exists()) { | |
| 101 String expected = await expectedFile.readAsString(); | |
| 102 if (expected.trim() != "$buffer".trim()) { | |
| 103 if (!updateExpectations) { | |
| 104 String diff = await runDiff(expectedFile.uri, "$buffer"); | |
| 105 return fail(null, "$uri doesn't match ${expectedFile.uri}\n$diff"); | |
| 106 } | |
| 107 } else { | |
| 108 return pass(program); | |
| 109 } | |
| 110 } | |
| 111 if (updateExpectations) { | |
| 112 await openWrite(expectedFile.uri, (IOSink sink) { | |
| 113 sink.writeln("$buffer".trim()); | |
| 114 }); | |
| 115 return pass(program); | |
| 116 } else { | |
| 117 return fail(program, """ | |
| 118 Please create file ${expectedFile.path} with this content: | |
| 119 $buffer"""); | |
| 120 } | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 class WriteDill extends Step<Program, Uri, dynamic> { | |
| 125 const WriteDill(); | |
| 126 | |
| 127 String get name => "write .dill"; | |
| 128 | |
| 129 Future<Result<Uri>> run(Program program, _) async { | |
| 130 Directory tmp = await Directory.systemTemp.createTemp(); | |
| 131 Uri uri = tmp.uri.resolve("generated.dill"); | |
| 132 File generated = new File.fromUri(uri); | |
| 133 IOSink sink = generated.openWrite(); | |
| 134 try { | |
| 135 new BinaryPrinter(sink).writeProgramFile(program); | |
| 136 } catch (e, s) { | |
| 137 return fail(uri, e, s); | |
| 138 } finally { | |
| 139 print("Wrote `${generated.path}`"); | |
| 140 await sink.close(); | |
| 141 } | |
| 142 return pass(uri); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 class ReadDill extends Step<Uri, Uri, dynamic> { | |
| 147 const ReadDill(); | |
| 148 | |
| 149 String get name => "read .dill"; | |
| 150 | |
| 151 Future<Result<Uri>> run(Uri uri, _) async { | |
| 152 try { | |
| 153 loadProgramFromBinary(uri.toFilePath()); | |
| 154 } catch (e, s) { | |
| 155 return fail(uri, e, s); | |
| 156 } | |
| 157 return pass(uri); | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 class Copy extends Step<Program, Program, dynamic> { | |
| 162 const Copy(); | |
| 163 | |
| 164 String get name => "copy program"; | |
| 165 | |
| 166 Future<Result<Program>> run(Program program, _) async { | |
| 167 BytesCollector sink = new BytesCollector(); | |
| 168 new BinaryPrinter(sink).writeProgramFile(program); | |
| 169 Uint8List bytes = sink.collect(); | |
| 170 BinaryLoader loader = new BinaryLoader(new Repository()); | |
| 171 return pass(new BinaryBuilder(loader, bytes).readProgramFile()); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 class BytesCollector implements Sink<List<int>> { | |
| 176 final List<List<int>> lists = <List<int>>[]; | |
| 177 | |
| 178 int length = 0; | |
| 179 | |
| 180 void add(List<int> data) { | |
| 181 lists.add(data); | |
| 182 length += data.length; | |
| 183 } | |
| 184 | |
| 185 Uint8List collect() { | |
| 186 Uint8List result = new Uint8List(length); | |
| 187 int offset = 0; | |
| 188 for (List<int> list in lists) { | |
| 189 result.setRange(offset, offset += list.length, list); | |
| 190 } | |
| 191 lists.clear(); | |
| 192 length = 0; | |
| 193 return result; | |
| 194 } | |
| 195 | |
| 196 void close() {} | |
| 197 } | |
| 198 | |
| 199 Future<String> runDiff(Uri expected, String actual) async { | |
| 200 StdioProcess process = await StdioProcess.run( | |
| 201 "diff", <String>["-u", expected.toFilePath(), "-"], input: actual); | |
| 202 return process.output; | |
| 203 } | |
| 204 | |
| 205 Future openWrite(Uri uri, f(IOSink sink)) async { | |
| 206 IOSink sink = new File.fromUri(uri).openWrite(); | |
| 207 try { | |
| 208 await f(sink); | |
| 209 } finally { | |
| 210 await sink.close(); | |
| 211 } | |
| 212 print("Wrote $uri"); | |
| 213 } | |
| OLD | NEW |