OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --harmony-optional-params |
| 6 |
| 7 function return_specified() { return "specified"; } |
| 8 |
| 9 var method_returns_specified = { |
| 10 method() { return "specified"; } |
| 11 }; |
| 12 |
| 13 |
| 14 (function testOptionalFunctions() { |
| 15 function optional_function(handler = function() { }) { |
| 16 assertEquals("function", typeof handler); |
| 17 |
| 18 // TODO(caitp): infer function name correctly |
| 19 // (https://code.google.com/p/v8/issues/detail?id=3699) |
| 20 // assertEquals("handler", handler.name); |
| 21 |
| 22 return handler(); |
| 23 } |
| 24 assertEquals(undefined, optional_function()); |
| 25 assertEquals(undefined, optional_function(undefined)); |
| 26 assertEquals("specified", optional_function(return_specified)); |
| 27 })(); |
| 28 |
| 29 |
| 30 (function testOptionalObjects() { |
| 31 function optional_object(object = { method() { return "method"; } }) { |
| 32 assertEquals("object", typeof object); |
| 33 |
| 34 assertEquals("function", typeof object.method); |
| 35 return object.method(); |
| 36 } |
| 37 |
| 38 assertEquals("method", optional_object()); |
| 39 assertEquals("method", optional_object(undefined)); |
| 40 assertEquals("specified", optional_object(method_returns_specified)); |
| 41 })(); |
| 42 |
| 43 |
| 44 // TDZ |
| 45 |
| 46 (function testReferencesUninitializedParameter(x = 1) { |
| 47 assertThrows(function(a = b, b) {}, ReferenceError); |
| 48 })(); |
| 49 |
| 50 |
| 51 (function testReferencesInitializedParameter(x = 1) { |
| 52 assertEquals(1, (function(a = 1, b = a) { return b; })()); |
| 53 })(); |
| 54 |
| 55 |
| 56 // Scoping |
| 57 // |
| 58 // TODO(caitp): fix scoping --- var declarations in function body can't be |
| 59 // resolved in formal parameters |
| 60 // assertThrows(function referencesVariableBodyDeclaration(a = body_var) { |
| 61 // var body_var = true; |
| 62 // return a; |
| 63 // }, ReferenceError); |
| 64 |
| 65 |
| 66 // TODO(caitp): default function length does not include any parameters |
| 67 // following the first optional parameter |
| 68 // assertEquals(0, (function(a = 1) {}).length); |
| 69 // assertEquals(1, (function(a, b = 1) {}).length); |
| 70 // assertEquals(2, (function(a, b, c = 1) {}).length); |
| 71 // assertEquals(3, (function(a, b, c, d = 1) {}).length); |
| 72 // assertEquals(1, (function(a, b = 1, c, d = 1) {}).length); |
OLD | NEW |