| 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 "matchers.dart". | 6 * Code generation for the file "matchers.dart". |
| 7 */ | 7 */ |
| 8 library codegen.matchers; | 8 library codegen.matchers; |
| 9 | 9 |
| 10 import 'dart:convert'; | 10 import 'dart:convert'; |
| 11 | 11 |
| 12 import 'api.dart'; | 12 import 'api.dart'; |
| 13 import 'codegen_tools.dart'; | 13 import 'codegen_tools.dart'; |
| 14 import 'from_html.dart'; | 14 import 'from_html.dart'; |
| 15 import 'implied_types.dart'; | 15 import 'implied_types.dart'; |
| 16 import 'to_html.dart'; | 16 import 'to_html.dart'; |
| 17 | 17 |
| 18 final GeneratedFile target = |
| 19 new GeneratedFile('../../test/integration/protocol_matchers.dart', () { |
| 20 CodegenMatchersVisitor visitor = new CodegenMatchersVisitor(readApi()); |
| 21 return visitor.collectCode(visitor.visitApi); |
| 22 }); |
| 23 |
| 24 /** |
| 25 * Translate spec_input.html into protocol_matchers.dart. |
| 26 */ |
| 27 main() { |
| 28 target.generate(); |
| 29 } |
| 30 |
| 18 class CodegenMatchersVisitor extends HierarchicalApiVisitor with CodeGenerator { | 31 class CodegenMatchersVisitor extends HierarchicalApiVisitor with CodeGenerator { |
| 19 /** | 32 /** |
| 20 * Visitor used to produce doc comments. | 33 * Visitor used to produce doc comments. |
| 21 */ | 34 */ |
| 22 final ToHtmlVisitor toHtmlVisitor; | 35 final ToHtmlVisitor toHtmlVisitor; |
| 23 | 36 |
| 24 /** | 37 /** |
| 25 * Short human-readable string describing the context of the matcher being | 38 * Short human-readable string describing the context of the matcher being |
| 26 * created. | 39 * created. |
| 27 */ | 40 */ |
| (...skipping 21 matching lines...) Expand all Loading... |
| 49 write('final Matcher ${camelJoin(['is', impliedType.camelName])} = '); | 62 write('final Matcher ${camelJoin(['is', impliedType.camelName])} = '); |
| 50 if (impliedType.type == null) { | 63 if (impliedType.type == null) { |
| 51 write('isNull'); | 64 write('isNull'); |
| 52 } else { | 65 } else { |
| 53 visitTypeDecl(impliedType.type); | 66 visitTypeDecl(impliedType.type); |
| 54 } | 67 } |
| 55 writeln(';'); | 68 writeln(';'); |
| 56 writeln(); | 69 writeln(); |
| 57 } | 70 } |
| 58 | 71 |
| 72 /** |
| 73 * Generate a map describing the given set of fields, for use as the |
| 74 * 'requiredFields' or 'optionalFields' argument to the [MatchesJsonObject] |
| 75 * constructor. |
| 76 */ |
| 77 void outputObjectFields(Iterable<TypeObjectField> fields) { |
| 78 if (fields.isEmpty) { |
| 79 write('null'); |
| 80 return; |
| 81 } |
| 82 writeln('{'); |
| 83 indent(() { |
| 84 bool commaNeeded = false; |
| 85 for (TypeObjectField field in fields) { |
| 86 if (commaNeeded) { |
| 87 writeln(','); |
| 88 } |
| 89 write('${JSON.encode(field.name)}: '); |
| 90 if (field.value != null) { |
| 91 write('equals(${JSON.encode(field.value)})'); |
| 92 } else { |
| 93 visitTypeDecl(field.type); |
| 94 } |
| 95 commaNeeded = true; |
| 96 } |
| 97 writeln(); |
| 98 }); |
| 99 write('}'); |
| 100 } |
| 101 |
| 59 @override | 102 @override |
| 60 visitApi() { | 103 visitApi() { |
| 61 outputHeader(); | 104 outputHeader(); |
| 62 writeln(); | 105 writeln(); |
| 63 writeln('/**'); | 106 writeln('/**'); |
| 64 writeln(' * Matchers for data types defined in the analysis server API'); | 107 writeln(' * Matchers for data types defined in the analysis server API'); |
| 65 writeln(' */'); | 108 writeln(' */'); |
| 66 writeln('library test.integration.protocol.matchers;'); | 109 writeln('library test.integration.protocol.matchers;'); |
| 67 writeln(); | 110 writeln(); |
| 68 writeln("import 'package:unittest/unittest.dart';"); | 111 writeln("import 'package:unittest/unittest.dart';"); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 write(', '); | 149 write(', '); |
| 107 visitTypeDecl(typeMap.valueType); | 150 visitTypeDecl(typeMap.valueType); |
| 108 write(')'); | 151 write(')'); |
| 109 } | 152 } |
| 110 | 153 |
| 111 @override | 154 @override |
| 112 void visitTypeObject(TypeObject typeObject) { | 155 void visitTypeObject(TypeObject typeObject) { |
| 113 writeln('new LazyMatcher(() => new MatchesJsonObject('); | 156 writeln('new LazyMatcher(() => new MatchesJsonObject('); |
| 114 indent(() { | 157 indent(() { |
| 115 write('${JSON.encode(context)}, '); | 158 write('${JSON.encode(context)}, '); |
| 116 Iterable<TypeObjectField> requiredFields = typeObject.fields.where( | 159 Iterable<TypeObjectField> requiredFields = |
| 117 (TypeObjectField field) => !field.optional); | 160 typeObject.fields.where((TypeObjectField field) => !field.optional); |
| 118 outputObjectFields(requiredFields); | 161 outputObjectFields(requiredFields); |
| 119 List<TypeObjectField> optionalFields = typeObject.fields.where( | 162 List<TypeObjectField> optionalFields = |
| 120 (TypeObjectField field) => field.optional).toList(); | 163 typeObject.fields.where((TypeObjectField field) => field.optional).toL
ist(); |
| 121 if (optionalFields.isNotEmpty) { | 164 if (optionalFields.isNotEmpty) { |
| 122 write(', optionalFields: '); | 165 write(', optionalFields: '); |
| 123 outputObjectFields(optionalFields); | 166 outputObjectFields(optionalFields); |
| 124 } | 167 } |
| 125 }); | 168 }); |
| 126 write('))'); | 169 write('))'); |
| 127 } | 170 } |
| 128 | 171 |
| 129 /** | |
| 130 * Generate a map describing the given set of fields, for use as the | |
| 131 * 'requiredFields' or 'optionalFields' argument to the [MatchesJsonObject] | |
| 132 * constructor. | |
| 133 */ | |
| 134 void outputObjectFields(Iterable<TypeObjectField> fields) { | |
| 135 if (fields.isEmpty) { | |
| 136 write('null'); | |
| 137 return; | |
| 138 } | |
| 139 writeln('{'); | |
| 140 indent(() { | |
| 141 bool commaNeeded = false; | |
| 142 for (TypeObjectField field in fields) { | |
| 143 if (commaNeeded) { | |
| 144 writeln(','); | |
| 145 } | |
| 146 write('${JSON.encode(field.name)}: '); | |
| 147 if (field.value != null) { | |
| 148 write('equals(${JSON.encode(field.value)})'); | |
| 149 } else { | |
| 150 visitTypeDecl(field.type); | |
| 151 } | |
| 152 commaNeeded = true; | |
| 153 } | |
| 154 writeln(); | |
| 155 }); | |
| 156 write('}'); | |
| 157 } | |
| 158 | |
| 159 @override | 172 @override |
| 160 void visitTypeReference(TypeReference typeReference) { | 173 void visitTypeReference(TypeReference typeReference) { |
| 161 String typeName = typeReference.typeName; | 174 String typeName = typeReference.typeName; |
| 162 if (typeName == 'long') { | 175 if (typeName == 'long') { |
| 163 typeName = 'int'; | 176 typeName = 'int'; |
| 164 } | 177 } |
| 165 write(camelJoin(['is', typeName])); | 178 write(camelJoin(['is', typeName])); |
| 166 } | 179 } |
| 167 | 180 |
| 168 @override | 181 @override |
| 169 void visitTypeUnion(TypeUnion typeUnion) { | 182 void visitTypeUnion(TypeUnion typeUnion) { |
| 170 bool commaNeeded = false; | 183 bool commaNeeded = false; |
| 171 write('isOneOf(['); | 184 write('isOneOf(['); |
| 172 for (TypeDecl choice in typeUnion.choices) { | 185 for (TypeDecl choice in typeUnion.choices) { |
| 173 if (commaNeeded) { | 186 if (commaNeeded) { |
| 174 write(', '); | 187 write(', '); |
| 175 } | 188 } |
| 176 visitTypeDecl(choice); | 189 visitTypeDecl(choice); |
| 177 commaNeeded = true; | 190 commaNeeded = true; |
| 178 } | 191 } |
| 179 write('])'); | 192 write('])'); |
| 180 } | 193 } |
| 181 } | 194 } |
| 182 | |
| 183 final GeneratedFile target = new GeneratedFile( | |
| 184 '../../test/integration/protocol_matchers.dart', () { | |
| 185 CodegenMatchersVisitor visitor = new CodegenMatchersVisitor(readApi()); | |
| 186 return visitor.collectCode(visitor.visitApi); | |
| 187 }); | |
| 188 | |
| 189 /** | |
| 190 * Translate spec_input.html into protocol_matchers.dart. | |
| 191 */ | |
| 192 main() { | |
| 193 target.generate(); | |
| 194 } | |
| OLD | NEW |