json_test.go 10.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package renderers

import (
	"testing"

	"github.com/google/go-cmp/cmp"
)

func TestMarshalWithSpaces(t *testing.T) {
	tests := []struct {
		name     string
		input    any
		expected string
	}{
		// basic formatting tests
		{
			name:     "simple object",
			input:    map[string]any{"key": "value"},
			expected: `{"key": "value"}`,
		},
		{
			name:     "simple array",
			input:    []any{"a", "b", "c"},
			expected: `["a", "b", "c"]`,
		},
		// escaped quotes
		{
			name:     "escaped quote in string",
			input:    map[string]any{"text": `quote"inside`},
			expected: `{"text": "quote\"inside"}`,
		},
		{
			name:     "multiple escaped quotes",
			input:    map[string]any{"text": `say "hello" and "goodbye"`},
			expected: `{"text": "say \"hello\" and \"goodbye\""}`,
		},
		// escaped backslashes
		{
			name:     "escaped backslash",
			input:    map[string]any{"path": `C:\windows\system32`},
			expected: `{"path": "C:\\windows\\system32"}`,
		},
		{
			name:     "double backslash",
			input:    map[string]any{"text": `test\\more`},
			expected: `{"text": "test\\\\more"}`,
		},
		{
			name:     "backslash before quote",
			input:    map[string]any{"text": `end with \"`},
			expected: `{"text": "end with \\\""}`,
		},
		// standard JSON escape sequences
		{
			name:     "newline in string",
			input:    map[string]any{"text": "line1\nline2"},
			expected: `{"text": "line1\nline2"}`,
		},
		{
			name:     "tab in string",
			input:    map[string]any{"text": "before\tafter"},
			expected: `{"text": "before\tafter"}`,
		},
		{
			name:     "carriage return",
			input:    map[string]any{"text": "before\rafter"},
			expected: `{"text": "before\rafter"}`,
		},
		{
			name:     "multiple escape sequences",
			input:    map[string]any{"text": "line1\nline2\ttab\rcarriage"},
			expected: `{"text": "line1\nline2\ttab\rcarriage"}`,
		},
		// strings containing colons and commas (no spaces should be added inside)
		{
			name:     "colon in string",
			input:    map[string]any{"url": "http://example.com"},
			expected: `{"url": "http://example.com"}`,
		},
		{
			name:     "comma in string",
			input:    map[string]any{"list": "apple, banana, cherry"},
			expected: `{"list": "apple, banana, cherry"}`,
		},
		{
			name:     "colon and comma in string",
			input:    map[string]any{"data": "key:value, key2:value2"},
			expected: `{"data": "key:value, key2:value2"}`,
		},
		// unicode characters
		{
			name:     "emoji",
			input:    map[string]any{"emoji": "😀🎉✨"},
			expected: `{"emoji": "😀🎉✨"}`,
		},
		{
			name:     "chinese characters",
			input:    map[string]any{"text": "你好世界"},
			expected: `{"text": "你好世界"}`,
		},
		{
			name:     "arabic characters",
			input:    map[string]any{"text": "مرحبا"},
			expected: `{"text": "مرحبا"}`,
		},
		{
			name:     "mixed unicode and ascii",
			input:    map[string]any{"text": "Hello 世界! 😀"},
			expected: `{"text": "Hello 世界! 😀"}`,
		},
		{
			name:     "unicode with special symbols",
			input:    map[string]any{"text": "®©™€£¥"},
			expected: `{"text": "®©™€£¥"}`,
		},
		// complex combinations - strings that look like JSON
		{
			name:     "json string inside value",
			input:    map[string]any{"nested": `{"key":"value"}`},
			expected: `{"nested": "{\"key\":\"value\"}"}`,
		},
		{
			name:     "json array inside value",
			input:    map[string]any{"array": `["a","b","c"]`},
			expected: `{"array": "[\"a\",\"b\",\"c\"]"}`,
		},
		// edge cases
		{
			name:     "empty string",
			input:    map[string]any{"empty": ""},
			expected: `{"empty": ""}`,
		},
		{
			name:     "empty object",
			input:    map[string]any{},
			expected: `{}`,
		},
		{
			name:     "empty array",
			input:    []any{},
			expected: `[]`,
		},
		{
			name:     "numbers",
			input:    map[string]any{"int": 42, "float": 3.14},
			expected: `{"float": 3.14, "int": 42}`,
		},
		{
			name:     "boolean",
			input:    map[string]any{"bool": true, "other": false},
			expected: `{"bool": true, "other": false}`,
		},
		{
			name:     "null value",
			input:    map[string]any{"value": nil},
			expected: `{"value": null}`,
		},
		// nested structures with complex strings
		{
			name: "nested object with escapes",
			input: map[string]any{
				"outer": map[string]any{
					"path":  `C:\folder\file.txt`,
					"quote": `He said "hi"`,
				},
			},
			expected: `{"outer": {"path": "C:\\folder\\file.txt", "quote": "He said \"hi\""}}`,
		},
		{
			name: "array with unicode and escapes",
			input: []any{
				"normal",
				"with\nnewline",
				"with\"quote",
				"emoji😀",
				"colon:comma,",
			},
			expected: `["normal", "with\nnewline", "with\"quote", "emoji😀", "colon:comma,"]`,
		},
		{
			name:     "backslash at positions before special chars",
			input:    map[string]any{"text": `a\b:c\d,e`},
			expected: `{"text": "a\\b:c\\d,e"}`,
		},
		{
			name:     "multiple backslashes before quote",
			input:    map[string]any{"text": `ends\\"`},
			expected: `{"text": "ends\\\\\""}`,
		},
		{
			name:     "unicode with escapes",
			input:    map[string]any{"text": "Hello\n世界\t😀"},
			expected: `{"text": "Hello\n世界\t😀"}`,
		},

		// Real-world tool call example
		{
			name: "tool call arguments",
			input: map[string]any{
				"location": "San Francisco, CA",
				"unit":     "fahrenheit",
				"format":   "json",
			},
			expected: `{"format": "json", "location": "San Francisco, CA", "unit": "fahrenheit"}`,
		},
		{
			name: "complex tool arguments with escapes",
			input: map[string]any{
				"query":       `SELECT * FROM "users" WHERE name = 'O'Brien'`,
				"description": "Fetch user\ndata from DB",
				"path":        `C:\data\users.db`,
			},
			expected: `{"description": "Fetch user\ndata from DB", "path": "C:\\data\\users.db", "query": "SELECT * FROM \"users\" WHERE name = 'O'Brien'"}`,
		},
		{
			name:     "unicode immediately adjacent to JSON structure chars",
			input:    map[string]any{"😀key": "😀value", "test": "😀:😀,😀"},
			expected: `{"test": "😀:😀,😀", "😀key": "😀value"}`,
		},
		{
			name:     "long unicode string stress test",
			input:    map[string]any{"text": "😀😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐😑😒😓😔😕😖😗😘😙😚😛😜😝😞😟"},
			expected: `{"text": "😀😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐😑😒😓😔😕😖😗😘😙😚😛😜😝😞😟"}`,
		},
		{
			name: "deeply nested with unicode everywhere",
			input: map[string]any{
				"😀": map[string]any{
					"你好": []any{"مرحبا", "®©™", "∑∫∂√"},
				},
			},
			expected: `{"😀": {"你好": ["مرحبا", "®©™", "∑∫∂√"]}}`,
		},
		{
			name:     "unicode with all JSON special chars interleaved",
			input:    map[string]any{"k😀:k": "v😀,v", "a:😀": "b,😀", "😀": ":,😀,:"},
			expected: `{"a:😀": "b,😀", "k😀:k": "v😀,v", "😀": ":,😀,:"}`,
		},
		{
			name:     "combining diacritics and RTL text",
			input:    map[string]any{"hebrew": "עִבְרִית", "combined": "é̀ñ", "mixed": "test:עִבְרִית,é̀ñ"},
			expected: `{"combined": "é̀ñ", "hebrew": "עִבְרִית", "mixed": "test:עִבְרִית,é̀ñ"}`,
		},
		{
			name:     "pathological case: unicode + escapes + special chars",
			input:    map[string]any{"😀": "test\n😀\"quote😀\\backslash😀:colon😀,comma😀"},
			expected: `{"😀": "test\n😀\"quote😀\\backslash😀:colon😀,comma😀"}`,
		},

		// all JSON structural characters inside strings
		{
			name:     "braces and brackets in strings",
			input:    map[string]any{"text": "test{with}braces[and]brackets"},
			expected: `{"text": "test{with}braces[and]brackets"}`,
		},
		{
			name:     "braces and brackets with colons and commas",
			input:    map[string]any{"code": "{key:value,[1,2,3]}"},
			expected: `{"code": "{key:value,[1,2,3]}"}`,
		},
		{
			name:     "json-like string with all structural chars",
			input:    map[string]any{"schema": `{"type":"object","properties":{"name":{"type":"string"},"items":{"type":"array"}}}`},
			expected: `{"schema": "{\"type\":\"object\",\"properties\":{\"name\":{\"type\":\"string\"},\"items\":{\"type\":\"array\"}}}"}`,
		},

		// forward slash tests (JSON allows \/ as an escape sequence)
		{
			name:     "forward slash in URL",
			input:    map[string]any{"url": "https://example.com/path/to/resource"},
			expected: `{"url": "https://example.com/path/to/resource"}`,
		},
		{
			name:     "regex pattern with slashes",
			input:    map[string]any{"regex": "/[a-z]+/gi"},
			expected: `{"regex": "/[a-z]+/gi"}`,
		},

		// all JSON escape sequences
		{
			name:     "backspace escape",
			input:    map[string]any{"text": "before\bafter"},
			expected: `{"text": "before\bafter"}`,
		},
		{
			name:     "form feed escape",
			input:    map[string]any{"text": "before\fafter"},
			expected: `{"text": "before\fafter"}`,
		},
		{
			name:     "all standard escapes combined",
			input:    map[string]any{"text": "\"\\\b\f\n\r\t"},
			expected: `{"text": "\"\\\b\f\n\r\t"}`,
		},

		// unicode escape sequences
		{
			name:     "string that forces unicode escapes",
			input:    map[string]any{"control": "\u0000\u0001\u001f"},
			expected: `{"control": "\u0000\u0001\u001f"}`,
		},

		// empty objects and arrays nested with strings
		{
			name:     "nested empty structures with string values",
			input:    map[string]any{"empty_obj": map[string]any{}, "empty_arr": []any{}, "text": "{}[]"},
			expected: `{"empty_arr": [], "empty_obj": {}, "text": "{}[]"}`,
		},

		// complex nesting with all structural characters
		{
			name: "deeply nested with all char types",
			input: map[string]any{
				"level1": map[string]any{
					"array": []any{
						map[string]any{"nested": "value:with,special{chars}[here]"},
						[]any{"a", "b", "c"},
					},
				},
			},
			expected: `{"level1": {"array": [{"nested": "value:with,special{chars}[here]"}, ["a", "b", "c"]]}}`,
		},

		// string containing escaped structural characters
		{
			name:     "string with multiple escape sequences and structural chars",
			input:    map[string]any{"data": "test\"quote\"{brace}[bracket]:colon,comma\\backslash/slash"},
			expected: `{"data": "test\"quote\"{brace}[bracket]:colon,comma\\backslash/slash"}`,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result, err := marshalWithSpaces(tt.input)
			if err != nil {
				t.Fatalf("marshalWithSpaces failed: %v", err)
			}

			resultStr := string(result)
			if diff := cmp.Diff(resultStr, tt.expected); diff != "" {
				t.Errorf("mismatch (-got +want):\n%s", diff)
			}
		})
	}
}