(t *testing.T)
| 133 | } |
| 134 | |
| 135 | func TestSortJSONKeys(t *testing.T) { |
| 136 | tests := []struct { |
| 137 | name string |
| 138 | input string |
| 139 | expected string |
| 140 | }{ |
| 141 | { |
| 142 | name: "simple object", |
| 143 | input: `{"z": 1, "a": 2, "m": 3}`, |
| 144 | expected: "{\n \"a\": 2,\n \"m\": 3,\n \"z\": 1\n}", |
| 145 | }, |
| 146 | { |
| 147 | name: "nested object", |
| 148 | input: `{"z": {"y": 1, "x": 2}, "a": 3}`, |
| 149 | expected: "{\n \"a\": 3,\n \"z\": {\n \"x\": 2,\n \"y\": 1\n }\n}", |
| 150 | }, |
| 151 | { |
| 152 | name: "array with objects", |
| 153 | input: `{"items": [{"z": 1, "a": 2}, {"y": 3, "b": 4}]}`, |
| 154 | expected: "{\n \"items\": [\n {\n \"a\": 2,\n \"z\": 1\n },\n {\n \"b\": 4,\n \"y\": 3\n }\n ]\n}", |
| 155 | }, |
| 156 | { |
| 157 | name: "deeply nested", |
| 158 | input: `{"z": {"y": {"x": 1, "a": 2}, "b": 3}, "m": 4}`, |
| 159 | expected: "{\n \"m\": 4,\n \"z\": {\n \"b\": 3,\n \"y\": {\n \"a\": 2,\n \"x\": 1\n }\n }\n}", |
| 160 | }, |
| 161 | { |
| 162 | name: "properties field like in toolsnaps", |
| 163 | input: `{"name": "test", "properties": {"repo": {"type": "string"}, "owner": {"type": "string"}, "page": {"type": "number"}}}`, |
| 164 | expected: "{\n \"name\": \"test\",\n \"properties\": {\n \"owner\": {\n \"type\": \"string\"\n },\n \"page\": {\n \"type\": \"number\"\n },\n \"repo\": {\n \"type\": \"string\"\n }\n }\n}", |
| 165 | }, |
| 166 | } |
| 167 | |
| 168 | for _, tt := range tests { |
| 169 | t.Run(tt.name, func(t *testing.T) { |
| 170 | result, err := sortJSONKeys([]byte(tt.input)) |
| 171 | require.NoError(t, err) |
| 172 | assert.Equal(t, tt.expected, string(result)) |
| 173 | }) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | func TestSortJSONKeysIdempotent(t *testing.T) { |
| 178 | // Given a JSON string that's already sorted |
nothing calls this directly
no test coverage detected