Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Flags: --harmony-destructuring | 5 // Flags: --harmony-destructuring |
| 6 | 6 |
| 7 (function TestObjectLiteralPattern() { | 7 (function TestObjectLiteralPattern() { |
| 8 var { x : x, y : y } = { x : 1, y : 2 }; | 8 var { x : x, y : y } = { x : 1, y : 2 }; |
| 9 assertEquals(1, x); | 9 assertEquals(1, x); |
| 10 assertEquals(2, y); | 10 assertEquals(2, y); |
| 11 | 11 |
| 12 var {z} = { z : 3 }; | 12 var {z} = { z : 3 }; |
| 13 assertEquals(3, z); | 13 assertEquals(3, z); |
| 14 | 14 |
| 15 | 15 |
| 16 var sum = 0; | 16 var sum = 0; |
| 17 for(var {z} = { z : 3 }; z != 0; z--) { | 17 for(var {z} = { z : 3 }; z != 0; z--) { |
| 18 sum += z; | 18 sum += z; |
| 19 } | 19 } |
| 20 assertEquals(6, sum); | 20 assertEquals(6, sum); |
| 21 | |
| 22 | |
| 23 var countX = 0; | |
| 24 var countY = 0; | |
| 25 var countZ = 0; | |
| 26 var o = { get x() { | |
|
arv (Not doing code reviews)
2015/05/18 13:12:53
var o = {
get x() {
...
}
};
Dmitry Lomov (no reviews)
2015/05/18 13:41:46
Done.
| |
| 27 countX++; | |
| 28 return 0; | |
| 29 }, | |
| 30 get y() { | |
| 31 countY++; | |
| 32 return { | |
| 33 get z() { countZ++; return 1; } | |
| 34 } | |
| 35 } | |
| 36 }; | |
| 37 var { x : x0, y : { z : z1 }, x : x1 } = o; | |
|
arv (Not doing code reviews)
2015/05/18 13:12:53
who would have ever thought :-)
| |
| 38 assertSame(0, x0); | |
| 39 assertSame(1, z1); | |
| 40 assertSame(0, x1); | |
| 41 assertEquals(2, countX); | |
| 42 assertEquals(1, countY); | |
| 43 assertEquals(1, countZ); | |
| 21 }()); | 44 }()); |
| 22 | 45 |
| 23 | 46 |
| 24 (function TestObjectLiteralPatternLexical() { | 47 (function TestObjectLiteralPatternLexical() { |
| 25 'use strict'; | 48 'use strict'; |
| 26 let { x : x, y : y } = { x : 1, y : 2 }; | 49 let { x : x, y : y } = { x : 1, y : 2 }; |
| 27 assertEquals(1, x); | 50 assertEquals(1, x); |
| 28 assertEquals(2, y); | 51 assertEquals(2, y); |
| 29 | 52 |
| 30 let {z} = { z : 3 }; | 53 let {z} = { z : 3 }; |
| 31 assertEquals(3, z); | 54 assertEquals(3, z); |
| 32 | 55 |
| 56 let countX = 0; | |
| 57 let countY = 0; | |
| 58 let countZ = 0; | |
| 59 let o = { get x() { | |
| 60 countX++; | |
| 61 return 0; | |
| 62 }, | |
| 63 get y() { | |
| 64 countY++; | |
| 65 return { | |
| 66 get z() { countZ++; return 1; } | |
| 67 } | |
| 68 } | |
| 69 }; | |
| 70 let { x : x0, y : { z : z1 }, x : x1 } = o; | |
| 71 assertSame(0, x0); | |
| 72 assertSame(1, z1); | |
| 73 assertSame(0, x1); | |
| 74 assertEquals(2, countX); | |
| 75 assertEquals(1, countY); | |
| 76 assertEquals(1, countZ); | |
| 77 | |
| 33 | 78 |
| 34 let sum = 0; | 79 let sum = 0; |
| 35 for(let {x, z} = { x : 0, z : 3 }; z != 0; z--) { | 80 for(let {x, z} = { x : 0, z : 3 }; z != 0; z--) { |
| 36 assertEquals(0, x); | 81 assertEquals(0, x); |
| 37 sum += z; | 82 sum += z; |
| 38 } | 83 } |
| 39 assertEquals(6, sum); | 84 assertEquals(6, sum); |
| 40 }()); | 85 }()); |
| 41 | 86 |
| 42 | 87 |
| 43 (function TestObjectLiteralPatternLexicalConst() { | 88 (function TestObjectLiteralPatternLexicalConst() { |
| 44 'use strict'; | 89 'use strict'; |
| 45 const { x : x, y : y } = { x : 1, y : 2 }; | 90 const { x : x, y : y } = { x : 1, y : 2 }; |
| 46 assertEquals(1, x); | 91 assertEquals(1, x); |
| 47 assertEquals(2, y); | 92 assertEquals(2, y); |
| 48 | 93 |
| 94 assertThrows(function() { x++; }, TypeError); | |
| 95 assertThrows(function() { y++; }, TypeError); | |
| 96 | |
| 49 const {z} = { z : 3 }; | 97 const {z} = { z : 3 }; |
| 50 assertEquals(3, z); | 98 assertEquals(3, z); |
| 51 | 99 |
| 52 | 100 |
| 53 for(const {x, z} = { x : 0, z : 3 }; z != 3 || x != 0;) { | 101 for(const {x, z} = { x : 0, z : 3 }; z != 3 || x != 0;) { |
| 54 assertTrue(false); | 102 assertTrue(false); |
| 55 } | 103 } |
| 56 }()); | 104 }()); |
| 105 | |
| 106 (function TestFailingMatchesSloppy() { | |
| 107 var {x, y} = {}; | |
| 108 assertSame(undefined, x); | |
| 109 assertSame(undefined, y); | |
| 110 | |
| 111 var { x : { z1 }, y2} = { x : {}, y2 : 42 } | |
| 112 assertSame(undefined, z1); | |
| 113 assertSame(42, y2); | |
| 114 }()); | |
| 115 | |
| 116 | |
| 117 (function TestFailingMatchesStrict() { | |
| 118 'use strict'; | |
| 119 var {x, y} = {}; | |
| 120 assertSame(undefined, x); | |
| 121 assertSame(undefined, y); | |
| 122 | |
| 123 var { x : { z1 }, y2} = { x : {}, y2 : 42 } | |
| 124 assertSame(undefined, z1); | |
| 125 assertSame(42, y2); | |
| 126 | |
| 127 { | |
| 128 let {x1,y1} = {}; | |
| 129 assertSame(undefined, x1); | |
| 130 assertSame(undefined, y1); | |
| 131 | |
| 132 let { x : { z1 }, y2} = { x : {}, y2 : 42 } | |
| 133 assertSame(undefined, z1); | |
| 134 assertSame(42, y2); | |
| 135 } | |
| 136 }()); | |
| 137 | |
| 138 | |
| 139 (function TestExceptions() { | |
| 140 assertThrows(function() { var {} = null; }, TypeError); | |
|
caitp (gmail)
2015/05/18 13:11:06
do we want to test the other cases that would caus
Dmitry Lomov (no reviews)
2015/05/18 13:41:46
Done.
| |
| 141 assertThrows(function() { 'use strict'; let {} = null; }, TypeError); | |
| 142 }()); | |
| OLD | NEW |