date_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package goja
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. const TESTLIB = `
  7. function $ERROR(message) {
  8. throw new Error(message);
  9. }
  10. function assert(mustBeTrue, message) {
  11. if (mustBeTrue === true) {
  12. return;
  13. }
  14. if (message === undefined) {
  15. message = 'Expected true but got ' + String(mustBeTrue);
  16. }
  17. $ERROR(message);
  18. }
  19. assert._isSameValue = function (a, b) {
  20. if (a === b) {
  21. // Handle +/-0 vs. -/+0
  22. return a !== 0 || 1 / a === 1 / b;
  23. }
  24. // Handle NaN vs. NaN
  25. return a !== a && b !== b;
  26. };
  27. assert.sameValue = function (actual, expected, message) {
  28. if (assert._isSameValue(actual, expected)) {
  29. return;
  30. }
  31. if (message === undefined) {
  32. message = '';
  33. } else {
  34. message += ' ';
  35. }
  36. message += 'Expected SameValue(«' + String(actual) + '», «' + String(expected) + '») to be true';
  37. $ERROR(message);
  38. };
  39. `
  40. func TestDateUTC(t *testing.T) {
  41. const SCRIPT = `
  42. assert.sameValue(Date.UTC(1970, 0), 0, '1970, 0');
  43. assert.sameValue(Date.UTC(2016, 0), 1451606400000, '2016, 0');
  44. assert.sameValue(Date.UTC(2016, 6), 1467331200000, '2016, 6');
  45. assert.sameValue(Date.UTC(2016, 6, 1), 1467331200000, '2016, 6, 1');
  46. assert.sameValue(Date.UTC(2016, 6, 5), 1467676800000, '2016, 6, 5');
  47. assert.sameValue(Date.UTC(2016, 6, 5, 0), 1467676800000, '2016, 6, 5, 0');
  48. assert.sameValue(Date.UTC(2016, 6, 5, 15), 1467730800000, '2016, 6, 5, 15');
  49. assert.sameValue(
  50. Date.UTC(2016, 6, 5, 15, 0), 1467730800000, '2016, 6, 5, 15, 0'
  51. );
  52. assert.sameValue(
  53. Date.UTC(2016, 6, 5, 15, 34), 1467732840000, '2016, 6, 5, 15, 34'
  54. );
  55. assert.sameValue(
  56. Date.UTC(2016, 6, 5, 15, 34, 0), 1467732840000, '2016, 6, 5, 15, 34, 0'
  57. );
  58. assert.sameValue(
  59. Date.UTC(2016, 6, 5, 15, 34, 45), 1467732885000, '2016, 6, 5, 15, 34, 45'
  60. );
  61. `
  62. testScript1(TESTLIB+SCRIPT, _undefined, t)
  63. }
  64. func TestNewDate(t *testing.T) {
  65. const SCRIPT = `
  66. var d1 = new Date("2016-09-01T12:34:56Z");
  67. d1.getUTCHours() === 12;
  68. `
  69. testScript1(SCRIPT, valueTrue, t)
  70. }
  71. func TestNewDate0(t *testing.T) {
  72. const SCRIPT = `
  73. (new Date(0)).toUTCString();
  74. `
  75. testScript1(SCRIPT, asciiString("Thu Jan 01 1970 00:00:00 GMT+0000 (UTC)"), t)
  76. }
  77. func TestSetHour(t *testing.T) {
  78. l := time.Local
  79. defer func() {
  80. time.Local = l
  81. }()
  82. var err error
  83. time.Local, err = time.LoadLocation("America/New_York")
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. const SCRIPT = `
  88. var d = new Date(2016, 8, 1, 12, 23, 45)
  89. assert.sameValue(d.getHours(), 12);
  90. assert.sameValue(d.getUTCHours(), 16);
  91. d.setHours(13);
  92. assert.sameValue(d.getHours(), 13);
  93. assert.sameValue(d.getMinutes(), 23);
  94. assert.sameValue(d.getSeconds(), 45);
  95. d.setUTCHours(13);
  96. assert.sameValue(d.getHours(), 9);
  97. assert.sameValue(d.getMinutes(), 23);
  98. assert.sameValue(d.getSeconds(), 45);
  99. `
  100. testScript1(TESTLIB+SCRIPT, _undefined, t)
  101. }
  102. func TestSetMinute(t *testing.T) {
  103. l := time.Local
  104. defer func() {
  105. time.Local = l
  106. }()
  107. time.Local = time.FixedZone("Asia/Delhi", 5*60*60+30*60)
  108. const SCRIPT = `
  109. var d = new Date(2016, 8, 1, 12, 23, 45)
  110. assert.sameValue(d.getHours(), 12);
  111. assert.sameValue(d.getUTCHours(), 6);
  112. assert.sameValue(d.getMinutes(), 23);
  113. assert.sameValue(d.getUTCMinutes(), 53);
  114. d.setMinutes(55);
  115. assert.sameValue(d.getMinutes(), 55);
  116. assert.sameValue(d.getSeconds(), 45);
  117. d.setUTCMinutes(52);
  118. assert.sameValue(d.getMinutes(), 22);
  119. assert.sameValue(d.getHours(), 13);
  120. `
  121. testScript1(TESTLIB+SCRIPT, _undefined, t)
  122. }
  123. func TestTimezoneOffset(t *testing.T) {
  124. const SCRIPT = `
  125. var d = new Date(0);
  126. d.getTimezoneOffset();
  127. `
  128. l := time.Local
  129. defer func() {
  130. time.Local = l
  131. }()
  132. var err error
  133. time.Local, err = time.LoadLocation("Europe/London")
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. testScript1(SCRIPT, intToValue(-60), t)
  138. }
  139. func TestDateValueOf(t *testing.T) {
  140. const SCRIPT = `
  141. var d9 = new Date(1.23e15);
  142. d9.valueOf();
  143. `
  144. testScript1(SCRIPT, intToValue(1.23e15), t)
  145. }