From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 199B96EC55; Thu, 15 Jul 2021 11:21:54 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 199B96EC55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1626337314; bh=pfjF9k5t07/eLVUMOaABJjjIi94TcH/6YUQ6gWeIeWE=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=AOqKqgvXX0Hs0JOUuAevoO/z1gXqzvKYi4lhyrt6gmT1ChVejTtWl7VNngC3731Mz 0KnM15qz/T25/ZjrvjPj0Kkmxf1Fg+PsjsR090gZ4LRSe/AhBbsg+L7v9yTcscshIs /jRM+bo7kj4Zw+NfRnv17/JVDnadfaHAAI5a4qsU= Received: from smtp57.i.mail.ru (smtp57.i.mail.ru [217.69.128.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 0E51F6EC7F for ; Thu, 15 Jul 2021 11:18:56 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 0E51F6EC7F Received: by smtp57.i.mail.ru with esmtpa (envelope-from ) id 1m3wa6-00070N-Rp; Thu, 15 Jul 2021 11:18:55 +0300 To: v.shpilevoy@tarantool.org Date: Thu, 15 Jul 2021 11:18:12 +0300 Message-Id: <0593a9ff8103b36f63b7cf42da440bbb6bf82173.1626335241.git.tsafin@tarantool.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: 78E4E2B564C1792B X-77F55803: 4F1203BC0FB41BD941C43E597735A9C36A98DBA789EBB6AEB5EDF95B45F83566182A05F5380850406A560AFC193D4682CAF3E9A9CCF47C0242ED5B9DDE767A2AB0A08341C5FE15D5 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE70A10A23A3B64B805EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637B100969577675F2D8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D82EDF72DD08ACA2E5DB2A31E2DD7BBD66117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC8C7ADC89C2F0B2A5A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18CB629EEF1311BF91D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EEC24E1E72F37C03A0A5FD2DD08EB4836CD8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE3BC0ADEB1C81BB3629735652A29929C6CC4224003CC836476DE5E5A0AC0E6B816E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F7900637AD0424077D726551EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CE68746B1F2AB10C60BD819FE242530872AE4FF50A494AB5B9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF3033054805BDE987699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34047E322BFAFD69BD7BA7609C055427C6D95CE03D137EB6FF2D6A0D01672A47984FAF14A2E69A11351D7E09C32AA3244C3ABBABC2805C5AC9AC49C3A6209912DB8894E9C85370243E83B48618A63566E0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojbL9S8ysBdXjnYxxC9kpVb5sz7xFu3/mc X-Mailru-Sender: B5B6A6EBBD94DAD8DA84A184D75F19DB2E7B7B20C3E75D0F708F0C6557166EA384AE352CB11A2F311EC9E4A2C82A33BC8C24925A86E657CE0C70AEE3C9A96FBAB3D7EE8ED63280BE112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [RFC PATCH 06/13] lua: positive/negative cases in datetime test X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Timur Safin via Tarantool-patches Reply-To: Timur Safin Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" - extended api of datetime.parse_date, .parse_time, .parse_time_zone with the length of parsed (sub)string. - This allow us to check partially valid strings like "20121224 Foo bar" in the updated test app-tap/datetime.test.lua --- src/lua/datetime.lua | 19 ++++----- test/app-tap/datetime.test.lua | 78 +++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 12 deletions(-) diff --git a/src/lua/datetime.lua b/src/lua/datetime.lua index cffa38cd5..670123b1f 100644 --- a/src/lua/datetime.lua +++ b/src/lua/datetime.lua @@ -318,7 +318,7 @@ local function datetime_new(o) end, month = function(v) - assert(v > 0 and v < 12 ) + assert(v > 0 and v < 13 ) M = v ymd = true end, @@ -393,9 +393,8 @@ end local function parse_date(str) local dt = ffi.new('dt_t[1]') - local rc = cdt.dt_parse_iso_date(str, #str, dt) - assert(rc > 0) - return mk_timestamp(dt[0]) + local len = cdt.dt_parse_iso_date(str, #str, dt) + return len > 0 and mk_timestamp(dt[0]) or nil, tonumber(len) end --[[ @@ -411,9 +410,8 @@ end local function parse_time(str) local sp = ffi.new('int[1]') local fp = ffi.new('int[1]') - local rc = cdt.dt_parse_iso_time(str, #str, sp, fp) - assert(rc > 0) - return mk_timestamp(nil, sp[0], fp[0]) + local len = cdt.dt_parse_iso_time(str, #str, sp, fp) + return len > 0 and mk_timestamp(nil, sp[0], fp[0]) or nil, tonumber(len) end --[[ @@ -424,9 +422,8 @@ end ]] local function parse_zone(str) local offset = ffi.new('int[1]') - local rc = cdt.dt_parse_iso_zone(str, #str, offset) - assert(rc > 0) - return mk_timestamp(nil, nil, nil, offset[0]) + local len = cdt.dt_parse_iso_zone(str, #str, offset) + return len > 0 and mk_timestamp(nil, nil, nil, offset[0]) or nil, tonumber(len) end @@ -449,7 +446,7 @@ local function parse_str(str) str = str:sub(tonumber(n) + 1) local ch = str:sub(1,1) - if ch ~= 't' and ch ~= 'T' and ch ~= ' ' then + if ch:match('[Tt ]') == nil then return mk_timestamp(dt_) end diff --git a/test/app-tap/datetime.test.lua b/test/app-tap/datetime.test.lua index 914371747..b79450ecc 100755 --- a/test/app-tap/datetime.test.lua +++ b/test/app-tap/datetime.test.lua @@ -4,7 +4,7 @@ local tap = require('tap') local test = tap.test("errno") local date = require('datetime') -test:plan(3) +test:plan(5) test:test("Simple tests for parser", function(test) test:plan(2) @@ -112,4 +112,80 @@ test:test("Datetime string formatting", function(test) test:ok(date.strftime('%A %d. %B %Y', t) == 'Thursday 01. January 1970', ('%s: strftime #2'):format(str)) end) +test:test("Parse iso date - valid strings", function(test) + test:plan(32) + local good = { + {2012, 12, 24, "20121224", 8 }, + {2012, 12, 24, "20121224 Foo bar", 8 }, + {2012, 12, 24, "2012-12-24", 10 }, + {2012, 12, 24, "2012-12-24 23:59:59", 10 }, + {2012, 12, 24, "2012-12-24T00:00:00+00:00", 10 }, + {2012, 12, 24, "2012359", 7 }, + {2012, 12, 24, "2012359T235959+0130", 7 }, + {2012, 12, 24, "2012-359", 8 }, + {2012, 12, 24, "2012W521", 8 }, + {2012, 12, 24, "2012-W52-1", 10 }, + {2012, 12, 24, "2012Q485", 8 }, + {2012, 12, 24, "2012-Q4-85", 10 }, + { 1, 1, 1, "0001-Q1-01", 10 }, + { 1, 1, 1, "0001-W01-1", 10 }, + { 1, 1, 1, "0001-01-01", 10 }, + { 1, 1, 1, "0001-001", 8 }, + } + + for _, value in ipairs(good) do + local year, month, day, str, date_part_len; + year, month, day, str, date_part_len = unpack(value) + local expected_date = date{year = year, month = month, day = day} + local date_part, len + date_part, len = date.parse_date(str) + test:ok(len == date_part_len, ('%s: length check %d'):format(str, len)) + test:ok(expected_date == date_part, ('%s: expected date'):format(str)) + end +end) + +test:test("Parse iso date - invalid strings", function(test) + test:plan(62) + local bad = { + "20121232" , -- Invalid day of month + "2012-12-310", -- Invalid day of month + "2012-13-24" , -- Invalid month + "2012367" , -- Invalid day of year + "2012-000" , -- Invalid day of year + "2012W533" , -- Invalid week of year + "2012-W52-8" , -- Invalid day of week + "2012Q495" , -- Invalid day of quarter + "2012-Q5-85" , -- Invalid quarter + "20123670" , -- Trailing digit + "201212320" , -- Trailing digit + "2012-12" , -- Reduced accuracy + "2012-Q4" , -- Reduced accuracy + "2012-Q42" , -- Invalid + "2012-Q1-1" , -- Invalid day of quarter + "2012Q--420" , -- Invalid + "2012-Q-420" , -- Invalid + "2012Q11" , -- Incomplete + "2012Q1234" , -- Trailing digit + "2012W12" , -- Incomplete + "2012W1234" , -- Trailing digit + "2012W-123" , -- Invalid + "2012-W12" , -- Incomplete + "2012-W12-12", -- Trailing digit + "2012U1234" , -- Invalid + "2012-1234" , -- Invalid + "2012-X1234" , -- Invalid + "0000-Q1-01" , -- Year less than 0001 + "0000-W01-1" , -- Year less than 0001 + "0000-01-01" , -- Year less than 0001 + "0000-001" , -- Year less than 0001 + } + + for _, str in ipairs(bad) do + local date_part, len + date_part, len = date.parse_date(str) + test:ok(len == 0, ('%s: length check %d'):format(str, len)) + test:ok(date_part == nil, ('%s: empty date check %s'):format(str, date_part)) + end +end) + os.exit(test:check() and 0 or 1) -- 2.29.2