@@ -6,6 +6,7 @@ package jsonpointer
66import (
77 "encoding/json"
88 "fmt"
9+ "os"
910 "strconv"
1011 "testing"
1112
@@ -31,7 +32,11 @@ const (
3132}`
3233)
3334
34- var testDocumentJSON any
35+ var (
36+ testDocumentJSON any
37+ testStructJSONDoc testStructJSON
38+ testStructJSONPtr * testStructJSON
39+ )
3540
3641type testStructJSON struct {
3742 Foo []string `json:"foo"`
@@ -48,18 +53,26 @@ type testStructJSON struct {
4853
4954type aliasedMap map [string ]any
5055
51- var testStructJSONDoc testStructJSON
52- var testStructJSONPtr * testStructJSON
56+ func TestMain (m * testing.M ) {
57+ // initializations to run tests in this package
58+ if err := initTestDocument (); err != nil {
59+ panic (err )
60+ }
61+
62+ os .Exit (m .Run ())
63+ }
5364
54- func init () {
65+ func initTestDocument () error {
5566 if err := json .Unmarshal ([]byte (TestDocumentString ), & testDocumentJSON ); err != nil {
56- panic ( err )
67+ return err
5768 }
5869 if err := json .Unmarshal ([]byte (TestDocumentString ), & testStructJSONDoc ); err != nil {
59- panic ( err )
70+ return err
6071 }
6172
6273 testStructJSONPtr = & testStructJSONDoc
74+
75+ return nil
6376}
6477
6578func TestEscaping (t * testing.T ) {
@@ -83,19 +96,24 @@ func TestFullDocument(t *testing.T) {
8396 p , err := New (in )
8497 require .NoErrorf (t , err , "New(%v) error %v" , in , err )
8598
86- result , _ , err := p .Get (testDocumentJSON )
87- require .NoErrorf (t , err , "Get(%v) error %v" , in , err )
99+ t .Run ("should resolve doc" , func (t * testing.T ) {
100+ result , _ , err := p .Get (testDocumentJSON )
101+ require .NoErrorf (t , err , "Get(%v) error %v" , in , err )
88102
89- asMap , ok := result .(map [string ]any )
90- require .True (t , ok )
91- require .Lenf (t , asMap , TestDocumentNBItems , "Get(%v) = %v, expect full document" , in , result )
103+ asMap , ok := result .(map [string ]any )
104+ require .True (t , ok )
105+
106+ require .Lenf (t , asMap , TestDocumentNBItems , "Get(%v) = %v, expect full document" , in , result )
107+ })
92108
93- result , _ , err = p .get (testDocumentJSON , nil )
94- require .NoErrorf (t , err , "Get(%v) error %v" , in , err )
109+ t .Run ("should resolve doc, with nil name provider" , func (t * testing.T ) {
110+ result , _ , err := p .get (testDocumentJSON , nil )
111+ require .NoErrorf (t , err , "Get(%v) error %v" , in , err )
95112
96- asMap , ok = result .(map [string ]any )
97- require .True (t , ok )
98- require .Lenf (t , asMap , TestDocumentNBItems , "Get(%v) = %v, expect full document" , in , result )
113+ asMap , ok := result .(map [string ]any )
114+ require .True (t , ok )
115+ require .Lenf (t , asMap , TestDocumentNBItems , "Get(%v) = %v, expect full document" , in , result )
116+ })
99117}
100118
101119func TestDecodedTokens (t * testing.T ) {
@@ -227,7 +245,10 @@ func TestGetNode(t *testing.T) {
227245 require .NoError (t , err )
228246 assert .Len (t , result , TestNodeObjNBItems )
229247
230- result , _ , err = p .Get (aliasedMap (testDocumentJSON .(map [string ]any )))
248+ asMap , ok := testDocumentJSON .(map [string ]any )
249+ require .True (t , ok )
250+
251+ result , _ , err = p .Get (aliasedMap (asMap ))
231252 require .NoError (t , err )
232253 assert .Len (t , result , TestNodeObjNBItems )
233254
@@ -301,9 +322,19 @@ func TestOtherThings(t *testing.T) {
301322
302323 p , err = New ("/foo/1" )
303324 require .NoError (t , err )
304- expected := "hello"
305- bbb := testDocumentJSON .(map [string ]any )["foo" ]
306- bbb .([]any )[1 ] = "hello"
325+
326+ const expected = "hello"
327+
328+ asMap , ok := testDocumentJSON .(map [string ]any )
329+ require .True (t , ok )
330+
331+ bbb , ok := asMap ["foo" ]
332+ require .True (t , ok )
333+
334+ asArray , ok := bbb .([]any )
335+ require .True (t , ok )
336+
337+ asArray [1 ] = expected
307338
308339 v , _ , err := p .Get (testDocumentJSON )
309340 require .NoError (t , err )
@@ -513,7 +544,9 @@ func TestSetNode(t *testing.T) {
513544 chNodeVI := changedNode ["c" ]
514545
515546 require .IsType (t , 0 , chNodeVI )
516- changedNodeValue := chNodeVI .(int )
547+ changedNodeValue , ok := chNodeVI .(int )
548+ require .True (t , ok )
549+
517550 require .Equal (t , 999 , changedNodeValue )
518551 assert .Len (t , sliceNode , 1 )
519552 })
0 commit comments