1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
// Copyright (c) 2020, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
package util_test
import (
"encoding/json"
"reflect"
"testing"
"github.com/maxatome/go-testdeep/internal/util"
)
func TestJSONPointer(t *testing.T) {
var ref any
err := json.Unmarshal([]byte(`
{
"foo": ["bar", "baz"],
"": 0,
"a/b": 1,
"c%d": 2,
"e^f": 3,
"g|h": 4,
"i\\j": 5,
"k\"l": 6,
" ": 7,
"m~n": 8
}`),
&ref)
if err != nil {
t.Fatalf("json.Unmarshal failed: %s", err)
}
checkOK := func(pointer string, expected any) {
t.Helper()
got, err := util.JSONPointer(ref, pointer)
if !reflect.DeepEqual(got, expected) {
t.Errorf("got: %v expected: %v", got, expected)
}
if err != nil {
t.Errorf("error <%s> received instead of nil", err)
}
}
checkErr := func(pointer, errExpected string) {
t.Helper()
got, err := util.JSONPointer(ref, pointer)
if got != nil {
t.Errorf("got: %v expected: nil", got)
}
if err == nil {
t.Errorf("error nil received instead of <%s>", errExpected)
} else if err.Error() != errExpected {
t.Errorf("error <%s> received instead of <%s>", err, errExpected)
}
}
checkOK(``, ref)
checkOK(`/foo`, []any{"bar", "baz"})
checkOK(`/foo/0`, "bar")
checkOK(`/`, float64(0))
checkOK(`/a~1b`, float64(1))
checkOK(`/c%d`, float64(2))
checkOK(`/e^f`, float64(3))
checkOK(`/g|h`, float64(4))
checkOK(`/i\j`, float64(5))
checkOK(`/k"l`, float64(6))
checkOK(`/ `, float64(7))
checkOK(`/m~0n`, float64(8))
checkErr("x", "invalid JSON pointer")
checkErr("/8", "key not found @/8")
checkErr("/foo/-1/pipo", "array but not an index in JSON pointer @/foo/-1")
checkErr("/foo/bingo/pipo", "array but not an index in JSON pointer @/foo/bingo")
checkErr("/foo/2/pipo", "out of array range @/foo/2")
checkErr("/foo/1/pipo", "not a map nor an array @/foo/1/pipo")
}
|