Debian Codegraph @

gopls/internal/util/bug/bug_test.go

raw symbols SCIP
1 // Copyright 2022 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 bug
6
7 import (
8 "encoding/json"
9 "fmt"
10 "testing"
11 "time"
12
13 "github.com/google/go-cmp/cmp"
14 )
15
16 func resetForTesting() {
17 exemplars = nil
18 handlers = nil
19 }
20
21 func TestListBugs(t *testing.T) {
22 defer resetForTesting()
23
24 Report("bad")
25
26 wantBugs(t, "bad")
27
28 for i := range 3 {
29 Report(fmt.Sprintf("index:%d", i))
30 }
31
32 wantBugs(t, "bad", "index:0")
33 }
34
35 func wantBugs(t *testing.T, want ...string) {
36 t.Helper()
37
38 bugs := List()
39 if got, want := len(bugs), len(want); got != want {
40 t.Errorf("List(): got %d bugs, want %d", got, want)
41 return
42 }
43
44 for i, b := range bugs {
45 if got, want := b.Description, want[i]; got != want {
46 t.Errorf("bug.List()[%d] = %q, want %q", i, got, want)
47 }
48 }
49 }
50
51 func TestBugHandler(t *testing.T) {
52 defer resetForTesting()
53
54 Report("unseen")
55
56 // Both handlers are called, in order of registration, only once.
57 var got string
58 Handle(func(b Bug) { got += "1:" + b.Description })
59 Handle(func(b Bug) { got += "2:" + b.Description })
60
61 Report("seen")
62
63 Report("again")
64
65 if want := "1:seen2:seen"; got != want {
66 t.Errorf("got %q, want %q", got, want)
67 }
68 }
69
70 func TestBugJSON(t *testing.T) {
71 b1 := Bug{
72 File: "foo.go",
73 Line: 1,
74 Description: "a bug",
75 Key: "foo.go:1",
76 Stack: "<stack>",
77 AtTime: time.Now(),
78 }
79
80 data, err := json.Marshal(b1)
81 if err != nil {
82 t.Fatal(err)
83 }
84 var b2 Bug
85 if err := json.Unmarshal(data, &b2); err != nil {
86 t.Fatal(err)
87 }
88 if diff := cmp.Diff(b1, b2); diff != "" {
89 t.Errorf("bugs differ after JSON Marshal/Unmarshal (-b1 +b2):\n%s", diff)
90 }
91 }