| OLD | NEW |
| (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/tree/tree.dart"); | |
| 7 | |
| 8 void testStatement(String statement) { | |
| 9 Node node = parseStatement(statement); | |
| 10 Expect.isNotNull(node.toString()); | |
| 11 } | |
| 12 | |
| 13 void testGenericTypes() { | |
| 14 testStatement('List<T> t;'); | |
| 15 testStatement('List<List<T>> t;'); | |
| 16 testStatement('List<List<List<T>>> t;'); | |
| 17 testStatement('List<List<List<List<T>>>> t;'); | |
| 18 testStatement('List<List<List<List<List<T>>>>> t;'); | |
| 19 | |
| 20 testStatement('List<List<T> > t;'); | |
| 21 testStatement('List<List<List<T> >> t;'); | |
| 22 testStatement('List<List<List<List<T> >>> t;'); | |
| 23 testStatement('List<List<List<List<List<T> >>>> t;'); | |
| 24 | |
| 25 testStatement('List<List<List<T> > > t;'); | |
| 26 testStatement('List<List<List<List<T> > >> t;'); | |
| 27 testStatement('List<List<List<List<List<T> > >>> t;'); | |
| 28 | |
| 29 testStatement('List<List<List<List<T> > > > t;'); | |
| 30 testStatement('List<List<List<List<List<T> > > >> t;'); | |
| 31 | |
| 32 testStatement('List<List<List<List<List<T> > > > > t;'); | |
| 33 | |
| 34 testStatement('List<List<List<List<List<T> >> >> t;'); | |
| 35 | |
| 36 testStatement('List<List<List<List<List<T> >>> > t;'); | |
| 37 | |
| 38 testStatement('List<List<List<List<List<T >>> >> t;'); | |
| 39 | |
| 40 testStatement('List<T> t;'); | |
| 41 testStatement('List<List<T>> t;'); | |
| 42 testStatement('List<List<List<T>>> t;'); | |
| 43 testStatement('List<List<List<List<T>>>> t;'); | |
| 44 testStatement('List<List<List<List<List<T>>>>> t;'); | |
| 45 } | |
| 46 | |
| 47 void testPrefixedGenericTypes() { | |
| 48 testStatement('lib.List<List<T> > t;'); | |
| 49 testStatement('lib.List<List<List<T> >> t;'); | |
| 50 testStatement('lib.List<List<List<List<T> >>> t;'); | |
| 51 testStatement('lib.List<List<List<List<List<T> >>>> t;'); | |
| 52 | |
| 53 testStatement('lib.List<List<List<T> > > t;'); | |
| 54 testStatement('lib.List<List<List<List<T> > >> t;'); | |
| 55 testStatement('lib.List<List<List<List<List<T> > >>> t;'); | |
| 56 | |
| 57 testStatement('lib.List<List<List<List<T> > > > t;'); | |
| 58 testStatement('lib.List<List<List<List<List<T> > > >> t;'); | |
| 59 | |
| 60 testStatement('lib.List<List<List<List<List<T> > > > > t;'); | |
| 61 | |
| 62 testStatement('lib.List<List<List<List<List<T> >> >> t;'); | |
| 63 | |
| 64 testStatement('lib.List<List<List<List<List<T> >>> > t;'); | |
| 65 | |
| 66 testStatement('lib.List<List<List<List<List<T >>> >> t;'); | |
| 67 } | |
| 68 | |
| 69 void testUnaryExpression() { | |
| 70 testStatement('x++;'); | |
| 71 // TODO(ahe): reenable following test. | |
| 72 // testStatement('++x++;'); | |
| 73 testStatement('++x;'); | |
| 74 testStatement('print(x++);'); | |
| 75 // TODO(ahe): reenable following test. | |
| 76 // testStatement('print(++x++);'); // Accepted by parser, rejected later. | |
| 77 testStatement('print(++x);'); | |
| 78 } | |
| 79 | |
| 80 void testChainedMethodCalls() { | |
| 81 testStatement('MyClass.foo().bar().baz();'); | |
| 82 // TODO(ahe): reenable following test. | |
| 83 // testStatement('MyClass.foo().-x;'); // Accepted by parser, rejected later. | |
| 84 testStatement('a.b.c.d();'); | |
| 85 } | |
| 86 | |
| 87 void testFunctionStatement() { | |
| 88 testStatement('int f() {}'); | |
| 89 testStatement('void f() {}'); | |
| 90 } | |
| 91 | |
| 92 void testDoStatement() { | |
| 93 testStatement('do fisk(); while (hest());'); | |
| 94 testStatement('do { fisk(); } while (hest());'); | |
| 95 } | |
| 96 | |
| 97 void testWhileStatement() { | |
| 98 testStatement('while (fisk()) hest();'); | |
| 99 testStatement('while (fisk()) { hest(); }'); | |
| 100 } | |
| 101 | |
| 102 void testConditionalExpression() { | |
| 103 ExpressionStatement node = parseStatement("a ? b : c;"); | |
| 104 Conditional conditional = node.expression; | |
| 105 | |
| 106 node = parseStatement("a ? b ? c : d : e;"); | |
| 107 // Should parse as: a ? ( b ? c : d ) : e. | |
| 108 conditional = node.expression; | |
| 109 Expect.isNotNull(conditional.thenExpression.asConditional()); | |
| 110 Expect.isNotNull(conditional.elseExpression.asSend()); | |
| 111 | |
| 112 node = parseStatement("a ? b : c ? d : e;"); | |
| 113 // Should parse as: a ? b : (c ? d : e). | |
| 114 conditional = node.expression; | |
| 115 Expect.isNotNull(conditional.thenExpression.asSend()); | |
| 116 Expect.isNotNull(conditional.elseExpression.asConditional()); | |
| 117 | |
| 118 node = parseStatement("a ? b ? c : d : e ? f : g;"); | |
| 119 // Should parse as: a ? (b ? c : d) : (e ? f : g). | |
| 120 conditional = node.expression; | |
| 121 Expect.isNotNull(conditional.thenExpression.asConditional()); | |
| 122 Expect.isNotNull(conditional.elseExpression.asConditional()); | |
| 123 | |
| 124 node = parseStatement("a = b ? c : d;"); | |
| 125 // Should parse as: a = (b ? c : d). | |
| 126 SendSet sendSet = node.expression; | |
| 127 Expect.isNotNull(sendSet.arguments.head.asConditional()); | |
| 128 | |
| 129 node = parseStatement("a ? b : c = d;"); | |
| 130 // Should parse as: a ? b : (c = d). | |
| 131 conditional = node.expression; | |
| 132 Expect.isNull(conditional.thenExpression.asSendSet()); | |
| 133 Expect.isNotNull(conditional.elseExpression.asSendSet()); | |
| 134 | |
| 135 node = parseStatement("a ? b = c : d;"); | |
| 136 // Should parse as: a ? (b = c) : d. | |
| 137 conditional = node.expression; | |
| 138 Expect.isNotNull(conditional.thenExpression.asSendSet()); | |
| 139 Expect.isNull(conditional.elseExpression.asSendSet()); | |
| 140 | |
| 141 node = parseStatement("a ? b = c : d = e;"); | |
| 142 // Should parse as: a ? (b = c) : (d = e). | |
| 143 conditional = node.expression; | |
| 144 Expect.isNotNull(conditional.thenExpression.asSendSet()); | |
| 145 Expect.isNotNull(conditional.elseExpression.asSendSet()); | |
| 146 } | |
| 147 | |
| 148 void testAssignment() { | |
| 149 ExpressionStatement node; | |
| 150 Expression expression; | |
| 151 SendSet sendSet; | |
| 152 | |
| 153 node = parseStatement("a = b;"); | |
| 154 expression = node.expression; | |
| 155 Expect.isNotNull(expression.asSendSet()); | |
| 156 | |
| 157 node = parseStatement("a = b = c;"); | |
| 158 // Should parse as: a = (b = c). | |
| 159 expression = node.expression; | |
| 160 Expect.isNotNull(sendSet = expression.asSendSet()); | |
| 161 Expect.isNotNull(sendSet.arguments.head.asSendSet()); | |
| 162 | |
| 163 node = parseStatement("a = b = c = d;"); | |
| 164 // Should parse as: a = (b = (c = d)). | |
| 165 expression = node.expression; | |
| 166 Expect.isNotNull(sendSet = expression.asSendSet()); | |
| 167 Expect.isNotNull(sendSet = sendSet.arguments.head.asSendSet()); | |
| 168 Expect.isNotNull(sendSet = sendSet.arguments.head.asSendSet()); | |
| 169 | |
| 170 node = parseStatement("a.b = c;"); | |
| 171 // Should parse as: receiver = a, selector = b, arguments = c. | |
| 172 expression = node.expression; | |
| 173 Expect.isNotNull(sendSet = expression.asSendSet()); | |
| 174 Expect.stringEquals("a", sendSet.receiver.toString()); | |
| 175 Expect.stringEquals("b", sendSet.selector.toString()); | |
| 176 Expect.stringEquals("c", sendSet.arguments.head.toString()); | |
| 177 | |
| 178 node = parseStatement("a.b = c.d;"); | |
| 179 // Should parse as: a.b = (c.d). | |
| 180 expression = node.expression; | |
| 181 Expect.isNotNull(sendSet = expression.asSendSet()); | |
| 182 Expect.stringEquals("a", sendSet.receiver.toString()); | |
| 183 Expect.stringEquals("b", sendSet.selector.toString()); | |
| 184 Expect.stringEquals("c.d", sendSet.arguments.head.toString()); | |
| 185 | |
| 186 node = parseStatement("a.b = c.d = e.f;"); | |
| 187 // Should parse as: a.b = (c.d = (e.f)). | |
| 188 expression = node.expression; | |
| 189 Expect.isNotNull(sendSet = expression.asSendSet()); | |
| 190 Expect.stringEquals("a", sendSet.receiver.toString()); | |
| 191 Expect.stringEquals("b", sendSet.selector.toString()); | |
| 192 Expect.isNotNull(sendSet = sendSet.arguments.head.asSendSet()); | |
| 193 Expect.stringEquals("c", sendSet.receiver.toString()); | |
| 194 Expect.stringEquals("d", sendSet.selector.toString()); | |
| 195 Expect.stringEquals("e.f", sendSet.arguments.head.toString()); | |
| 196 } | |
| 197 | |
| 198 void testIndex() { | |
| 199 ExpressionStatement node; | |
| 200 Expression expression; | |
| 201 Send send; | |
| 202 SendSet sendSet; | |
| 203 | |
| 204 node = parseStatement("a[b];"); | |
| 205 // Should parse as: (a)[b]. | |
| 206 expression = node.expression; | |
| 207 Expect.isNotNull(send = expression.asSend()); | |
| 208 Expect.stringEquals("a", send.receiver.toString()); | |
| 209 Expect.stringEquals("[]", send.selector.toString()); | |
| 210 Expect.stringEquals("b", send.arguments.head.toString()); | |
| 211 | |
| 212 node = parseStatement("a[b] = c;"); | |
| 213 // Should parse as: (a)[b] = c. | |
| 214 expression = node.expression; | |
| 215 Expect.isNotNull(sendSet = expression.asSendSet()); | |
| 216 Expect.stringEquals("a", sendSet.receiver.toString()); | |
| 217 Expect.stringEquals("[]", sendSet.selector.toString()); | |
| 218 Expect.stringEquals("=", sendSet.assignmentOperator.toString()); | |
| 219 Expect.stringEquals("b", sendSet.arguments.head.toString()); | |
| 220 Expect.stringEquals("c", sendSet.arguments.tail.head.toString()); | |
| 221 | |
| 222 node = parseStatement("a.b[c];"); | |
| 223 // Should parse as: (a.b)[c]. | |
| 224 expression = node.expression; | |
| 225 Expect.isNotNull(send = expression.asSend()); | |
| 226 Expect.stringEquals("a.b", send.receiver.toString()); | |
| 227 Expect.stringEquals("[]", send.selector.toString()); | |
| 228 Expect.stringEquals("c", send.arguments.head.toString()); | |
| 229 | |
| 230 node = parseStatement("a.b[c] = d;"); | |
| 231 // Should parse as: (a.b)[] = (c, d). | |
| 232 expression = node.expression; | |
| 233 Expect.isNotNull(sendSet = expression.asSendSet()); | |
| 234 Expect.isNotNull(send = sendSet.receiver.asSend()); | |
| 235 Expect.stringEquals("a.b", send.toString()); | |
| 236 Expect.stringEquals("[]", sendSet.selector.toString()); | |
| 237 Expect.stringEquals("=", sendSet.assignmentOperator.toString()); | |
| 238 Expect.stringEquals("c", sendSet.arguments.head.toString()); | |
| 239 Expect.stringEquals("d", sendSet.arguments.tail.head.toString()); | |
| 240 } | |
| 241 | |
| 242 void testPostfix() { | |
| 243 ExpressionStatement node; | |
| 244 Expression expression; | |
| 245 SendSet sendSet; | |
| 246 | |
| 247 node = parseStatement("a.b++;"); | |
| 248 // Should parse as: (a.b)++. | |
| 249 expression = node.expression; | |
| 250 Expect.isNotNull(sendSet = expression.asSendSet()); | |
| 251 Expect.stringEquals("a", sendSet.receiver.toString()); | |
| 252 Expect.stringEquals("b", sendSet.selector.toString()); | |
| 253 Expect.stringEquals("++", sendSet.assignmentOperator.toString()); | |
| 254 Expect.isTrue(sendSet.arguments.isEmpty()); | |
| 255 } | |
| 256 | |
| 257 void main() { | |
| 258 testGenericTypes(); | |
| 259 // TODO(ahe): Enable this test when we handle library prefixes. | |
| 260 // testPrefixedGenericTypes(); | |
| 261 testUnaryExpression(); | |
| 262 testChainedMethodCalls(); | |
| 263 testFunctionStatement(); | |
| 264 testDoStatement(); | |
| 265 testWhileStatement(); | |
| 266 testConditionalExpression(); | |
| 267 testAssignment(); | |
| 268 testIndex(); | |
| 269 testPostfix(); | |
| 270 } | |
| OLD | NEW |