6 local gr
= dofile'mime.lua'
8 local match
= function (s
, r
, c
)
10 local ret
= { string = s
, rule
= r
, match
= true, capture
= c
}
15 { string = '\r\n ', rule
= 'FWS', match
= true, capture
= ' ' },
16 { string = '"rrr\r\n aaa"', rule
='quoted-string', match
= true, capture
= 'rrr aaa'},
17 { string = '(some stupid @(comment) \\( ** \r\n )', rule
='comment', match
= true, capture
= ' '},
18 { string = 'single_atom_1', rule
='atom', match
= true, capture
= 'single_atom_1'},
19 { string = '2nd{atom}2', rule
='atom', match
= true, capture
= '2nd{atom}2'},
20 { string = '2nd{atom}2.&checking!', rule
='dot-atom', match
= true, capture
= '2nd{atom}2.&checking!'},
21 { string = '\r\n (another comment ) \t\r\n (id %(crmnr) \\( ** \r\n ) ', rule
='CFWS', match
= true, capture
= ' '},
22 { string = '()', rule
='CFWS', match
= true, capture
= ' '},
23 { string = 'Icanwrite"a \r\n simple"phrase', rule
='phrase', match
= true},
24 { 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' }},
25 { string = '(a CFWS) \r\n (that ends here)[ 127.0.0.1:8888 oooo]', rule
='domain-literal', match
= true, },
26 { string = 'mauro.iazzi@gmail.com', rule
='address', match
= true, },
27 { string = '"Mauro Iazzi" <mauro.iazzi@gmail.com>', rule
='address', match
= true, },
28 { string = '<mauro.iazzi@gmail.com>', rule
='msg-id', match
= true, },
29 { string = '<"sent\\ by\\ \\"M.I.\\"mauro.iazzi"@[from\\ domain\\ gmail.com]>', rule
='msg-id', match
= true, },
32 local dee
= function (...)
37 local gather
= function (...)
38 local n
= select('#', ...)
39 return n
==1 and ... or { ... }
42 local function equal (a
, b
)
43 if a
==b
then return true end
44 if type(a
)~=type(b
) then return false end
45 if type(a
)=='table' then
46 for k
, v
in pairs(a
) do
47 if not equal(b
[k
], v
) then return false end
49 for k
, v
in pairs(b
) do
50 if not equal(a
[k
], v
) then return false end
57 print("starting the tests ...")
58 for i
, t
in ipairs(tests
) do
59 local s
, r
, m
, c
= t
.string, t
.rule
, t
.match
, t
.capture
60 gr
[1] = (lpeg
.V(r
)) / gather
* lpeg
.Cp()
61 local patt
= lpeg
.P(gr
)
62 print('performing test ' .. i
.. ' on rule ' .. r
)
63 local ret
, n
= dee(patt
:match(s
))
65 assert(ret
, 'test '..i
..' failed by not matching a conforming string')
66 assert(n
==#s
+1, 'test '..i
..' failed by not matching the full string (match was '..string.sub(s
, 1, n
-1)..')')
68 assert(equal(ret
, c
), 'test '..i
..' failed by returning the wrong capture '..tostring(ret
)..' instead of '..tostring(c
))
71 assert(ret
==nil, 'test '..i
..' failed by matching a wrong string')