aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/test_lexlua.lua
diff options
context:
space:
mode:
authormitchell <unknown>2020-04-25 16:26:31 -0400
committermitchell <unknown>2020-04-25 16:26:31 -0400
commitfad15f79b1230b3076be515d6894c8919562809b (patch)
tree72c848ef02c3331de5ca54eff7adaea3a9a6fb88 /test/test_lexlua.lua
parent1fd02a367dec125c0b49dd9246a0928433866b96 (diff)
downloadscintilla-mirror-fad15f79b1230b3076be515d6894c8919562809b.tar.gz
Reformatted Lua LPeg lexers and added new convenience functions and pattern.
`lexer.range()` replaces `lexer.delimited_range()` and `lexer.nested_pair()`. `lexer.to_eol()` replaces `patt * lexer.nonnewline^0` constructs. `lexer.number` replaces `lexer.float + lexer.integer`. Also added unit tests for lexer functions.
Diffstat (limited to 'test/test_lexlua.lua')
-rw-r--r--test/test_lexlua.lua41
1 files changed, 40 insertions, 1 deletions
diff --git a/test/test_lexlua.lua b/test/test_lexlua.lua
index 276f8273c..cd27f4bed 100644
--- a/test/test_lexlua.lua
+++ b/test/test_lexlua.lua
@@ -178,13 +178,50 @@ end
-- Unit tests.
+function test_to_eol()
+ local code = '#foo\\\nbar\\\nbaz'
+ assert(lpeg.match(lexer.to_eol('#'), code) == 6)
+ assert(lpeg.match(lexer.to_eol('#', true), code) == #code + 1)
+end
+
+function test_range()
+ assert(lpeg.match(lexer.range('"'), '"foo\\"bar\n"baz') == 12)
+ assert(lpeg.match(lexer.range('"', true), '"foo\\"bar\n"baz') == 10)
+ assert(lpeg.match(lexer.range('"', false, false), '"foo\\"bar\n"baz') == 7)
+
+ assert(lpeg.match(lexer.range('(', ')'), '(foo\\)bar)baz') == 7)
+
+ assert(lpeg.match(lexer.range('/*', '*/'), '/*/*foo*/bar*/baz') == 10)
+ assert(lpeg.match(lexer.range('/*', '*/', false, false, true),
+ '/*/*foo*/bar*/baz') == 15)
+end
+
+function test_starts_line()
+ assert(lpeg.match(lexer.starts_line('#'), '#foo') == 2)
+ assert(lpeg.match(lexer.starts_line('#'), '\n#foo', 2) == 3)
+ assert(not lpeg.match(lexer.starts_line('#'), ' #foo', 2))
+end
+
+function test_last_char_includes()
+ assert(lpeg.match(lexer.last_char_includes('=,'), '/foo/'))
+ assert(lpeg.match(lexer.last_char_includes('=,'), 'foo=/bar/', 5) == 5)
+ assert(lpeg.match(lexer.last_char_includes('=,'), 'foo, /bar/', 6) == 6)
+ assert(not lpeg.match(lexer.last_char_includes('=,'), 'foo/bar', 4))
+end
+
+function test_word_match()
+ assert(lpeg.match(lexer.word_match[[foo bar baz]], 'foo') == 4)
+ assert(not lpeg.match(lexer.word_match[[foo bar baz]], 'foo_bar'))
+ assert(lpeg.match(lexer.word_match([[foo! bar? baz.]], true), 'FOO!') == 5)
+end
+
-- Tests a basic lexer with a few simple rules and no custom styles.
function test_basics()
local lex = lexer.new('test')
assert_default_styles(lex)
lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
lex:add_rule('keyword', token(lexer.KEYWORD, word_match[[foo bar baz]]))
- lex:add_rule('string', token(lexer.STRING, lexer.delimited_range('"')))
+ lex:add_rule('string', token(lexer.STRING, lexer.range('"')))
lex:add_rule('number', token(lexer.NUMBER, lexer.integer))
local code = [[foo bar baz "foo bar baz" 123]]
local tokens = {
@@ -933,6 +970,8 @@ function test_php()
assert_lex(php, code, tokens, initial_style)
initial_style = php._TOKENSTYLES['default'] -- also test non-ws init style
assert_lex(php, code, tokens, initial_style)
+ initial_style = php._TOKENSTYLES['default'] -- also test non-ws init style
+ assert_lex(php, code, tokens, initial_style)
-- Starting in PHP.
code = [[echo "hi";]]
initial_style = php._TOKENSTYLES['php_whitespace']