1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-21 21:17:50 +02:00
chroma/regexp_test.go
2019-11-05 13:57:51 +11:00

46 lines
1.1 KiB
Go

package chroma
import (
"testing"
"github.com/alecthomas/assert"
)
func TestNewlineAtEndOfFile(t *testing.T) {
l := Coalesce(MustNewLexer(&Config{EnsureNL: true}, Rules{
"root": {
{`(\w+)(\n)`, ByGroups(Keyword, Whitespace), nil},
},
}))
it, err := l.Tokenise(nil, `hello`)
assert.NoError(t, err)
assert.Equal(t, []Token{{Keyword, "hello"}, {Whitespace, "\n"}}, it.Tokens())
l = Coalesce(MustNewLexer(nil, Rules{
"root": {
{`(\w+)(\n)`, ByGroups(Keyword, Whitespace), nil},
},
}))
it, err = l.Tokenise(nil, `hello`)
assert.NoError(t, err)
assert.Equal(t, []Token{{Error, "hello"}}, it.Tokens())
}
func TestMatchingAtStart(t *testing.T) {
l := Coalesce(MustNewLexer(&Config{}, Rules{
"root": {
{`\s+`, Whitespace, nil},
{`^-`, Punctuation, Push("directive")},
{`->`, Operator, nil},
},
"directive": {
{"module", NameEntity, Pop(1)},
},
}))
it, err := l.Tokenise(nil, `-module ->`)
assert.NoError(t, err)
assert.Equal(t,
[]Token{{Punctuation, "-"}, {NameEntity, "module"}, {Whitespace, " "}, {Operator, "->"}},
it.Tokens())
}