| 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 import 'dart:io'; | 11 import 'dart:io'; |
| 12 | 12 |
| 13 import 'api.dart'; | 13 import 'api.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 /** | 18 /** |
| 19 * Visitor that generates the code for integration_test_methods.dart | 19 * Visitor that generates the code for integration_test_methods.dart |
| 20 */ | 20 */ |
| 21 class CodegenInttestMethodsVisitor extends HierarchicalApiVisitor with | 21 class CodegenInttestMethodsVisitor extends HierarchicalApiVisitor with |
| 22 CodeGenerator { | 22 CodeGenerator { |
| 23 /** | 23 /** |
| 24 * Visitor used to produce doc comments. | 24 * Visitor used to produce doc comments. |
| 25 */ | 25 */ |
| 26 final ToHtmlVisitor toHtmlVisitor; | 26 final ToHtmlVisitor toHtmlVisitor; |
| 27 | 27 |
| 28 /** |
| 29 * Code snippets concatenated to initialize all of the class fields. |
| 30 */ |
| 31 List<String> fieldInitializationCode = <String>[]; |
| 32 |
| 33 /** |
| 34 * Code snippets concatenated to produce the contents of the switch statement |
| 35 * for dispatching notifications. |
| 36 */ |
| 37 List<String> notificationSwitchContents = <String>[]; |
| 38 |
| 28 CodegenInttestMethodsVisitor(Api api) | 39 CodegenInttestMethodsVisitor(Api api) |
| 29 : super(api), | 40 : super(api), |
| 30 toHtmlVisitor = new ToHtmlVisitor(api); | 41 toHtmlVisitor = new ToHtmlVisitor(api); |
| 31 | 42 |
| 32 @override | 43 @override |
| 33 visitApi() { | 44 visitApi() { |
| 34 outputHeader(); | 45 outputHeader(); |
| 35 writeln('/**'); | 46 writeln('/**'); |
| 36 writeln(' * Convenience methods for running integration tests'); | 47 writeln(' * Convenience methods for running integration tests'); |
| 37 writeln(' */'); | 48 writeln(' */'); |
| 38 writeln('library test.integration.methods;'); | 49 writeln('library test.integration.methods;'); |
| 39 writeln(); | 50 writeln(); |
| 40 writeln("import 'dart:async';"); | 51 writeln("import 'dart:async';"); |
| 41 writeln(); | 52 writeln(); |
| 42 writeln("import 'package:unittest/unittest.dart';"); | 53 writeln("import 'package:unittest/unittest.dart';"); |
| 43 writeln(); | 54 writeln(); |
| 44 writeln("import 'integration_tests.dart';"); | 55 writeln("import 'integration_tests.dart';"); |
| 45 writeln("import 'protocol_matchers.dart';"); | 56 writeln("import 'protocol_matchers.dart';"); |
| 46 writeln(); | 57 writeln(); |
| 47 writeln(); | 58 writeln(); |
| 48 writeln('/**'); | 59 writeln('/**'); |
| 49 writeln(' * Convenience methods for running integration tests'); | 60 writeln(' * Convenience methods for running integration tests'); |
| 50 writeln(' */'); | 61 writeln(' */'); |
| 51 writeln('abstract class InttestMixin {'); | 62 writeln('abstract class InttestMixin {'); |
| 52 indent(() { | 63 indent(() { |
| 53 writeln('Server get server;'); | 64 writeln('Server get server;'); |
| 54 super.visitApi(); | 65 super.visitApi(); |
| 66 writeln(); |
| 67 docComment(toHtmlVisitor.collectHtml(() { |
| 68 toHtmlVisitor.writeln('Initialize the fields in InttestMixin, and'); |
| 69 toHtmlVisitor.writeln('ensure that notifications will be handled.'); |
| 70 }), false); |
| 71 writeln('void initializeInttestMixin() {'); |
| 72 indent(() { |
| 73 write(fieldInitializationCode.join()); |
| 74 }); |
| 75 writeln('}'); |
| 76 writeln(); |
| 77 docComment(toHtmlVisitor.collectHtml(() { |
| 78 toHtmlVisitor.writeln('Dispatch the notification named [event], and'); |
| 79 toHtmlVisitor.writeln('containing parameters [params], to the'); |
| 80 toHtmlVisitor.writeln('appropriate stream.'); |
| 81 }), false); |
| 82 writeln('void dispatchNotification(String event, params) {'); |
| 83 indent(() { |
| 84 writeln('switch (event) {'); |
| 85 indent(() { |
| 86 write(notificationSwitchContents.join()); |
| 87 writeln('default:'); |
| 88 indent(() { |
| 89 writeln("fail('Unexpected notification: \$event');"); |
| 90 writeln('break;'); |
| 91 }); |
| 92 }); |
| 93 writeln('}'); |
| 94 }); |
| 95 writeln('}'); |
| 55 }); | 96 }); |
| 56 writeln('}'); | 97 writeln('}'); |
| 57 } | 98 } |
| 58 | 99 |
| 59 @override | 100 @override |
| 101 visitNotification(Notification notification) { |
| 102 String streamName = camelJoin(['on', notification.domainName, notification.e
vent]); |
| 103 writeln(); |
| 104 docComment(toHtmlVisitor.collectHtml(() { |
| 105 toHtmlVisitor.translateHtml(notification.html); |
| 106 toHtmlVisitor.describePayload(notification.params, 'Parameters'); |
| 107 }), false); |
| 108 writeln('Stream $streamName;'); |
| 109 writeln(); |
| 110 docComment(toHtmlVisitor.collectHtml(() { |
| 111 toHtmlVisitor.write('Stream controller for [$streamName].'); |
| 112 }), false); |
| 113 writeln('StreamController _$streamName;'); |
| 114 fieldInitializationCode.add(collectCode(() { |
| 115 writeln('_$streamName = new StreamController(sync: true);'); |
| 116 writeln('$streamName = _$streamName.stream.asBroadcastStream();'); |
| 117 })); |
| 118 notificationSwitchContents.add(collectCode(() { |
| 119 writeln('case ${JSON.encode(notification.longEvent)}:'); |
| 120 indent(() { |
| 121 String paramsValidator = camelJoin(['is', notification.domainName, |
| 122 notification.event, 'params']); |
| 123 writeln('expect(params, $paramsValidator);'); |
| 124 writeln('_$streamName.add(params);'); |
| 125 writeln('break;'); |
| 126 }); |
| 127 })); |
| 128 } |
| 129 |
| 130 @override |
| 60 visitRequest(Request request) { | 131 visitRequest(Request request) { |
| 61 String methodName = camelJoin(['send', request.domainName, request.method]); | 132 String methodName = camelJoin(['send', request.domainName, request.method]); |
| 62 List<String> args = <String>[]; | 133 List<String> args = <String>[]; |
| 63 List<String> optionalArgs = <String>[]; | 134 List<String> optionalArgs = <String>[]; |
| 64 if (request.params != null) { | 135 if (request.params != null) { |
| 65 for (TypeObjectField field in request.params.fields) { | 136 for (TypeObjectField field in request.params.fields) { |
| 66 if (field.optional) { | 137 if (field.optional) { |
| 67 optionalArgs.add(formatArgument(field)); | 138 optionalArgs.add(formatArgument(field)); |
| 68 } else { | 139 } else { |
| 69 args.add(formatArgument(field)); | 140 args.add(formatArgument(field)); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 * Translate spec_input.html into protocol_matchers.dart. | 242 * Translate spec_input.html into protocol_matchers.dart. |
| 172 */ | 243 */ |
| 173 main() { | 244 main() { |
| 174 CodegenInttestMethodsVisitor visitor = new CodegenInttestMethodsVisitor( | 245 CodegenInttestMethodsVisitor visitor = new CodegenInttestMethodsVisitor( |
| 175 readApi()); | 246 readApi()); |
| 176 String code = visitor.collectCode(visitor.visitApi); | 247 String code = visitor.collectCode(visitor.visitApi); |
| 177 File outputFile = new File( | 248 File outputFile = new File( |
| 178 '../../test/integration/integration_test_methods.dart'); | 249 '../../test/integration/integration_test_methods.dart'); |
| 179 outputFile.writeAsStringSync(code); | 250 outputFile.writeAsStringSync(code); |
| 180 } | 251 } |
| OLD | NEW |