aboutsummaryrefslogtreecommitdiff
path: root/test.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test.lua')
-rwxr-xr-xtest.lua98
1 files changed, 92 insertions, 6 deletions
diff --git a/test.lua b/test.lua
index 2c05dd0..3989f30 100755
--- a/test.lua
+++ b/test.lua
@@ -48,8 +48,8 @@ end
48 48
49print"General tests for LPeg library" 49print"General tests for LPeg library"
50 50
51assert(type(m.version()) == "string") 51assert(type(m.version) == "string")
52print("version " .. m.version()) 52print(m.version)
53assert(m.type("alo") ~= "pattern") 53assert(m.type("alo") ~= "pattern")
54assert(m.type(io.input) ~= "pattern") 54assert(m.type(io.input) ~= "pattern")
55assert(m.type(m.P"alo") == "pattern") 55assert(m.type(m.P"alo") == "pattern")
@@ -70,7 +70,6 @@ assert(m.match(#m.P(true) * "a", "a") == 2)
70assert(m.match("a" * #m.P(false), "a") == nil) 70assert(m.match("a" * #m.P(false), "a") == nil)
71assert(m.match("a" * #m.P(true), "a") == 2) 71assert(m.match("a" * #m.P(true), "a") == 2)
72 72
73
74-- tests for locale 73-- tests for locale
75do 74do
76 assert(m.locale(m) == m) 75 assert(m.locale(m) == m)
@@ -406,7 +405,7 @@ assert(p:match('abcx') == 5 and p:match('ayzx') == 5 and not p:match'abc')
406 405
407 406
408do 407do
409 -- large dynamic Cc 408 print "testing large dynamic Cc"
410 local lim = 2^16 - 1 409 local lim = 2^16 - 1
411 local c = 0 410 local c = 0
412 local function seq (n) 411 local function seq (n)
@@ -985,10 +984,10 @@ for i = 1, 10 do
985 assert(p:match("aaaaaaaaaaa") == 11 - i + 1) 984 assert(p:match("aaaaaaaaaaa") == 11 - i + 1)
986end 985end
987 986
988print"+"
989 987
990 988
991-- tests for back references 989print "testing back references"
990
992checkerr("back reference 'x' not found", m.match, m.Cb('x'), '') 991checkerr("back reference 'x' not found", m.match, m.Cb('x'), '')
993checkerr("back reference 'b' not found", m.match, m.Cg(1, 'a') * m.Cb('b'), 'a') 992checkerr("back reference 'b' not found", m.match, m.Cg(1, 'a') * m.Cb('b'), 'a')
994 993
@@ -1032,6 +1031,17 @@ local function id (s, i, ...)
1032 return true, ... 1031 return true, ...
1033end 1032end
1034 1033
1034do -- run-time capture in an end predicate (should discard its value)
1035 local x = 0
1036 function foo (s, i)
1037 x = x + 1
1038 return true, x
1039 end
1040
1041 local p = #(m.Cmt("", foo) * "xx") * m.Cmt("", foo)
1042 assert(p:match("xx") == 2)
1043end
1044
1035assert(m.Cmt(m.Cs((m.Cmt(m.S'abc' / { a = 'x', c = 'y' }, id) + 1045assert(m.Cmt(m.Cs((m.Cmt(m.S'abc' / { a = 'x', c = 'y' }, id) +
1036 m.R'09'^1 / string.char + 1046 m.R'09'^1 / string.char +
1037 m.P(1))^0), id):match"acb98+68c" == "xyb\98+\68y") 1047 m.P(1))^0), id):match"acb98+68c" == "xyb\98+\68y")
@@ -1171,9 +1181,85 @@ t = {p:match('abacc')}
1171checkeq(t, {'a', 'aa', 20, 'a', 'aaa', 'aaa'}) 1181checkeq(t, {'a', 'aa', 20, 'a', 'aaa', 'aaa'})
1172 1182
1173 1183
1184do print"testing large grammars"
1185 local lim = 1000 -- number of rules
1186 local t = {}
1187
1188 for i = 3, lim do
1189 t[i] = m.V(i - 1) -- each rule calls previous one
1190 end
1191 t[1] = m.V(lim) -- start on last rule
1192 t[2] = m.C("alo") -- final rule
1193
1194 local P = m.P(t) -- build grammar
1195 assert(P:match("alo") == "alo")
1196
1197 t[#t + 1] = m.P("x") -- one more rule...
1198 checkerr("too many rules", m.P, t)
1199end
1200
1201
1202print "testing UTF-8 ranges"
1203
1204do -- a few typical UTF-8 ranges
1205 local p = m.utfR(0x410, 0x44f)^1 / "cyr: %0"
1206 + m.utfR(0x4e00, 0x9fff)^1 / "cjk: %0"
1207 + m.utfR(0x1F600, 0x1F64F)^1 / "emot: %0"
1208 + m.utfR(0, 0x7f)^1 / "ascii: %0"
1209 + m.utfR(0, 0x10ffff) / "other: %0"
1210
1211 p = m.Ct(p^0) * -m.P(1)
1212
1213 local cyr = "ждюя"
1214 local emot = "\240\159\152\128\240\159\153\128" -- 😀🙀
1215 local cjk = "专举乸"
1216 local ascii = "alo"
1217 local last = "\244\143\191\191" -- U+10FFFF
1218
1219 local s = cyr .. "—" .. emot .. "—" .. cjk .. "—" .. ascii .. last
1220 t = (p:match(s))
1221
1222 assert(t[1] == "cyr: " .. cyr and t[2] == "other: —" and
1223 t[3] == "emot: " .. emot and t[4] == "other: —" and
1224 t[5] == "cjk: " .. cjk and t[6] == "other: —" and
1225 t[7] == "ascii: " .. ascii and t[8] == "other: " .. last and
1226 t[9] == nil)
1227end
1228
1229
1230do -- valid and invalid code points
1231 local p = m.utfR(0, 0x10ffff)^0
1232 assert(p:match("汉字\128") == #"汉字" + 1)
1233 assert(p:match("\244\159\191") == 1)
1234 assert(p:match("\244\159\191\191") == 1)
1235 assert(p:match("\255") == 1)
1236
1237 -- basic errors
1238 checkerr("empty range", m.utfR, 1, 0)
1239 checkerr("invalid code point", m.utfR, 1, 0x10ffff + 1)
1240end
1241
1242
1243do -- back references (fixed width)
1244 -- match a byte after a CJK point
1245 local p = m.B(m.utfR(0x4e00, 0x9fff)) * m.C(1)
1246 p = m.P{ p + m.P(1) * m.V(1) } -- search for 'p'
1247 assert(p:match("ab д 专X x") == "X")
1248
1249 -- match a byte after a hebrew point
1250 local p = m.B(m.utfR(0x5d0, 0x5ea)) * m.C(1)
1251 p = m.P(#"ש") * p
1252 assert(p:match("שX") == "X")
1253
1254 checkerr("fixed length", m.B, m.utfR(0, 0x10ffff))
1255end
1256
1257
1258
1174------------------------------------------------------------------- 1259-------------------------------------------------------------------
1175-- Tests for 're' module 1260-- Tests for 're' module
1176------------------------------------------------------------------- 1261-------------------------------------------------------------------
1262print"testing 're' module"
1177 1263
1178local re = require "relabel" 1264local re = require "relabel"
1179 1265