| 1 |
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ |
| 2 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
const expectedDate = new Date("1995-09-26T00:00:00"); |
| 7 |
|
| 8 |
// Each prefix will be tested with each format: |
| 9 |
const prefixes = [ |
| 10 |
"Tuesday, ", |
| 11 |
"Tuesday ", |
| 12 |
"Tuesday,", |
| 13 |
"Tuesday.", |
| 14 |
"Tuesday-", |
| 15 |
"Tuesday/", |
| 16 |
|
| 17 |
// Case insensitive |
| 18 |
"tuesday, ", |
| 19 |
"tUeSdAy ", |
| 20 |
|
| 21 |
// Abbreviations are valid down to the first character |
| 22 |
"Tuesda ", |
| 23 |
"Tue ", |
| 24 |
"T ", |
| 25 |
"t,", |
| 26 |
|
| 27 |
// Floating delimiters at beginning are allowed/ignored |
| 28 |
" ", |
| 29 |
",", |
| 30 |
", ", |
| 31 |
".", |
| 32 |
"-", |
| 33 |
"/", |
| 34 |
|
| 35 |
// It doesn't actually need to be the correct day of the week, or |
| 36 |
// a day of week at all...you can put anything there |
| 37 |
"Monday ", |
| 38 |
"foo bar " |
| 39 |
]; |
| 40 |
const formats = [ |
| 41 |
"Sep 26 1995", |
| 42 |
"26 Sep 1995", |
| 43 |
"September 26, 1995", |
| 44 |
"26-Sep-1995", |
| 45 |
"1995-9-26", |
| 46 |
// ISO format is non-formal with day of week in front |
| 47 |
"1995-09-26", |
| 48 |
|
| 49 |
// You can put anything between the month and mday |
| 50 |
"Sep foo bar 26 1995", |
| 51 |
"Sep-foo bar-26 1995", |
| 52 |
"Sep-foo-bar-26 1995", |
| 53 |
|
| 54 |
// Redundant month names are allowed |
| 55 |
"Sep sep 26 1995", |
| 56 |
"Sep 26 sep 1995", |
| 57 |
// Edge case: if multiple month names, use the last one |
| 58 |
"Jan 26 1995 sep", |
| 59 |
]; |
| 60 |
|
| 61 |
const rejected = [ |
| 62 |
"Sep 26 foo 1995", |
| 63 |
"Sep 26 1995 foo", |
| 64 |
"1995 foo Sep 26", |
| 65 |
"foo2 Sep 26 1995", |
| 66 |
"Tuesday_Sep 26 1995", |
| 67 |
"foo_12", |
| 68 |
]; |
| 69 |
|
| 70 |
for (const format of formats) { |
| 71 |
for (const prefix of prefixes) { |
| 72 |
const test = prefix + format; |
| 73 |
const testDate = new Date(test); |
| 74 |
|
| 75 |
assertEq( |
| 76 |
false, isNaN(testDate), |
| 77 |
`${test} should be accepted.` |
| 78 |
); |
| 79 |
|
| 80 |
assertEq( |
| 81 |
testDate.getTime(), expectedDate.getTime(), |
| 82 |
`"${test}" should be ${expectedDate} (got ${testDate}).` |
| 83 |
); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
for (const reject of rejected) { |
| 88 |
assertEq( |
| 89 |
true, isNaN(new Date(reject)), |
| 90 |
`"${reject}" should be rejected.` |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
if (typeof reportCompare === "function") |
| 95 |
reportCompare(true, true); |