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