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 'to_html.dart'; | 16 import 'to_html.dart'; |
16 | 17 |
17 class CodegenMatchersVisitor extends HierarchicalApiVisitor with CodeGenerator { | 18 class CodegenMatchersVisitor extends HierarchicalApiVisitor with CodeGenerator { |
18 /** | 19 /** |
19 * Visitor used to produce doc comments. | 20 * Visitor used to produce doc comments. |
20 */ | 21 */ |
21 final ToHtmlVisitor toHtmlVisitor; | 22 final ToHtmlVisitor toHtmlVisitor; |
22 | 23 |
23 /** | 24 /** |
24 * Short human-readable string describing the context of the matcher being | 25 * Short human-readable string describing the context of the matcher being |
25 * created. | 26 * created. |
26 */ | 27 */ |
27 String context; | 28 String context; |
28 | 29 |
29 CodegenMatchersVisitor(Api api) | 30 CodegenMatchersVisitor(Api api) |
30 : super(api), | 31 : super(api), |
31 toHtmlVisitor = new ToHtmlVisitor(api); | 32 toHtmlVisitor = new ToHtmlVisitor(api); |
32 | 33 |
33 /** | 34 /** |
34 * Create a matcher for the part of the API called [name], optionally | 35 * Create a matcher for the part of the API called [name], optionally |
35 * clarified by [nameSuffix]. The matcher should verify that its input | 36 * clarified by [nameSuffix]. The matcher should verify that its input |
36 * matches the given [type]. | 37 * matches the given [type]. |
37 */ | 38 */ |
38 void makeMatcher(String name, String nameSuffix, TypeDecl type) { | 39 void makeMatcher(ImpliedType impliedType) { |
39 context = name; | 40 context = impliedType.humanReadableName; |
40 List<String> nameParts = ['is']; | |
41 nameParts.addAll(name.split('.')); | |
42 if (nameSuffix != null) { | |
43 context += ' $nameSuffix'; | |
44 nameParts.add(nameSuffix); | |
45 } | |
46 docComment(toHtmlVisitor.collectHtml(() { | 41 docComment(toHtmlVisitor.collectHtml(() { |
47 toHtmlVisitor.p(() { | 42 toHtmlVisitor.p(() { |
48 toHtmlVisitor.write(context); | 43 toHtmlVisitor.write(context); |
49 }); | 44 }); |
50 if (type != null) { | 45 if (impliedType.type != null) { |
51 toHtmlVisitor.showType(null, type); | 46 toHtmlVisitor.showType(null, impliedType.type); |
52 } | 47 } |
53 })); | 48 })); |
54 write('final Matcher ${camelJoin(nameParts)} = '); | 49 write('final Matcher ${camelJoin(['is', impliedType.camelName])} = '); |
55 if (type == null) { | 50 if (impliedType.type == null) { |
56 write('isNull'); | 51 write('isNull'); |
57 } else { | 52 } else { |
58 visitTypeDecl(type); | 53 visitTypeDecl(impliedType.type); |
59 } | 54 } |
60 writeln(';'); | 55 writeln(';'); |
61 writeln(); | 56 writeln(); |
62 } | 57 } |
63 | 58 |
64 @override | 59 @override |
65 visitApi() { | 60 visitApi() { |
66 outputHeader(); | 61 outputHeader(); |
67 writeln(); | 62 writeln(); |
68 writeln('/**'); | 63 writeln('/**'); |
69 writeln(' * Matchers for data types defined in the analysis server API'); | 64 writeln(' * Matchers for data types defined in the analysis server API'); |
70 writeln(' */'); | 65 writeln(' */'); |
71 writeln('library test.integration.protocol.matchers;'); | 66 writeln('library test.integration.protocol.matchers;'); |
72 writeln(); | 67 writeln(); |
73 writeln("import 'package:unittest/unittest.dart';"); | 68 writeln("import 'package:unittest/unittest.dart';"); |
74 writeln(); | 69 writeln(); |
75 writeln("import 'integration_tests.dart';"); | 70 writeln("import 'integration_tests.dart';"); |
76 writeln(); | 71 writeln(); |
77 writeln(); | 72 writeln(); |
78 super.visitApi(); | 73 for (ImpliedType impliedType in computeImpliedTypes(api).values) { |
| 74 makeMatcher(impliedType); |
| 75 } |
79 } | 76 } |
80 | 77 |
81 @override | 78 @override |
82 visitNotification(Notification notification) { | |
83 makeMatcher(notification.longEvent, 'params', notification.params); | |
84 } | |
85 | |
86 @override | |
87 visitRequest(Request request) { | |
88 makeMatcher(request.longMethod, 'params', request.params); | |
89 makeMatcher(request.longMethod, 'result', request.result); | |
90 } | |
91 | |
92 @override | |
93 visitRefactoring(Refactoring refactoring) { | |
94 String camelKind = camelJoin(refactoring.kind.toLowerCase().split('_')); | |
95 makeMatcher(camelKind, 'feedback', refactoring.feedback); | |
96 makeMatcher(camelKind, 'options', refactoring.options); | |
97 } | |
98 | |
99 @override | |
100 visitTypeDefinition(TypeDefinition typeDefinition) { | |
101 makeMatcher(typeDefinition.name, null, typeDefinition.type); | |
102 } | |
103 | |
104 @override | |
105 visitTypeEnum(TypeEnum typeEnum) { | 79 visitTypeEnum(TypeEnum typeEnum) { |
106 writeln('new MatchesEnum(${JSON.encode(context)}, ['); | 80 writeln('new MatchesEnum(${JSON.encode(context)}, ['); |
107 indent(() { | 81 indent(() { |
108 bool commaNeeded = false; | 82 bool commaNeeded = false; |
109 for (TypeEnumValue value in typeEnum.values) { | 83 for (TypeEnumValue value in typeEnum.values) { |
110 if (commaNeeded) { | 84 if (commaNeeded) { |
111 writeln(','); | 85 writeln(','); |
112 } | 86 } |
113 write('${JSON.encode(value.value)}'); | 87 write('${JSON.encode(value.value)}'); |
114 commaNeeded = true; | 88 commaNeeded = true; |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 CodegenMatchersVisitor visitor = new CodegenMatchersVisitor(readApi()); | 181 CodegenMatchersVisitor visitor = new CodegenMatchersVisitor(readApi()); |
208 return visitor.collectCode(visitor.visitApi); | 182 return visitor.collectCode(visitor.visitApi); |
209 }); | 183 }); |
210 | 184 |
211 /** | 185 /** |
212 * Translate spec_input.html into protocol_matchers.dart. | 186 * Translate spec_input.html into protocol_matchers.dart. |
213 */ | 187 */ |
214 main() { | 188 main() { |
215 target.generate(); | 189 target.generate(); |
216 } | 190 } |
OLD | NEW |