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 /** | 5 /** |
6 * Code generation for the file "integration_test_methods.dart". | 6 * Code generation for the file "integration_test_methods.dart". |
7 */ | 7 */ |
8 library codegenInttestMethods; | 8 library codegenInttestMethods; |
9 | 9 |
10 import 'dart:convert'; | 10 import 'dart:convert'; |
11 | 11 |
12 import 'api.dart'; | 12 import 'api.dart'; |
13 import 'codegen_dart.dart'; | 13 import 'codegen_dart.dart'; |
14 import 'codegen_tools.dart'; | 14 import 'codegen_tools.dart'; |
15 import 'from_html.dart'; | 15 import 'from_html.dart'; |
16 import 'to_html.dart'; | 16 import 'to_html.dart'; |
17 | 17 |
| 18 final GeneratedFile target = |
| 19 new GeneratedFile('../../test/integration/integration_test_methods.dart', ()
{ |
| 20 CodegenInttestMethodsVisitor visitor = |
| 21 new CodegenInttestMethodsVisitor(readApi()); |
| 22 return visitor.collectCode(visitor.visitApi); |
| 23 }); |
| 24 |
| 25 /** |
| 26 * Translate spec_input.html into protocol_matchers.dart. |
| 27 */ |
| 28 main() { |
| 29 target.generate(); |
| 30 } |
| 31 |
18 /** | 32 /** |
19 * Visitor that generates the code for integration_test_methods.dart | 33 * Visitor that generates the code for integration_test_methods.dart |
20 */ | 34 */ |
21 class CodegenInttestMethodsVisitor extends DartCodegenVisitor with CodeGenerator | 35 class CodegenInttestMethodsVisitor extends DartCodegenVisitor with CodeGenerator |
22 { | 36 { |
23 /** | 37 /** |
24 * Visitor used to produce doc comments. | 38 * Visitor used to produce doc comments. |
25 */ | 39 */ |
26 final ToHtmlVisitor toHtmlVisitor; | 40 final ToHtmlVisitor toHtmlVisitor; |
27 | 41 |
28 /** | 42 /** |
29 * Code snippets concatenated to initialize all of the class fields. | 43 * Code snippets concatenated to initialize all of the class fields. |
30 */ | 44 */ |
31 List<String> fieldInitializationCode = <String>[]; | 45 List<String> fieldInitializationCode = <String>[]; |
32 | 46 |
33 /** | 47 /** |
34 * Code snippets concatenated to produce the contents of the switch statement | 48 * Code snippets concatenated to produce the contents of the switch statement |
35 * for dispatching notifications. | 49 * for dispatching notifications. |
36 */ | 50 */ |
37 List<String> notificationSwitchContents = <String>[]; | 51 List<String> notificationSwitchContents = <String>[]; |
38 | 52 |
39 CodegenInttestMethodsVisitor(Api api) | 53 CodegenInttestMethodsVisitor(Api api) |
40 : super(api), | 54 : super(api), |
41 toHtmlVisitor = new ToHtmlVisitor(api); | 55 toHtmlVisitor = new ToHtmlVisitor(api); |
42 | 56 |
| 57 /** |
| 58 * Generate a function argument for the given parameter field. |
| 59 */ |
| 60 String formatArgument(TypeObjectField field) => |
| 61 '${dartType(field.type)} ${field.name}'; |
| 62 |
| 63 /** |
| 64 * Figure out the appropriate Dart type for data having the given API |
| 65 * protocol [type]. |
| 66 */ |
| 67 String jsonType(TypeDecl type) { |
| 68 type = resolveTypeReferenceChain(type); |
| 69 if (type is TypeEnum) { |
| 70 return 'String'; |
| 71 } else if (type is TypeList) { |
| 72 return 'List<${jsonType(type.itemType)}>'; |
| 73 } else if (type is TypeMap) { |
| 74 return 'Map<String, ${jsonType(type.valueType)}>'; |
| 75 } else if (type is TypeObject) { |
| 76 return 'Map<String, dynamic>'; |
| 77 } else if (type is TypeReference) { |
| 78 switch (type.typeName) { |
| 79 case 'String': |
| 80 case 'int': |
| 81 case 'bool': |
| 82 // These types correspond exactly to Dart types |
| 83 return type.typeName; |
| 84 case 'object': |
| 85 return 'Map<String, dynamic>'; |
| 86 default: |
| 87 throw new Exception(type.typeName); |
| 88 } |
| 89 } else if (type is TypeUnion) { |
| 90 return 'Object'; |
| 91 } else { |
| 92 throw new Exception('Unexpected kind of TypeDecl'); |
| 93 } |
| 94 } |
| 95 |
43 @override | 96 @override |
44 visitApi() { | 97 visitApi() { |
45 outputHeader(); | 98 outputHeader(); |
46 writeln(); | 99 writeln(); |
47 writeln('/**'); | 100 writeln('/**'); |
48 writeln(' * Convenience methods for running integration tests'); | 101 writeln(' * Convenience methods for running integration tests'); |
49 writeln(' */'); | 102 writeln(' */'); |
50 writeln('library test.integration.methods;'); | 103 writeln('library test.integration.methods;'); |
51 writeln(); | 104 writeln(); |
52 writeln("import 'dart:async';"); | 105 writeln("import 'dart:async';"); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 }); | 150 }); |
98 writeln('}'); | 151 writeln('}'); |
99 }); | 152 }); |
100 writeln('}'); | 153 writeln('}'); |
101 } | 154 } |
102 | 155 |
103 @override | 156 @override |
104 visitNotification(Notification notification) { | 157 visitNotification(Notification notification) { |
105 String streamName = | 158 String streamName = |
106 camelJoin(['on', notification.domainName, notification.event]); | 159 camelJoin(['on', notification.domainName, notification.event]); |
107 String className = camelJoin([notification.domainName, notification.event, '
params'], doCapitalize: true); | 160 String className = camelJoin( |
| 161 [notification.domainName, notification.event, 'params'], |
| 162 doCapitalize: true); |
108 writeln(); | 163 writeln(); |
109 docComment(toHtmlVisitor.collectHtml(() { | 164 docComment(toHtmlVisitor.collectHtml(() { |
110 toHtmlVisitor.translateHtml(notification.html); | 165 toHtmlVisitor.translateHtml(notification.html); |
111 toHtmlVisitor.describePayload(notification.params, 'Parameters'); | 166 toHtmlVisitor.describePayload(notification.params, 'Parameters'); |
112 })); | 167 })); |
113 writeln('Stream<$className> $streamName;'); | 168 writeln('Stream<$className> $streamName;'); |
114 writeln(); | 169 writeln(); |
115 docComment(toHtmlVisitor.collectHtml(() { | 170 docComment(toHtmlVisitor.collectHtml(() { |
116 toHtmlVisitor.write('Stream controller for [$streamName].'); | 171 toHtmlVisitor.write('Stream controller for [$streamName].'); |
117 })); | 172 })); |
118 writeln('StreamController<$className> _$streamName;'); | 173 writeln('StreamController<$className> _$streamName;'); |
119 fieldInitializationCode.add(collectCode(() { | 174 fieldInitializationCode.add(collectCode(() { |
120 writeln('_$streamName = new StreamController<$className>(sync: true);'); | 175 writeln('_$streamName = new StreamController<$className>(sync: true);'); |
121 writeln('$streamName = _$streamName.stream.asBroadcastStream();'); | 176 writeln('$streamName = _$streamName.stream.asBroadcastStream();'); |
122 })); | 177 })); |
123 notificationSwitchContents.add(collectCode(() { | 178 notificationSwitchContents.add(collectCode(() { |
124 writeln('case ${JSON.encode(notification.longEvent)}:'); | 179 writeln('case ${JSON.encode(notification.longEvent)}:'); |
125 indent(() { | 180 indent(() { |
126 String paramsValidator = | 181 String paramsValidator = |
127 camelJoin(['is', notification.domainName, notification.event, 'param
s']); | 182 camelJoin(['is', notification.domainName, notification.event, 'param
s']); |
128 writeln('expect(params, $paramsValidator);'); | 183 writeln('expect(params, $paramsValidator);'); |
129 String constructorCall; | 184 String constructorCall; |
130 if (notification.params == null) { | 185 if (notification.params == null) { |
131 constructorCall = 'new $className()'; | 186 constructorCall = 'new $className()'; |
132 } else { | 187 } else { |
133 constructorCall = "new $className.fromJson(decoder, 'params', params)"
; | 188 constructorCall = |
| 189 "new $className.fromJson(decoder, 'params', params)"; |
134 } | 190 } |
135 writeln('_$streamName.add($constructorCall);'); | 191 writeln('_$streamName.add($constructorCall);'); |
136 writeln('break;'); | 192 writeln('break;'); |
137 }); | 193 }); |
138 })); | 194 })); |
139 } | 195 } |
140 | 196 |
141 @override | 197 @override |
142 visitRequest(Request request) { | 198 visitRequest(Request request) { |
143 String methodName = camelJoin(['send', request.domainName, request.method]); | 199 String methodName = camelJoin(['send', request.domainName, request.method]); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 "return new $resultClass.fromJson(decoder, 'result', result);"); | 259 "return new $resultClass.fromJson(decoder, 'result', result);"); |
204 } else { | 260 } else { |
205 writeln('expect(result, isNull);'); | 261 writeln('expect(result, isNull);'); |
206 writeln('return null;'); | 262 writeln('return null;'); |
207 } | 263 } |
208 }); | 264 }); |
209 writeln('});'); | 265 writeln('});'); |
210 }); | 266 }); |
211 writeln('}'); | 267 writeln('}'); |
212 } | 268 } |
213 | |
214 /** | |
215 * Generate a function argument for the given parameter field. | |
216 */ | |
217 String formatArgument(TypeObjectField field) => | |
218 '${dartType(field.type)} ${field.name}'; | |
219 | |
220 /** | |
221 * Figure out the appropriate Dart type for data having the given API | |
222 * protocol [type]. | |
223 */ | |
224 String jsonType(TypeDecl type) { | |
225 type = resolveTypeReferenceChain(type); | |
226 if (type is TypeEnum) { | |
227 return 'String'; | |
228 } else if (type is TypeList) { | |
229 return 'List<${jsonType(type.itemType)}>'; | |
230 } else if (type is TypeMap) { | |
231 return 'Map<String, ${jsonType(type.valueType)}>'; | |
232 } else if (type is TypeObject) { | |
233 return 'Map<String, dynamic>'; | |
234 } else if (type is TypeReference) { | |
235 switch (type.typeName) { | |
236 case 'String': | |
237 case 'int': | |
238 case 'bool': | |
239 // These types correspond exactly to Dart types | |
240 return type.typeName; | |
241 case 'object': | |
242 return 'Map<String, dynamic>'; | |
243 default: | |
244 throw new Exception(type.typeName); | |
245 } | |
246 } else if (type is TypeUnion) { | |
247 return 'Object'; | |
248 } else { | |
249 throw new Exception('Unexpected kind of TypeDecl'); | |
250 } | |
251 } | |
252 } | 269 } |
253 | |
254 final GeneratedFile target = | |
255 new GeneratedFile('../../test/integration/integration_test_methods.dart', ()
{ | |
256 CodegenInttestMethodsVisitor visitor = | |
257 new CodegenInttestMethodsVisitor(readApi()); | |
258 return visitor.collectCode(visitor.visitApi); | |
259 }); | |
260 | |
261 /** | |
262 * Translate spec_input.html into protocol_matchers.dart. | |
263 */ | |
264 main() { | |
265 target.generate(); | |
266 } | |
OLD | NEW |