MCPcopy Create free account
hub / github.com/google/go-cloud / TestEncodeErrors

Function TestEncodeErrors

docstore/driver/codec_test.go:214–235  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

212func (*badTextMarshaler) UnmarshalText([]byte) error { return errors.New("bad") }
213
214func TestEncodeErrors(t *testing.T) {
215 for _, test := range []struct {
216 desc string
217 val any
218 }{
219 {"MarshalBinary fails", badBinaryMarshaler{}},
220 {"MarshalText fails", badTextMarshaler{}},
221 {"bad type", make(chan int)},
222 {"bad type in list", []any{func() {}}},
223 {"bad type in map", map[string]any{"a": func() {}}},
224 {"bad type in struct", &struct{ C chan int }{}},
225 {"bad map key type", map[float32]int{1: 1}},
226 {"MarshalText for map key fails", map[badTextMarshaler]int{{}: 1}},
227 } {
228 enc := &testEncoder{}
229 if err := Encode(reflect.ValueOf(test.val), enc); err == nil {
230 t.Errorf("%s: got nil, want error", test.desc)
231 } else if c := gcerrors.Code(err); c != gcerrors.InvalidArgument {
232 t.Errorf("%s: got code %s, want InvalidArgument", test.desc, c)
233 }
234 }
235}
236
237type testEncoder struct {
238 val any

Callers

nothing calls this directly

Calls 2

CodeFunction · 0.92
EncodeFunction · 0.85

Tested by

no test coverage detected