fixed and finished section 3.6.4 + msg-id tests
[mime-lua.git] / test.lua
blob7d6257f01d4fb733450837f3dbed91d688c68c9d
1 #!/usr/bin/lua
3 require 'my'
4 require 'lpeg'
6 local gr = dofile'mime.lua'
8 local match = function (s, r, c)
9 c= c or s
10 local ret = { string = s, rule = r, match = true, capture = c }
11 return ret
12 end
14 local tests = {
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'},
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 (...)
33 --print(...)
34 return ...
35 end
37 local gather = function (...)
38 local n = select('#', ...)
39 return n==1 and ... or { ... }
40 end
42 for i, t in ipairs(tests) do
43 local s, r, m, c = t.string, t.rule, t.match, t.capture
44 gr[1] = (lpeg.V(r)) / gather * lpeg.Cp()
45 local patt = lpeg.P(gr)
46 print('performing test ' .. i .. ' on rule ' .. r)
47 local ret, n = dee(patt:match(s))
48 if m then
49 assert(ret, 'test '..i..' failed by not matching a conforming string')
50 assert(n==#s+1, 'test '..i..' failed by not matching the full string (match was '..string.sub(s, 1, n-1)..')')
51 if c then
52 assert(ret==c, 'test '..i..' failed by returning the wrong capture '..ret..' instead of '..c)
53 end
54 else
55 assert(ret==nil, 'test '..i..' failed by matching a wrong string')
56 end
57 end