Skip to content

Commit

Permalink
DRY up test assertions for date into 2 lambas & separate into sections.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Nov 29, 2023
1 parent beb9578 commit c5bd744
Showing 1 changed file with 52 additions and 58 deletions.
110 changes: 52 additions & 58 deletions test/20-dateRegex.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,59 @@ chai.should();

import * as vc from '../lib/index.js';

const assertValid = date => vc.dateRegex.test(date).should.be.true;
const assertInvalid = date => vc.dateRegex.test(date).should.be.false;

describe('verifies RFC3339 Dates', function() {
it('should verify a valid date', function() {
const latest = new Date().toISOString();
vc.dateRegex.test(latest).should.be.true;
});
it('should verify a date with lowercase time designator', function() {
const valid = '2019-03-26t14:00Z';
vc.dateRegex.test(valid).should.be.true;
});
it('should verify a date with lowercase UTC designator', function() {
const valid = '2019-03-26T14:00z';
vc.dateRegex.test(valid).should.be.true;
});
it('should not verify a date that uses / as a separator', function() {
const invalid = '2017/09/27';
vc.dateRegex.test(invalid).should.be.false;
});
it('should not verify 2 digit years', function() {
const invalid = '17-09-27T22:07:22.563z';
vc.dateRegex.test(invalid).should.be.false;
});
it('should not verify 1 digit months', function() {
const invalid = '2017-9-27T22:07:22.563z';
vc.dateRegex.test(invalid).should.be.false;
});
it('should verify 2 digit months starting with 0', function() {
const valid = '2017-09-27T22:07:22.563z';
vc.dateRegex.test(valid).should.be.true;
});
it('should not verify 1 digit days', function() {
const invalid = '2017-9-7T22:07:22.563z';
vc.dateRegex.test(invalid).should.be.false;
});
it('should verify 2 digit days starting with 0', function() {
const valid = '2017-12-07T22:07:22.563z';
vc.dateRegex.test(valid).should.be.true;
});
it('should not verify comma separators', function() {
const invalid = '2019-03-26T14:00:00,999Z';
vc.dateRegex.test(invalid).should.be.false;
});
it('should not verify an ISO 8601 date with hours only offset', function() {
const invalid = '2019-03-26T10:00-04';
vc.dateRegex.test(invalid).should.be.false;
});
it('should not verify an ISO 8601 date with fractional minutes', function() {
const invalid = '2019-03-26T14:00.9Z';
vc.dateRegex.test(invalid).should.be.false;
});
it('should verify leap seconds', function() {
const valid = '1972-06-30T23:59:60Z';
vc.dateRegex.test(valid).should.be.true;
});
it('should verify single digit fractional seconds', function() {
const valid = '2019-03-26T14:00:00.9Z';
vc.dateRegex.test(valid).should.be.true;
describe('positive', function() {
it('should verify a valid date', function() {
const latest = new Date().toISOString();
assertValid(latest);
});
it('should verify a date with lowercase time designator', function() {
assertValid('2019-03-26t14:00Z');
});
it('should verify a date with lowercase UTC designator', function() {
assertValid('2019-03-26T14:00z');
});
it('should verify 2 digit months starting with 0', function() {
assertValid('2017-09-27T22:07:22.563z');
});
it('should verify 2 digit days starting with 0', function() {
assertValid('2017-12-07T22:07:22.563z');
});
it('should verify leap seconds', function() {
assertValid('1972-06-30T23:59:60Z');
});
it('should verify single digit fractional seconds', function() {
assertValid('2019-03-26T14:00:00.9Z');
});
it('should verify multi-digit fractional seconds', function() {
assertValid('2019-03-26T14:00:00.4999Z');
});
});
it('should verify multi-digit fractional seconds', function() {
const valid = '2019-03-26T14:00:00.4999Z';
vc.dateRegex.test(valid).should.be.true;
describe('negative', function() {
it('should not verify a date that uses / as a separator', function() {
assertInvalid('2017/09/27');
});
it('should not verify 2 digit years', function() {
assertInvalid('17-09-27T22:07:22.563z');
});
it('should not verify 1 digit months', function() {
assertInvalid('2017-9-27T22:07:22.563z');
});
it('should not verify 1 digit days', function() {
assertInvalid('2017-9-7T22:07:22.563z');
});
it('should not verify comma separators', function() {
assertInvalid('2019-03-26T14:00:00,999Z');
});
it('should not verify an ISO 8601 date with hours only offset', function() {
assertInvalid('2019-03-26T10:00-04');
});
it('should not verify an ISO 8601 date with fractional minutes',
function() {
assertInvalid('2019-03-26T14:00.9Z');
});
});
});

0 comments on commit c5bd744

Please sign in to comment.