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 library trydart.test.program_result; | 5 library trydart.test.program_result; |
6 | 6 |
| 7 import 'dart:convert' show |
| 8 JSON; |
| 9 |
7 import '../poi/source_update.dart'; | 10 import '../poi/source_update.dart'; |
8 | 11 |
9 class ProgramResult { | 12 class ProgramResult { |
10 final String code; | 13 final /* Map<String, String> or String */ code; |
11 | 14 |
12 final List<String> messages; | 15 final List<String> messages; |
13 | 16 |
14 final bool compileUpdatesShouldThrow; | 17 final bool compileUpdatesShouldThrow; |
15 | 18 |
16 const ProgramResult( | 19 const ProgramResult( |
17 this.code, this.messages, {this.compileUpdatesShouldThrow: false}); | 20 this.code, this.messages, {this.compileUpdatesShouldThrow: false}); |
18 | 21 |
19 List<String> messagesWith(String extra) { | 22 List<String> messagesWith(String extra) { |
20 return new List<String>.from(messages)..add(extra); | 23 return new List<String>.from(messages)..add(extra); |
21 } | 24 } |
| 25 |
| 26 String toString() { |
| 27 return """ |
| 28 ProgramResult( |
| 29 ${JSON.encode(code)}, |
| 30 ${JSON.encode(messages)}, |
| 31 compileUpdatesShouldThrow: $compileUpdatesShouldThrow)"""; |
| 32 } |
22 } | 33 } |
23 | 34 |
24 class ProgramExpectation { | 35 class ProgramExpectation { |
25 final List<String> messages; | 36 final List<String> messages; |
26 | 37 |
27 final bool compileUpdatesShouldThrow; | 38 final bool compileUpdatesShouldThrow; |
28 | 39 |
29 const ProgramExpectation( | 40 const ProgramExpectation( |
30 this.messages, {this.compileUpdatesShouldThrow: false}); | 41 this.messages, {this.compileUpdatesShouldThrow: false}); |
31 | 42 |
32 ProgramResult toResult(String code) { | 43 ProgramResult toResult(String code) { |
33 return new ProgramResult( | 44 return new ProgramResult( |
34 code, messages, compileUpdatesShouldThrow: compileUpdatesShouldThrow); | 45 code, messages, compileUpdatesShouldThrow: compileUpdatesShouldThrow); |
35 } | 46 } |
36 } | 47 } |
37 | 48 |
38 class EncodedResult { | 49 class EncodedResult { |
39 final List updates; | 50 final /* String or List */ updates; |
40 | 51 |
41 final List<ProgramExpectation> expectations; | 52 final List expectations; |
42 | 53 |
43 const EncodedResult(this.updates, this.expectations); | 54 const EncodedResult(this.updates, this.expectations); |
44 | 55 |
45 List<ProgramResult> decode() { | 56 List<ProgramResult> decode() { |
46 if (updates.length == 1) { | 57 if (updates is List) { |
47 throw new StateError("Trivial diff, no reason to use decode."); | 58 if (updates.length == 1) { |
| 59 throw new StateError("Trivial diff, no reason to use decode."); |
| 60 } |
| 61 List<String> sources = expandUpdates(updates); |
| 62 if (sources.length != expectations.length) { |
| 63 throw new StateError( |
| 64 "Number of sources and expectations differ" |
| 65 " (${sources.length} sources," |
| 66 " ${expectations.length} expectations)."); |
| 67 } |
| 68 List<ProgramResult> result = new List<ProgramResult>(sources.length); |
| 69 for (int i = 0; i < sources.length; i++) { |
| 70 result[i] = expectations[i].toResult(sources[i]); |
| 71 } |
| 72 return result; |
| 73 } else if (updates is String) { |
| 74 Map<String, String> files = splitFiles(updates); |
| 75 Map<String, List<String>> fileMap = <String, List<String>>{}; |
| 76 int updateCount = -1; |
| 77 for (String name in files.keys) { |
| 78 if (name.endsWith(".patch")) { |
| 79 String realname = name.substring(0, name.length - ".patch".length); |
| 80 if (files.containsKey(realname)) { |
| 81 throw new StateError("Patch '$name' conflicts with '$realname'"); |
| 82 } |
| 83 if (fileMap.containsKey(realname)) { |
| 84 // Can't happen. |
| 85 throw new StateError("Duplicated entry for '$realname'."); |
| 86 } |
| 87 List<String> updates = expandUpdates(expandDiff(files[name])); |
| 88 if (updates.length == 1) { |
| 89 throw new StateError("No patches found in:\n ${files[name]}"); |
| 90 } |
| 91 if (updateCount == -1) { |
| 92 updateCount = updates.length; |
| 93 } else if (updateCount != updates.length) { |
| 94 throw new StateError( |
| 95 "Unexpected number of patches: ${updates.length}," |
| 96 " expected ${updateCount}"); |
| 97 } |
| 98 fileMap[realname] = updates; |
| 99 } |
| 100 } |
| 101 if (updateCount == -1) { |
| 102 throw new StateError("No patch files in $updates"); |
| 103 } |
| 104 for (String name in files.keys) { |
| 105 if (!name.endsWith(".patch")) { |
| 106 fileMap[name] = new List<String>.filled(updateCount, files[name]); |
| 107 } |
| 108 } |
| 109 if (updateCount != expectations.length) { |
| 110 throw new StateError( |
| 111 "Number of patches and expectations differ " |
| 112 "(${updateCount} patches, ${expectations.length} expectations)."); |
| 113 } |
| 114 List<ProgramResult> result = new List<ProgramResult>(updateCount); |
| 115 int i = 0; |
| 116 for (String name in fileMap.keys) { |
| 117 ProgramExpectation expectation = decodeExpectation(expectations[i]); |
| 118 result[i] = new ProgramResult( |
| 119 <String, String>{}, |
| 120 expectation.messages, |
| 121 compileUpdatesShouldThrow: expectation.compileUpdatesShouldThrow); |
| 122 i++; |
| 123 } |
| 124 for (String name in fileMap.keys) { |
| 125 for (int j = 0; j < updateCount; j++) { |
| 126 result[j].code[name] = fileMap[name][j]; |
| 127 } |
| 128 } |
| 129 return result; |
| 130 } else { |
| 131 throw new StateError("Unknown encoding of updates"); |
48 } | 132 } |
49 List<String> sources = expandUpdates(updates); | |
50 if (sources.length != expectations.length) { | |
51 throw new StateError( | |
52 "Number of sources and expectations differ " | |
53 "(${sources.length} sources, ${expectations.length} expectations)."); | |
54 } | |
55 List<ProgramResult> result = new List<ProgramResult>(sources.length); | |
56 for (int i = 0; i < sources.length; i++) { | |
57 result[i] = expectations[i].toResult(sources[i]); | |
58 } | |
59 return result; | |
60 } | 133 } |
61 } | 134 } |
| 135 |
| 136 ProgramExpectation decodeExpectation(expectation) { |
| 137 if (expectation is ProgramExpectation) { |
| 138 return expectation; |
| 139 } else if (expectation is String) { |
| 140 return new ProgramExpectation(<String>[expectation]); |
| 141 } else if (expectation is List) { |
| 142 return new ProgramExpectation(new List<String>.from(expectation)); |
| 143 } else { |
| 144 throw new ArgumentError("Don't know how to decode $expectation"); |
| 145 } |
| 146 } |
OLD | NEW |