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 /// And end-to-end test that generates code and checks that the output matches | |
6 /// the code in `static_test.dart`. Techincally we could run the result in an | |
7 /// isolate, but instead we decided to split that up in two tests. This test | |
8 /// ensures that we generate the code as it was written in static_test, and | |
9 /// separately static_test ensures that the smoke.static library behaves as | |
10 /// expected. | |
11 library smoke.test.codegen.end_to_end_test; | |
12 | |
13 import 'dart:io'; | |
14 | |
15 import 'package:analyzer/src/generated/element.dart'; | |
16 import 'package:smoke/codegen/generator.dart'; | |
17 import 'package:smoke/codegen/recorder.dart'; | |
18 import 'package:test/test.dart'; | |
19 import 'package:path/path.dart' as path; | |
20 | |
21 import 'testing_resolver_utils.dart' show initAnalyzer; | |
22 | |
23 void main([List<String> args]) { | |
24 final updateStaticTest = | |
25 args != null && args.length > 0 && args[0] == '--update_static_test'; | |
26 | |
27 test('static_test is up to date', () { | |
28 var scriptPath = path.fromUri(Platform.script); | |
29 var testDir = path.dirname(path.dirname(scriptPath)); | |
30 var commonPath = path.join(testDir, 'common.dart'); | |
31 var testCode = new File('$commonPath').readAsStringSync(); | |
32 var lib = initAnalyzer({'common.dart': testCode}).libraryFor('common.dart'); | |
33 var generator = new SmokeCodeGenerator(); | |
34 var recorder = new Recorder(generator, _resolveImportUrl); | |
35 | |
36 lookupMember(String className, String memberName, bool recursive) { | |
37 recorder.lookupMember(lib.getType(className), memberName, | |
38 recursive: recursive, includeAccessors: false); | |
39 } | |
40 | |
41 runQuery(String className, QueryOptions options) { | |
42 recorder.runQuery(lib.getType(className), options, | |
43 includeAccessors: false); | |
44 } | |
45 | |
46 // Record all getters and setters we use in the tests. | |
47 [ | |
48 'i', | |
49 'j', | |
50 'j2', | |
51 'inc0', | |
52 'inc1', | |
53 'inc2', | |
54 'toString' | |
55 ].forEach(generator.addGetter); | |
56 ['i', 'j2'].forEach(generator.addSetter); | |
57 | |
58 // Record static methods used in the tests | |
59 recorder.addStaticMethod(lib.getType('A'), 'staticInc'); | |
60 | |
61 // Record symbol convertions. | |
62 generator.addSymbol('i'); | |
63 | |
64 /// Record all parent-class relations that we explicitly request. | |
65 [ | |
66 'AnnotB', | |
67 'A', | |
68 'B', | |
69 'D', | |
70 'H' | |
71 ].forEach((className) => recorder.lookupParent(lib.getType(className))); | |
72 | |
73 // Record members for which we implicitly request their declaration in | |
74 // has-getter and has-setter tests. | |
75 lookupMember('A', 'i', true); | |
76 lookupMember('A', 'j2', true); | |
77 lookupMember('A', 'inc2', true); | |
78 lookupMember('B', 'a', true); | |
79 lookupMember('B', 'f', true); | |
80 lookupMember('D', 'i', true); | |
81 lookupMember('E', 'y', true); | |
82 | |
83 // Record also lookups for non-exisiting members. | |
84 lookupMember('B', 'i', true); | |
85 lookupMember('E', 'x', true); | |
86 lookupMember('E', 'z', true); | |
87 | |
88 // Record members for which we explicitly request their declaration. | |
89 lookupMember('B', 'a', false); | |
90 lookupMember('B', 'w', false); | |
91 lookupMember('A', 'inc1', false); | |
92 lookupMember('F', 'staticMethod', false); | |
93 lookupMember('G', 'b', false); | |
94 lookupMember('G', 'd', false); | |
95 | |
96 // Lookups from no-such-method test. | |
97 lookupMember('A', 'noSuchMethod', true); | |
98 lookupMember('E', 'noSuchMethod', true); | |
99 lookupMember('E2', 'noSuchMethod', true); | |
100 | |
101 // Lookups from has-instance-method and has-static-method tests. | |
102 lookupMember('A', 'inc0', true); | |
103 lookupMember('A', 'inc3', true); | |
104 lookupMember('C', 'inc', true); | |
105 lookupMember('D', 'inc', true); | |
106 lookupMember('D', 'inc0', true); | |
107 lookupMember('F', 'staticMethod', true); | |
108 lookupMember('F2', 'staticMethod', true); | |
109 | |
110 // Record all queries done by the test. | |
111 runQuery('A', new QueryOptions()); | |
112 runQuery('D', new QueryOptions(includeInherited: true)); | |
113 | |
114 var vars = lib.definingCompilationUnit.topLevelVariables; | |
115 expect(vars[0].name, 'a1'); | |
116 expect(vars[1].name, 'a2'); | |
117 | |
118 runQuery('H', new QueryOptions( | |
119 includeInherited: true, | |
120 withAnnotations: [vars[0], vars[1], lib.getType('Annot')])); | |
121 | |
122 runQuery('K', new QueryOptions( | |
123 includeInherited: true, withAnnotations: [lib.getType('AnnotC')])); | |
124 | |
125 runQuery('L', new QueryOptions(includeMethods: true)); | |
126 runQuery('L2', new QueryOptions( | |
127 includeInherited: true, includeMethods: true)); | |
128 | |
129 var code = _createEntrypoint(generator); | |
130 var staticTestFile = new File(path.join(testDir, 'static_test.dart')); | |
131 var existingCode = staticTestFile.readAsStringSync(); | |
132 if (!updateStaticTest) { | |
133 expect(code, existingCode); | |
134 } else if (code == existingCode) { | |
135 print('static_test.dart is already up to date'); | |
136 } else { | |
137 staticTestFile.writeAsStringSync(code); | |
138 print('static_test.dart has been updated.'); | |
139 } | |
140 }, skip: 'https://github.com/dart-lang/smoke/issues/26'); | |
141 } | |
142 | |
143 String _createEntrypoint(SmokeCodeGenerator generator) { | |
144 var sb = new StringBuffer() | |
145 ..writeln('/// ---- AUTOGENERATED: DO NOT EDIT THIS FILE --------------') | |
146 ..writeln('/// To update this test file, call:') | |
147 ..writeln('/// > dart codegen/end_to_end_test.dart --update_static_test') | |
148 ..writeln('/// --------------------------------------------------------') | |
149 ..writeln('\nlibrary smoke.test.static_test;\n') | |
150 ..writeln("import 'package:unittest/unittest.dart';"); | |
151 | |
152 generator.writeImports(sb); | |
153 sb.writeln("import 'common.dart' as common show main;\n"); | |
154 generator.writeTopLevelDeclarations(sb); | |
155 sb.write('\nfinal configuration = '); | |
156 generator.writeStaticConfiguration(sb, 0); | |
157 | |
158 sb | |
159 ..writeln(';\n') | |
160 ..writeln('main() {') | |
161 ..writeln(' setUp(() => useGeneratedCode(configuration));') | |
162 ..writeln(' common.main();') | |
163 ..writeln('}'); | |
164 return sb.toString(); | |
165 } | |
166 | |
167 String _resolveImportUrl(LibraryElement lib) { | |
168 if (lib.isDartCore) return 'dart:core'; | |
169 if (lib.displayName == 'smoke.test.common') return 'common.dart'; | |
170 return 'unknown.dart'; | |
171 } | |
OLD | NEW |