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 |
| 6 function return_specified() { return "specified"; } |
| 7 |
| 8 var method_returns_specified = { |
| 9 method() { return "specified"; } |
| 10 }; |
| 11 |
| 12 |
| 13 (function testOptionalFunctions() { |
| 14 function optional_function(handler = function() { }) { |
| 15 assertEquals("function", typeof handler); |
| 16 |
| 17 // TODO(caitp): infer function name correctly |
| 18 // assertEquals("handler", handler.name); |
| 19 |
| 20 return handler(); |
| 21 } |
| 22 assertEquals(undefined, optional_function()); |
| 23 assertEquals(undefined, optional_function(undefined)); |
| 24 assertEquals("specified", optional_function(return_specified)); |
| 25 })(); |
| 26 |
| 27 |
| 28 (function testOptionalObjects() { |
| 29 function optional_object(object = { method() { return "method"; } }) { |
| 30 assertEquals("object", typeof object); |
| 31 |
| 32 assertEquals("function", typeof object.method); |
| 33 return object.method(); |
| 34 } |
| 35 |
| 36 assertEquals("method", optional_object()); |
| 37 assertEquals("method", optional_object(undefined)); |
| 38 assertEquals("specified", optional_object(method_returns_specified)); |
| 39 })(); |
| 40 |
| 41 |
| 42 // TDZ |
| 43 |
| 44 (function testReferencesUninitializedParameter(x = 1) { |
| 45 assertThrows(function(a = b, b) {}, ReferenceError); |
| 46 })(); |
| 47 |
| 48 |
| 49 (function testReferencesInitializedParameter(x = 1) { |
| 50 assertEquals(1, (function(a = 1, b = a) { return b; })()); |
| 51 })(); |
| 52 |
| 53 |
| 54 // Scoping |
| 55 assertThrows(function referencesVariableBodyDeclaration(a = body_var) { |
| 56 var body_var = true; |
| 57 return a; |
| 58 }, ReferenceError); |
| 59 |
| 60 |
| 61 // TODO(caitp): default function length does not include any parameters |
| 62 // following the first optional parameter |
| 63 // assertEquals(0, (function(a = 1) {}).length); |
| 64 // assertEquals(1, (function(a, b = 1) {}).length); |
| 65 // assertEquals(2, (function(a, b, c = 1) {}).length); |
| 66 // assertEquals(3, (function(a, b, c, d = 1) {}).length); |
| 67 // assertEquals(1, (function(a, b = 1, c, d = 1) {}).length); |
OLD | NEW |