| 212 | func (*badTextMarshaler) UnmarshalText([]byte) error { return errors.New("bad") } |
| 213 | |
| 214 | func 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 | |
| 237 | type testEncoder struct { |
| 238 | val any |