2017-09-30 12:44:22 +10:00
|
|
|
package chroma
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-06-18 16:55:49 +10:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-09-30 12:44:22 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
func makeDelegationTestLexers() (lang Lexer, root Lexer) {
|
2021-04-29 12:07:50 +10:00
|
|
|
return MustNewLexer(nil, Rules{ // nolint: forbidigo
|
2017-09-30 12:44:22 +10:00
|
|
|
"root": {
|
|
|
|
{`\<\?`, CommentPreproc, Push("inside")},
|
|
|
|
{`.`, Other, nil},
|
|
|
|
},
|
|
|
|
"inside": {
|
|
|
|
{`\?\>`, CommentPreproc, Pop(1)},
|
|
|
|
{`\bwhat\b`, Keyword, nil},
|
|
|
|
{`\s+`, Whitespace, nil},
|
|
|
|
},
|
|
|
|
}),
|
2021-04-29 12:07:50 +10:00
|
|
|
MustNewLexer(nil, Rules{ // nolint: forbidigo
|
2017-09-30 12:44:22 +10:00
|
|
|
"root": {
|
|
|
|
{`\bhello\b`, Keyword, nil},
|
|
|
|
{`\b(world|there)\b`, Name, nil},
|
|
|
|
{`\s+`, Whitespace, nil},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:38:29 +11:00
|
|
|
func TestDelegate(t *testing.T) {
|
|
|
|
testdata := []struct {
|
|
|
|
name string
|
|
|
|
source string
|
2018-11-03 16:22:51 -07:00
|
|
|
expected []Token
|
2018-03-18 20:38:29 +11:00
|
|
|
}{
|
2018-11-03 16:22:51 -07:00
|
|
|
{"SourceInMiddle", `hello world <? what ?> there`, []Token{
|
2018-03-18 20:38:29 +11:00
|
|
|
{Keyword, "hello"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Name, "world"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
// lang
|
2017-09-30 12:44:22 +10:00
|
|
|
{CommentPreproc, "<?"},
|
|
|
|
{Whitespace, " "},
|
|
|
|
{Keyword, "what"},
|
|
|
|
{Whitespace, " "},
|
|
|
|
{CommentPreproc, "?>"},
|
2018-03-18 20:38:29 +11:00
|
|
|
// /lang
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Name, "there"},
|
|
|
|
}},
|
2018-11-03 16:22:51 -07:00
|
|
|
{"SourceBeginning", `<? what ?> hello world there`, []Token{
|
2018-03-18 20:38:29 +11:00
|
|
|
{CommentPreproc, "<?"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Keyword, "what"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{CommentPreproc, "?>"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Keyword, "hello"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Name, "world"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Name, "there"},
|
|
|
|
}},
|
2018-11-03 16:22:51 -07:00
|
|
|
{"SourceEnd", `hello world <? what there`, []Token{
|
2018-03-18 20:38:29 +11:00
|
|
|
{Keyword, "hello"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Name, "world"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
// lang
|
2017-09-30 12:44:22 +10:00
|
|
|
{CommentPreproc, "<?"},
|
|
|
|
{Whitespace, " "},
|
|
|
|
{Keyword, "what"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Error, "there"},
|
2018-03-18 20:38:29 +11:00
|
|
|
}},
|
2018-11-03 16:22:51 -07:00
|
|
|
{"SourceMultiple", "hello world <? what ?> hello there <? what ?> hello", []Token{
|
2018-03-18 20:38:29 +11:00
|
|
|
{Keyword, "hello"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Name, "world"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{CommentPreproc, "<?"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Keyword, "what"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{CommentPreproc, "?>"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Keyword, "hello"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Name, "there"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{CommentPreproc, "<?"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Keyword, "what"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{CommentPreproc, "?>"},
|
|
|
|
{TextWhitespace, " "},
|
|
|
|
{Keyword, "hello"},
|
|
|
|
}},
|
2017-09-30 12:44:22 +10:00
|
|
|
}
|
|
|
|
lang, root := makeDelegationTestLexers()
|
|
|
|
delegate := DelegatingLexer(root, lang)
|
2018-03-18 20:38:29 +11:00
|
|
|
for _, test := range testdata {
|
2019-07-19 18:57:05 +10:00
|
|
|
// nolint: scopelint
|
2018-03-18 20:38:29 +11:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
it, err := delegate.Tokenise(nil, test.source)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
actual := it.Tokens()
|
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
})
|
2017-09-30 12:44:22 +10:00
|
|
|
}
|
|
|
|
}
|