| 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_tools.dart'; | 14 import 'codegen_tools.dart'; |
| 14 import 'from_html.dart'; | 15 import 'from_html.dart'; |
| 15 import 'to_html.dart'; | 16 import 'to_html.dart'; |
| 16 | 17 |
| 17 /** | 18 /** |
| 18 * Visitor that generates the code for integration_test_methods.dart | 19 * Visitor that generates the code for integration_test_methods.dart |
| 19 */ | 20 */ |
| 20 class CodegenInttestMethodsVisitor extends HierarchicalApiVisitor with | 21 class CodegenInttestMethodsVisitor extends DartCodegenVisitor with CodeGenerator |
| 21 CodeGenerator { | 22 { |
| 22 /** | 23 /** |
| 23 * Visitor used to produce doc comments. | 24 * Visitor used to produce doc comments. |
| 24 */ | 25 */ |
| 25 final ToHtmlVisitor toHtmlVisitor; | 26 final ToHtmlVisitor toHtmlVisitor; |
| 26 | 27 |
| 27 /** | 28 /** |
| 28 * Code snippets concatenated to initialize all of the class fields. | 29 * Code snippets concatenated to initialize all of the class fields. |
| 29 */ | 30 */ |
| 30 List<String> fieldInitializationCode = <String>[]; | 31 List<String> fieldInitializationCode = <String>[]; |
| 31 | 32 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 43 visitApi() { | 44 visitApi() { |
| 44 outputHeader(); | 45 outputHeader(); |
| 45 writeln(); | 46 writeln(); |
| 46 writeln('/**'); | 47 writeln('/**'); |
| 47 writeln(' * Convenience methods for running integration tests'); | 48 writeln(' * Convenience methods for running integration tests'); |
| 48 writeln(' */'); | 49 writeln(' */'); |
| 49 writeln('library test.integration.methods;'); | 50 writeln('library test.integration.methods;'); |
| 50 writeln(); | 51 writeln(); |
| 51 writeln("import 'dart:async';"); | 52 writeln("import 'dart:async';"); |
| 52 writeln(); | 53 writeln(); |
| 54 writeln("import 'package:analysis_server/src/protocol.dart';"); |
| 53 writeln("import 'package:unittest/unittest.dart';"); | 55 writeln("import 'package:unittest/unittest.dart';"); |
| 54 writeln(); | 56 writeln(); |
| 55 writeln("import 'integration_tests.dart';"); | 57 writeln("import 'integration_tests.dart';"); |
| 56 writeln("import 'protocol_matchers.dart';"); | 58 writeln("import 'protocol_matchers.dart';"); |
| 57 writeln(); | 59 writeln(); |
| 58 writeln(); | 60 writeln(); |
| 59 writeln('/**'); | 61 writeln('/**'); |
| 60 writeln(' * Convenience methods for running integration tests'); | 62 writeln(' * Convenience methods for running integration tests'); |
| 61 writeln(' */'); | 63 writeln(' */'); |
| 62 writeln('abstract class IntegrationTestMixin {'); | 64 writeln('abstract class IntegrationTestMixin {'); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 92 }); | 94 }); |
| 93 writeln('}'); | 95 writeln('}'); |
| 94 }); | 96 }); |
| 95 writeln('}'); | 97 writeln('}'); |
| 96 }); | 98 }); |
| 97 writeln('}'); | 99 writeln('}'); |
| 98 } | 100 } |
| 99 | 101 |
| 100 @override | 102 @override |
| 101 visitNotification(Notification notification) { | 103 visitNotification(Notification notification) { |
| 102 String streamName = camelJoin(['on', notification.domainName, | 104 String streamName = |
| 103 notification.event]); | 105 camelJoin(['on', notification.domainName, notification.event]); |
| 104 writeln(); | 106 writeln(); |
| 105 docComment(toHtmlVisitor.collectHtml(() { | 107 docComment(toHtmlVisitor.collectHtml(() { |
| 106 toHtmlVisitor.translateHtml(notification.html); | 108 toHtmlVisitor.translateHtml(notification.html); |
| 107 toHtmlVisitor.describePayload(notification.params, 'Parameters'); | 109 toHtmlVisitor.describePayload(notification.params, 'Parameters'); |
| 108 })); | 110 })); |
| 109 writeln('Stream $streamName;'); | 111 writeln('Stream $streamName;'); |
| 110 writeln(); | 112 writeln(); |
| 111 docComment(toHtmlVisitor.collectHtml(() { | 113 docComment(toHtmlVisitor.collectHtml(() { |
| 112 toHtmlVisitor.write('Stream controller for [$streamName].'); | 114 toHtmlVisitor.write('Stream controller for [$streamName].'); |
| 113 })); | 115 })); |
| 114 writeln('StreamController _$streamName;'); | 116 writeln('StreamController _$streamName;'); |
| 115 fieldInitializationCode.add(collectCode(() { | 117 fieldInitializationCode.add(collectCode(() { |
| 116 writeln('_$streamName = new StreamController(sync: true);'); | 118 writeln('_$streamName = new StreamController(sync: true);'); |
| 117 writeln('$streamName = _$streamName.stream.asBroadcastStream();'); | 119 writeln('$streamName = _$streamName.stream.asBroadcastStream();'); |
| 118 })); | 120 })); |
| 119 notificationSwitchContents.add(collectCode(() { | 121 notificationSwitchContents.add(collectCode(() { |
| 120 writeln('case ${JSON.encode(notification.longEvent)}:'); | 122 writeln('case ${JSON.encode(notification.longEvent)}:'); |
| 121 indent(() { | 123 indent(() { |
| 122 String paramsValidator = camelJoin(['is', notification.domainName, | 124 String paramsValidator = |
| 123 notification.event, 'params']); | 125 camelJoin(['is', notification.domainName, notification.event, 'param
s']); |
| 124 writeln('expect(params, $paramsValidator);'); | 126 writeln('expect(params, $paramsValidator);'); |
| 125 writeln('_$streamName.add(params);'); | 127 writeln('_$streamName.add(params);'); |
| 126 writeln('break;'); | 128 writeln('break;'); |
| 127 }); | 129 }); |
| 128 })); | 130 })); |
| 129 } | 131 } |
| 130 | 132 |
| 131 @override | 133 @override |
| 132 visitRequest(Request request) { | 134 visitRequest(Request request) { |
| 133 String methodName = camelJoin(['send', request.domainName, request.method]); | 135 String methodName = camelJoin(['send', request.domainName, request.method]); |
| 134 List<String> args = <String>[]; | 136 List<String> args = <String>[]; |
| 135 List<String> optionalArgs = <String>[]; | 137 List<String> optionalArgs = <String>[]; |
| 136 if (request.params != null) { | 138 if (request.params != null) { |
| 137 for (TypeObjectField field in request.params.fields) { | 139 for (TypeObjectField field in request.params.fields) { |
| 138 if (field.optional) { | 140 if (field.optional) { |
| 139 optionalArgs.add(formatArgument(field)); | 141 optionalArgs.add(formatArgument(field)); |
| 140 } else { | 142 } else { |
| 141 args.add(formatArgument(field)); | 143 args.add(formatArgument(field)); |
| 142 } | 144 } |
| 143 } | 145 } |
| 144 } | 146 } |
| 145 optionalArgs.add('bool checkTypes: true'); | 147 if (optionalArgs.isNotEmpty) { |
| 146 args.add('{${optionalArgs.join(', ')}}'); | 148 args.add('{${optionalArgs.join(', ')}}'); |
| 149 } |
| 147 writeln(); | 150 writeln(); |
| 148 docComment(toHtmlVisitor.collectHtml(() { | 151 docComment(toHtmlVisitor.collectHtml(() { |
| 149 toHtmlVisitor.translateHtml(request.html); | 152 toHtmlVisitor.translateHtml(request.html); |
| 150 toHtmlVisitor.describePayload(request.params, 'Parameters'); | 153 toHtmlVisitor.describePayload(request.params, 'Parameters'); |
| 151 toHtmlVisitor.describePayload(request.result, 'Returns'); | 154 toHtmlVisitor.describePayload(request.result, 'Returns'); |
| 152 })); | 155 })); |
| 153 writeln('Future $methodName(${args.join(', ')}) {'); | 156 writeln('Future $methodName(${args.join(', ')}) {'); |
| 154 indent(() { | 157 indent(() { |
| 155 String paramsValidator = camelJoin(['is', request.domainName, | 158 String requestClass = |
| 156 request.method, 'params']); | 159 camelJoin([request.domainName, request.method, 'params'], doCapitalize
: true); |
| 157 String resultValidator = camelJoin(['is', request.domainName, | 160 String resultValidator = |
| 158 request.method, 'result']); | 161 camelJoin(['is', request.domainName, request.method, 'result']); |
| 159 String paramsVar = 'null'; | 162 String paramsVar = 'null'; |
| 160 if (request.params != null) { | 163 if (request.params != null) { |
| 161 paramsVar = 'params'; | 164 paramsVar = 'params'; |
| 162 writeln('Map<String, dynamic> params = {};'); | 165 List<String> args = <String>[]; |
| 166 List<String> optionalArgs = <String>[]; |
| 163 for (TypeObjectField field in request.params.fields) { | 167 for (TypeObjectField field in request.params.fields) { |
| 164 if (field.optional) { | 168 if (field.optional) { |
| 165 writeln('if (${field.name} != null) {'); | 169 optionalArgs.add('${field.name}: ${field.name}'); |
| 166 indent(() { | |
| 167 populateField(field); | |
| 168 }); | |
| 169 writeln('}'); | |
| 170 } else { | 170 } else { |
| 171 populateField(field); | 171 args.add(field.name); |
| 172 } | 172 } |
| 173 } | 173 } |
| 174 writeln('if (checkTypes) {'); | 174 args.addAll(optionalArgs); |
| 175 indent(() { | 175 writeln('var params = new $requestClass(${args.join(', ')}).toJson();'); |
| 176 writeln('expect(params, $paramsValidator);'); | |
| 177 }); | |
| 178 writeln('}'); | |
| 179 } | 176 } |
| 180 writeln( | 177 writeln( |
| 181 'return server.send(${JSON.encode(request.longMethod)}, $paramsVar)'); | 178 'return server.send(${JSON.encode(request.longMethod)}, $paramsVar)'); |
| 182 indent(() { | 179 indent(() { |
| 183 writeln(' .then((result) {'); | 180 writeln(' .then((result) {'); |
| 184 writeln('if (checkTypes) {'); | 181 writeln('expect(result, $resultValidator);'); |
| 185 indent(() { | |
| 186 writeln('expect(result, $resultValidator);'); | |
| 187 }); | |
| 188 writeln('}'); | |
| 189 writeln('return result;'); | 182 writeln('return result;'); |
| 190 }); | 183 }); |
| 191 writeln('});'); | 184 writeln('});'); |
| 192 }); | 185 }); |
| 193 writeln('}'); | 186 writeln('}'); |
| 194 } | 187 } |
| 195 | 188 |
| 196 /** | 189 /** |
| 197 * Generate a function argument for the given parameter field. | 190 * Generate a function argument for the given parameter field. |
| 198 */ | 191 */ |
| 199 String formatArgument(TypeObjectField field) => | 192 String formatArgument(TypeObjectField field) => |
| 200 '${jsonType(field.type)} ${field.name}'; | 193 '${dartType(field.type)} ${field.name}'; |
| 201 | |
| 202 /** | |
| 203 * Generate code that populates the given parameter field based on the | |
| 204 * function argument from [formatArgument]. | |
| 205 */ | |
| 206 void populateField(TypeObjectField field) { | |
| 207 writeln('params[${JSON.encode(field.name)}] = ${field.name};'); | |
| 208 } | |
| 209 | 194 |
| 210 /** | 195 /** |
| 211 * Figure out the appropriate Dart type for data having the given API | 196 * Figure out the appropriate Dart type for data having the given API |
| 212 * protocol [type]. | 197 * protocol [type]. |
| 213 */ | 198 */ |
| 214 String jsonType(TypeDecl type) { | 199 String jsonType(TypeDecl type) { |
| 215 type = resolveTypeReferenceChain(type); | 200 type = resolveTypeReferenceChain(type); |
| 216 if (type is TypeEnum) { | 201 if (type is TypeEnum) { |
| 217 return 'String'; | 202 return 'String'; |
| 218 } else if (type is TypeList) { | 203 } else if (type is TypeList) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 234 throw new Exception(type.typeName); | 219 throw new Exception(type.typeName); |
| 235 } | 220 } |
| 236 } else if (type is TypeUnion) { | 221 } else if (type is TypeUnion) { |
| 237 return 'Object'; | 222 return 'Object'; |
| 238 } else { | 223 } else { |
| 239 throw new Exception('Unexpected kind of TypeDecl'); | 224 throw new Exception('Unexpected kind of TypeDecl'); |
| 240 } | 225 } |
| 241 } | 226 } |
| 242 } | 227 } |
| 243 | 228 |
| 244 final GeneratedFile target = new GeneratedFile( | 229 final GeneratedFile target = |
| 245 '../../test/integration/integration_test_methods.dart', () { | 230 new GeneratedFile('../../test/integration/integration_test_methods.dart', ()
{ |
| 246 CodegenInttestMethodsVisitor visitor = new CodegenInttestMethodsVisitor( | 231 CodegenInttestMethodsVisitor visitor = |
| 247 readApi()); | 232 new CodegenInttestMethodsVisitor(readApi()); |
| 248 return visitor.collectCode(visitor.visitApi); | 233 return visitor.collectCode(visitor.visitApi); |
| 249 }); | 234 }); |
| 250 | 235 |
| 251 /** | 236 /** |
| 252 * Translate spec_input.html into protocol_matchers.dart. | 237 * Translate spec_input.html into protocol_matchers.dart. |
| 253 */ | 238 */ |
| 254 main() { | 239 main() { |
| 255 target.generate(); | 240 target.generate(); |
| 256 } | 241 } |
| OLD | NEW |