diff --git a/processor/json_test.go b/processor/json_test.go index 9d0a7bb..fee8c9c 100644 --- a/processor/json_test.go +++ b/processor/json_test.go @@ -16,46 +16,25 @@ func TestProcessJSON(t *testing.T) { expectedMods int }{ { - name: "Basic JSON object modification", - input: `{"name": "test", "value": 42}`, - luaExpression: `data.value = data.value * 2; return true`, - expectedOutput: `{ - "name": "test", - "value": 84 -}`, - expectedMods: 1, + name: "Basic JSON object modification", + input: `{"name": "test", "value": 42}`, + luaExpression: `data.value = data.value * 2; return true`, + expectedOutput: `{"name": "test", "value": 84}`, // Surgical editing preserves compact format + expectedMods: 1, }, { - name: "JSON array modification", - input: `{"items": [{"id": 1, "value": 10}, {"id": 2, "value": 20}]}`, - luaExpression: `for i, item in ipairs(data.items) do data.items[i].value = item.value * 1.5 end; return true`, - expectedOutput: `{ - "items": [ - { - "id": 1, - "value": 15 - }, - { - "id": 2, - "value": 30 - } - ] -}`, - expectedMods: 1, + name: "JSON array modification", + input: `{"items": [{"id": 1, "value": 10}, {"id": 2, "value": 20}]}`, + luaExpression: `for i, item in ipairs(data.items) do data.items[i].value = item.value * 1.5 end; return true`, + expectedOutput: `{"items": [{"id": 1, "value": 15}, {"id": 2, "value": 30}]}`, // Surgical editing preserves compact format + expectedMods: 1, }, { - name: "JSON nested object modification", - input: `{"config": {"settings": {"enabled": false, "timeout": 30}}}`, - luaExpression: `data.config.settings.enabled = true; data.config.settings.timeout = 60; return true`, - expectedOutput: `{ - "config": { - "settings": { - "enabled": true, - "timeout": 60 - } - } -}`, - expectedMods: 1, + name: "JSON nested object modification", + input: `{"config": {"settings": {"enabled": false, "timeout": 30}}}`, + luaExpression: `data.config.settings.enabled = true; data.config.settings.timeout = 60; return true`, + expectedOutput: `{"config": {"settings": {"enabled": true, "timeout": 60}}}`, // Surgical editing preserves compact format + expectedMods: 1, }, { name: "JSON no modification", diff --git a/processor/surgical_json_test.go b/processor/surgical_json_test.go index a2d0148..056d937 100644 --- a/processor/surgical_json_test.go +++ b/processor/surgical_json_test.go @@ -41,9 +41,8 @@ modified = true `, expected: `{ "name": "test", - "value": 42, - "newField": "added" -}`, + "value": 42 +,"newField":"added"}`, // sjson.Set() adds new fields in compact format }, { name: "Modify nested field", @@ -93,19 +92,9 @@ modified = true result = result[:cmd.From] + cmd.With + result[cmd.To:] } - // Instead of exact string comparison, check that key values are present - // This accounts for field ordering differences in JSON - if !contains(result, `"value": 84`) && tt.name == "Modify single field" { - t.Errorf("Expected value to be 84, got:\n%s", result) - } - if !contains(result, `"newField": "added"`) && tt.name == "Add new field" { - t.Errorf("Expected newField to be added, got:\n%s", result) - } - if !contains(result, `"enabled": true`) && tt.name == "Modify nested field" { - t.Errorf("Expected enabled to be true, got:\n%s", result) - } - if !contains(result, `"timeout": 60`) && tt.name == "Modify nested field" { - t.Errorf("Expected timeout to be 60, got:\n%s", result) + // Check the actual result matches expected + if result != tt.expected { + t.Errorf("Expected:\n%s\n\nGot:\n%s", tt.expected, result) } }) } @@ -265,19 +254,3 @@ func TestRetardedJSONEditing(t *testing.T) { t.Errorf("Expected:\n%s\nGot:\n%s", expected, result) } } - -func contains(s, substr string) bool { - return len(s) >= len(substr) && (s == substr || - (len(s) > len(substr) && (s[:len(substr)] == substr || - s[len(s)-len(substr):] == substr || - containsSubstring(s, substr)))) -} - -func containsSubstring(s, substr string) bool { - for i := 0; i <= len(s)-len(substr); i++ { - if s[i:i+len(substr)] == substr { - return true - } - } - return false -}