1package goja 2 3import ( 4 "testing" 5 "time" 6) 7 8const TESTLIB = ` 9function $ERROR(message) { 10 throw new Error(message); 11} 12 13function assert(mustBeTrue, message) { 14 if (mustBeTrue === true) { 15 return; 16 } 17 18 if (message === undefined) { 19 message = 'Expected true but got ' + String(mustBeTrue); 20 } 21 $ERROR(message); 22} 23 24assert._isSameValue = function (a, b) { 25 if (a === b) { 26 // Handle +/-0 vs. -/+0 27 return a !== 0 || 1 / a === 1 / b; 28 } 29 30 // Handle NaN vs. NaN 31 return a !== a && b !== b; 32}; 33 34assert.sameValue = function (actual, expected, message) { 35 if (assert._isSameValue(actual, expected)) { 36 return; 37 } 38 39 if (message === undefined) { 40 message = ''; 41 } else { 42 message += ' '; 43 } 44 45 message += 'Expected SameValue(«' + String(actual) + '», «' + String(expected) + '») to be true'; 46 47 $ERROR(message); 48}; 49 50 51` 52 53func TestDateUTC(t *testing.T) { 54 const SCRIPT = ` 55 assert.sameValue(Date.UTC(1970, 0), 0, '1970, 0'); 56 assert.sameValue(Date.UTC(2016, 0), 1451606400000, '2016, 0'); 57 assert.sameValue(Date.UTC(2016, 6), 1467331200000, '2016, 6'); 58 59 assert.sameValue(Date.UTC(2016, 6, 1), 1467331200000, '2016, 6, 1'); 60 assert.sameValue(Date.UTC(2016, 6, 5), 1467676800000, '2016, 6, 5'); 61 62 assert.sameValue(Date.UTC(2016, 6, 5, 0), 1467676800000, '2016, 6, 5, 0'); 63 assert.sameValue(Date.UTC(2016, 6, 5, 15), 1467730800000, '2016, 6, 5, 15'); 64 65 assert.sameValue( 66 Date.UTC(2016, 6, 5, 15, 0), 1467730800000, '2016, 6, 5, 15, 0' 67 ); 68 assert.sameValue( 69 Date.UTC(2016, 6, 5, 15, 34), 1467732840000, '2016, 6, 5, 15, 34' 70 ); 71 72 assert.sameValue( 73 Date.UTC(2016, 6, 5, 15, 34, 0), 1467732840000, '2016, 6, 5, 15, 34, 0' 74 ); 75 assert.sameValue( 76 Date.UTC(2016, 6, 5, 15, 34, 45), 1467732885000, '2016, 6, 5, 15, 34, 45' 77 ); 78 79 ` 80 81 testScript1(TESTLIB+SCRIPT, _undefined, t) 82} 83 84func TestNewDate(t *testing.T) { 85 const SCRIPT = ` 86 var d1 = new Date("2016-09-01T12:34:56Z"); 87 d1.getUTCHours() === 12; 88 89 ` 90 testScript1(SCRIPT, valueTrue, t) 91} 92 93func TestNewDate0(t *testing.T) { 94 const SCRIPT = ` 95 (new Date(0)).toUTCString(); 96 97 ` 98 testScript1(SCRIPT, asciiString("Thu Jan 01 1970 00:00:00 GMT+0000 (UTC)"), t) 99} 100 101func TestSetHour(t *testing.T) { 102 l := time.Local 103 defer func() { 104 time.Local = l 105 }() 106 var err error 107 time.Local, err = time.LoadLocation("America/New_York") 108 if err != nil { 109 t.Fatal(err) 110 } 111 112 const SCRIPT = ` 113 var d = new Date(2016, 8, 1, 12, 23, 45) 114 assert.sameValue(d.getHours(), 12); 115 assert.sameValue(d.getUTCHours(), 16); 116 117 d.setHours(13); 118 assert.sameValue(d.getHours(), 13); 119 assert.sameValue(d.getMinutes(), 23); 120 assert.sameValue(d.getSeconds(), 45); 121 122 d.setUTCHours(13); 123 assert.sameValue(d.getHours(), 9); 124 assert.sameValue(d.getMinutes(), 23); 125 assert.sameValue(d.getSeconds(), 45); 126 127 ` 128 testScript1(TESTLIB+SCRIPT, _undefined, t) 129 130} 131 132func TestSetMinute(t *testing.T) { 133 l := time.Local 134 defer func() { 135 time.Local = l 136 }() 137 time.Local = time.FixedZone("Asia/Delhi", 5*60*60+30*60) 138 139 const SCRIPT = ` 140 var d = new Date(2016, 8, 1, 12, 23, 45) 141 assert.sameValue(d.getHours(), 12); 142 assert.sameValue(d.getUTCHours(), 6); 143 assert.sameValue(d.getMinutes(), 23); 144 assert.sameValue(d.getUTCMinutes(), 53); 145 146 d.setMinutes(55); 147 assert.sameValue(d.getMinutes(), 55); 148 assert.sameValue(d.getSeconds(), 45); 149 150 d.setUTCMinutes(52); 151 assert.sameValue(d.getMinutes(), 22); 152 assert.sameValue(d.getHours(), 13); 153 154 ` 155 testScript1(TESTLIB+SCRIPT, _undefined, t) 156 157} 158 159func TestTimezoneOffset(t *testing.T) { 160 const SCRIPT = ` 161 var d = new Date(0); 162 d.getTimezoneOffset(); 163 ` 164 165 l := time.Local 166 defer func() { 167 time.Local = l 168 }() 169 var err error 170 time.Local, err = time.LoadLocation("Europe/London") 171 if err != nil { 172 t.Fatal(err) 173 } 174 175 testScript1(SCRIPT, intToValue(-60), t) 176} 177 178func TestDateValueOf(t *testing.T) { 179 const SCRIPT = ` 180 var d9 = new Date(1.23e15); 181 d9.valueOf(); 182 ` 183 184 testScript1(SCRIPT, intToValue(1.23e15), t) 185} 186 187func TestDateSetters(t *testing.T) { 188 const SCRIPT = ` 189 assert.sameValue((new Date(0)).setMilliseconds(2345), 2345, "setMilliseconds()"); 190 assert.sameValue((new Date(0)).setUTCMilliseconds(2345), 2345, "setUTCMilliseconds()"); 191 assert.sameValue((new Date(0)).setSeconds(12), 12000, "setSeconds()"); 192 assert.sameValue((new Date(0)).setUTCSeconds(12), 12000, "setUTCSeconds()"); 193 assert.sameValue((new Date(0)).setMinutes(12), 12 * 60 * 1000, "setMinutes()"); 194 assert.sameValue((new Date(0)).setUTCMinutes(12), 12 * 60 * 1000, "setUTCMinutes()"); 195 assert.sameValue((new Date("2016-06-01")).setHours(1), 1464739200000, "setHours()"); 196 assert.sameValue((new Date("2016-06-01")).setUTCHours(1), 1464742800000, "setUTCHours()"); 197 assert.sameValue((new Date(0)).setDate(2), 86400000, "setDate()"); 198 assert.sameValue((new Date(0)).setUTCDate(2), 86400000, "setUTCDate()"); 199 assert.sameValue((new Date(0)).setMonth(2), 5097600000, "setMonth()"); 200 assert.sameValue((new Date(0)).setUTCMonth(2), 5097600000, "setUTCMonth()"); 201 assert.sameValue((new Date(0)).setFullYear(1971), 31536000000, "setFullYear()"); 202 assert.sameValue((new Date(0)).setFullYear(1971, 2, 3), 36806400000, "setFullYear(Y,M,D)"); 203 assert.sameValue((new Date(0)).setUTCFullYear(1971), 31536000000, "setUTCFullYear()"); 204 assert.sameValue((new Date(0)).setUTCFullYear(1971, 2, 3), 36806400000, "setUTCFullYear(Y,M,D)"); 205 206 ` 207 208 l := time.Local 209 defer func() { 210 time.Local = l 211 }() 212 var err error 213 time.Local, err = time.LoadLocation("Europe/London") 214 if err != nil { 215 t.Fatal(err) 216 } 217 218 testScript1(TESTLIB+SCRIPT, _undefined, t) 219} 220