1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 09:59:03 +01:00
gojekyll/cache/cache_test.go
2017-07-23 16:21:24 -04:00

57 lines
1.3 KiB
Go

package cache
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestWithFile(t *testing.T) {
Enable()
require.NoError(t, Clear())
callCount := 0
stringMaker := func() (string, error) {
callCount++
return "ok", nil
}
errMaker := func() (string, error) {
return "", fmt.Errorf("expected error")
}
requireOk := func(t *testing.T, s string, err error, expectCallCount int) {
require.Equal(t, expectCallCount, callCount)
require.NoError(t, err)
require.Equal(t, "ok", s)
}
t.Run("calls underlying function", func(t *testing.T) {
s, err := WithFile("h1", "c1", stringMaker)
requireOk(t, s, err, 1)
})
t.Run("cache hit when keys match", func(t *testing.T) {
callCount = 0
s, err := WithFile("h1", "c1", stringMaker)
requireOk(t, s, err, 0)
})
t.Run("cache miss when header differs", func(t *testing.T) {
callCount = 0
s, err := WithFile("h2", "c1", stringMaker)
requireOk(t, s, err, 1)
})
t.Run("cache miss when content differs", func(t *testing.T) {
callCount = 0
s, err := WithFile("h1", "c2", stringMaker)
requireOk(t, s, err, 1)
})
t.Run("propagates error", func(t *testing.T) {
_, err := WithFile("h1-err", "c1", errMaker)
require.Error(t, err)
require.Contains(t, err.Error(), "expected error")
})
}