diff --git a/examples/simple/simple_test.go b/examples/simple/simple_test.go index 4706d62..40296a3 100644 --- a/examples/simple/simple_test.go +++ b/examples/simple/simple_test.go @@ -12,7 +12,7 @@ import ( // counter fixture - increment globalCounter every non cached call // and return new globalCounter value func counter(e fixenv.Env) int { - return fixenv.Cache(e, "", nil, func() (res int, err error) { + return fixenv.Cache(e, nil, nil, func() (res int, err error) { globalCounter++ return globalCounter, nil }) diff --git a/examples/simple/use_cache_params_test.go b/examples/simple/use_cache_params_test.go new file mode 100644 index 0000000..0f9146f --- /dev/null +++ b/examples/simple/use_cache_params_test.go @@ -0,0 +1,29 @@ +//go:build go1.18 +// +build go1.18 + +package simple + +import ( + "math/rand" + "testing" + + "github.com/rekby/fixenv" + "github.com/stretchr/testify/require" +) + +// namedRandom return random number for new name args +// but return same value for all calls with same names +func namedRandom(e fixenv.Env, name string) int { + return fixenv.Cache(e, name, nil, func() (res int, err error) { + return rand.Int(), nil + }) +} + +func TestNamedRandom(t *testing.T) { + e := fixenv.NewEnv(t) + first := namedRandom(e, "first") + second := namedRandom(e, "second") + require.NotEqual(t, first, second) + require.Equal(t, first, namedRandom(e, "first")) + require.Equal(t, second, namedRandom(e, "second")) +}