2017-07-19 23:51:16 -07:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
2017-09-20 22:30:25 +10:00
|
|
|
"errors"
|
2017-09-19 23:04:10 +10:00
|
|
|
"io/ioutil"
|
2017-12-05 18:00:44 +01:00
|
|
|
"strings"
|
2017-07-19 23:51:16 -07:00
|
|
|
"testing"
|
|
|
|
|
2017-09-29 21:59:52 +10:00
|
|
|
"github.com/alecthomas/assert"
|
2017-09-20 13:30:46 +10:00
|
|
|
"github.com/alecthomas/chroma"
|
2017-09-19 23:04:10 +10:00
|
|
|
"github.com/alecthomas/chroma/lexers"
|
|
|
|
"github.com/alecthomas/chroma/styles"
|
2017-07-19 23:51:16 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCompressStyle(t *testing.T) {
|
2017-09-18 14:19:59 +10:00
|
|
|
style := "color: #888888; background-color: #faffff"
|
2017-07-19 23:51:16 -07:00
|
|
|
actual := compressStyle(style)
|
2017-09-18 14:19:59 +10:00
|
|
|
expected := "color:#888;background-color:#faffff"
|
2017-09-19 23:04:10 +10:00
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHTMLFormatter(b *testing.B) {
|
|
|
|
formatter := New()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2018-02-15 21:01:44 +11:00
|
|
|
it, err := lexers.Get("go").Tokenise(nil, "package main\nfunc main()\n{\nprintln(`hello world`)\n}\n")
|
2017-09-20 22:19:36 +10:00
|
|
|
assert.NoError(b, err)
|
|
|
|
err = formatter.Format(ioutil.Discard, styles.Fallback, it)
|
2017-09-19 23:04:10 +10:00
|
|
|
assert.NoError(b, err)
|
|
|
|
}
|
2017-07-19 23:51:16 -07:00
|
|
|
}
|
2017-09-20 13:30:46 +10:00
|
|
|
|
|
|
|
func TestSplitTokensIntoLines(t *testing.T) {
|
|
|
|
in := []*chroma.Token{
|
|
|
|
{Value: "hello", Type: chroma.NameKeyword},
|
|
|
|
{Value: " world\nwhat?\n", Type: chroma.NameKeyword},
|
|
|
|
}
|
|
|
|
expected := [][]*chroma.Token{
|
2017-09-26 22:05:44 +10:00
|
|
|
{
|
2017-09-20 13:30:46 +10:00
|
|
|
{Type: chroma.NameKeyword, Value: "hello"},
|
|
|
|
{Type: chroma.NameKeyword, Value: " world\n"},
|
|
|
|
},
|
2017-09-26 22:05:44 +10:00
|
|
|
{
|
2017-09-20 13:30:46 +10:00
|
|
|
{Type: chroma.NameKeyword, Value: "what?\n"},
|
|
|
|
},
|
2017-09-26 22:05:44 +10:00
|
|
|
{
|
2017-09-20 13:30:46 +10:00
|
|
|
{Type: chroma.NameKeyword},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actual := splitTokensIntoLines(in)
|
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|
2017-09-20 22:30:25 +10:00
|
|
|
|
|
|
|
func TestIteratorPanicRecovery(t *testing.T) {
|
|
|
|
it := func() *chroma.Token {
|
|
|
|
panic(errors.New("bad"))
|
|
|
|
}
|
|
|
|
err := New().Format(ioutil.Discard, styles.Fallback, it)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2017-12-05 18:00:44 +01:00
|
|
|
|
2018-01-02 20:59:02 +11:00
|
|
|
func TestFormatterStyleToCSS(t *testing.T) {
|
2017-12-05 18:00:44 +01:00
|
|
|
builder := styles.Get("github").Builder()
|
|
|
|
builder.Add(chroma.LineHighlight, "bg:#ffffcc")
|
|
|
|
builder.Add(chroma.LineNumbers, "bold")
|
|
|
|
style, err := builder.Build()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
formatter := New(WithClasses())
|
|
|
|
css := formatter.styleToCSS(style)
|
|
|
|
for _, s := range css {
|
|
|
|
if strings.HasPrefix(strings.TrimSpace(s), ";") {
|
|
|
|
t.Errorf("rule starts with semicolon - expected valid css rule without semicolon: %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|