|
| 1 | +package id |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/ifnotnil/x/tst" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +type id = [uuidSize]byte |
| 13 | + |
| 14 | +func TestUUID_JSON(t *testing.T) { |
| 15 | + type Foo struct { |
| 16 | + ID Base64UUID[id] `json:"id"` |
| 17 | + } |
| 18 | + |
| 19 | + type FooPointer struct { |
| 20 | + ID *Base64UUID[id] `json:"id"` |
| 21 | + } |
| 22 | + |
| 23 | + type FooOZ struct { |
| 24 | + ID Base64UUID[id] `json:"id,omitzero"` |
| 25 | + } |
| 26 | + |
| 27 | + type FooPointerOZ struct { |
| 28 | + ID *Base64UUID[id] `json:"id,omitzero"` |
| 29 | + } |
| 30 | + |
| 31 | + const js = `{"id":"AZomiURKfF6MYQcDvjFM_A"}` |
| 32 | + uuid := id{0x01, 0x9a, 0x26, 0x89, 0x44, 0x4a, 0x7c, 0x5e, 0x8c, 0x61, 0x07, 0x03, 0xbe, 0x31, 0x4c, 0xfc} |
| 33 | + |
| 34 | + unmarshalTests := []struct { |
| 35 | + input string |
| 36 | + destination any |
| 37 | + expected any |
| 38 | + errorAsserter tst.ErrorAssertionFunc |
| 39 | + }{ |
| 40 | + { |
| 41 | + input: js, |
| 42 | + destination: &Foo{ID: Base64UUID[id]{Value: zeroUUID}}, |
| 43 | + expected: &Foo{ID: Base64UUID[id]{Value: uuid}}, |
| 44 | + errorAsserter: tst.NoError(), |
| 45 | + }, |
| 46 | + { |
| 47 | + input: js, |
| 48 | + destination: &FooPointer{ID: nil}, |
| 49 | + expected: &FooPointer{ID: &Base64UUID[id]{Value: uuid}}, |
| 50 | + errorAsserter: tst.NoError(), |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + for i, tc := range unmarshalTests { |
| 55 | + t.Run(fmt.Sprintf("unmarshal_%d", i), func(t *testing.T) { |
| 56 | + gotErr := json.Unmarshal([]byte(tc.input), tc.destination) |
| 57 | + tc.errorAsserter(t, gotErr) |
| 58 | + assert.Equal(t, tc.expected, tc.destination) |
| 59 | + }) |
| 60 | + } |
| 61 | + |
| 62 | + marshalTests := []struct { |
| 63 | + input any |
| 64 | + expectedJSON string |
| 65 | + errorAsserter tst.ErrorAssertionFunc |
| 66 | + }{ |
| 67 | + 0: { |
| 68 | + input: Foo{ID: Base64UUID[id]{Value: uuid}}, |
| 69 | + expectedJSON: js, |
| 70 | + errorAsserter: tst.NoError(), |
| 71 | + }, |
| 72 | + 1: { |
| 73 | + input: &Foo{ID: Base64UUID[id]{Value: uuid}}, |
| 74 | + expectedJSON: js, |
| 75 | + errorAsserter: tst.NoError(), |
| 76 | + }, |
| 77 | + 2: { |
| 78 | + input: FooPointer{ID: &Base64UUID[id]{Value: uuid}}, |
| 79 | + expectedJSON: js, |
| 80 | + errorAsserter: tst.NoError(), |
| 81 | + }, |
| 82 | + 3: { |
| 83 | + input: &FooPointer{ID: &Base64UUID[id]{Value: uuid}}, |
| 84 | + expectedJSON: js, |
| 85 | + errorAsserter: tst.NoError(), |
| 86 | + }, |
| 87 | + 4: { |
| 88 | + input: FooOZ{ID: Base64UUID[id]{Value: zeroUUID}}, |
| 89 | + expectedJSON: `{}`, |
| 90 | + errorAsserter: tst.NoError(), |
| 91 | + }, |
| 92 | + 5: { |
| 93 | + input: &FooOZ{ID: Base64UUID[id]{Value: zeroUUID}}, |
| 94 | + expectedJSON: `{}`, |
| 95 | + errorAsserter: tst.NoError(), |
| 96 | + }, |
| 97 | + 6: { |
| 98 | + input: FooPointerOZ{ID: &Base64UUID[id]{Value: zeroUUID}}, |
| 99 | + expectedJSON: `{}`, |
| 100 | + errorAsserter: tst.NoError(), |
| 101 | + }, |
| 102 | + 7: { |
| 103 | + input: &FooPointerOZ{ID: &Base64UUID[id]{Value: zeroUUID}}, |
| 104 | + expectedJSON: `{}`, |
| 105 | + errorAsserter: tst.NoError(), |
| 106 | + }, |
| 107 | + 8: { |
| 108 | + input: &FooPointerOZ{ID: nil}, |
| 109 | + expectedJSON: `{}`, |
| 110 | + errorAsserter: tst.NoError(), |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + for i, tc := range marshalTests { |
| 115 | + t.Run(fmt.Sprintf("marshal_%d", i), func(t *testing.T) { |
| 116 | + got, gotErr := json.Marshal(tc.input) |
| 117 | + tc.errorAsserter(t, gotErr) |
| 118 | + assert.Equal(t, tc.expectedJSON, string(got)) |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
0 commit comments