diff --git a/test/lib/util.js b/test/lib/util.js index d5afed72..89381beb 100644 --- a/test/lib/util.js +++ b/test/lib/util.js @@ -2,9 +2,11 @@ const isRoughlyEqual = require('is-roughly-equal') const {DateTime} = require('luxon') +const a = require('assert') const hour = 60 * 60 * 1000 -const week = 7 * 24 * hour +const day = 24 * hour +const week = 7 * day // next Monday 10 am const createWhen = (timezone, locale) => { @@ -14,13 +16,13 @@ const createWhen = (timezone, locale) => { }).startOf('week').plus({weeks: 1, hours: 10}).toJSDate() } -const isValidWhen = (actual, expected) => { +const assertValidWhen = (actual, expected, name) => { const ts = +new Date(actual) - if (Number.isNaN(ts)) return false + a.ok(!Number.isNaN(ts), name + ' is not parsable by Date') // the timestamps might be from long-distance trains - return isRoughlyEqual(14 * hour, +expected, ts) + a.ok(isRoughlyEqual(day, +expected, ts), name + ' is out of range') } module.exports = { - hour, createWhen, isValidWhen + hour, createWhen, assertValidWhen } diff --git a/test/lib/validators.js b/test/lib/validators.js index 8a19db86..ec864a04 100644 --- a/test/lib/validators.js +++ b/test/lib/validators.js @@ -4,7 +4,7 @@ const a = require('assert') const {defaultValidators} = require('validate-fptf') const anyOf = require('validate-fptf/lib/any-of') -const {isValidWhen} = require('./util') +const {assertValidWhen} = require('./util') const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o) const is = val => val !== null && val !== undefined @@ -80,11 +80,11 @@ const createValidateStopover = (cfg) => { const validateStopover = (val, s, name = 'stopover') => { if (is(s.arrival)) { val.date(val, s.arrival, name + '.arrival') - a.ok(isValidWhen(s.arrival, cfg.when), name + '.arrival is invalid') + assertValidWhen(s.arrival, cfg.when, name) } if (is(s.departure)) { val.date(val, s.departure, name + '.departure') - a.ok(isValidWhen(s.departure, cfg.when), name + '.departure is invalid') + assertValidWhen(s.departure, cfg.when, name) } if (!is(s.arrival) && !is(s.departure)) { a.fail(name + ' contains neither arrival nor departure') @@ -153,12 +153,10 @@ const createValidateJourneyLeg = (cfg) => { defaultValidators.journeyLeg(val, withFakeScheduleAndOperator, name) if (leg.arrival !== null) { - const msg = name + '.arrival is invalid' - a.ok(isValidWhen(leg.arrival, cfg.when), msg) + assertValidWhen(leg.arrival, cfg.when, name + '.arrival') } if (leg.departure !== null) { - const msg = name + '.departure is invalid' - a.ok(isValidWhen(leg.departure, cfg.when), msg) + assertValidWhen(leg.departure, cfg.when, name + '.departure') } // todo: leg.arrivalPlatform !== null if (is(leg.arrivalPlatform)) { @@ -226,7 +224,7 @@ const createValidateDeparture = (cfg) => { val.station(val, dep.station, name + '.station') - a.ok(isValidWhen(dep.when, cfg.when), name + '.when is invalid') + assertValidWhen(dep.when, cfg.when, name) if (dep.delay !== null) { const msg = name + '.delay must be a number' a.strictEqual(typeof dep.delay, 'number', msg)