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

Side by Side Diff: tests/compiler/dart2js/unparser2_test.dart

Issue 10834374: Add a field to ClassNode that holds a NodeList of its members. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 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 #import("../../../lib/compiler/implementation/scanner/scannerlib.dart");
6 #import("../../../lib/compiler/implementation/elements/elements.dart"); // only need CompilationUnitElement
ahe 2012/08/21 11:56:17 If you import parser_helper.dart from this directo
messick 2012/08/21 15:35:40 If I import parser_helper I don't have visibility
7 #import("../../../lib/compiler/implementation/tree/tree.dart");
8 #import("../../../lib/compiler/implementation/leg.dart"); // only need Diagnosti cListener & Script
9
10 main() {
11 testClassDef();
12 testClass1Field();
13 testClass2Fields();
14 testClass1Field1Method();
15 testClass1Field2Method();
16 testClassDefTypeParam();
17 }
18
19 testClassDef() {
20 compareCode('''
21 class T{}
22 ''', '''
23 class T {
24 }
25 ''');
26 }
27
28 testClass1Field() {
29 compareCode('''
30 class T{var x;}
31 ''', '''
32 class T {
33 var x;
34 }
35 ''');
36 }
37
38 testClass2Fields() {
39 compareCode('''
40 class T{var x; var y;}
41 ''', '''
42 class T {
43 var x;
44 var y;
45 }
46 ''');
47 }
48
49 testClass1Field1Method() {
50 compareCode('''
51 class T{var x;m(){}}
52 ''', '''
53 class T {
54 var x;
55 m(){}
56 }
57 ''');
58 }
59
60 testClass1Field2Method() {
61 compareCode('''
62 class T{a(){}b(){}}
63 ''', '''
64 class T {
65 a(){}
66 b(){}
67 }
68 ''');
69 }
70
71 testClassDefTypeParam() {
72 compareCode('''
73 class T<X>{}
74 ''', '''
75 class T<X> {
76 }
77 ''');
78 }
79
80 void compareCode(String original, String expected) {
81 String unparsed = doUnparse(cleanUpLiteral(original));
82 Expect.equals(cleanUpLiteral(expected), unparsed);
83 }
84
85 String cleanUpLiteral(String text) {
86 var lines = text.split('\n');
87 for (var j = 0; j < lines.length; j++) {
88 if (lines[j].length > 6) {
89 lines[j] = lines[j].substring(6, lines[j].length);
90 } else {
91 lines[j] = '';
92 }
93 }
94 return Strings.join(lines, '\n');
95 }
96
97 String doUnparse(String source) {
98 MessageCollector diagnosticListener = new MessageCollector();
99 Script script = new Script(null,null);
ahe 2012/08/21 11:56:17 Space after comma.
messick 2012/08/21 15:35:40 Done.
100 LibraryElement lib = new LibraryElement(script);
101 CompilationUnitElement element = new CompilationUnitElement(script, lib);
102 StringScanner scanner = new StringScanner(source);
103 Token beginToken = scanner.tokenize();
104 NodeListener listener = new NodeListener(diagnosticListener, element);
105 Parser parser = new Parser(listener);
106 parser.parseUnit(beginToken);
107 Node node = listener.popNode();
ahe 2012/08/21 11:56:17 Consider adding: Expect.isTrue(listener.nodes.isE
messick 2012/08/21 15:35:40 Done.
108 return new Unparser().unparse(node);
109 }
110
111 class MessageCollector implements DiagnosticListener {
112 List<String> messages;
113 MessageCollector() {
114 messages = [];
115 }
116 void cancel([String reason, node, token, instruction, element]) {
117 messages.add(reason);
118 throw new Exception(reason);
ahe 2012/08/21 11:56:17 Why are you creating a new Exception?
messick 2012/08/21 15:35:40 Java hold-over? Changed.
119 }
120 void log(message) {
121 messages.add(message);
122 }
123 void internalErrorOnElement(Element element, String message) {
ahe 2012/08/21 11:56:17 You probably want to say: throw message; Or Exp
messick 2012/08/21 15:35:40 Done.
124 }
125 void internalError(String message,
126 [Node node, Token token, Dynamic instruction,
127 Element element]) {
ahe 2012/08/21 11:56:17 Ditto.
messick 2012/08/21 15:35:40 Done.
128 }
129 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698