github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/ref/ref_spec_pattern_test.go (about) 1 // Copyright 2019 Dolthub, 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 ref 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 ) 22 23 func TestStringPattern(t *testing.T) { 24 sp := strPattern("refs/heads/master") 25 26 captured, matchesMaster := sp.matches("refs/heads/master") 27 assert.True(t, matchesMaster, "should match master branch ref") 28 assert.True(t, captured == "", "nothing to capture") 29 30 captured, matchesFeature := sp.matches("refs/heads/feature") 31 assert.False(t, matchesFeature, "shouldn't match feature branch ref") 32 assert.True(t, captured == "", "nothing to capture") 33 } 34 35 func TestWildcardPattern(t *testing.T) { 36 type patternTest struct { 37 refStr string 38 expectedCaptured string 39 expectedMatch bool 40 } 41 42 tests := []struct { 43 pattern string 44 patternTest []patternTest 45 }{ 46 { 47 "refs/heads/*", 48 []patternTest{ 49 {"refs/heads/master", "master", true}, 50 {"refs/heads/feature", "feature", true}, 51 {"refs/heads/bh/my/feature", "bh/my/feature", true}, 52 }, 53 }, 54 { 55 "refs/heads/bh/*", 56 []patternTest{ 57 {"refs/heads/master", "", false}, 58 {"refs/heads/bh/my/feature", "my/feature", true}, 59 }, 60 }, 61 { 62 "refs/heads/*/master", 63 []patternTest{ 64 {"refs/heads/master", "", false}, 65 {"refs/heads/bh/master", "bh", true}, 66 {"refs/heads/as/master", "as", true}, 67 }, 68 }, 69 } 70 71 for _, test := range tests { 72 t.Run("'"+test.pattern+"'", func(t *testing.T) { 73 wcp := newWildcardPattern(test.pattern) 74 75 for _, patternTest := range test.patternTest { 76 t.Run("'"+patternTest.refStr+"'", func(t *testing.T) { 77 captured, matches := wcp.matches(patternTest.refStr) 78 79 assert.Truef(t, captured == patternTest.expectedCaptured, "%s != %s", captured, patternTest.expectedCaptured) 80 assert.Truef(t, matches == patternTest.expectedMatch, "%b != %b", matches, patternTest.expectedMatch) 81 }) 82 } 83 }) 84 } 85 }