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

Unified 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, 6 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 side-by-side diff with in-line comments
Download patch
Index: tests/compiler/dart2js/begin_end_token_test.dart
diff --git a/tests/compiler/dart2js/begin_end_token_test.dart b/tests/compiler/dart2js/begin_end_token_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..4469d2f8b12cd59b63b8268c4faeed4e25fc1d08
--- /dev/null
+++ b/tests/compiler/dart2js/begin_end_token_test.dart
@@ -0,0 +1,154 @@
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+#import('parser_helper.dart');
+#import("../../../lib/compiler/implementation/scanner/scannerlib.dart");
+#import("../../../lib/compiler/implementation/tree/tree.dart");
+
+
+void testNode(Node node, String expected, String text, [bool hard = true]) {
+ var debug = 'text=$text,expected=$expected,node:${node}';
+ Expect.isNotNull(node, debug);
+
+ Token beginToken = node.getBeginToken();
+ Expect.isNotNull(beginToken, debug);
+ Token endToken = node.getEndToken();
+
+ Expect.isNotNull(endToken, debug);
+
+ int begin = beginToken.charOffset;
+ int end = endToken.charOffset + endToken.slowCharCount;
+ Expect.isTrue(begin <= end, debug);
+
+ if (hard) {
+ Expect.stringEquals(expected, text.substring(begin, end), debug);
+ }
+}
+
+Node testExpression(String text) {
+ var node = parseStatement('$text;').expression;
+ testNode(node, text, text);
+ return node;
+}
+
+void testUnaryExpression() {
+ testExpression('x++');
+ testExpression('++x');
+}
+
+void testAssignment() {
+ Expression node;
+ SendSet sendSet;
+ String text;
+
+ text = "a = b";
+ node = testExpression(text);
+
+ text = "a = b = c";
+ node = testExpression(text);
+ // Should parse as: a = (b = c).
+ Expect.isNotNull(sendSet = node.asSendSet());
+ testNode(sendSet.arguments.head.asSendSet(), 'b = c', text);
+
+ text = "a = b = c = d";
+ node = testExpression(text);
+ // Should parse as: a = (b = (c = d)).
+ Expect.isNotNull(sendSet = node.asSendSet());
+ testNode(sendSet = sendSet.arguments.head.asSendSet(), 'b = c = d', text);
+ testNode(sendSet = sendSet.arguments.head.asSendSet(), 'c = d', text);
+
+ text = "a.b = c";
+ node = testExpression(text);
+ // Should parse as: receiver = a, selector = b, arguments = c.
+ Expect.isNotNull(sendSet = node.asSendSet());
+ testNode(sendSet.receiver, "a", "a.b = c");
+ testNode(sendSet.selector, "b", "a.b = c");
+ testNode(sendSet.arguments.head, "c", "a.b = c");
+
+ text = "a.b = c.d";
+ node = testExpression(text);
+ // Should parse as: a.b = (c.d).
+ Expect.isNotNull(sendSet = node.asSendSet());
+ Expect.stringEquals("a", sendSet.receiver.toString());
+ Expect.stringEquals("b", sendSet.selector.toString());
+ Expect.stringEquals("c.d", sendSet.arguments.head.toString());
+
+ text = "a.b = c.d = e.f";
+ node = testExpression(text);
+ // Should parse as: a.b = (c.d = (e.f)).
+ Expect.isNotNull(sendSet = node.asSendSet());
+ testNode(sendSet.receiver, "a", text);
+ testNode(sendSet.selector, "b", text);
+ Expect.isNotNull(sendSet = sendSet.arguments.head.asSendSet());
+ testNode(sendSet.receiver, "c", text);
+ testNode(sendSet.selector, "d", text);
+ testNode(sendSet.arguments.head, "e.f", text);
+}
+
+void testIndex() {
+ Expression node;
+ Send send;
+ SendSet sendSet;
+ String text;
+
+ text = "a[b]";
+ node = testExpression(text);
+ // Should parse as: (a)[b].
+ Expect.isNotNull(send = node.asSend());
+ testNode(send.receiver, "a", text);
+ //
+ //testNode(send.selector, "[b]", text);
+ testNode(send.arguments.head, "b", text);
+
+ text = "a[b] = c";
+ node = testExpression(text);
+ // Should parse as: (a)[b] = c.
+ Expect.isNotNull(sendSet = node.asSendSet());
+ testNode(sendSet.receiver, "a", text);
+ testNode(sendSet.selector, "[]", text, false); // Operator token is synthetic.
+ testNode(sendSet.assignmentOperator, "=", text);
+ testNode(sendSet.arguments.head, "b", text);
+ testNode(sendSet.arguments.tail.head, "c", text);
+
+ text = "a.b[c]";
+ node = testExpression(text);
+ // Should parse as: (a.b)[c].
+ Expect.isNotNull(send = node.asSend());
+ testNode(send.receiver, "a.b", text);
+ testNode(send.selector, "[]", text, false); // Operator token is synthetic.
+ testNode(send.arguments.head, "c", text);
+
+ text = "a.b[c] = d";
+ node = testExpression(text);
+ // Should parse as: (a.b)[] = (c, d).
+ Expect.isNotNull(sendSet = node.asSendSet());
+ Expect.isNotNull(send = sendSet.receiver.asSend());
+ testNode(send, "a.b", text);
+ testNode(sendSet.selector, "[]", text, false); // Operator token is synthetic.
+ testNode(sendSet.assignmentOperator, "=", text);
+ testNode(sendSet.arguments.head, "c", text);
+ testNode(sendSet.arguments.tail.head, "d", text);
+}
+
+void testPostfix() {
+ Expression node;
+ SendSet sendSet;
+ String text;
+
+ text = "a.b++";
Lasse Reichstein Nielsen 2012/06/28 07:45:14 How about testing a[b]++ / ++a[b] too?
+ node = testExpression(text);
+ // Should parse as: (a.b)++.
+ Expect.isNotNull(sendSet = node.asSendSet());
+ testNode(sendSet.receiver, "a", text);
+ testNode(sendSet.selector, "b", text);
+ testNode(sendSet.assignmentOperator, "++", text);
+ Expect.isTrue(sendSet.arguments.isEmpty());
+}
+
+void main() {
+ testUnaryExpression();
+ testAssignment();
+ testIndex();
+ testPostfix();
+}

Powered by Google App Engine
This is Rietveld 408576698