From 7f414d7b101046679e311f35c849a9ecfe60a538 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Fri, 23 Feb 2018 15:32:36 +0100 Subject: [PATCH] =?utf8?q?Validator:=20time=20-=20striktere=20Pr=C3=BCfung?= =?utf8?q?=20und=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- js/kivi.js | 4 +++ js/t/kivi/parse_format_time.js | 46 +++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/js/kivi.js b/js/kivi.js index 7c1f00a43..444892117 100644 --- a/js/kivi.js +++ b/js/kivi.js @@ -138,6 +138,10 @@ namespace("kivi", function(ns) { var parts = time.replace(/\s+/g, "").split(ns._time_format.sep); if (parts.length == 2) { + for (var idx in parts) { + if (Number.isNaN(Number.parseInt(parts[idx]))) + return undefined; + } now.setHours(parts[ns._time_format.h], parts[ns._time_format.m]); return now; } else diff --git a/js/t/kivi/parse_format_time.js b/js/t/kivi/parse_format_time.js index 901de6415..393d790c3 100644 --- a/js/t/kivi/parse_format_time.js +++ b/js/t/kivi/parse_format_time.js @@ -5,25 +5,43 @@ function custom_time(h,m) { } QUnit.test("kivi.parse_time function for German time style with colon", function( assert ) { + assert.equalTimes = function(actual, expected, message) { + console.log(this); + var result = (expected === undefined && actual === undefined) + || (expected === null && actual === null) + || (expected instanceof Date && actual instanceof Date && + expected.getHours() == actual.getHours() && + expected.getMinutes() == actual.getMinutes()); + + this.push( { + result: result, + actual: actual, + expected: expected, + message: message + } ); + } + kivi.setup_formats({ times: "hh:mm" }); - assert.deepEqual(kivi.parse_time("12:34"), custom_time(12,34)); - assert.deepEqual(kivi.parse_time("10:00"), custom_time(10,0)); - assert.deepEqual(kivi.parse_time(" 12 : 23 "), custom_time(12,23)); + assert.equalTimes(kivi.parse_time("12:34"), custom_time(12,34)); + assert.equalTimes(kivi.parse_time("10:00"), custom_time(10,0)); + assert.equalTimes(kivi.parse_time(" 12 : 23 ") - custom_time(12,23)); - assert.deepEqual(kivi.parse_time("00:20"), custom_time(0,20)); + assert.equalTimes(kivi.parse_time("00:20"), custom_time(0,20)); - assert.deepEqual(kivi.parse_time("23:60"), custom_time(23,60)); + assert.equalTimes(kivi.parse_time("23:60"), custom_time(23,60)); - assert.deepEqual(kivi.parse_time("1142"), custom_time(11,42)); + assert.equalTimes(kivi.parse_time("1142"), custom_time(11,42)); - assert.deepEqual(kivi.parse_time("Totally Invalid!"), undefined); - assert.deepEqual(kivi.parse_time("."), undefined); - assert.deepEqual(kivi.parse_time(".."), undefined); - assert.deepEqual(kivi.parse_time(":"), custom_time(0,0)); - assert.deepEqual(kivi.parse_time("::"), undefined); - assert.deepEqual(kivi.parse_time(""), null); - assert.deepEqual(kivi.parse_time("0"), new Date()); - assert.deepEqual(kivi.parse_time("29:20008"), custom_time(29,20008)); + assert.equalTimes(kivi.parse_time("Totally Invalid!"), undefined); + assert.equalTimes(kivi.parse_time("."), undefined); + assert.equalTimes(kivi.parse_time(".."), undefined); + assert.equalTimes(kivi.parse_time(":"), undefined); + assert.equalTimes(kivi.parse_time("::"), undefined); + assert.equalTimes(kivi.parse_time("aa:bb"), undefined); + assert.equalTimes(kivi.parse_time("aasd:bbaf"), undefined); + assert.equalTimes(kivi.parse_time(""), null); + assert.equalTimes(kivi.parse_time("0"), new Date()); + assert.equalTimes(kivi.parse_time("29:20008"), custom_time(29,20008)); }); -- 2.20.1