| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 68 |
func testA(ctx context.Context) { event.Log(ctx, "test A") } |
| 69 |
|
| 70 |
|
| 71 |
func testB(ctx context.Context) { testHelperB(ctx) } |
| 72 |
|
| 73 |
|
| 74 |
func testHelperB(ctx context.Context) { event.Log(ctx, "test B") } |
| 75 |
|
| 76 |
|
| 77 |
func testC(ctx context.Context) { testHelperC(ctx) } |
| 78 |
|
| 79 |
|
| 80 |
func testHelperC(ctx context.Context) { event.Log(ctx, "test C") } |
| 81 |
|
| 82 |
|
| 83 |
func testD(ctx context.Context) { tester{}.D(ctx) } |
| 84 |
|
| 85 |
|
| 86 |
func (tester) D(ctx context.Context) { event.Log(ctx, "test D") } |