| 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 |
// basic self |
| 18 |
std.assertEqual({ x: 1, y: self.x }.y, 1) && |
| 19 |
std.assertEqual(({ x: 1, y: self.x } + {}).y, 1) && |
| 20 |
|
| 21 |
// self as object |
| 22 |
std.assertEqual({ x: 1, y: self }.y.y.x, 1) && |
| 23 |
std.assertEqual(({ x: 1, y: self } + {}).y.y.x, 1) && |
| 24 |
|
| 25 |
// self bound correctly in superobjects |
| 26 |
std.assertEqual(({ x: 1, y: self.x } + { x: 2 }).y, 2) && |
| 27 |
|
| 28 |
// basic super |
| 29 |
std.assertEqual(({ x: 1, y: self.x } + { x: 2, y: super.y + 1, z: super.y }), { x: 2, y: 3, z: 2 }) && |
| 30 |
|
| 31 |
// self bound correctly in super objects when calling via super |
| 32 |
std.assertEqual(({ x: self.x } + { x: 2, y: super.x }).y, 2) && |
| 33 |
|
| 34 |
// super up 2 levels (note + is left associative) |
| 35 |
std.assertEqual({ x: 1 } + { x: 2, y: super.x } + { x: 3, z: super.x }, { x: 3, y: 1, z: 2 }) && |
| 36 |
|
| 37 |
// variables bound statically in superobjects |
| 38 |
std.assertEqual((local z = 3; { x: z }) + (local z = 4; { x: z, y: super.x }), { x: 4, y: 3 }) && |
| 39 |
|
| 40 |
// static binding in superobject |
| 41 |
std.assertEqual(local A = { a1: 1, a2: self.a1, a3: A.a2 }; |
| 42 |
A { a1: 2, a2: super.a2 + 1, b1: super.a2, b2: super.a3 }, { a1: 2, a2: 3, a3: 1, b1: 2, b2: 1 }) && |
| 43 |
|
| 44 |
|
| 45 |
// multiple inheritance |
| 46 |
std.assertEqual({ x: 1, y: self.x } + ({ x: 2 } + { x: 3, y: 5, z: super.y }), { x: 3, y: 5, z: 3 }) && |
| 47 |
|
| 48 |
// multiple inheritance of empty middle |
| 49 |
|
| 50 |
std.assertEqual({ x: 1 } + ({} + { y: super.x }), { x: 1, y: 1 }) && |
| 51 |
|
| 52 |
|
| 53 |
// grandparents with super visiting all leaves |
| 54 |
local A = { name: 'A' }, |
| 55 |
B = { name: 'B', sB: super.name }, |
| 56 |
C = { name: 'C', sC: super.name }, |
| 57 |
D = { name: 'D', sD: super.name }; |
| 58 |
|
| 59 |
std.assertEqual((A + B) + (C + D), { name: 'D', sB: 'A', sC: 'B', sD: 'C' }) && |
| 60 |
|
| 61 |
// Outer variable |
| 62 |
local a = { |
| 63 |
d: 0, |
| 64 |
local outer = self, |
| 65 |
b: { c: outer.d + 1, c2: a.d + 1 }, |
| 66 |
}; |
| 67 |
|
| 68 |
local e = a.b { d: 4 }; |
| 69 |
local e2 = (a { d: 4 }).b; |
| 70 |
|
| 71 |
std.assertEqual(e.c, 1) && |
| 72 |
std.assertEqual(e.c2, 1) && |
| 73 |
std.assertEqual(e2.c, 5) && |
| 74 |
std.assertEqual(e2.c2, 1) && |
| 75 |
|
| 76 |
// $ is late-bound |
| 77 |
std.assertEqual(({ x: 1, y: { a: $.x } } + { x: 2 }).y.a, 2) && |
| 78 |
|
| 79 |
// DAG |
| 80 |
std.assertEqual({ x: 1, y: 2 } + (local A = { x: super.y, y: super.x }; A + A), { x: 1, y: 2 }) && |
| 81 |
|
| 82 |
|
| 83 |
// Object composition: inheritance |
| 84 |
std.assertEqual(local f = 'x'; { x: 2 } + { [f]: 1 }, { x: 1 }) && |
| 85 |
// more object composition |
| 86 |
std.assertEqual({ x: 3, z: 4 } + { [pair[0]]: pair[1] for pair in [['x', 1], ['y', 2]] }, { x: 1, y: 2, z: 4 }) && |
| 87 |
// more inheritance and object composition |
| 88 |
std.assertEqual({} + { [k]: k + '_' for k in ['x', 'y', 'foo', 'foobar'] }, { x: 'x_', y: 'y_', foo: 'foo_', foobar: 'foobar_' }) && |
| 89 |
true |