From d1d22e9e3eace20b8161b469a3ee2d20b713e65f Mon Sep 17 00:00:00 2001 From: Mauro Iazzi Date: Tue, 14 Apr 2009 15:26:55 +0200 Subject: [PATCH] always perform all tests --- test.lua | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/test.lua b/test.lua index 15ba6a2..3b5f551 100644 --- a/test.lua +++ b/test.lua @@ -13,7 +13,8 @@ end local tests = { { string = '\r\n ', rule = 'FWS', match = true, capture = ' ' }, - { string = '"rrr\r\n aaa"', rule ='quoted-string', match = true, capture = 'rrr aaa'}, + { string = '"rrr\r\n aaa"', rule = lpeg.Cs(lpeg.V'quoted-string'), match = true, capture = 'rrr aaa'}, + { string = '"rrr \\$\r\n aaa"', rule = lpeg.Cs'quoted-string', match = true, capture = 'rrr $ aaa'}, { string = '(some stupid @(comment) \\( ** \r\n )', rule ='comment', match = true, capture = ' '}, { string = 'single_atom_1', rule ='atom', match = true, capture = 'single_atom_1'}, { string = '2nd{atom}2', rule ='atom', match = true, capture = '2nd{atom}2'}, @@ -23,8 +24,9 @@ local tests = { { string = 'Icanwrite"a \r\n simple"phrase', rule ='phrase', match = true}, { string = 'Thu, 2\r\n Apr 2009 14:36:04 +0000', rule ='date-time', match = true, capture={ 'Thu, 2 Apr 2009 14:36:04 +0000', weekday='Thu', year='2009', month='Apr', day='2', zone='+0000', minute='36', hour='14', second='04' }}, { string = '(a CFWS) \r\n (that ends here)[ 127.0.0.1:8888 oooo]', rule ='domain-literal', match = true, }, - { string = 'mauro.iazzi@gmail.com', rule ='address', match = true, }, - { string = '"Mauro Iazzi" ', rule ='address', match = true, }, + { string = 'mauro.iazzi@gmail.com', rule ='address', match = true, capture={ [1]='mauro.iazzi@gmail.com', box='mauro.iazzi', domain='gmail.com' }, }, + { string = '"Mauro Iazzi" <"mauro\\2\r\n .iazzi"@gmail.com>', rule ='address', match = true, capture={ [1]='"Mauro Iazzi" <"mauro2.iazzi"@gmail.com>', box='mauro.iazzi', domain='gmail.com' }, }, + { string = '"Mauro \r\n Iazzi" (a comment?) ', rule ='address', match = true, capture={ [1]='"Mauro Iazzi" ', box='mauro.iazzi', domain='gmail.com' }, }, { string = '', rule ='msg-id', match = true, }, { string = '<"sent\\ by\\ \\"M.I.\\"mauro.iazzi"@[from\\ domain\\ gmail.com]>', rule ='msg-id', match = true, }, } @@ -40,26 +42,29 @@ local gather = function (...) end local function equal (a, b) + --print('', a, b) if a==b then return true end if type(a)~=type(b) then return false end if type(a)=='table' then for k, v in pairs(a) do - if not equal(b[k], v) then return false end + if not equal(v, b[k]) then return false end end for k, v in pairs(b) do - if not equal(a[k], v) then return false end + if not equal(v, a[k]) then return false end end return true end return false end -print("starting the tests ...") -for i, t in ipairs(tests) do +local do_test = function (i, t) local s, r, m, c = t.string, t.rule, t.match, t.capture - gr[1] = (lpeg.V(r)) / gather * lpeg.Cp() + local rn + if type(r)=='string' then + r = lpeg.V(r) + end + gr[1] = r / gather * lpeg.Cp() local patt = lpeg.P(gr) - print('performing test ' .. i .. ' on rule ' .. r) local ret, n = dee(patt:match(s)) if m then assert(ret, 'test '..i..' failed by not matching a conforming string') @@ -71,5 +76,12 @@ for i, t in ipairs(tests) do assert(ret==nil, 'test '..i..' failed by matching a wrong string') end end +print("starting the tests ...") +for i, t in ipairs(tests) do + io.write('performing test ' .. i .. ' on rule ' .. tostring(t.rule)) + local st, err = pcall(do_test, i, t) + if st then err = 'success' end + print('', err) +end -- 2.11.4.GIT