Enable root modifications

Though I can not see why you would want to.....
But there's no reason you would not be able to
This commit is contained in:
2025-03-25 18:57:32 +01:00
parent aba10267d1
commit 4640281fbf
3 changed files with 140 additions and 19 deletions

View File

@@ -937,16 +937,16 @@ func TestJSONProcessor_RestructuringData(t *testing.T) {
"people": {
"developers": [
{
"age": 25,
"id": 1,
"name": "Alice",
"age": 25
"name": "Alice"
}
],
"managers": [
{
"age": 30,
"id": 2,
"name": "Bob",
"age": 30
"name": "Bob"
}
]
}
@@ -1042,3 +1042,83 @@ func TestJSONProcessor_FilteringArrayElements(t *testing.T) {
t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result)
}
}
// TestJSONProcessor_RootNodeModification tests modifying the root node directly
func TestJSONProcessor_RootNodeModification(t *testing.T) {
content := `{
"name": "original",
"value": 100
}`
expected := `{
"name": "modified",
"description": "This is a completely modified root",
"values": [1, 2, 3]
}`
p := &JSONProcessor{}
result, modCount, matchCount, err := p.ProcessContent(content, "$", `
-- Completely replace the root node
v = {
name = "modified",
description = "This is a completely modified root",
values = {1, 2, 3}
}
`)
if err != nil {
t.Fatalf("Error processing content: %v", err)
}
if matchCount != 1 {
t.Errorf("Expected 1 match, got %d", matchCount)
}
if modCount != 1 {
t.Errorf("Expected 1 modification, got %d", modCount)
}
// Normalize whitespace for comparison
normalizedResult := normalizeWhitespace(result)
normalizedExpected := normalizeWhitespace(expected)
if normalizedResult != normalizedExpected {
t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result)
}
}
// TestJSONProcessor_RootNodeModificationToPrimitive tests modifying the root node to a primitive value
func TestJSONProcessor_RootNodeModificationToPrimitive(t *testing.T) {
content := `{
"name": "original",
"value": 100
}`
expected := `42`
p := &JSONProcessor{}
result, modCount, matchCount, err := p.ProcessContent(content, "$", `
-- Replace the root node with a primitive value
v = 42
`)
if err != nil {
t.Fatalf("Error processing content: %v", err)
}
if matchCount != 1 {
t.Errorf("Expected 1 match, got %d", matchCount)
}
if modCount != 1 {
t.Errorf("Expected 1 modification, got %d", modCount)
}
// Normalize whitespace for comparison
normalizedResult := normalizeWhitespace(result)
normalizedExpected := normalizeWhitespace(expected)
if normalizedResult != normalizedExpected {
t.Errorf("Expected content to be:\n%s\n\nGot:\n%s", expected, result)
}
}