1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-02-09 13:23:51 +02:00
chroma/delegate_test.go

112 lines
2.6 KiB
Go
Raw Normal View History

2017-09-30 12:44:22 +10:00
package chroma
import (
"testing"
"github.com/stretchr/testify/assert"
2017-09-30 12:44:22 +10:00
)
func makeDelegationTestLexers() (lang Lexer, root Lexer) {
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},
},
}),
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},
},
})
}
func TestDelegate(t *testing.T) {
testdata := []struct {
name string
source string
expected []Token
}{
{"SourceInMiddle", `hello world <? what ?> there`, []Token{
{Keyword, "hello"},
{TextWhitespace, " "},
{Name, "world"},
{TextWhitespace, " "},
// lang
2017-09-30 12:44:22 +10:00
{CommentPreproc, "<?"},
{Whitespace, " "},
{Keyword, "what"},
{Whitespace, " "},
{CommentPreproc, "?>"},
// /lang
{TextWhitespace, " "},
{Name, "there"},
}},
{"SourceBeginning", `<? what ?> hello world there`, []Token{
{CommentPreproc, "<?"},
{TextWhitespace, " "},
{Keyword, "what"},
{TextWhitespace, " "},
{CommentPreproc, "?>"},
{TextWhitespace, " "},
{Keyword, "hello"},
{TextWhitespace, " "},
{Name, "world"},
{TextWhitespace, " "},
{Name, "there"},
}},
{"SourceEnd", `hello world <? what there`, []Token{
{Keyword, "hello"},
{TextWhitespace, " "},
{Name, "world"},
{TextWhitespace, " "},
// lang
2017-09-30 12:44:22 +10:00
{CommentPreproc, "<?"},
{Whitespace, " "},
{Keyword, "what"},
{TextWhitespace, " "},
{Error, "there"},
}},
{"SourceMultiple", "hello world <? what ?> hello there <? what ?> hello", []Token{
{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)
for _, test := range testdata {
2019-07-19 18:57:05 +10:00
// nolint: scopelint
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
}
}