[Tarantool-patches] [RFC PATCH 06/13] lua: positive/negative cases in datetime test
Timur Safin
tsafin at tarantool.org
Thu Jul 15 11:18:12 MSK 2021
- 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
More information about the Tarantool-patches
mailing list