marshaler_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package test
  2. import (
  3. "encoding/json"
  4. "encoding"
  5. )
  6. func init() {
  7. jsonMarshaler := json.Marshaler(fakeJsonMarshaler{})
  8. textMarshaler := encoding.TextMarshaler(fakeTextMarshaler{})
  9. textMarshaler2 := encoding.TextMarshaler(&fakeTextMarshaler2{})
  10. marshalCases = append(marshalCases,
  11. fakeJsonMarshaler{},
  12. &jsonMarshaler,
  13. fakeTextMarshaler{},
  14. &textMarshaler,
  15. fakeTextMarshaler2{},
  16. &textMarshaler2,
  17. map[fakeTextMarshaler]int{
  18. fakeTextMarshaler{}: 100,
  19. },
  20. map[*fakeTextMarshaler]int{
  21. &fakeTextMarshaler{}: 100,
  22. },
  23. map[encoding.TextMarshaler]int{
  24. textMarshaler: 100,
  25. },
  26. )
  27. }
  28. type fakeJsonMarshaler struct {
  29. F2 chan []byte
  30. }
  31. func (q fakeJsonMarshaler) MarshalJSON() ([]byte, error) {
  32. return []byte(`""`), nil
  33. }
  34. func (q *fakeJsonMarshaler) UnmarshalJSON(value []byte) error {
  35. return nil
  36. }
  37. type fakeTextMarshaler struct {
  38. F2 chan []byte
  39. }
  40. func (q fakeTextMarshaler) MarshalText() ([]byte, error) {
  41. return []byte(`""`), nil
  42. }
  43. func (q *fakeTextMarshaler) UnmarshalText(value []byte) error {
  44. return nil
  45. }
  46. type fakeTextMarshaler2 struct {
  47. Field2 int
  48. }
  49. func (q *fakeTextMarshaler2) MarshalText() ([]byte, error) {
  50. return []byte(`"abc"`), nil
  51. }
  52. func (q *fakeTextMarshaler2) UnmarshalText(value []byte) error {
  53. return nil
  54. }