diff --git a/libraries/cron.lua b/libraries/cron.lua index 3c42114..3c0d56a 100644 --- a/libraries/cron.lua +++ b/libraries/cron.lua @@ -72,16 +72,16 @@ cron._compare_tables = function(d1,d2) end -- Token types, in (regex, type, preprocessor) format local token_types = { - {"@(%w+)", "directive", function(text) + {"^@(%w+)$", "directive", function(text) return text end}, - {"(%d%d)%.(%d%d)%.(%d%d%d?%d?)","date",function(d,m,y) + {"^(%d%d)%.(%d%d)%.(%d%d%d?%d?)$","date",function(d,m,y) return cron._date(d,m,y) end}, - {"(%d%d):(%d%d)","time",function(h,m) + {"^(%d%d):(%d%d)$","time",function(h,m) return cron._time(h,m) end}, - {"(%d*,.*)", "any_list",function(text) + {"^(%d+,[%d,]+)$", "any_list",function(text) return function(num) local status = false text:gsub("%d*",function(number) @@ -92,27 +92,27 @@ local token_types = { return status end end}, - {"%*/(%d+)", "any_modulo", function(text) + {"^%*/(%d+)$", "any_modulo", function(text) return function(num) return (num % tonumber(text) == 0) end end}, - {"%*", "any", function() + {"^%*$", "any", function() return function() return true end end}, - {"%d+", "number", function(text) + {"^%d+$", "number", function(text) return function(num) return num == tonumber(text) end end}, {"^%s*$","spacer", function(text) return text end}, - {"%S+","command", function(text) return text end} + {"^%S+$","command", function(text) return text end} } -- Valid argument matching predicates for directives local predtypes = { - {"([<>])(=?)(%d*)","comparison",function(lm,eq,number) + {"^([<>])(=?)(%d+)$","comparison",function(lm,eq,number) local number = tonumber(number) return function(input) local input = tonumber(input) @@ -122,42 +122,42 @@ local predtypes = { ((lm == "<") and number > input) end end}, - {"/([^/]*)/","regex",function(regex) + {"^/([^/]*)/$","regex",function(regex) return function(input) return (tostring(input):match(regex) ~= nil) end end}, - {"\"([^\"]*)\"","string",function(str) + {"^\"([^\"]*)\"$","string",function(str) return function(input) return str==tostring(input) end end}, - {"'([^']*)'","string",function(str) + {"^'([^']*)'$","string",function(str) return function(input) return str==tostring(input) end end}, - {"%d+","number",function(number) + {"^%d+$","number",function(number) return function(input) return number == tostring(input) end end}, - {"%*","any",function() + {"^%*$","any",function() return function() return true end end}, - {":","delimiter",function() + {"^:$","delimiter",function() return function() error("Delimiter is not a predicate!") end end}, - {"%s+","spacer",function() + {"^%s+$","spacer",function() return function() error("Spacer is not a predicate!") end end}, - {"%S+","command", function(text) + {"^%S+$","command", function(text) return function() return text end diff --git a/libraries/crontests.lua b/libraries/crontests.lua new file mode 100644 index 0000000..f214949 --- /dev/null +++ b/libraries/crontests.lua @@ -0,0 +1,21 @@ +cron = require("cron"); b,err = cron.parse_line("@directive /test/ >6 : s"); print(b,err); s,comm = b({"test",7}); print(s,comm) +-- Testing basic functionality +b,err = cron.parse_line("@directive /test/ >6 : s"); print(b,err); print(b({"test",5})); +b,err = cron.parse_line("@directive \"test\" >6 : s"); print(b,err); print(b({"nottest",7})); +b,err = cron.parse_line("*/2 14 */2 * * test"); print(b,err); print(b(os.date("*t"))); +b,err = cron.parse_line("08.05.22 15:30 test"); print(b,err); print(b(os.date("*t"))); +b,err = cron.parse_line("12:00 08.05.22 test"); print(b,err); print(b(os.date("*t"))); +-- Testing command forming +b,err = cron.parse_line("08.05.22 15:30 test 2 the mega test"); print(b,err); print(b(os.date("*t"))); +b,err = cron.parse_line("12:00 08.05.22 test supreme"); print(b,err); print(b(os.date("*t"))); +b,err = cron.parse_line("*/2 14 */2 * * test of course yet another"); print(b,err); print(b(os.date("*t"))); +-- Testing parsing limits +b,err = cron.parse_line("* * * test"); print(b,err); +b,err = cron.parse_line("* * * * * * test"); print(b,err); --actually valid - and should be! +b,err = cron.parse_line("@directive /test/ >6 no delimiter error"); print(b,err); +b,err = cron.parse_line("08.05.22 shit"); print(b,err); +b,err = cron.parse_line("10:20 shit"); print(b,err); +-- Testing parsing dates and time +b,err = cron.parse_line("51.02.2022 shit"); print(b,err); +b,err = cron.parse_line("25:69 shit"); print(b,err); +