-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for delete operation, make it more forgiving in case user w…
…ants to delete a non-existing field
- Loading branch information
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters