1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 11:44:38 +01:00
gojekyll/cache/cache_test.go

57 lines
1.3 KiB
Go
Raw Normal View History

2017-07-20 17:32:52 +02:00
package cache
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestWithFile(t *testing.T) {
2017-07-21 22:28:42 +02:00
Enable()
require.NoError(t, Clear())
2017-07-20 17:32:52 +02:00
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")
})
}