| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library parser_test; | 5 library parser_test; |
| 6 | 6 |
| 7 import 'package:polymer_expressions/parser.dart'; | 7 import 'package:polymer_expressions/parser.dart'; |
| 8 import 'package:polymer_expressions/expression.dart'; | 8 import 'package:polymer_expressions/expression.dart'; |
| 9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
| 10 | 10 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 '>=', '<=', '||', '&&', '&']; | 57 '>=', '<=', '||', '&&', '&']; |
| 58 for (var op in operators) { | 58 for (var op in operators) { |
| 59 expectParse('a $op b', binary(ident('a'), op, ident('b'))); | 59 expectParse('a $op b', binary(ident('a'), op, ident('b'))); |
| 60 expectParse('1 $op 2', binary(literal(1), op, literal(2))); | 60 expectParse('1 $op 2', binary(literal(1), op, literal(2))); |
| 61 expectParse('this $op null', binary(ident('this'), op, literal(null))); | 61 expectParse('this $op null', binary(ident('this'), op, literal(null))); |
| 62 } | 62 } |
| 63 }); | 63 }); |
| 64 | 64 |
| 65 test('should give multiply higher associativity than plus', () { | 65 test('should give multiply higher associativity than plus', () { |
| 66 expectParse('a + b * c', | 66 expectParse('a + b * c', |
| 67 binary( | 67 binary(ident('a'), '+', binary(ident('b'), '*', ident('c')))); |
| 68 ident('a'), | 68 expectParse('a * b + c', |
| 69 '+', | 69 binary(binary(ident('a'), '*', ident('b')), '+', ident('c'))); |
| 70 binary(ident('b'), '*', ident('c')))); | |
| 71 }); | 70 }); |
| 72 | 71 |
| 73 test('should parse a dot operator', () { | 72 test('should parse a dot operator', () { |
| 74 expectParse('a.b', getter(ident('a'), 'b')); | 73 expectParse('a.b', getter(ident('a'), 'b')); |
| 75 }); | 74 }); |
| 76 | 75 |
| 77 test('should parse chained dot operators', () { | 76 test('should parse chained dot operators', () { |
| 78 expectParse('a.b.c', getter(getter(ident('a'), 'b'), 'c')); | 77 expectParse('a.b.c', getter(getter(ident('a'), 'b'), 'c')); |
| 79 }); | 78 }); |
| 80 | 79 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 expectParse('a.a ? b.a : c.a', ternary(getter(ident('a'), 'a'), | 156 expectParse('a.a ? b.a : c.a', ternary(getter(ident('a'), 'a'), |
| 158 getter(ident('b'), 'a'), getter(ident('c'), 'a'))); | 157 getter(ident('b'), 'a'), getter(ident('c'), 'a'))); |
| 159 expect(() => parse('a + 1 ? b + 1 :: c.d + 3'), throws); | 158 expect(() => parse('a + 1 ? b + 1 :: c.d + 3'), throws); |
| 160 }); | 159 }); |
| 161 | 160 |
| 162 test('should parse a filter chain', () { | 161 test('should parse a filter chain', () { |
| 163 expectParse('a | b | c', binary(binary(ident('a'), '|', ident('b')), | 162 expectParse('a | b | c', binary(binary(ident('a'), '|', ident('b')), |
| 164 '|', ident('c'))); | 163 '|', ident('c'))); |
| 165 }); | 164 }); |
| 166 | 165 |
| 167 test('should parse comprehension', () { | 166 test('should parse "in" expressions', () { |
| 168 expectParse('a in b', inExpr(ident('a'), ident('b'))); | 167 expectParse('a in b', inExpr(ident('a'), ident('b'))); |
| 169 expectParse('a in b.c', | 168 expectParse('a in b.c', |
| 170 inExpr(ident('a'), getter(ident('b'), 'c'))); | 169 inExpr(ident('a'), getter(ident('b'), 'c'))); |
| 171 expectParse('a in b + c', | 170 expectParse('a in b + c', |
| 172 inExpr(ident('a'), binary(ident('b'), '+', ident('c')))); | 171 inExpr(ident('a'), binary(ident('b'), '+', ident('c')))); |
| 173 }); | 172 }); |
| 174 | 173 |
| 175 test('should reject comprehension with non-assignable left expression', () { | 174 test('should reject comprehension with non-assignable left expression', () { |
| 176 expect(() => parse('a + 1 in b'), throwsException); | 175 expect(() => parse('a + 1 in b'), throwsException); |
| 177 }); | 176 }); |
| 178 | 177 |
| 178 test('should parse "as" expressions', () { |
| 179 expectParse('a as b', asExpr(ident('a'), ident('b'))); |
| 180 }); |
| 181 |
| 179 test('should reject keywords as identifiers', () { | 182 test('should reject keywords as identifiers', () { |
| 180 expect(() => parse('a.in'), throwsException); | 183 expect(() => parse('a.in'), throwsException); |
| 181 }); | 184 }); |
| 182 | 185 |
| 183 test('should parse map literals', () { | 186 test('should parse map literals', () { |
| 184 expectParse("{'a': 1}", | 187 expectParse("{'a': 1}", |
| 185 mapLiteral([mapLiteralEntry(literal('a'), literal(1))])); | 188 mapLiteral([mapLiteralEntry(literal('a'), literal(1))])); |
| 186 expectParse("{'a': 1, 'b': 2 + 3}", | 189 expectParse("{'a': 1, 'b': 2 + 3}", |
| 187 mapLiteral([ | 190 mapLiteral([ |
| 188 mapLiteralEntry(literal('a'), literal(1)), | 191 mapLiteralEntry(literal('a'), literal(1)), |
| (...skipping 15 matching lines...) Expand all Loading... |
| 204 | 207 |
| 205 test('should parse list literals', () { | 208 test('should parse list literals', () { |
| 206 expectParse('[1, "a", b]', | 209 expectParse('[1, "a", b]', |
| 207 listLiteral([literal(1), literal('a'), ident('b')])); | 210 listLiteral([literal(1), literal('a'), ident('b')])); |
| 208 expectParse('[[1, 2], [3, 4]]', | 211 expectParse('[[1, 2], [3, 4]]', |
| 209 listLiteral([listLiteral([literal(1), literal(2)]), | 212 listLiteral([listLiteral([literal(1), literal(2)]), |
| 210 listLiteral([literal(3), literal(4)])])); | 213 listLiteral([literal(3), literal(4)])])); |
| 211 }); | 214 }); |
| 212 }); | 215 }); |
| 213 } | 216 } |
| OLD | NEW |