| 1 |
/* |
| 2 |
Copyright 2015 Google Inc. All rights reserved. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
std.assertEqual({}, {}) && |
| 18 |
std.assertEqual({ x: 1 }, { x: 1 }) && |
| 19 |
std.assertEqual({ '': 1 }, { '': 1 }) && |
| 20 |
std.assertEqual({ x: 1, y: 2 }.y, 2) && |
| 21 |
std.assertEqual({ x: 1, y: 2 }.y, 2) && |
| 22 |
std.assertEqual({ x: error 'foo', y: 2 }.y, 2) && |
| 23 |
std.assertEqual({ x: 1, y: 2 } + { z: 3 }, { x: 1, y: 2, z: 3 }) && |
| 24 |
std.assertEqual({ x: 1, y: 2 } + { y: 3 }, { x: 1, y: 3 }) && |
| 25 |
std.assertEqual({ x: 1, y: 2 } + { y+: 3 }, { x: 1, y: 5 }) && |
| 26 |
std.assertEqual({ a: { x: 1 }, b: { x: 2 } } + { a+: { y: 3 }, b: { x: 3 } }, { a: { x: 1, y: 3 }, b: { x: 3 } }) && |
| 27 |
|
| 28 |
std.assertEqual({ x: 1, y: 2 } == { x: 1, y: 2 }, true) && |
| 29 |
std.assertEqual({ x: 1, y: 2 } == { x: 1, y: 2, z: 3 }, false) && |
| 30 |
std.assertEqual({ x: 1, y: 2 } != { x: 1, y: 2 }, false) && |
| 31 |
std.assertEqual({ x: 1, y: 2 } != { x: 1, y: 2, z: 3 }, true) && |
| 32 |
|
| 33 |
std.assertEqual({ f(x, y, z): x, y: self.f(1, 2, 3) }.y, 1) && |
| 34 |
std.assertEqual({ f(x, y, z):: x, y(x): self.f(x, 2, 3), z: self.y(4) }.z, 4) && |
| 35 |
|
| 36 |
// Object Local |
| 37 |
|
| 38 |
std.assertEqual({ local foo = 3, local bar(n) = foo + n, x: foo, y: foo + bar(1) }, { x: 3, y: 7 }) && |
| 39 |
std.assertEqual({ local foo = bar(3)[1], local bar(n) = [foo, n], x: foo, y: [foo] + bar(3) }, { x: 3, y: [3, 3, 3] }) && |
| 40 |
|
| 41 |
|
| 42 |
// Computed field name |
| 43 |
std.assertEqual(local f = 'x'; { [f]: 1 }, { x: 1 }) && |
| 44 |
std.assertEqual({ [pair[0]]: pair[1] for pair in [['x', 1], ['y', 2]] }, { x: 1, y: 2 }) && |
| 45 |
std.assertEqual(local t = { a: 1, b: 2, c: null, d: 4 }; { [x]: t[x] for x in [y for y in ['a', 'b', 'c', 'd'] if t[y] != null] }, { a: 1, b: 2, d: 4 }) && |
| 46 |
|
| 47 |
// Comprehension object |
| 48 |
std.assertEqual(local x = 10; { ['' + x]: x for x in [x, x + 1, x + 2] }, { '10': 10, '11': 11, '12': 12 }) && |
| 49 |
std.assertEqual(local x = 'foo'; { local x = 'bar', [x]: 1 }, { foo: 1 }) && |
| 50 |
std.assertEqual({ [x + '']: if x == 1 then 1 else x + $['1'] for x in [1, 2, 3] }, { '1': 1, '2': 3, '3': 4 }) && |
| 51 |
|
| 52 |
std.assertEqual(local x = 'baz'; { local x = 'bar', [x]: x for x in ['foo'] }, { foo: 'bar' }) && |
| 53 |
|
| 54 |
|
| 55 |
std.assertEqual({ f: 'foo', g: { [self.f]: 7 } }, { f: 'foo', g: { foo: 7 } }) && |
| 56 |
|
| 57 |
std.assertEqual({ [k]: null for k in [] }, {}) && |
| 58 |
std.assertEqual({ [null]: 'test' for k in [1] }, {}) && |
| 59 |
std.assertEqual({ [null]: 'test' }, {}) && |
| 60 |
|
| 61 |
std.assertEqual({ ['' + k]: k for k in [1, 2, 3] }, { '1': 1, '2': 2, '3': 3 }) && |
| 62 |
std.assertEqual({ ['' + (k + 1)]: (k + 1) for k in [0, 1, 2] }, { ['' + k]: k for k in [1, 2, 3] }) && |
| 63 |
std.assertEqual({ ['' + k]: k for k in [1, 2, 3] }, { '1': 1, '2': 2, '3': 3 }) && |
| 64 |
std.assertEqual({ [x + '']: x + foo, local foo = 3 for x in [1, 2, 3] }, { '1': 4, '2': 5, '3': 6 }) && |
| 65 |
|
| 66 |
// Test for #791 |
| 67 |
std.assertEqual({ [x]: true for x in ['\\k'] }, { '\\k': true }) && |
| 68 |
|
| 69 |
|
| 70 |
local obj = { |
| 71 |
f14true: { x: 1, y: 4, z: true }, |
| 72 |
f14false: { x: 1, y: 4, z: false }, |
| 73 |
f16true: { x: 1, y: 6, z: true }, |
| 74 |
f16false: { x: 1, y: 6, z: false }, |
| 75 |
f26true: { x: 2, y: 6, z: true }, |
| 76 |
f26false: { x: 2, y: 6, z: false }, |
| 77 |
f36true: { x: 3, y: 6, z: true }, |
| 78 |
f36false: { x: 3, y: 6, z: false }, |
| 79 |
}; |
| 80 |
|
| 81 |
std.assertEqual(obj, { ['f' + x + y + z]: { x: x, y: y, z: z } for x in [1, 2, 3] for y in [1, 4, 6] if x + 2 < y for z in [true, false] }) && |
| 82 |
|
| 83 |
// Tests for #1111 - object comprehension fields should use "inherit" visibility. |
| 84 |
std.assertEqual(std.objectFields({ x: 1 } + { [k]: 2 for k in ['x'] }), ['x']) && |
| 85 |
std.assertEqual(std.objectFields({ x:: 1 } + { [k]: 2 for k in ['x'] }), []) && |
| 86 |
std.assertEqual(std.objectFields({ x::: 1 } + { [k]: 2 for k in ['x'] }), ['x']) && |
| 87 |
|
| 88 |
std.assertEqual({ f: { foo: 7, bar: 1 } { [self.name]+: 3, name:: 'foo' }, name:: 'bar' }, |
| 89 |
{ f: { foo: 7, bar: 4 } }) && |
| 90 |
|
| 91 |
std.assertEqual({ name:: 'supername' } { name:: 'selfname', f: { wrongname: 7, supername: 1, name:: 'wrongname' } { [super.name]+: 3 } }, |
| 92 |
{ f: { wrongname: 7, supername: 4 } }) && |
| 93 |
|
| 94 |
std.assertEqual({} + { f+: 3 }, { f: 3 }) && |
| 95 |
std.assertEqual({} + { f+: { g+: 'foo' } }, { f: { g: 'foo' } }) && |
| 96 |
std.assertEqual({} + { f+: [3] }, { f: [3] }) && |
| 97 |
|
| 98 |
std.assertEqual({ f+: 3 }, { f: 3 }) && |
| 99 |
std.assertEqual({ f+: { g+: 'foo' } }, { f: { g: 'foo' } }) && |
| 100 |
std.assertEqual({ f+: [3] }, { f: [3] }) && |
| 101 |
|
| 102 |
// Ensure that e in super is handled correctly during the +: desugaring, because it moves |
| 103 |
// into a different object scope: |
| 104 |
std.assertEqual( |
| 105 |
{ opt:: true, f: { y: 5 } } + { f+: { [if 'opt' in super then 'x' else 'y']+: 3 } }, |
| 106 |
{ f: { x: 3, y: 5 } } |
| 107 |
) && |
| 108 |
|
| 109 |
std.assertEqual({ x: 1 } + { a: 'x' in super, b: 'y' in super }, { x: 1, a: true, b: false }) && |
| 110 |
std.assertEqual({ x:: 1 } + { a: 'x' in super, b: 'y' in super }, { a: true, b: false }) && |
| 111 |
|
| 112 |
std.assertEqual('x' in { x: 3 }, true) && |
| 113 |
std.assertEqual('x' in { y: 3 }, false) && |
| 114 |
|
| 115 |
std.assertEqual({ x: 1, a: 'x' in self, b: 'y' in self }, { x: 1, a: true, b: false }) && |
| 116 |
std.assertEqual({ x:: 1, a: 'x' in self, b: 'y' in self }, { a: true, b: false }) && |
| 117 |
std.assertEqual({ f: 'f' in self }, { f: true }) && |
| 118 |
|
| 119 |
true |