| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library parser_test; |
| 6 |
| 7 import 'package:polymer_expression/parser.dart'; |
| 8 import 'package:polymer_expression/expression.dart'; |
| 9 import 'package:unittest/unittest.dart'; |
| 10 |
| 11 expectParse(String s, Expression e) => |
| 12 expect(new Parser(s).parse(), e, reason: s); |
| 13 |
| 14 main() { |
| 15 |
| 16 group('parser', () { |
| 17 |
| 18 test('should parse an empty expression', () { |
| 19 expectParse('', empty()); |
| 20 }); |
| 21 |
| 22 test('should parse an identifier', () { |
| 23 expectParse('abc', ident('abc')); |
| 24 }); |
| 25 |
| 26 test('should parse a string literal', () { |
| 27 expectParse('"abc"', literal('abc')); |
| 28 }); |
| 29 |
| 30 test('should parse an integer literal', () { |
| 31 expectParse('123', literal(123)); |
| 32 }); |
| 33 |
| 34 test('should parse a double literal', () { |
| 35 expectParse('1.23', literal(1.23)); |
| 36 }); |
| 37 |
| 38 test('should parse a positive double literal', () { |
| 39 expectParse('+1.23', literal(1.23)); |
| 40 }); |
| 41 |
| 42 test('should parse a negative double literal', () { |
| 43 expectParse('-1.23', literal(-1.23)); |
| 44 }); |
| 45 |
| 46 test('should parse a plus operator with literals', () { |
| 47 expectParse('1 + 2', binary(literal(1), '+', literal(2))); |
| 48 }); |
| 49 |
| 50 test('should parse binary operators', () { |
| 51 expectParse('a && b', binary(ident('a'), '&&', ident('b'))); |
| 52 expectParse('1 && 2', binary(literal(1), '&&', literal(2))); |
| 53 expectParse('false && true', binary(literal(false), '&&', literal(true))); |
| 54 expectParse('false || true', binary(literal(false), '||', literal(true))); |
| 55 }); |
| 56 |
| 57 test('should give multiply higher associativity than plus', () { |
| 58 expectParse('a + b * c', |
| 59 binary( |
| 60 ident('a'), |
| 61 '+', |
| 62 binary(ident('b'), '*', ident('c')))); |
| 63 }); |
| 64 |
| 65 test('should give multiply higher associativity than plus 2', () { |
| 66 expectParse('a * b + c', |
| 67 binary( |
| 68 binary(ident('a'), '*', ident('b')), |
| 69 '+', |
| 70 ident('c'))); |
| 71 }); |
| 72 |
| 73 test('should parse a dot operator', () { |
| 74 expectParse('a.b', invoke(ident('a'), 'b')); |
| 75 }); |
| 76 |
| 77 test('should parse chained dot operators', () { |
| 78 expectParse('a.b.c', invoke(invoke(ident('a'), 'b'), 'c')); |
| 79 }); |
| 80 |
| 81 test('should give dot high associativity', () { |
| 82 expectParse('a * b.c', binary(ident('a'), '*', invoke(ident('b'), 'c'))); |
| 83 }); |
| 84 |
| 85 test('should parse a function with no arguments', () { |
| 86 expectParse('a()', invoke(ident('a'), null, [])); |
| 87 }); |
| 88 |
| 89 test('should parse a single function argument', () { |
| 90 expectParse('a(b)', invoke(ident('a'), null, [ident('b')])); |
| 91 }); |
| 92 |
| 93 test('should parse a function call as a subexpression', () { |
| 94 expectParse('a() + 1', |
| 95 binary( |
| 96 invoke(ident('a'), null, []), |
| 97 '+', |
| 98 literal(1))); |
| 99 }); |
| 100 |
| 101 test('should parse multiple function arguments', () { |
| 102 expectParse('a(b, c)', |
| 103 invoke(ident('a'), null, [ident('b'), ident('c')])); |
| 104 }); |
| 105 |
| 106 test('should parse nested function calls', () { |
| 107 expectParse('a(b(c))', invoke(ident('a'), null, [ |
| 108 invoke(ident('b'), null, [ident('c')])])); |
| 109 }); |
| 110 |
| 111 test('should parse an empty method call', () { |
| 112 expectParse('a.b()', invoke(ident('a'), 'b', [])); |
| 113 }); |
| 114 |
| 115 test('should parse a method call with a single argument', () { |
| 116 expectParse('a.b(c)', invoke(ident('a'), 'b', [ident('c')])); |
| 117 }); |
| 118 |
| 119 test('should parse a method call with multiple arguments', () { |
| 120 expectParse('a.b(c, d)', |
| 121 invoke(ident('a'), 'b', [ident('c'), ident('d')])); |
| 122 }); |
| 123 |
| 124 test('should parse chained method calls', () { |
| 125 expectParse('a.b().c()', invoke(invoke(ident('a'), 'b', []), 'c', [])); |
| 126 }); |
| 127 |
| 128 test('should parse chained function calls', () { |
| 129 expectParse('a()()', invoke(invoke(ident('a'), null, []), null, [])); |
| 130 }); |
| 131 |
| 132 test('should parse parenthesized expression', () { |
| 133 expectParse('(a)', paren(ident('a'))); |
| 134 expectParse('(( 3 * ((1 + 2)) ))', paren(paren( |
| 135 binary(literal(3), '*', paren(paren( |
| 136 binary(literal(1), '+', literal(2)))))))); |
| 137 }); |
| 138 |
| 139 test('should parse an index operator', () { |
| 140 expectParse('a[b]', invoke(ident('a'), '[]', [ident('b')])); |
| 141 expectParse('a.b[c]', invoke(invoke(ident('a'), 'b', null), |
| 142 '[]', [ident('c')])); |
| 143 }); |
| 144 |
| 145 test('should parse chained index operators', () { |
| 146 expectParse('a[][]', invoke(invoke(ident('a'), '[]', []), '[]', [])); |
| 147 }); |
| 148 |
| 149 test('should parse multiple index operators', () { |
| 150 expectParse('a[b] + c[d]', binary( |
| 151 invoke(ident('a'), '[]', [ident('b')]), |
| 152 '+', |
| 153 invoke(ident('c'), '[]', [ident('d')]))); |
| 154 }); |
| 155 |
| 156 test('should parse a filter chain', () { |
| 157 expectParse('a | b | c', binary(binary(ident('a'), '|', ident('b')), |
| 158 '|', ident('c'))); |
| 159 }); |
| 160 |
| 161 test('should parse comprehension', () { |
| 162 expectParse('a in b', inExpr(ident('a'), ident('b'))); |
| 163 expectParse('a in b.c', |
| 164 inExpr(ident('a'), invoke(ident('b'), 'c', null))); |
| 165 expectParse('a in b + c', |
| 166 inExpr(ident('a'), binary(ident('b'), '+', ident('c')))); |
| 167 }); |
| 168 |
| 169 test('should reject comprehension with non-assignable left expression', () { |
| 170 expect(() => parse('a + 1 in b'), throwsException); |
| 171 }); |
| 172 |
| 173 test('should reject keywords as identifiers', () { |
| 174 expect(() => parse('a.in'), throwsException); |
| 175 }); |
| 176 |
| 177 test('should parse map literals', () { |
| 178 expectParse("{'a': 1}", |
| 179 mapLiteral([mapLiteralEntry(literal('a'), literal(1))])); |
| 180 expectParse("{'a': 1, 'b': 2 + 3}", |
| 181 mapLiteral([ |
| 182 mapLiteralEntry(literal('a'), literal(1)), |
| 183 mapLiteralEntry(literal('b'), |
| 184 binary(literal(2), '+', literal(3)))])); |
| 185 expectParse("{'a': foo()}", |
| 186 mapLiteral([mapLiteralEntry( |
| 187 literal('a'), invoke(ident('foo'), null, []))])); |
| 188 expectParse("{'a': foo('a')}", |
| 189 mapLiteral([mapLiteralEntry( |
| 190 literal('a'), invoke(ident('foo'), null, [literal('a')]))])); |
| 191 }); |
| 192 |
| 193 test('should parse map literals with method calls', () { |
| 194 expectParse("{'a': 1}.length", |
| 195 invoke(mapLiteral([mapLiteralEntry(literal('a'), literal(1))]), |
| 196 'length')); |
| 197 }); |
| 198 }); |
| 199 } |
| OLD | NEW |