summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Date/parse-day-of-week.js
blob: 52a8c6339a8b84fb41b9c453e5d9ab8d9ecd1c24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const expectedDate = new Date("1995-09-26T00:00:00");

// Each prefix will be tested with each format:
const prefixes = [
  "Tuesday, ",
  "Tuesday ",
  "Tuesday,",
  "Tuesday.",
  "Tuesday-",
  "Tuesday/",

  // Case insensitive
  "tuesday, ",
  "tUeSdAy ",

  // Abbreviations are valid down to the first character
  "Tuesda ",
  "Tue ",
  "T ",
  "t,",

  // Floating delimiters at beginning are allowed/ignored
  " ",
  ",",
  ", ",
  ".",
  "-",
  "/",

  // It doesn't actually need to be the correct day of the week, or
  // a day of week at all...you can put anything there
  "Monday ",
  "foo bar "
];
const formats = [
  "Sep 26 1995",
  "26 Sep 1995",
  "September 26, 1995",
  "26-Sep-1995",
  "1995-9-26",
  // ISO format is non-formal with day of week in front
  "1995-09-26",

  // You can put anything between the month and mday
  "Sep foo bar 26 1995",
  "Sep-foo bar-26 1995",
  "Sep-foo-bar-26 1995",

  // Redundant month names are allowed
  "Sep sep 26 1995",
  "Sep 26 sep 1995",
  // Edge case: if multiple month names, use the last one
  "Jan 26 1995 sep",
];

const rejected = [
  "Sep 26 foo 1995",
  "Sep 26 1995 foo",
  "1995 foo Sep 26",
  "foo2 Sep 26 1995",
  "Tuesday_Sep 26 1995",
  "foo_12",
];

for (const format of formats) {
  for (const prefix of prefixes) {
    const test = prefix + format;
    const testDate = new Date(test);

    assertEq(
      false, isNaN(testDate),
      `${test} should be accepted.`
    );

    assertEq(
      testDate.getTime(), expectedDate.getTime(),
      `"${test}" should be ${expectedDate} (got ${testDate}).`
    );
  }
}

for (const reject of rejected) {
  assertEq(
    true, isNaN(new Date(reject)),
    `"${reject}" should be rejected.`
  );
}

if (typeof reportCompare === "function")
  reportCompare(true, true);