gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/internal/binarylog/regexp_test.go (about) 1 /* 2 * 3 * Copyright 2018 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package binarylog 20 21 import ( 22 "reflect" 23 "testing" 24 ) 25 26 func (s) TestLongMethodConfigRegexp(t *testing.T) { 27 testCases := []struct { 28 in string 29 out []string 30 }{ 31 {in: "", out: nil}, 32 {in: "*/m", out: nil}, 33 34 { 35 in: "p.s/m{}", 36 out: []string{"p.s/m{}", "p.s", "m", "{}"}, 37 }, 38 39 { 40 in: "p.s/m", 41 out: []string{"p.s/m", "p.s", "m", ""}, 42 }, 43 { 44 in: "p.s/m{h}", 45 out: []string{"p.s/m{h}", "p.s", "m", "{h}"}, 46 }, 47 { 48 in: "p.s/m{m}", 49 out: []string{"p.s/m{m}", "p.s", "m", "{m}"}, 50 }, 51 { 52 in: "p.s/m{h:123}", 53 out: []string{"p.s/m{h:123}", "p.s", "m", "{h:123}"}, 54 }, 55 { 56 in: "p.s/m{m:123}", 57 out: []string{"p.s/m{m:123}", "p.s", "m", "{m:123}"}, 58 }, 59 { 60 in: "p.s/m{h:123,m:123}", 61 out: []string{"p.s/m{h:123,m:123}", "p.s", "m", "{h:123,m:123}"}, 62 }, 63 64 { 65 in: "p.s/*", 66 out: []string{"p.s/*", "p.s", "*", ""}, 67 }, 68 { 69 in: "p.s/*{h}", 70 out: []string{"p.s/*{h}", "p.s", "*", "{h}"}, 71 }, 72 73 { 74 in: "s/m*", 75 out: []string{"s/m*", "s", "m", "*"}, 76 }, 77 { 78 in: "s/**", 79 out: []string{"s/**", "s", "*", "*"}, 80 }, 81 } 82 for _, tc := range testCases { 83 match := longMethodConfigRegexp.FindStringSubmatch(tc.in) 84 if !reflect.DeepEqual(match, tc.out) { 85 t.Errorf("in: %q, out: %q, want: %q", tc.in, match, tc.out) 86 } 87 } 88 } 89 90 func (s) TestHeaderConfigRegexp(t *testing.T) { 91 testCases := []struct { 92 in string 93 out []string 94 }{ 95 {in: "{}", out: nil}, 96 {in: "{a:b}", out: nil}, 97 {in: "{m:123}", out: nil}, 98 {in: "{h:123;m:123}", out: nil}, 99 100 { 101 in: "{h}", 102 out: []string{"{h}", ""}, 103 }, 104 { 105 in: "{h:123}", 106 out: []string{"{h:123}", "123"}, 107 }, 108 } 109 for _, tc := range testCases { 110 match := headerConfigRegexp.FindStringSubmatch(tc.in) 111 if !reflect.DeepEqual(match, tc.out) { 112 t.Errorf("in: %q, out: %q, want: %q", tc.in, match, tc.out) 113 } 114 } 115 } 116 117 func (s) TestMessageConfigRegexp(t *testing.T) { 118 testCases := []struct { 119 in string 120 out []string 121 }{ 122 {in: "{}", out: nil}, 123 {in: "{a:b}", out: nil}, 124 {in: "{h:123}", out: nil}, 125 {in: "{h:123;m:123}", out: nil}, 126 127 { 128 in: "{m}", 129 out: []string{"{m}", ""}, 130 }, 131 { 132 in: "{m:123}", 133 out: []string{"{m:123}", "123"}, 134 }, 135 } 136 for _, tc := range testCases { 137 match := messageConfigRegexp.FindStringSubmatch(tc.in) 138 if !reflect.DeepEqual(match, tc.out) { 139 t.Errorf("in: %q, out: %q, want: %q", tc.in, match, tc.out) 140 } 141 } 142 } 143 144 func (s) TestHeaderMessageConfigRegexp(t *testing.T) { 145 testCases := []struct { 146 in string 147 out []string 148 }{ 149 {in: "{}", out: nil}, 150 {in: "{a:b}", out: nil}, 151 {in: "{h}", out: nil}, 152 {in: "{h:123}", out: nil}, 153 {in: "{m}", out: nil}, 154 {in: "{m:123}", out: nil}, 155 156 { 157 in: "{h;m}", 158 out: []string{"{h;m}", "", ""}, 159 }, 160 { 161 in: "{h:123;m}", 162 out: []string{"{h:123;m}", "123", ""}, 163 }, 164 { 165 in: "{h;m:123}", 166 out: []string{"{h;m:123}", "", "123"}, 167 }, 168 { 169 in: "{h:123;m:123}", 170 out: []string{"{h:123;m:123}", "123", "123"}, 171 }, 172 } 173 for _, tc := range testCases { 174 match := headerMessageConfigRegexp.FindStringSubmatch(tc.in) 175 if !reflect.DeepEqual(match, tc.out) { 176 t.Errorf("in: %q, out: %q, want: %q", tc.in, match, tc.out) 177 } 178 } 179 }