| 56 | } |
| 57 | |
| 58 | func TestListToMapMarshalOperationsJSON(t *testing.T) { |
| 59 | testCases := []struct { |
| 60 | name string |
| 61 | inputJSON string |
| 62 | expectedListToMapJSON ListToMap |
| 63 | marshalledJSON string |
| 64 | }{ |
| 65 | { |
| 66 | name: "empty map", |
| 67 | inputJSON: "[]", |
| 68 | expectedListToMapJSON: ListToMap{}, |
| 69 | marshalledJSON: "[]", |
| 70 | }, |
| 71 | { |
| 72 | name: "map with entries", |
| 73 | inputJSON: "[\"foo\"]", |
| 74 | expectedListToMapJSON: ListToMap{ |
| 75 | "foo": {}, |
| 76 | }, |
| 77 | marshalledJSON: "[\"foo\"]", |
| 78 | }, |
| 79 | } |
| 80 | |
| 81 | for _, tc := range testCases { |
| 82 | t.Run(tc.name, func(t *testing.T) { |
| 83 | // JSON to struct |
| 84 | var l ListToMap |
| 85 | assert.NoError(t, json.Unmarshal([]byte(tc.inputJSON), &l)) |
| 86 | assert.NotNil(t, l.GetMap()) |
| 87 | assert.Equal(t, tc.expectedListToMapJSON, l) |
| 88 | |
| 89 | // struct to JSON |
| 90 | bytes, err := json.Marshal(tc.expectedListToMapJSON) |
| 91 | assert.NoError(t, err) |
| 92 | assert.Equal(t, tc.marshalledJSON, string(bytes)) |
| 93 | }) |
| 94 | } |
| 95 | } |