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, ${expectations.length} expectations).") ; | |
Johnni Winther
2014/12/18 14:01:36
Long line.
ahe
2014/12/18 14:59:10
Done.
| |
66 } | |
67 List<ProgramResult> result = new List<ProgramResult>(sources.length); | |
68 for (int i = 0; i < sources.length; i++) { | |
69 result[i] = expectations[i].toResult(sources[i]); | |
70 } | |
71 return result; | |
72 } else if (updates is String) { | |
73 Map<String, String> files = splitFiles(updates); | |
74 Map<String, List<String>> fileMap = <String, List<String>>{}; | |
75 int updateCount = -1; | |
76 for (String name in files.keys) { | |
77 if (name.endsWith(".patch")) { | |
78 String realname = name.substring(0, name.length - ".patch".length); | |
79 if (files.containsKey(realname)) { | |
80 throw new StateError("Patch '$name' conflicts with '$realname'"); | |
81 } | |
82 if (fileMap.containsKey(realname)) { | |
83 // Can't happen. | |
84 throw new StateError("Duplicated entry for '$realname'."); | |
85 } | |
86 List<String> updates = expandUpdates(expandDiff(files[name])); | |
87 if (updates.length == 1) { | |
88 throw new StateError("No patches found in:\n ${files[name]}"); | |
89 } | |
90 if (updateCount == -1) { | |
91 updateCount = updates.length; | |
92 } else if (updateCount != updates.length) { | |
93 throw new StateError( | |
94 "Unexpected number of patches: ${updates.length}," | |
95 " expected ${updateCount}"); | |
96 } | |
97 fileMap[realname] = updates; | |
98 } | |
99 } | |
100 if (updateCount == -1) { | |
101 throw new StateError("No patch files in $updates"); | |
102 } | |
103 for (String name in files.keys) { | |
104 if (!name.endsWith(".patch")) { | |
105 fileMap[name] = new List<String>.filled(updateCount, files[name]); | |
106 } | |
107 } | |
108 if (updateCount != expectations.length) { | |
109 throw new StateError( | |
110 "Number of patches and expectations differ " | |
111 "(${updateCount} patches, ${expectations.length} expectations)."); | |
112 } | |
113 List<ProgramResult> result = new List<ProgramResult>(updateCount); | |
114 int i = 0; | |
115 for (String name in fileMap.keys) { | |
116 ProgramExpectation expectation = decodeExpectation(expectations[i]); | |
117 result[i] = new ProgramResult( | |
118 <String, String>{}, | |
119 expectation.messages, | |
120 compileUpdatesShouldThrow: expectation.compileUpdatesShouldThrow); | |
121 i++; | |
122 } | |
123 for (String name in fileMap.keys) { | |
124 for (int j = 0; j < updateCount; j++) { | |
125 result[j].code[name] = fileMap[name][j]; | |
126 } | |
127 } | |
128 return result; | |
129 } else { | |
130 throw new StateError("Unknown encoding of updates"); | |
48 } | 131 } |
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 } | 132 } |
61 } | 133 } |
134 | |
135 ProgramExpectation decodeExpectation(expectation) { | |
136 if (expectation is ProgramExpectation) { | |
137 return expectation; | |
138 } else if (expectation is String) { | |
139 return new ProgramExpectation(<String>[expectation]); | |
140 } else if (expectation is List) { | |
141 return new ProgramExpectation(new List<String>.from(expectation)); | |
142 } else { | |
143 throw new ArgumentError("Don't know how to decode $expectation"); | |
144 } | |
145 } | |
OLD | NEW |