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

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

Issue 10697003: Various token issues fixed in the compiler (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: SendSet.getEndToken() fixed, unit test added Created 8 years, 5 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) 2011, 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('parser_helper.dart');
6 #import("../../../lib/compiler/implementation/scanner/scannerlib.dart");
7 #import("../../../lib/compiler/implementation/tree/tree.dart");
8
9
10 void testNode(Node node, String expected, String text, [bool hard = true]) {
11 var debug = 'text=$text,expected=$expected,node:${node}';
12 Expect.isNotNull(node, debug);
13
14 Token beginToken = node.getBeginToken();
15 Expect.isNotNull(beginToken, debug);
16 Token endToken = node.getEndToken();
17
18 Expect.isNotNull(endToken, debug);
19
20 int begin = beginToken.charOffset;
21 int end = endToken.charOffset + endToken.slowCharCount;
22 Expect.isTrue(begin <= end, debug);
23
24 if (hard) {
25 Expect.stringEquals(expected, text.substring(begin, end), debug);
26 }
27 }
28
29 Node testExpression(String text) {
30 var node = parseStatement('$text;').expression;
31 testNode(node, text, text);
32 return node;
33 }
34
35 void testUnaryExpression() {
36 testExpression('x++');
37 testExpression('++x');
38 }
39
40 void testAssignment() {
41 Expression node;
42 SendSet sendSet;
43 String text;
44
45 text = "a = b";
46 node = testExpression(text);
47
48 text = "a = b = c";
49 node = testExpression(text);
50 // Should parse as: a = (b = c).
51 Expect.isNotNull(sendSet = node.asSendSet());
52 testNode(sendSet.arguments.head.asSendSet(), 'b = c', text);
53
54 text = "a = b = c = d";
55 node = testExpression(text);
56 // Should parse as: a = (b = (c = d)).
57 Expect.isNotNull(sendSet = node.asSendSet());
58 testNode(sendSet = sendSet.arguments.head.asSendSet(), 'b = c = d', text);
59 testNode(sendSet = sendSet.arguments.head.asSendSet(), 'c = d', text);
60
61 text = "a.b = c";
62 node = testExpression(text);
63 // Should parse as: receiver = a, selector = b, arguments = c.
64 Expect.isNotNull(sendSet = node.asSendSet());
65 testNode(sendSet.receiver, "a", "a.b = c");
66 testNode(sendSet.selector, "b", "a.b = c");
67 testNode(sendSet.arguments.head, "c", "a.b = c");
68
69 text = "a.b = c.d";
70 node = testExpression(text);
71 // Should parse as: a.b = (c.d).
72 Expect.isNotNull(sendSet = node.asSendSet());
73 Expect.stringEquals("a", sendSet.receiver.toString());
74 Expect.stringEquals("b", sendSet.selector.toString());
75 Expect.stringEquals("c.d", sendSet.arguments.head.toString());
76
77 text = "a.b = c.d = e.f";
78 node = testExpression(text);
79 // Should parse as: a.b = (c.d = (e.f)).
80 Expect.isNotNull(sendSet = node.asSendSet());
81 testNode(sendSet.receiver, "a", text);
82 testNode(sendSet.selector, "b", text);
83 Expect.isNotNull(sendSet = sendSet.arguments.head.asSendSet());
84 testNode(sendSet.receiver, "c", text);
85 testNode(sendSet.selector, "d", text);
86 testNode(sendSet.arguments.head, "e.f", text);
87 }
88
89 void testIndex() {
90 Expression node;
91 Send send;
92 SendSet sendSet;
93 String text;
94
95 text = "a[b]";
96 node = testExpression(text);
97 // Should parse as: (a)[b].
98 Expect.isNotNull(send = node.asSend());
99 testNode(send.receiver, "a", text);
100 //
101 //testNode(send.selector, "[b]", text);
102 testNode(send.arguments.head, "b", text);
103
104 text = "a[b] = c";
105 node = testExpression(text);
106 // Should parse as: (a)[b] = c.
107 Expect.isNotNull(sendSet = node.asSendSet());
108 testNode(sendSet.receiver, "a", text);
109 testNode(sendSet.selector, "[]", text, false); // Operator token is synthetic.
110 testNode(sendSet.assignmentOperator, "=", text);
111 testNode(sendSet.arguments.head, "b", text);
112 testNode(sendSet.arguments.tail.head, "c", text);
113
114 text = "a.b[c]";
115 node = testExpression(text);
116 // Should parse as: (a.b)[c].
117 Expect.isNotNull(send = node.asSend());
118 testNode(send.receiver, "a.b", text);
119 testNode(send.selector, "[]", text, false); // Operator token is synthetic.
120 testNode(send.arguments.head, "c", text);
121
122 text = "a.b[c] = d";
123 node = testExpression(text);
124 // Should parse as: (a.b)[] = (c, d).
125 Expect.isNotNull(sendSet = node.asSendSet());
126 Expect.isNotNull(send = sendSet.receiver.asSend());
127 testNode(send, "a.b", text);
128 testNode(sendSet.selector, "[]", text, false); // Operator token is synthetic.
129 testNode(sendSet.assignmentOperator, "=", text);
130 testNode(sendSet.arguments.head, "c", text);
131 testNode(sendSet.arguments.tail.head, "d", text);
132 }
133
134 void testPostfix() {
135 Expression node;
136 SendSet sendSet;
137 String text;
138
139 text = "a.b++";
Lasse Reichstein Nielsen 2012/06/28 07:45:14 How about testing a[b]++ / ++a[b] too?
140 node = testExpression(text);
141 // Should parse as: (a.b)++.
142 Expect.isNotNull(sendSet = node.asSendSet());
143 testNode(sendSet.receiver, "a", text);
144 testNode(sendSet.selector, "b", text);
145 testNode(sendSet.assignmentOperator, "++", text);
146 Expect.isTrue(sendSet.arguments.isEmpty());
147 }
148
149 void main() {
150 testUnaryExpression();
151 testAssignment();
152 testIndex();
153 testPostfix();
154 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698