mirror of
https://github.com/alecthomas/chroma.git
synced 2025-03-21 21:17:50 +02:00
This is to solve an issue where writers returned by the Formatter were often stateful, but this fact was not obvious to the API consumer, and failed in interesting ways.
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package html
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/alecthomas/chroma"
|
|
"github.com/alecthomas/chroma/lexers"
|
|
"github.com/alecthomas/chroma/styles"
|
|
)
|
|
|
|
func TestCompressStyle(t *testing.T) {
|
|
style := "color: #888888; background-color: #faffff"
|
|
actual := compressStyle(style)
|
|
expected := "color:#888;background-color:#faffff"
|
|
assert.Equal(t, expected, actual)
|
|
}
|
|
|
|
func BenchmarkHTMLFormatter(b *testing.B) {
|
|
formatter := New()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
it, err := lexers.Go.Tokenise(nil, "package main\nfunc main()\n{\nprintln(`hello world`)\n}\n")
|
|
assert.NoError(b, err)
|
|
err = formatter.Format(ioutil.Discard, styles.Fallback, it)
|
|
assert.NoError(b, err)
|
|
}
|
|
}
|
|
|
|
func TestSplitTokensIntoLines(t *testing.T) {
|
|
in := []*chroma.Token{
|
|
{Value: "hello", Type: chroma.NameKeyword},
|
|
{Value: " world\nwhat?\n", Type: chroma.NameKeyword},
|
|
}
|
|
expected := [][]*chroma.Token{
|
|
[]*chroma.Token{
|
|
{Type: chroma.NameKeyword, Value: "hello"},
|
|
{Type: chroma.NameKeyword, Value: " world\n"},
|
|
},
|
|
[]*chroma.Token{
|
|
{Type: chroma.NameKeyword, Value: "what?\n"},
|
|
},
|
|
[]*chroma.Token{
|
|
{Type: chroma.NameKeyword},
|
|
},
|
|
}
|
|
actual := splitTokensIntoLines(in)
|
|
assert.Equal(t, expected, actual)
|
|
}
|