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