1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "golang.org/x/tools/internal/memoize"
15 func TestGet(t *testing.T) {
17 g := s.Generation("x")
21 h := g.Bind("key", func(context.Context, memoize.Arg) interface{} {
25 expectGet(t, h, g, "res")
26 expectGet(t, h, g, "res")
28 t.Errorf("got %v calls to function, wanted 1", evaled)
32 func expectGet(t *testing.T, h *memoize.Handle, g *memoize.Generation, wantV interface{}) {
33 gotV, gotErr := h.Get(context.Background(), g, nil)
34 if gotV != wantV || gotErr != nil {
35 t.Fatalf("Get() = %v, %v, wanted %v, nil", gotV, gotErr, wantV)
39 func expectGetError(t *testing.T, h *memoize.Handle, g *memoize.Generation, substr string) {
40 gotV, gotErr := h.Get(context.Background(), g, nil)
41 if gotErr == nil || !strings.Contains(gotErr.Error(), substr) {
42 t.Fatalf("Get() = %v, %v, wanted err %q", gotV, gotErr, substr)
45 func TestGenerations(t *testing.T) {
47 // Evaluate key in g1.
48 g1 := s.Generation("g1")
49 h1 := g1.Bind("key", func(context.Context, memoize.Arg) interface{} { return "res" })
50 expectGet(t, h1, g1, "res")
52 // Get key in g2. It should inherit the value from g1.
53 g2 := s.Generation("g2")
54 h2 := g2.Bind("key", func(context.Context, memoize.Arg) interface{} {
55 t.Fatal("h2 should not need evaluation")
58 expectGet(t, h2, g2, "res")
60 // With g1 destroyed, g2 should still work.
62 expectGet(t, h2, g2, "res")
64 // With all generations destroyed, key should be re-evaluated.
66 g3 := s.Generation("g3")
67 h3 := g3.Bind("key", func(context.Context, memoize.Arg) interface{} { return "new res" })
68 expectGet(t, h3, g3, "new res")