File: source_test.go

package info (click to toggle)
golang-golang-x-exp 0.0~git20230522.2e198f4-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 6,404 kB
  • sloc: ansic: 1,900; objc: 276; sh: 272; asm: 48; makefile: 26
file content (86 lines) | stat: -rw-r--r-- 2,264 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package event_test

import (
	"context"
	"testing"

	"golang.org/x/exp/event"
	"golang.org/x/exp/event/eventtest"
)

const thisImportPath = "golang.org/x/exp/event_test"

func TestNamespace(t *testing.T) {
	event.RegisterHelper(testHelperB)
	event.RegisterHelper(thisImportPath + ".testHelperC")
	h := &eventtest.CaptureHandler{}
	opt := eventtest.ExporterOptions()
	opt.EnableNamespaces = true
	ctx := event.WithExporter(context.Background(), event.NewExporter(h, opt))
	for _, test := range []struct {
		name   string
		do     func(context.Context)
		expect event.Source
	}{{
		name:   "simple",
		do:     testA,
		expect: event.Source{Space: thisImportPath, Name: "testA"},
	}, {
		name:   "pointer helper",
		do:     testB,
		expect: event.Source{Space: thisImportPath, Name: "testB"},
	}, {
		name:   "named helper",
		do:     testC,
		expect: event.Source{Space: thisImportPath, Name: "testC"},
	}, {
		name:   "method",
		do:     testD,
		expect: event.Source{Space: thisImportPath, Owner: "tester", Name: "D"},
	}} {
		t.Run(test.name, func(t *testing.T) {
			h.Got = h.Got[:0]
			test.do(ctx)
			if len(h.Got) != 1 {
				t.Fatalf("Expected 1 event, got %v", len(h.Got))
			}
			got := h.Got[0].Source
			if got.Space != test.expect.Space {
				t.Errorf("got namespace %q, want, %q", got.Space, test.expect.Space)
			}
			if got.Owner != test.expect.Owner {
				t.Errorf("got owner %q, want, %q", got.Owner, test.expect.Owner)
			}
			if got.Name != test.expect.Name {
				t.Errorf("got name %q, want, %q", got.Name, test.expect.Name)
			}
		})
	}
}

type tester struct{}

//go:noinline
func testA(ctx context.Context) { event.Log(ctx, "test A") }

//go:noinline
func testB(ctx context.Context) { testHelperB(ctx) }

//go:noinline
func testHelperB(ctx context.Context) { event.Log(ctx, "test B") }

//go:noinline
func testC(ctx context.Context) { testHelperC(ctx) }

//go:noinline
func testHelperC(ctx context.Context) { event.Log(ctx, "test C") }

//go:noinline
func testD(ctx context.Context) { tester{}.D(ctx) }

//go:noinline
func (tester) D(ctx context.Context) { event.Log(ctx, "test D") }