Fix tests

This commit is contained in:
2025-08-21 22:20:07 +02:00
parent 491a030bf8
commit e0d3b938e3
2 changed files with 49 additions and 76 deletions

View File

@@ -19,42 +19,21 @@ func TestProcessJSON(t *testing.T) {
name: "Basic JSON object modification", name: "Basic JSON object modification",
input: `{"name": "test", "value": 42}`, input: `{"name": "test", "value": 42}`,
luaExpression: `data.value = data.value * 2; return true`, luaExpression: `data.value = data.value * 2; return true`,
expectedOutput: `{ expectedOutput: `{"name": "test", "value": 84}`, // Surgical editing preserves compact format
"name": "test",
"value": 84
}`,
expectedMods: 1, expectedMods: 1,
}, },
{ {
name: "JSON array modification", name: "JSON array modification",
input: `{"items": [{"id": 1, "value": 10}, {"id": 2, "value": 20}]}`, 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`, luaExpression: `for i, item in ipairs(data.items) do data.items[i].value = item.value * 1.5 end; return true`,
expectedOutput: `{ expectedOutput: `{"items": [{"id": 1, "value": 15}, {"id": 2, "value": 30}]}`, // Surgical editing preserves compact format
"items": [
{
"id": 1,
"value": 15
},
{
"id": 2,
"value": 30
}
]
}`,
expectedMods: 1, expectedMods: 1,
}, },
{ {
name: "JSON nested object modification", name: "JSON nested object modification",
input: `{"config": {"settings": {"enabled": false, "timeout": 30}}}`, input: `{"config": {"settings": {"enabled": false, "timeout": 30}}}`,
luaExpression: `data.config.settings.enabled = true; data.config.settings.timeout = 60; return true`, luaExpression: `data.config.settings.enabled = true; data.config.settings.timeout = 60; return true`,
expectedOutput: `{ expectedOutput: `{"config": {"settings": {"enabled": true, "timeout": 60}}}`, // Surgical editing preserves compact format
"config": {
"settings": {
"enabled": true,
"timeout": 60
}
}
}`,
expectedMods: 1, expectedMods: 1,
}, },
{ {

View File

@@ -41,9 +41,8 @@ modified = true
`, `,
expected: `{ expected: `{
"name": "test", "name": "test",
"value": 42, "value": 42
"newField": "added" ,"newField":"added"}`, // sjson.Set() adds new fields in compact format
}`,
}, },
{ {
name: "Modify nested field", name: "Modify nested field",
@@ -93,19 +92,9 @@ modified = true
result = result[:cmd.From] + cmd.With + result[cmd.To:] result = result[:cmd.From] + cmd.With + result[cmd.To:]
} }
// Instead of exact string comparison, check that key values are present // Check the actual result matches expected
// This accounts for field ordering differences in JSON if result != tt.expected {
if !contains(result, `"value": 84`) && tt.name == "Modify single field" { t.Errorf("Expected:\n%s\n\nGot:\n%s", tt.expected, result)
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)
} }
}) })
} }
@@ -139,6 +128,32 @@ func TestSurgicalJSONPreservesFormatting(t *testing.T) {
] ]
}` }`
expected := `{
"Defaults": {
"Behaviour": "None",
"Description": "",
"DisplayName": "",
"FlavorText": "",
"Icon": "None",
"MaxStack": 1,
"Override_Glow_Icon": "None",
"Weight": 0,
"bAllowZeroWeight": false
},
"RowStruct": "/Script/Icarus.ItemableData",
"Rows": [
{
"Description": "NSLOCTEXT(\"D_Itemable\", \"Item_Fiber-Description\", \"A bundle of soft fiber, highly useful.\")",
"DisplayName": "NSLOCTEXT(\"D_Itemable\", \"Item_Fiber-DisplayName\", \"Fiber\")",
"FlavorText": "NSLOCTEXT(\"D_Itemable\", \"Item_Fiber-FlavorText\", \"Fiber is collected from bast, the strong inner bark of certain flowering plants.\")",
"Icon": "/Game/Assets/2DArt/UI/Items/Item_Icons/Resources/ITEM_Fibre.ITEM_Fibre",
"MaxStack": 1000000,
"Name": "Item_Fiber",
"Weight": 500
}
]
}`
command := utils.ModifyCommand{ command := utils.ModifyCommand{
Name: "test", Name: "test",
Lua: ` Lua: `
@@ -163,14 +178,9 @@ modified = true
result = result[:cmd.From] + cmd.With + result[cmd.To:] result = result[:cmd.From] + cmd.With + result[cmd.To:]
} }
// Check that the weight was changed // Check that the result matches expected (preserves formatting and changes weight)
if !contains(result, `"Weight": 500`) { if result != expected {
t.Errorf("Expected weight to be changed to 500, got:\n%s", result) t.Errorf("Expected:\n%s\n\nGot:\n%s", expected, result)
}
// Check that formatting is preserved (should have proper indentation)
if !contains(result, " \"Weight\": 500") {
t.Errorf("Expected proper indentation, got:\n%s", result)
} }
} }
@@ -265,19 +275,3 @@ func TestRetardedJSONEditing(t *testing.T) {
t.Errorf("Expected:\n%s\nGot:\n%s", expected, result) 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
}