JDK-8011382.js revision 168:82fed56d8dce
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * JDK-8011382: Data prototype methods and constructor do not call user defined toISOString, valueOf methods per spec.
26 *
27 * @test
28 * @run
29 */
30
31var yearValueOf = 0;
32var monthValueOf = 0;
33var dayValueOf = 0;
34
35var d = new Date(
36    {
37        valueOf: function() { yearValueOf++; return NaN; }
38    },
39    {
40        valueOf: function() { monthValueOf++; return NaN; }
41    },
42    {
43        valueOf: function() { dayValueOf++; return NaN; }
44    }
45);
46
47if (yearValueOf !== 1) {
48    fail("Date constructor does not call valueOf on year argument once");
49}
50
51if (monthValueOf !== 1) {
52    fail("Date constructor does not call valueOf on month argument once");
53}
54
55if (dayValueOf !== 1) {
56    fail("Date constructor does not call valueOf on day argument once");
57}
58
59yearValueOf = 0;
60monthValueOf = 0;
61dayValueOf = 0;
62
63d = new Date();
64
65d.setFullYear(
66    {
67        valueOf: function() { yearValueOf++; return NaN; }
68    },
69    {
70        valueOf: function() { monthValueOf++; return NaN; }
71    },
72    {
73        valueOf: function() { dayValueOf++; return NaN; }
74    }
75);
76
77if (yearValueOf !== 1) {
78    fail("Date setFullYear does not call valueOf on year argument once");
79}
80
81if (monthValueOf !== 1) {
82    fail("Date setFullYear does not call valueOf on month argument once");
83}
84
85if (dayValueOf !== 1) {
86    fail("Date setFullYear does not call valueOf on day argument once");
87}
88
89// check toJSON calls toISOString override
90var toISOStringCalled = 0;
91d = new Date();
92d.toISOString = function() {
93    toISOStringCalled++;
94};
95
96d.toJSON();
97if (toISOStringCalled !== 1) {
98    fail("toISOString was not called by Date.prototype.toJSON once");
99}
100
101toISOStringCalled = 0;
102
103// toJSON is generic - try for non-Date object
104Date.prototype.toJSON.call({
105    toISOString: function() {
106        toISOStringCalled++;
107    },
108    valueOf: function() {
109        return 12;
110    }
111});
112
113if (toISOStringCalled !== 1) {
114    fail("toISOString was not called by Date.prototype.toJSON once");
115}
116