2022-03-22 09:21:55 +01:00
|
|
|
package jsonpointer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pointerSetTestCase struct {
|
|
|
|
DocPath string
|
|
|
|
Pointer string
|
2022-09-27 22:20:44 +02:00
|
|
|
Force bool
|
2022-03-22 09:21:55 +01:00
|
|
|
Value interface{}
|
2022-09-27 22:20:44 +02:00
|
|
|
ExpectedError error
|
2022-03-22 09:21:55 +01:00
|
|
|
ExpectedRawDocument string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPointerSet(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testCases := []pointerSetTestCase{
|
|
|
|
{
|
|
|
|
DocPath: "./testdata/set/basic.json",
|
|
|
|
Pointer: "/foo",
|
|
|
|
Value: "bar",
|
|
|
|
ExpectedRawDocument: `{"foo":"bar"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DocPath: "./testdata/set/nested.json",
|
|
|
|
Pointer: "/nestedObject/foo/1",
|
|
|
|
Value: "test",
|
|
|
|
ExpectedRawDocument: `
|
|
|
|
{
|
|
|
|
"nestedObject": {
|
|
|
|
"foo": [
|
|
|
|
"bar",
|
2022-09-27 22:20:44 +02:00
|
|
|
"test",
|
|
|
|
{
|
|
|
|
"prop1": {
|
|
|
|
"subProp": 1
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 09:21:55 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DocPath: "./testdata/set/nested.json",
|
|
|
|
Pointer: "/nestedObject/foo/-",
|
|
|
|
Value: "baz",
|
|
|
|
ExpectedRawDocument: `
|
|
|
|
{
|
|
|
|
"nestedObject": {
|
|
|
|
"foo": [
|
|
|
|
"bar",
|
|
|
|
0,
|
2022-09-27 22:20:44 +02:00
|
|
|
{
|
|
|
|
"prop1": {
|
|
|
|
"subProp": 1
|
|
|
|
}
|
|
|
|
},
|
2022-03-22 09:21:55 +01:00
|
|
|
"baz"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
},
|
2022-09-27 22:20:44 +02:00
|
|
|
{
|
|
|
|
DocPath: "./testdata/set/nested.json",
|
|
|
|
Pointer: "/nestedObject/foo/2/prop2",
|
|
|
|
Value: "baz",
|
|
|
|
Force: true,
|
|
|
|
ExpectedRawDocument: `
|
|
|
|
{
|
|
|
|
"nestedObject": {
|
|
|
|
"foo": [
|
|
|
|
"bar",
|
|
|
|
0,
|
|
|
|
{
|
|
|
|
"prop2": "baz",
|
|
|
|
"prop1": {
|
|
|
|
"subProp": 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DocPath: "./testdata/set/nested.json",
|
|
|
|
Pointer: "/nestedObject/foo/2/prop2",
|
|
|
|
Value: "baz",
|
|
|
|
Force: false,
|
|
|
|
ExpectedError: ErrNotFound,
|
|
|
|
},
|
2022-03-22 09:21:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
func(index int, tc pointerSetTestCase) {
|
|
|
|
t.Run(fmt.Sprintf("#%d: '%s'", i, tc.Pointer), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
baseRawDocument, err := ioutil.ReadFile(tc.DocPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
var baseDoc interface{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(baseRawDocument), &baseDoc); err != nil {
|
|
|
|
t.Fatal(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
pointer := New(tc.Pointer)
|
|
|
|
|
2022-09-27 22:20:44 +02:00
|
|
|
var updatedDoc interface{}
|
|
|
|
|
|
|
|
if tc.Force {
|
|
|
|
updatedDoc, err = pointer.Force(baseDoc, tc.Value)
|
|
|
|
} else {
|
|
|
|
updatedDoc, err = pointer.Set(baseDoc, tc.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.ExpectedError != nil && !errors.Is(err, tc.ExpectedError) {
|
|
|
|
t.Fatalf("Expected error '%v', got '%v'", tc.ExpectedError, errors.Cause(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.ExpectedError == nil && err != nil {
|
2022-03-22 09:21:55 +01:00
|
|
|
t.Fatal(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
rawDoc, err := json.MarshalIndent(updatedDoc, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
var expectedDoc interface{}
|
|
|
|
|
2022-09-27 22:20:44 +02:00
|
|
|
if tc.ExpectedRawDocument == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 09:21:55 +01:00
|
|
|
if err := json.Unmarshal([]byte(tc.ExpectedRawDocument), &expectedDoc); err != nil {
|
|
|
|
t.Fatal(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expectedDoc, updatedDoc) {
|
2022-09-27 22:20:44 +02:00
|
|
|
command := "Set"
|
|
|
|
if tc.Force {
|
|
|
|
command = "Force"
|
|
|
|
}
|
|
|
|
t.Errorf("%s pointer '%s' -> '%v': expected document '%s', got '%s'", command, tc.Pointer, tc.Value, strings.TrimSpace(tc.ExpectedRawDocument), rawDoc)
|
2022-03-22 09:21:55 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}(i, tc)
|
|
|
|
}
|
|
|
|
}
|