OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library trydart.test.program_result; | |
6 | |
7 import 'dart:convert' show | |
8 JSON; | |
9 | |
10 import '../poi/source_update.dart'; | |
11 | |
12 class ProgramResult { | |
13 final /* Map<String, String> or String */ code; | |
14 | |
15 final List<String> messages; | |
16 | |
17 final bool compileUpdatesShouldThrow; | |
18 | |
19 const ProgramResult( | |
20 this.code, this.messages, {this.compileUpdatesShouldThrow: false}); | |
21 | |
22 List<String> messagesWith(String extra) { | |
23 return new List<String>.from(messages)..add(extra); | |
24 } | |
25 | |
26 String toString() { | |
27 return """ | |
28 ProgramResult( | |
29 ${JSON.encode(code)}, | |
30 ${JSON.encode(messages)}, | |
31 compileUpdatesShouldThrow: $compileUpdatesShouldThrow)"""; | |
32 } | |
33 } | |
34 | |
35 class ProgramExpectation { | |
36 final List<String> messages; | |
37 | |
38 final bool compileUpdatesShouldThrow; | |
39 | |
40 const ProgramExpectation( | |
41 this.messages, {this.compileUpdatesShouldThrow: false}); | |
42 | |
43 ProgramResult toResult(String code) { | |
44 return new ProgramResult( | |
45 code, messages, compileUpdatesShouldThrow: compileUpdatesShouldThrow); | |
46 } | |
47 } | |
48 | |
49 class EncodedResult { | |
50 final /* String or List */ updates; | |
51 | |
52 final List expectations; | |
53 | |
54 const EncodedResult(this.updates, this.expectations); | |
55 | |
56 List<ProgramResult> decode() { | |
57 if (updates is List) { | |
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 for (int i = 0; i < updateCount; i++) { | |
116 ProgramExpectation expectation = decodeExpectation(expectations[i]); | |
117 result[i] = new ProgramResult( | |
118 <String, String>{}, | |
119 expectation.messages, | |
120 compileUpdatesShouldThrow: expectation.compileUpdatesShouldThrow); | |
121 } | |
122 for (String name in fileMap.keys) { | |
123 for (int i = 0; i < updateCount; i++) { | |
124 result[i].code[name] = fileMap[name][i]; | |
125 } | |
126 } | |
127 return result; | |
128 } else { | |
129 throw new StateError("Unknown encoding of updates"); | |
130 } | |
131 } | |
132 } | |
133 | |
134 ProgramExpectation decodeExpectation(expectation) { | |
135 if (expectation is ProgramExpectation) { | |
136 return expectation; | |
137 } else if (expectation is String) { | |
138 return new ProgramExpectation(<String>[expectation]); | |
139 } else if (expectation is List) { | |
140 return new ProgramExpectation(new List<String>.from(expectation)); | |
141 } else { | |
142 throw new ArgumentError("Don't know how to decode $expectation"); | |
143 } | |
144 } | |
OLD | NEW |