Debian Codegraph @

event/source_test.go

raw symbols SCIP
1 // Copyright 2021 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 event_test
6
7 import (
8 "context"
9 "testing"
10
11 "golang.org/x/exp/event"
12 "golang.org/x/exp/event/eventtest"
13 )
14
15 const thisImportPath = "golang.org/x/exp/event_test"
16
17 func TestNamespace(t *testing.T) {
18 event.RegisterHelper(testHelperB)
19 event.RegisterHelper(thisImportPath + ".testHelperC")
20 h := &eventtest.CaptureHandler{}
21 opt := eventtest.ExporterOptions()
22 opt.EnableNamespaces = true
23 ctx := event.WithExporter(context.Background(), event.NewExporter(h, opt))
24 for _, test := range []struct {
25 name string
26 do func(context.Context)
27 expect event.Source
28 }{{
29 name: "simple",
30 do: testA,
31 expect: event.Source{Space: thisImportPath, Name: "testA"},
32 }, {
33 name: "pointer helper",
34 do: testB,
35 expect: event.Source{Space: thisImportPath, Name: "testB"},
36 }, {
37 name: "named helper",
38 do: testC,
39 expect: event.Source{Space: thisImportPath, Name: "testC"},
40 }, {
41 name: "method",
42 do: testD,
43 expect: event.Source{Space: thisImportPath, Owner: "tester", Name: "D"},
44 }} {
45 t.Run(test.name, func(t *testing.T) {
46 h.Got = h.Got[:0]
47 test.do(ctx)
48 if len(h.Got) != 1 {
49 t.Fatalf("Expected 1 event, got %v", len(h.Got))
50 }
51 got := h.Got[0].Source
52 if got.Space != test.expect.Space {
53 t.Errorf("got namespace %q, want, %q", got.Space, test.expect.Space)
54 }
55 if got.Owner != test.expect.Owner {
56 t.Errorf("got owner %q, want, %q", got.Owner, test.expect.Owner)
57 }
58 if got.Name != test.expect.Name {
59 t.Errorf("got name %q, want, %q", got.Name, test.expect.Name)
60 }
61 })
62 }
63 }
64
65 type tester struct{}
66
67 //go:noinline
68 func testA(ctx context.Context) { event.Log(ctx, "test A") }
69
70 //go:noinline
71 func testB(ctx context.Context) { testHelperB(ctx) }
72
73 //go:noinline
74 func testHelperB(ctx context.Context) { event.Log(ctx, "test B") }
75
76 //go:noinline
77 func testC(ctx context.Context) { testHelperC(ctx) }
78
79 //go:noinline
80 func testHelperC(ctx context.Context) { event.Log(ctx, "test C") }
81
82 //go:noinline
83 func testD(ctx context.Context) { tester{}.D(ctx) }
84
85 //go:noinline
86 func (tester) D(ctx context.Context) { event.Log(ctx, "test D") }