From 2b12f707daf254ec9b67a52f6673699864b402de Mon Sep 17 00:00:00 2001 From: Jaroslavs Samcuks Date: Tue, 8 Jun 2021 19:53:31 +0200 Subject: [PATCH] font: add test --- font.go | 3 +++ font_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 font_test.go diff --git a/font.go b/font.go index 48a2a3d..e1a59bb 100644 --- a/font.go +++ b/font.go @@ -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 } diff --git a/font_test.go b/font_test.go new file mode 100644 index 0000000..34eafb5 --- /dev/null +++ b/font_test.go @@ -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") + } +}