github.com/matrixorigin/matrixone@v1.2.0/pkg/container/bytejson/path_test.go (about) 1 // Copyright 2022 Matrix Origin 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 bytejson 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestValid(t *testing.T) { 24 var kases = []struct { 25 path string 26 valid bool 27 sz int 28 }{ 29 {"$", true, 0}, 30 {"$.", false, 0}, 31 {"$.*", true, 1}, 32 {"$.a", true, 1}, 33 {"$..a.", false, 0}, 34 {"$.a.b", true, 2}, 35 {"$.a..b", false, 0}, 36 {"$[1]", true, 1}, 37 {"$[1].", false, 1}, 38 {"$[1].a", true, 2}, 39 {"$[1]..a", false, 0}, 40 {"$[1].a.b", true, 3}, 41 {"$[1].a..b", false, 0}, 42 {"$[-1]", false, 0}, 43 {"$[1][2]", true, 2}, 44 {"$[1][2].", false, 2}, 45 {"$[1][2].a", true, 3}, 46 {"$[1][2]..a", false, 0}, 47 {"$[*]", true, 1}, 48 {"$[*].", false, 1}, 49 {"$[*].a", true, 2}, 50 {`$[*]."a"`, true, 2}, 51 {"$[*]..a", false, 0}, 52 {"$[*].a.b", true, 3}, 53 {"$[*].a..b", false, 0}, 54 {"$[1][*]", true, 2}, 55 {"$[1][*].", false, 2}, 56 {"cscdwg", false, 0}, 57 {"$.**", false, 0}, 58 {"$**", false, 0}, 59 {"$**.a", true, 2}, 60 {"$**.a[1]", true, 3}, 61 {"$*1", false, 0}, 62 {"$*a", false, 0}, 63 {"$***", false, 0}, 64 {"$**a", false, 0}, 65 {"$**[1]", true, 2}, 66 {"$.a**.1", false, 0}, 67 {"$.a**.a", true, 3}, 68 {"$.a**.\"1\"", true, 3}, 69 {"$[last]", true, 1}, 70 {"$[last-1]", true, 1}, 71 {"$[last -1]", true, 1}, 72 {"$[last - 1]", true, 1}, 73 {"$[ last - 1 ]", true, 1}, 74 {"$[last +1]", false, 1}, 75 {"$[0 to 1]", true, 1}, 76 {"$[last to 0]", true, 1}, 77 {"$[last - 1 to 1]", true, 1}, 78 {"$[last -2 to last -3]", false, 1}, 79 {"$[last -3 to last -2]", true, 1}, 80 {"$[0to1]", false, 1}, 81 {"$[0 to1", false, 1}, 82 {"$[0to 1]", false, 1}, 83 {"$[lastto0]", false, 1}, 84 {"$[last-1to1", false, 1}, 85 {"$[last -1to 1", false, 1}, 86 {"$[last -1t to0", false, 1}, 87 } 88 for _, kase := range kases { 89 v, err := ParseJsonPath(kase.path) 90 if kase.valid { 91 if err != nil { 92 t.Errorf("%s is valid, but error: %s", kase.path, err) 93 } 94 require.Nil(t, err) 95 require.Equal(t, kase.sz, len(v.paths)) 96 } else { 97 if err == nil { 98 t.Errorf("%s is invalid, but no error", kase.path) 99 } 100 require.NotNil(t, err) 101 } 102 } 103 } 104 func TestStar(t *testing.T) { 105 var kases = []struct { 106 path string 107 flag pathFlag 108 }{ 109 {"$[*]", pathFlagSingleStar}, 110 {"$[*].a", pathFlagSingleStar}, 111 {`$[*]."a"`, pathFlagSingleStar}, 112 {"$.a[*]", pathFlagSingleStar}, 113 {"$.a[*].b", pathFlagSingleStar}, 114 {`$.a[*]."b"`, pathFlagSingleStar}, 115 {"$**.a", pathFlagDoubleStar}, 116 {`$**.a[1]`, pathFlagDoubleStar}, 117 {`$.a[1]`, 0}, 118 } 119 for _, kase := range kases { 120 p, err := ParseJsonPath(kase.path) 121 if err != nil { 122 t.Errorf("%s is invalid, but no error", kase.path) 123 } 124 require.Nil(t, err) 125 require.Equal(t, kase.flag, p.flag) 126 } 127 }