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 = { | |
| 27 get x() { | |
| 28 countX++; | |
| 29 return 0; | |
| 30 }, | |
| 31 get y() { | |
| 32 countY++; | |
| 33 return { | |
| 34 get z() { countZ++; return 1; } | |
| 35 } | |
| 36 } | |
| 37 }; | |
| 38 var { x : x0, y : { z : z1 }, x : x1 } = o; | |
| 39 assertSame(0, x0); | |
| 40 assertSame(1, z1); | |
| 41 assertSame(0, x1); | |
| 42 assertEquals(2, countX); | |
| 43 assertEquals(1, countY); | |
| 44 assertEquals(1, countZ); | |
| 21 }()); | 45 }()); |
| 22 | 46 |
| 23 | 47 |
| 24 (function TestObjectLiteralPatternLexical() { | 48 (function TestObjectLiteralPatternLexical() { |
| 25 'use strict'; | 49 'use strict'; |
| 26 let { x : x, y : y } = { x : 1, y : 2 }; | 50 let { x : x, y : y } = { x : 1, y : 2 }; |
| 27 assertEquals(1, x); | 51 assertEquals(1, x); |
| 28 assertEquals(2, y); | 52 assertEquals(2, y); |
| 29 | 53 |
| 30 let {z} = { z : 3 }; | 54 let {z} = { z : 3 }; |
| 31 assertEquals(3, z); | 55 assertEquals(3, z); |
| 32 | 56 |
| 57 let countX = 0; | |
| 58 let countY = 0; | |
| 59 let countZ = 0; | |
| 60 let o = { | |
| 61 get x() { | |
| 62 countX++; | |
| 63 return 0; | |
| 64 }, | |
| 65 get y() { | |
| 66 countY++; | |
| 67 return { | |
| 68 get z() { countZ++; return 1; } | |
| 69 } | |
| 70 } | |
| 71 }; | |
| 72 let { x : x0, y : { z : z1 }, x : x1 } = o; | |
|
arv (Not doing code reviews)
2015/05/18 14:00:27
This makes me wish we had a test that makes sure w
Dmitry Lomov (no reviews)
2015/05/18 14:25:56
Done.
| |
| 73 assertSame(0, x0); | |
| 74 assertSame(1, z1); | |
| 75 assertSame(0, x1); | |
| 76 assertEquals(2, countX); | |
| 77 assertEquals(1, countY); | |
| 78 assertEquals(1, countZ); | |
| 79 | |
| 33 | 80 |
| 34 let sum = 0; | 81 let sum = 0; |
| 35 for(let {x, z} = { x : 0, z : 3 }; z != 0; z--) { | 82 for(let {x, z} = { x : 0, z : 3 }; z != 0; z--) { |
| 36 assertEquals(0, x); | 83 assertEquals(0, x); |
| 37 sum += z; | 84 sum += z; |
| 38 } | 85 } |
| 39 assertEquals(6, sum); | 86 assertEquals(6, sum); |
| 40 }()); | 87 }()); |
| 41 | 88 |
| 42 | 89 |
| 43 (function TestObjectLiteralPatternLexicalConst() { | 90 (function TestObjectLiteralPatternLexicalConst() { |
| 44 'use strict'; | 91 'use strict'; |
| 45 const { x : x, y : y } = { x : 1, y : 2 }; | 92 const { x : x, y : y } = { x : 1, y : 2 }; |
| 46 assertEquals(1, x); | 93 assertEquals(1, x); |
| 47 assertEquals(2, y); | 94 assertEquals(2, y); |
| 48 | 95 |
| 96 assertThrows(function() { x++; }, TypeError); | |
| 97 assertThrows(function() { y++; }, TypeError); | |
| 98 | |
| 49 const {z} = { z : 3 }; | 99 const {z} = { z : 3 }; |
| 50 assertEquals(3, z); | 100 assertEquals(3, z); |
| 51 | 101 |
| 52 | 102 |
| 53 for(const {x, z} = { x : 0, z : 3 }; z != 3 || x != 0;) { | 103 for(const {x, z} = { x : 0, z : 3 }; z != 3 || x != 0;) { |
| 54 assertTrue(false); | 104 assertTrue(false); |
| 55 } | 105 } |
| 56 }()); | 106 }()); |
| 107 | |
| 108 (function TestFailingMatchesSloppy() { | |
| 109 var {x, y} = {}; | |
| 110 assertSame(undefined, x); | |
| 111 assertSame(undefined, y); | |
| 112 | |
| 113 var { x : { z1 }, y2} = { x : {}, y2 : 42 } | |
| 114 assertSame(undefined, z1); | |
| 115 assertSame(42, y2); | |
| 116 }()); | |
| 117 | |
| 118 | |
| 119 (function TestFailingMatchesStrict() { | |
| 120 'use strict'; | |
| 121 var {x, y} = {}; | |
| 122 assertSame(undefined, x); | |
| 123 assertSame(undefined, y); | |
| 124 | |
| 125 var { x : { z1 }, y2} = { x : {}, y2 : 42 } | |
| 126 assertSame(undefined, z1); | |
| 127 assertSame(42, y2); | |
| 128 | |
| 129 { | |
| 130 let {x1,y1} = {}; | |
| 131 assertSame(undefined, x1); | |
| 132 assertSame(undefined, y1); | |
| 133 | |
| 134 let { x : { z1 }, y2} = { x : {}, y2 : 42 } | |
| 135 assertSame(undefined, z1); | |
| 136 assertSame(42, y2); | |
| 137 } | |
| 138 }()); | |
| 139 | |
| 140 | |
| 141 (function TestExceptions() { | |
| 142 assertThrows(function() { var {} = null; }, TypeError); | |
| 143 assertThrows(function() { var {x} = null; }, TypeError); | |
| 144 assertThrows(function() { var { x : {} } = { x : null }; }, TypeError); | |
| 145 assertThrows(function() { 'use strict'; let {} = null; }, TypeError); | |
| 146 assertThrows(function() { 'use strict'; let {x} = null; }, TypeError); | |
| 147 assertThrows(function() { 'use strict'; let { x : {} } = { x : null }; }, Type Error); | |
| 148 }()); | |
| OLD | NEW |