Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201028153306-37f0764111ff / internal / memoize / memoize_test.go
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.
4
5 package memoize_test
6
7 import (
8         "context"
9         "strings"
10         "testing"
11
12         "golang.org/x/tools/internal/memoize"
13 )
14
15 func TestGet(t *testing.T) {
16         s := &memoize.Store{}
17         g := s.Generation("x")
18
19         evaled := 0
20
21         h := g.Bind("key", func(context.Context, memoize.Arg) interface{} {
22                 evaled++
23                 return "res"
24         })
25         expectGet(t, h, g, "res")
26         expectGet(t, h, g, "res")
27         if evaled != 1 {
28                 t.Errorf("got %v calls to function, wanted 1", evaled)
29         }
30 }
31
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)
36         }
37 }
38
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)
43         }
44 }
45 func TestGenerations(t *testing.T) {
46         s := &memoize.Store{}
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")
51
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")
56                 return "error"
57         })
58         expectGet(t, h2, g2, "res")
59
60         // With g1 destroyed, g2 should still work.
61         g1.Destroy()
62         expectGet(t, h2, g2, "res")
63
64         // With all generations destroyed, key should be re-evaluated.
65         g2.Destroy()
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")
69 }