Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: frog/tests/leg/src/mock_compiler.dart

Issue 10250002: test rename overhaul: step 5 - frog and leg tests (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, 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 #library('mock_compiler');
6
7 #import("dart:uri");
8
9 #import("../../../../lib/compiler/implementation/elements/elements.dart");
10 #import("../../../../lib/compiler/implementation/leg.dart");
11 #import("../../../../lib/compiler/implementation/tree/tree.dart");
12 #import("../../../../lib/compiler/implementation/util/util.dart");
13 #import("parser_helper.dart");
14
15 class WarningMessage {
16 Node node;
17 Message message;
18 WarningMessage(this.node, this.message);
19
20 toString() => message.toString();
21 }
22
23 final String DEFAULT_HELPERLIB = @'''
24 lt() {} add(var a, var b) {} sub() {} mul() {} div() {} tdiv() {} mod() {}
25 neg() {} shl() {} shr() {} eq() {} le() {} gt() {} ge() {}
26 or() {} and() {} not() {} eqNull(a) {} eqq() {}
27 iae(x) {throw x;} ioore(x) {throw x;}
28 guard$array(x) { return x; }
29 guard$num(x) { return x; }
30 guard$string(x) { return x; }
31 guard$stringOrArray(x) { return x; }
32 builtin$add$1(receiver, value) {}
33 builtin$get$length(var receiver) {}
34 builtin$filter$1(receiver, predicate) {}
35 builtin$removeLast$0(receiver) {}
36 index(a, index) {}
37 indexSet(a, index, value) {}
38 builtin$iterator$0() {}
39 builtin$next$0() {}
40 builtin$hasNext$0() {}''';
41
42 final String DEFAULT_CORELIB = @'''
43 print(var obj) {}
44 class int {}
45 class double {}
46 class bool {}
47 class String {}
48 class Object {}
49 class num {}
50 class Function {}
51 class List {}
52 class Closure {}
53 class Null {}
54 class Dynamic {}''';
55
56 class MockCompiler extends Compiler {
57 List<WarningMessage> warnings;
58 List<WarningMessage> errors;
59 final Map<String, String> sources;
60 Node parsedTree;
61
62 MockCompiler([String coreSource = DEFAULT_CORELIB,
63 String helperSource = DEFAULT_HELPERLIB])
64 : warnings = [], errors = [],
65 sources = new Map<String, String>(),
66 super() {
67 Uri uri = new Uri(scheme: "source");
68 var script = new Script(uri, new MockFile(coreSource));
69 coreLibrary = new LibraryElement(script);
70 parseScript(coreSource, coreLibrary);
71 script = new Script(uri, new MockFile(helperSource));
72 jsHelperLibrary = new LibraryElement(script);
73 parseScript(helperSource, jsHelperLibrary);
74 scanner.importLibrary(jsHelperLibrary, coreLibrary, null);
75 mainApp = mockLibrary(this, "");
76 initializeSpecialClasses();
77 }
78
79 void reportWarning(Node node, var message) {
80 warnings.add(new WarningMessage(node, message.message));
81 }
82
83 void reportError(Node node, var message) {
84 if (message is String && message.startsWith("no #library tag found in")) {
85 // TODO(ahe): Fix the MockCompiler to not have this problem.
86 return;
87 }
88 errors.add(new WarningMessage(node, message.message));
89 }
90
91 void reportDiagnostic(SourceSpan span, String message, bool fatal) {
92 print(message);
93 }
94
95 void clearWarnings() {
96 warnings = [];
97 }
98
99 void clearErrors() {
100 errors = [];
101 }
102
103 TreeElementMapping resolveStatement(String text) {
104 parsedTree = parseStatement(text);
105 return resolveNodeStatement(parsedTree, mainApp);
106 }
107
108 TreeElementMapping resolveNodeStatement(Node tree, Element element) {
109 ResolverVisitor visitor = new ResolverVisitor(this, element);
110 if (visitor.context is TopScope) {
111 visitor.context = new BlockScope(visitor.context);
112 }
113 visitor.visit(tree);
114 visitor.context = new TopScope(element.getLibrary());
115 // Resolve the type annotations encountered in the code.
116 while (!resolver.toResolve.isEmpty()) {
117 resolver.toResolve.removeFirst().ensureResolved(this);
118 }
119 return visitor.mapping;
120 }
121
122 resolverVisitor() {
123 Element mockElement =
124 new Element(buildSourceString(''), ElementKind.FUNCTION, mainApp);
125 ResolverVisitor visitor = new ResolverVisitor(this, mockElement);
126 visitor.context = new BlockScope(visitor.context);
127 return visitor;
128 }
129
130 parseScript(String text, [LibraryElement library]) {
131 if (library === null) library = mainApp;
132 parseUnit(text, this, library);
133 }
134
135 void enqueue(WorkItem work) {
136 super.enqueue(work);
137 }
138
139 void scanBuiltinLibraries() {
140 // Do nothing. The mock core library is already handled in the constructor.
141 }
142
143 LibraryElement scanBuiltinLibrary(String name) {
144 // Do nothing. The mock core library is already handled in the constructor.
145 }
146
147 Script readScript(Uri uri, [ScriptTag node]) {
148 String code = sources[uri.toString()];
149 if (code === null) throw new IllegalArgumentException(uri);
150 return new StringScript(code);
151 }
152 }
153
154 void compareWarningKinds(String text, expectedWarnings, foundWarnings) {
155 var fail = (message) => Expect.fail('$text: $message');
156 Iterator<MessageKind> expected = expectedWarnings.iterator();
157 Iterator<WarningMessage> found = foundWarnings.iterator();
158 while (expected.hasNext() && found.hasNext()) {
159 Expect.equals(expected.next(), found.next().message.kind);
160 }
161 if (expected.hasNext()) {
162 do {
163 print('Expected warning "${expected.next()}" did not occur');
164 } while (expected.hasNext());
165 fail('Too few warnings');
166 }
167 if (found.hasNext()) {
168 do {
169 print('Additional warning "${found.next()}"');
170 } while (found.hasNext());
171 fail('Too many warnings');
172 }
173 }
174
175 void importLibrary(LibraryElement target, LibraryElement imported,
176 Compiler compiler) {
177 for (Link<Element> link = imported.topLevelElements; !link.isEmpty();
178 link = link.tail) {
179 compiler.withCurrentElement(link.head, () {
180 target.define(link.head, compiler);
181 });
182 }
183 }
184
185 LibraryElement mockLibrary(Compiler compiler, String source) {
186 Uri uri = new Uri(scheme: "source");
187 var library = new LibraryElement(new Script(uri, new MockFile(source)));
188 importLibrary(library, compiler.coreLibrary, compiler);
189 return library;
190 }
191
192 class StringScript extends Script {
193 final String code;
194 StringScript(this.code) : super(null, null);
195 String get text() => code;
196 String get name() => "mock script";
197 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698