go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/data/text/pattern/pattern_test.go (about) 1 // Copyright 2015 The LUCI Authors. 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 pattern 16 17 import ( 18 "regexp" 19 "testing" 20 21 . "github.com/smartystreets/goconvey/convey" 22 ) 23 24 func TestPattern(t *testing.T) { 25 t.Parallel() 26 27 Convey("Pattern", t, func() { 28 Convey("Exact", func() { 29 p := Exact("a") 30 So(p.Match("a"), ShouldBeTrue) 31 So(p.Match("b"), ShouldBeFalse) 32 So(p.String(), ShouldEqual, "exact:a") 33 }) 34 35 Convey("Regex", func() { 36 p := Regexp(regexp.MustCompile("^ab?$")) 37 So(p.Match("a"), ShouldBeTrue) 38 So(p.Match("ab"), ShouldBeTrue) 39 So(p.Match("b"), ShouldBeFalse) 40 So(p.String(), ShouldEqual, "regex:^ab?$") 41 }) 42 43 Convey("Any", func() { 44 So(Any.Match("a"), ShouldBeTrue) 45 So(Any.Match("b"), ShouldBeTrue) 46 So(Any.String(), ShouldEqual, "*") 47 }) 48 49 Convey("None", func() { 50 So(None.Match("a"), ShouldBeFalse) 51 So(None.Match("b"), ShouldBeFalse) 52 So(None.String(), ShouldEqual, "") 53 }) 54 55 Convey("Parse", func() { 56 Convey("Good", func() { 57 patterns := []Pattern{ 58 Exact("a"), 59 Regexp(regexp.MustCompile("^ab$")), 60 Any, 61 None, 62 } 63 for _, p := range patterns { 64 p2, err := Parse(p.String()) 65 So(err, ShouldBeNil) 66 So(p2.String(), ShouldEqual, p.String()) 67 } 68 }) 69 70 Convey("Without '^' and '$'", func() { 71 p, err := Parse("regex:deadbeef") 72 So(err, ShouldBeNil) 73 So(p.String(), ShouldEqual, "regex:^deadbeef$") 74 }) 75 76 Convey("Bad", func() { 77 bad := []string{ 78 ":", 79 "a:", 80 "a:b", 81 "regex:)", 82 } 83 for _, s := range bad { 84 _, err := Parse(s) 85 So(err, ShouldNotBeNil) 86 } 87 }) 88 }) 89 }) 90 }