From c5bd7441a686fb026e6a06627730bf3ddae38ce6 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 29 Nov 2023 17:43:14 +0000 Subject: [PATCH] DRY up test assertions for date into 2 lambas & separate into sections. --- test/20-dateRegex.spec.js | 110 ++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 58 deletions(-) diff --git a/test/20-dateRegex.spec.js b/test/20-dateRegex.spec.js index 8bfaf2a0..3f407861 100644 --- a/test/20-dateRegex.spec.js +++ b/test/20-dateRegex.spec.js @@ -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'); + }); }); });