font: add test

This commit is contained in:
Jaroslavs Samcuks 2021-06-08 19:53:31 +02:00
parent 29d53b2e35
commit 2b12f707da
2 changed files with 45 additions and 0 deletions

View file

@ -20,9 +20,12 @@ type defaultFont struct {
once sync.Once
}
var _testingHook = func() {}
func (df *defaultFont) Font() (*truetype.Font, error) {
df.once.Do(func() {
df.font, df.err = truetype.Parse(roboto.Roboto)
_testingHook()
})
return df.font, df.err
}

42
font_test.go Normal file
View file

@ -0,0 +1,42 @@
package chart
import (
"testing"
"github.com/golang/freetype/truetype"
)
func TestDefaultFont(t *testing.T) {
inits := make(chan struct{}, 2)
old := _testingHook
_testingHook = func() { inits <- struct{}{} }
defer func() { _testingHook = old }()
type testData struct {
font *truetype.Font
err error
}
calls := make(chan testData, 2)
for i := 0; i < cap(calls); i++ {
go func() {
font, err := GetDefaultFont()
calls <- testData{font, err}
}()
}
var c []testData
for i := 0; i < cap(calls); i++ {
c = append(c, <-calls)
}
if c[0].err != nil || c[1].err != nil {
t.Error("GetDefaultFont got unexpected error: ", c)
}
if c[0].font != c[1].font {
t.Error("GetDefaultFont got different fonts: ", c)
}
if len(inits) > 1 { // Safe to check since we already have two results.
t.Error("GetDefaultFont initialized more than once")
}
}