| 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 /** |
| 6 * Code generation for the file "integration_test_methods.dart". |
| 7 */ |
| 8 library codegenInttestMethods; |
| 9 |
| 10 import 'dart:convert'; |
| 11 import 'dart:io'; |
| 12 |
| 13 import 'api.dart'; |
| 14 import 'codegen_tools.dart'; |
| 15 import 'from_html.dart'; |
| 16 import 'to_html.dart'; |
| 17 |
| 18 /** |
| 19 * Visitor that generates the code for integration_test_methods.dart |
| 20 */ |
| 21 class CodegenInttestMethodsVisitor extends HierarchicalApiVisitor with |
| 22 CodeGenerator { |
| 23 /** |
| 24 * Visitor used to produce doc comments. |
| 25 */ |
| 26 final ToHtmlVisitor toHtmlVisitor; |
| 27 |
| 28 CodegenInttestMethodsVisitor(Api api) |
| 29 : super(api), |
| 30 toHtmlVisitor = new ToHtmlVisitor(api); |
| 31 |
| 32 @override |
| 33 visitApi() { |
| 34 outputHeader(); |
| 35 writeln('/**'); |
| 36 writeln(' * Convenience methods for running integration tests'); |
| 37 writeln(' */'); |
| 38 writeln('library test.integration.methods;'); |
| 39 writeln(); |
| 40 writeln("import 'dart:async';"); |
| 41 writeln(); |
| 42 writeln("import 'package:unittest/unittest.dart';"); |
| 43 writeln(); |
| 44 writeln("import 'integration_tests.dart';"); |
| 45 writeln("import 'protocol_matchers.dart';"); |
| 46 writeln(); |
| 47 writeln(); |
| 48 writeln('/**'); |
| 49 writeln(' * Convenience methods for running integration tests'); |
| 50 writeln(' */'); |
| 51 writeln('abstract class InttestMixin {'); |
| 52 indent(() { |
| 53 writeln('Server get server;'); |
| 54 super.visitApi(); |
| 55 }); |
| 56 writeln('}'); |
| 57 } |
| 58 |
| 59 @override |
| 60 visitRequest(Request request) { |
| 61 String methodName = camelJoin(['send', request.domainName, request.method]); |
| 62 List<String> args = <String>[]; |
| 63 List<String> optionalArgs = <String>[]; |
| 64 if (request.params != null) { |
| 65 for (TypeObjectField field in request.params.fields) { |
| 66 if (field.optional) { |
| 67 optionalArgs.add(formatArgument(field)); |
| 68 } else { |
| 69 args.add(formatArgument(field)); |
| 70 } |
| 71 } |
| 72 } |
| 73 optionalArgs.add('bool checkTypes: true'); |
| 74 args.add('{${optionalArgs.join(', ')}}'); |
| 75 writeln(); |
| 76 docComment(toHtmlVisitor.collectHtml(() { |
| 77 toHtmlVisitor.translateHtml(request.html); |
| 78 toHtmlVisitor.describePayload(request.params, 'Parameters'); |
| 79 toHtmlVisitor.describePayload(request.result, 'Returns'); |
| 80 }), false); |
| 81 writeln('Future $methodName(${args.join(', ')}) {'); |
| 82 indent(() { |
| 83 String paramsValidator = camelJoin(['is', request.domainName, |
| 84 request.method, 'params']); |
| 85 String resultValidator = camelJoin(['is', request.domainName, |
| 86 request.method, 'result']); |
| 87 String paramsVar = 'null'; |
| 88 if (request.params != null) { |
| 89 paramsVar = 'params'; |
| 90 writeln('Map<String, dynamic> params = {};'); |
| 91 for (TypeObjectField field in request.params.fields) { |
| 92 if (field.optional) { |
| 93 writeln('if (${field.name} != null) {'); |
| 94 indent(() { |
| 95 populateField(field); |
| 96 }); |
| 97 writeln('}'); |
| 98 } else { |
| 99 populateField(field); |
| 100 } |
| 101 } |
| 102 writeln('if (checkTypes) {'); |
| 103 indent(() { |
| 104 writeln('expect(params, $paramsValidator);'); |
| 105 }); |
| 106 writeln('}'); |
| 107 } |
| 108 writeln( |
| 109 'return server.send(${JSON.encode(request.longMethod)}, $paramsVar)'); |
| 110 indent(() { |
| 111 writeln(' .then((result) {'); |
| 112 writeln('if (checkTypes) {'); |
| 113 indent(() { |
| 114 writeln('expect(result, $resultValidator);'); |
| 115 }); |
| 116 writeln('}'); |
| 117 writeln('return result;'); |
| 118 }); |
| 119 writeln('});'); |
| 120 }); |
| 121 writeln('}'); |
| 122 } |
| 123 |
| 124 /** |
| 125 * Generate a function argument for the given parameter field. |
| 126 */ |
| 127 String formatArgument(TypeObjectField field) => |
| 128 '${jsonType(field.type)} ${field.name}'; |
| 129 |
| 130 /** |
| 131 * Generate code that populates the given parameter field based on the |
| 132 * function argument from [formatArgument]. |
| 133 */ |
| 134 void populateField(TypeObjectField field) { |
| 135 writeln('params[${JSON.encode(field.name)}] = ${field.name};'); |
| 136 } |
| 137 |
| 138 /** |
| 139 * Figure out the appropriate Dart type for data having the given API |
| 140 * protocol [type]. |
| 141 */ |
| 142 String jsonType(TypeDecl type) { |
| 143 type = resolveTypeReferenceChain(type); |
| 144 if (type is TypeEnum) { |
| 145 return 'String'; |
| 146 } else if (type is TypeList) { |
| 147 return 'List<${jsonType(type.itemType)}>'; |
| 148 } else if (type is TypeMap) { |
| 149 return 'Map<String, ${jsonType(type.valueType)}>'; |
| 150 } else if (type is TypeObject) { |
| 151 return 'Map<String, dynamic>'; |
| 152 } else if (type is TypeReference) { |
| 153 switch (type.typeName) { |
| 154 case 'String': |
| 155 case 'int': |
| 156 case 'bool': |
| 157 // These types correspond exactly to Dart types |
| 158 return type.typeName; |
| 159 case 'object': |
| 160 return 'Map<String, dynamic>'; |
| 161 default: |
| 162 throw new Exception(type.typeName); |
| 163 } |
| 164 } else { |
| 165 throw new Exception('Unexpected kind of TypeDecl'); |
| 166 } |
| 167 } |
| 168 } |
| 169 |
| 170 /** |
| 171 * Translate spec_input.html into protocol_matchers.dart. |
| 172 */ |
| 173 main() { |
| 174 CodegenInttestMethodsVisitor visitor = new CodegenInttestMethodsVisitor( |
| 175 readApi()); |
| 176 String code = visitor.collectCode(visitor.visitApi); |
| 177 File outputFile = new File( |
| 178 '../../test/integration/integration_test_methods.dart'); |
| 179 outputFile.writeAsStringSync(code); |
| 180 } |
| OLD | NEW |