github.com/vanus-labs/vanus/lib@v0.0.0-20231221070800-1334a7b9605e/json/path/parse_test.go (about) 1 // Copyright 2023 Linkall Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package path 16 17 import ( 18 // standard libraries. 19 "strings" 20 "testing" 21 22 // third-party libraries. 23 . "github.com/smartystreets/goconvey/convey" 24 25 // this project. 26 "github.com/vanus-labs/vanus/lib/bytes" 27 ) 28 29 func TestParse_consumeSliceSelector(t *testing.T) { 30 buildScanner := func(sliceSelector string) *bytes.MarkScanner { 31 s := bytes.NewMarkScanner([]byte(sliceSelector)) 32 m := strings.Index(sliceSelector, ":") + 1 33 _ = s.Resume(m) 34 return s 35 } 36 37 soResult := func(selector, expect *arraySliceSelector) { 38 if expect.start == nil { 39 So(selector.start, ShouldBeNil) 40 } else { 41 So(selector.start, ShouldNotBeNil) 42 So(*selector.start, ShouldEqual, *expect.start) 43 } 44 if expect.end == nil { 45 So(selector.end, ShouldBeNil) 46 } else { 47 So(selector.end, ShouldNotBeNil) 48 So(*selector.end, ShouldEqual, *expect.end) 49 } 50 So(selector.step, ShouldEqual, expect.step) 51 } 52 53 newInt := func(i int) *int { 54 return &i 55 } 56 57 cases := []struct { 58 seg string 59 expect *arraySliceSelector 60 }{ 61 {"[ : ]", &arraySliceSelector{start: nil, end: nil, step: 1}}, 62 {"[ : : ]", &arraySliceSelector{start: nil, end: nil, step: 1}}, 63 {"[ : : -1 ]", &arraySliceSelector{start: nil, end: nil, step: -1}}, 64 {"[ 0 : : ]", &arraySliceSelector{start: newInt(0), end: nil, step: 1}}, 65 {"[ : 0 : ]", &arraySliceSelector{start: nil, end: newInt(0), step: 1}}, 66 {"[ 1 : 3 ]", &arraySliceSelector{start: newInt(1), end: newInt(3), step: 1}}, 67 {"[ 1 : 5 : 2 ]", &arraySliceSelector{start: newInt(1), end: newInt(5), step: 2}}, 68 {"[ 5 : 1 : -2 ]", &arraySliceSelector{start: newInt(5), end: newInt(1), step: -2}}, 69 } 70 71 for i := range cases { 72 c := &cases[i] 73 74 Convey("parse array slice selector: "+c.seg, t, func() { 75 s := buildScanner(c.seg) 76 77 selector, err := consumeSliceSelector(c.expect.start, s) 78 So(err, ShouldBeNil) 79 So(s.Mark(0), ShouldEqual, len(c.seg)-2) 80 soResult(selector, c.expect) 81 }) 82 } 83 } 84 85 func TestParse_ConsumeSegments(t *testing.T) { 86 Convey("parse", t, func() { 87 jp := "$.foo['bar'].baz[0].qux" 88 s := bytes.NewMarkScanner([]byte(jp)) 89 s.Resume(1) 90 91 segments, err := ConsumeSegments(s) 92 So(err, ShouldBeNil) 93 So(s.Mark(0), ShouldEqual, len(jp)) 94 So(segments, ShouldHaveLength, 5) 95 }) 96 }