MCPcopy Index your code
hub / github.com/tailscale/tailscale / TestInterfaceCoders

Function TestInterfaceCoders

types/jsonx/json_test.go:47–139  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

45}
46
47func TestInterfaceCoders(t *testing.T) {
48 var opts json.Options = json.JoinOptions(
49 json.WithMarshalers(json.MarshalToFunc(interfaceCoders.Marshal)),
50 json.WithUnmarshalers(json.UnmarshalFromFunc(interfaceCoders.Unmarshal)),
51 )
52
53 errSkipMarshal := errors.New("skip marshal")
54 makeFiller := func() InterfaceWrapper {
55 return InterfaceWrapper{&Baz{"fizz", "buzz"}}
56 }
57
58 for _, tt := range []struct {
59 label string
60 wantVal InterfaceWrapper
61 wantJSON string
62 wantMarshalError error
63 wantUnmarshalError error
64 }{{
65 label: "Null",
66 wantVal: InterfaceWrapper{},
67 wantJSON: `null`,
68 }, {
69 label: "Foo",
70 wantVal: InterfaceWrapper{Foo("hello")},
71 wantJSON: `{"Foo":"hello"}`,
72 }, {
73 label: "BarPointer",
74 wantVal: InterfaceWrapper{new(Bar(5))},
75 wantJSON: `{"Bar":5}`,
76 }, {
77 label: "BarValue",
78 wantVal: InterfaceWrapper{Bar(5)},
79 // NOTE: We could handle BarValue just like BarPointer,
80 // but round-trip marshal/unmarshal would not be identical.
81 wantMarshalError: errUnknownTypeName,
82 }, {
83 label: "Baz",
84 wantVal: InterfaceWrapper{&Baz{"alpha", "omega"}},
85 wantJSON: `{"Baz":{"Fizz":"alpha","Buzz":"omega"}}`,
86 }, {
87 label: "Unknown",
88 wantVal: makeFiller(),
89 wantJSON: `{"Unknown":[1,2,3]}`,
90 wantMarshalError: errSkipMarshal,
91 wantUnmarshalError: errUnknownTypeName,
92 }, {
93 label: "Empty",
94 wantVal: makeFiller(),
95 wantJSON: `{}`,
96 wantMarshalError: errSkipMarshal,
97 wantUnmarshalError: errNonSingularValue,
98 }, {
99 label: "Duplicate",
100 wantVal: InterfaceWrapper{Foo("hello")}, // first entry wins
101 wantJSON: `{"Foo":"hello","Bar":5}`,
102 wantMarshalError: errSkipMarshal,
103 wantUnmarshalError: errNonSingularValue,
104 }} {

Callers

nothing calls this directly

Calls 8

FooTypeAlias · 0.85
BarTypeAlias · 0.85
DiffMethod · 0.80
RunMethod · 0.65
MarshalMethod · 0.65
FatalfMethod · 0.65
NewMethod · 0.45
IsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…