284 lines
6.8 KiB
Go
284 lines
6.8 KiB
Go
package processor
|
|
|
|
import (
|
|
"cook/utils"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// TestJSONFloat tests line 298 - float formatting for non-integer floats
|
|
func TestJSONFloatFormatting(t *testing.T) {
|
|
jsonContent := `{
|
|
"value": 10.5,
|
|
"another": 3.14159
|
|
}`
|
|
|
|
command := utils.ModifyCommand{
|
|
Name: "test_float",
|
|
JSON: true,
|
|
Lua: `
|
|
data.value = data.value * 2
|
|
data.another = data.another * 10
|
|
modified = true
|
|
`,
|
|
}
|
|
|
|
commands, err := ProcessJSON(jsonContent, command, "test.json")
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, commands)
|
|
|
|
result, _ := utils.ExecuteModifications(commands, jsonContent)
|
|
assert.Contains(t, result, "21") // 10.5 * 2
|
|
assert.Contains(t, result, "31.4159") // 3.14159 * 10
|
|
}
|
|
|
|
// TestJSONNestedObjectAddition tests lines 303-320 - map[string]interface{} case
|
|
func TestJSONNestedObjectAddition(t *testing.T) {
|
|
jsonContent := `{
|
|
"items": {}
|
|
}`
|
|
|
|
command := utils.ModifyCommand{
|
|
Name: "test_nested",
|
|
JSON: true,
|
|
Lua: `
|
|
data.items.newObject = {
|
|
name = "test",
|
|
value = 42,
|
|
enabled = true
|
|
}
|
|
modified = true
|
|
`,
|
|
}
|
|
|
|
commands, err := ProcessJSON(jsonContent, command, "test.json")
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, commands)
|
|
|
|
result, _ := utils.ExecuteModifications(commands, jsonContent)
|
|
assert.Contains(t, result, `"newObject"`)
|
|
assert.Contains(t, result, `"name"`)
|
|
assert.Contains(t, result, `"test"`)
|
|
assert.Contains(t, result, `"value"`)
|
|
assert.Contains(t, result, "42")
|
|
}
|
|
|
|
// TestJSONKeyWithQuotes tests line 315 - key escaping with quotes
|
|
func TestJSONKeyWithQuotes(t *testing.T) {
|
|
jsonContent := `{
|
|
"data": {}
|
|
}`
|
|
|
|
command := utils.ModifyCommand{
|
|
Name: "test_key_quotes",
|
|
JSON: true,
|
|
Lua: `
|
|
data.data["key-with-dash"] = "value1"
|
|
data.data.normalKey = "value2"
|
|
modified = true
|
|
`,
|
|
}
|
|
|
|
commands, err := ProcessJSON(jsonContent, command, "test.json")
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, commands)
|
|
|
|
result, _ := utils.ExecuteModifications(commands, jsonContent)
|
|
assert.Contains(t, result, `"key-with-dash"`)
|
|
assert.Contains(t, result, `"normalKey"`)
|
|
}
|
|
|
|
// TestJSONArrayInValue tests lines 321-327 - default case with json.Marshal for arrays
|
|
func TestJSONArrayInValue(t *testing.T) {
|
|
jsonContent := `{
|
|
"data": {}
|
|
}`
|
|
|
|
command := utils.ModifyCommand{
|
|
Name: "test_array_value",
|
|
JSON: true,
|
|
Lua: `
|
|
data.data.items = {1, 2, 3, 4, 5}
|
|
data.data.strings = {"a", "b", "c"}
|
|
modified = true
|
|
`,
|
|
}
|
|
|
|
commands, err := ProcessJSON(jsonContent, command, "test.json")
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, commands)
|
|
|
|
result, _ := utils.ExecuteModifications(commands, jsonContent)
|
|
assert.Contains(t, result, `"items"`)
|
|
assert.Contains(t, result, `[1,2,3,4,5]`)
|
|
assert.Contains(t, result, `"strings"`)
|
|
assert.Contains(t, result, `["a","b","c"]`)
|
|
}
|
|
|
|
// TestJSONRootArrayElementRemoval tests line 422 - removing from root-level array
|
|
func TestJSONRootArrayElementRemoval(t *testing.T) {
|
|
jsonContent := `[
|
|
{"id": 1, "name": "first"},
|
|
{"id": 2, "name": "second"},
|
|
{"id": 3, "name": "third"}
|
|
]`
|
|
|
|
command := utils.ModifyCommand{
|
|
Name: "test_root_array_removal",
|
|
JSON: true,
|
|
Lua: `
|
|
-- Remove the second element
|
|
table.remove(data, 2)
|
|
modified = true
|
|
`,
|
|
}
|
|
|
|
commands, err := ProcessJSON(jsonContent, command, "test.json")
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, commands)
|
|
|
|
result, _ := utils.ExecuteModifications(commands, jsonContent)
|
|
assert.Contains(t, result, `"first"`)
|
|
assert.Contains(t, result, `"third"`)
|
|
assert.NotContains(t, result, `"second"`)
|
|
}
|
|
|
|
// TestJSONRootArrayElementChange tests lines 434 and 450 - changing primitive values in root array
|
|
func TestJSONRootArrayElementChange(t *testing.T) {
|
|
jsonContent := `[10, 20, 30, 40, 50]`
|
|
|
|
command := utils.ModifyCommand{
|
|
Name: "test_root_array_change",
|
|
JSON: true,
|
|
Lua: `
|
|
-- Double all values
|
|
for i = 1, #data do
|
|
data[i] = data[i] * 2
|
|
end
|
|
modified = true
|
|
`,
|
|
}
|
|
|
|
commands, err := ProcessJSON(jsonContent, command, "test.json")
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, commands)
|
|
|
|
result, _ := utils.ExecuteModifications(commands, jsonContent)
|
|
assert.Contains(t, result, "20")
|
|
assert.Contains(t, result, "40")
|
|
assert.Contains(t, result, "60")
|
|
assert.Contains(t, result, "80")
|
|
assert.Contains(t, result, "100")
|
|
assert.NotContains(t, result, "10,")
|
|
}
|
|
|
|
// TestJSONRootArrayStringElements tests deepEqual with strings in root array
|
|
func TestJSONRootArrayStringElements(t *testing.T) {
|
|
jsonContent := `["apple", "banana", "cherry"]`
|
|
|
|
command := utils.ModifyCommand{
|
|
Name: "test_root_array_strings",
|
|
JSON: true,
|
|
Lua: `
|
|
data[2] = "orange"
|
|
modified = true
|
|
`,
|
|
}
|
|
|
|
commands, err := ProcessJSON(jsonContent, command, "test.json")
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, commands)
|
|
|
|
result, _ := utils.ExecuteModifications(commands, jsonContent)
|
|
assert.Contains(t, result, `"apple"`)
|
|
assert.Contains(t, result, `"orange"`)
|
|
assert.Contains(t, result, `"cherry"`)
|
|
assert.NotContains(t, result, `"banana"`)
|
|
}
|
|
|
|
// TestJSONComplexNestedStructure tests multiple untested paths together
|
|
func TestJSONComplexNestedStructure(t *testing.T) {
|
|
jsonContent := `{
|
|
"config": {
|
|
"multiplier": 2.5
|
|
}
|
|
}`
|
|
|
|
command := utils.ModifyCommand{
|
|
Name: "test_complex",
|
|
JSON: true,
|
|
Lua: `
|
|
-- Add nested object with array
|
|
data.config.settings = {
|
|
enabled = true,
|
|
values = {1.5, 2.5, 3.5},
|
|
names = {"alpha", "beta"}
|
|
}
|
|
-- Change float
|
|
data.config.multiplier = 7.777
|
|
modified = true
|
|
`,
|
|
}
|
|
|
|
commands, err := ProcessJSON(jsonContent, command, "test.json")
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, commands)
|
|
|
|
result, _ := utils.ExecuteModifications(commands, jsonContent)
|
|
assert.Contains(t, result, "7.777")
|
|
assert.Contains(t, result, `"settings"`)
|
|
assert.Contains(t, result, `"values"`)
|
|
assert.Contains(t, result, `[1.5,2.5,3.5]`)
|
|
}
|
|
|
|
// TestJSONRemoveFirstArrayElement tests line 358-365 - removing first element with comma handling
|
|
func TestJSONRemoveFirstArrayElement(t *testing.T) {
|
|
jsonContent := `{
|
|
"items": [1, 2, 3, 4, 5]
|
|
}`
|
|
|
|
command := utils.ModifyCommand{
|
|
Name: "test_remove_first",
|
|
JSON: true,
|
|
Lua: `
|
|
table.remove(data.items, 1)
|
|
modified = true
|
|
`,
|
|
}
|
|
|
|
commands, err := ProcessJSON(jsonContent, command, "test.json")
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, commands)
|
|
|
|
result, _ := utils.ExecuteModifications(commands, jsonContent)
|
|
assert.NotContains(t, result, "[1,")
|
|
assert.Contains(t, result, "2")
|
|
assert.Contains(t, result, "5")
|
|
}
|
|
|
|
// TestJSONRemoveLastArrayElement tests line 366-374 - removing last element with comma handling
|
|
func TestJSONRemoveLastArrayElement(t *testing.T) {
|
|
jsonContent := `{
|
|
"items": [1, 2, 3, 4, 5]
|
|
}`
|
|
|
|
command := utils.ModifyCommand{
|
|
Name: "test_remove_last",
|
|
JSON: true,
|
|
Lua: `
|
|
table.remove(data.items, 5)
|
|
modified = true
|
|
`,
|
|
}
|
|
|
|
commands, err := ProcessJSON(jsonContent, command, "test.json")
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, commands)
|
|
|
|
result, _ := utils.ExecuteModifications(commands, jsonContent)
|
|
assert.Contains(t, result, "1")
|
|
assert.Contains(t, result, "4")
|
|
assert.NotContains(t, result, ", 5")
|
|
}
|