1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-01-14 02:23:16 +02:00
chroma/remap_test.go
Alec Thomas 102aa61ac3 Add RemappingLexer.
This pattern is used heavily in Pygments for certain lexers,
particularly Lisp variants. See #43.
2017-09-27 11:30:55 +10:00

30 lines
722 B
Go

package chroma
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRemappingLexer(t *testing.T) {
var lexer Lexer = MustNewLexer(nil, Rules{
"root": {
{`\s+`, Whitespace, nil},
{`\w+`, Name, nil},
},
})
lexer = TypeRemappingLexer(lexer, TypeRemappingMap{
{Name, Keyword}: {"if", "else"},
})
it, err := lexer.Tokenise(nil, `if true then print else end`)
assert.NoError(t, err)
expected := []*Token{
{Keyword, "if"}, {TextWhitespace, " "}, {Name, "true"}, {TextWhitespace, " "}, {Name, "then"},
{TextWhitespace, " "}, {Name, "print"}, {TextWhitespace, " "}, {Keyword, "else"},
{TextWhitespace, " "}, {Name, "end"},
}
actual := it.Tokens()
assert.Equal(t, expected, actual)
}