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 
  31 var yearValueOf = 0;
  32 var monthValueOf = 0;
  33 var dayValueOf = 0;
  34 
  35 var 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 
  47 if (yearValueOf !== 1) {
  48     fail("Date constructor does not call valueOf on year argument once");
  49 }
  50 
  51 if (monthValueOf !== 1) {
  52     fail("Date constructor does not call valueOf on month argument once");
  53 }
  54 
  55 if (dayValueOf !== 1) {
  56     fail("Date constructor does not call valueOf on day argument once");
  57 }
  58 
  59 yearValueOf = 0;
  60 monthValueOf = 0;
  61 dayValueOf = 0;
  62 
  63 d = new Date();
  64 
  65 d.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 
  77 if (yearValueOf !== 1) {
  78     fail("Date setFullYear does not call valueOf on year argument once");
  79 }
  80 
  81 if (monthValueOf !== 1) {
  82     fail("Date setFullYear does not call valueOf on month argument once");
  83 }
  84 
  85 if (dayValueOf !== 1) {
  86     fail("Date setFullYear does not call valueOf on day argument once");
  87 }
  88 
  89 // check toJSON calls toISOString override
  90 var toISOStringCalled = 0;
  91 d = new Date();
  92 d.toISOString = function() {
  93     toISOStringCalled++;
  94 };
  95 
  96 d.toJSON();
  97 if (toISOStringCalled !== 1) {
  98     fail("toISOString was not called by Date.prototype.toJSON once");
  99 }
 100 
 101 toISOStringCalled = 0;
 102 
 103 // toJSON is generic - try for non-Date object
 104 Date.prototype.toJSON.call({
 105     toISOString: function() {
 106         toISOStringCalled++;
 107     },
 108     valueOf: function() {
 109         return 12;
 110     }
 111 });
 112 
 113 if (toISOStringCalled !== 1) {
 114     fail("toISOString was not called by Date.prototype.toJSON once");
 115 }