Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover existing operations with tests #3

Merged
merged 2 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/operations/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ func Delete(root types.Node, path []string, args ...any) error {

node, err := root.Visit(path[0])

if err != nil {
if err == types.ErrFieldMissing {
return nil
} else if err != nil {
return err
}

Expand Down
46 changes: 46 additions & 0 deletions pkg/operations/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package operations

import (
"testing"

"github.com/alecthomas/assert/v2"
"github.com/can3p/sackmesser/pkg/traverse/simplejson"
)

func TestDeleteOperation(t *testing.T) {
jstr := `{ "abc": { "def": [ 1, 2, 3 ] } }`

examples := []struct {
description string
path []string
expected string
isErr bool
}{
{
description: "delete existing field",
path: []string{"abc"},
expected: `{}`,
},
{
description: "delete missing field is fine, it was deleted already",
path: []string{"nonexistant"},
expected: jstr,
},
}

for idx, ex := range examples {
node := simplejson.MustParse([]byte(jstr))

err := Delete(node, ex.path)

if ex.isErr {
assert.Error(t, err, "[Ex %d - %s]", idx+1, ex.description)
continue
}

expected := simplejson.MustParse([]byte(ex.expected))

assert.Equal(t, expected.Value(), node.Value(), "[Ex %d - %s]", idx+1, ex.description)
}

}
81 changes: 81 additions & 0 deletions pkg/operations/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package operations

import (
"testing"

"github.com/alecthomas/assert/v2"
"github.com/can3p/sackmesser/pkg/traverse/simplejson"
)

func TestSetOperation(t *testing.T) {
jstr := `{ "abc": { "def": [ 1, 2, 3 ] } }`

examples := []struct {
description string
path []string
arg any
expected string
isErr bool
}{
{
description: "set existing field bool",
path: []string{"abc"},
arg: true,
expected: `{ "abc": true }`,
},
// not testing integers since json parser parses everything into floats
// by default
{
description: "set existing field number",
path: []string{"abc"},
arg: 1234.0,
expected: `{ "abc": 1234.0 }`,
},
{
description: "set existing field string",
path: []string{"abc"},
arg: "test",
expected: `{ "abc": "test" }`,
},
{
description: "set existing field null",
path: []string{"abc"},
arg: nil,
expected: `{ "abc": null }`,
},
{
description: "set existing field json",
path: []string{"abc"},
arg: map[string]any{"one": "two"},
expected: `{ "abc": { "one": "two" } }`,
},
{
description: "set new field",
path: []string{"new field"},
arg: true,
expected: `{ "abc": { "def": [ 1, 2, 3 ] }, "new field": true }`,
},
{
description: "set array field",
path: []string{"abc", "def", "0"},
arg: true,
expected: `{ "abc": { "def": [ true, 2, 3 ] } }`,
},
}

for idx, ex := range examples {
node := simplejson.MustParse([]byte(jstr))

err := Set(node, ex.path, ex.arg)

if ex.isErr {
assert.Error(t, err, "[Ex %d - %s]", idx+1, ex.description)
continue
}

expected := simplejson.MustParse([]byte(ex.expected))

assert.Equal(t, expected.Value(), node.Value(), "[Ex %d - %s]", idx+1, ex.description)
}

}
10 changes: 10 additions & 0 deletions pkg/traverse/simplejson/simplejson.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ func Parse(b []byte) (types.Node, error) {
}, nil
}

func MustParse(b []byte) types.Node {
n, err := Parse(b)

if err != nil {
panic(err)
}

return n
}

func FromNode(n types.Node) types.Node {
j := n.Value()

Expand Down
Loading