]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[tests] add tests for isLeapYear and daysInMonth (#5614)
authorabeaton <ambeaton3@gmail.com>
Fri, 18 Sep 2020 10:42:58 +0000 (06:42 -0400)
committerGitHub <noreply@github.com>
Fri, 18 Sep 2020 10:42:58 +0000 (03:42 -0700)
src/test/moment/days_in_month.js
src/test/moment/is-leap-year.js [new file with mode: 0644]

index 0a72c03e0747fa23b82b9cb34af088ae33bc7aad..45f6a1d2407b3bbd67a00cd6ee26b48858faa0a2 100644 (file)
@@ -5,6 +5,20 @@ import { daysInMonth } from '../../lib/units/month';
 
 module('days in month');
 
+test('days in month of all but february', function (assert) {
+    var days = [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
+        year,
+        month;
+    for (year = 1899; year < 2100; year++) {
+        for (month = 0; month < 12; month++) {
+            if (month != 1) {
+                assert.equal(moment([year, month]).daysInMonth(), days[month]);
+                assert.equal(daysInMonth(year, month), days[month]);
+            }
+        }
+    }
+});
+
 test('days in month', function (assert) {
     each([31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], function (days, i) {
         var firstDay = moment([2012, i]),
@@ -46,6 +60,10 @@ test('days in month leap years', function (assert) {
 });
 
 test('days in month with NaN inputs', function (assert) {
+    assert.ok(
+        isNaN(daysInMonth(NaN, NaN)),
+        'year and month NaN inputs should return NaN'
+    );
     assert.ok(isNaN(daysInMonth(2, NaN)), 'month NaN inputs should return NaN');
     assert.ok(isNaN(daysInMonth(NaN, 0)), 'year NaN inputs should return NaN');
     assert.ok(
diff --git a/src/test/moment/is-leap-year.js b/src/test/moment/is-leap-year.js
new file mode 100644 (file)
index 0000000..a53f3cc
--- /dev/null
@@ -0,0 +1,89 @@
+import { test, module } from '../qunit';
+import each from '../helpers/each';
+import { isLeapYear } from '../../lib/utils/is-leap-year';
+
+module('is leap year');
+
+test('leap years', function (assert) {
+    var tests = [
+        { year: 1, expected: false, description: '1 was not a leap year' },
+        { year: 4, expected: true, description: '4 was a leap year' },
+        { year: 100, expected: false, description: '100 was not a leap year' },
+        { year: 400, expected: true, description: '400 was a leap year' },
+        {
+            year: 1700,
+            expected: false,
+            description:
+                '1700 was a leap year in the Julian calendar, but not Gregorian',
+        },
+        {
+            year: 1900,
+            expected: false,
+            description:
+                '1900 was not a leap year, but this is a well known Microsoft Excel bug',
+        },
+        { year: 1904, expected: true, description: '1904 was a leap year' },
+        { year: 2000, expected: true, description: '2000 was a leap year' },
+        { year: 2008, expected: true, description: '2008 was a leap year' },
+        {
+            year: 2010,
+            expected: false,
+            description: '2010 was not a leap year',
+        },
+        { year: 2024, expected: true, description: '2024 will be a leap year' },
+        { year: 3448, expected: true, description: '3448 will be a leap year' },
+        { year: 4000, expected: true, description: '4000 will be a leap year' },
+        {
+            year: NaN,
+            expected: false,
+            description:
+                'NaN returns false explicitly, as math operations on non-numbers yields NaN',
+        },
+        {
+            year: '2000',
+            expected: true,
+            description:
+                'The string literal "2000" yields true as it is parsed as the number 2000',
+        },
+        {
+            year: Infinity,
+            expected: false,
+            description:
+                'Non-finite years are technically not years and definitely not leap years',
+        },
+        {
+            year: 3.14,
+            expected: false,
+            description:
+                'Non-integer years are technically not years and definitely not leap years',
+        },
+        {
+            year: 0,
+            expected: true,
+            description:
+                'While unclear whether this case is exceptional, it is a leap year based on existing formula',
+        },
+        {
+            year: -400,
+            expected: true,
+            description:
+                'While unclear whether this case is exceptional, it is a leap year based on existing formula',
+        },
+        {
+            year: {},
+            expected: false,
+            description: 'Objects are not a number, so not a leap year',
+        },
+        {
+            year: [],
+            expected: true,
+            description:
+                'An empty array is equivalent to 0, which is a leap year',
+        },
+    ];
+
+    each(tests, function (testCase) {
+        var actual = isLeapYear(testCase.year);
+        assert.equal(actual, testCase.expected, testCase.description);
+    });
+});