go.mway.dev/x@v0.0.0-20240520034138-950aede9a3fb/slices/slices_test.go (about) 1 // Copyright (c) 2024 Matt Way 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to 5 // deal in the Software without restriction, including without limitation the 6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 // sell copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 // IN THE THE SOFTWARE. 20 21 package slices_test 22 23 import ( 24 "testing" 25 26 "github.com/stretchr/testify/require" 27 "go.mway.dev/x/slices" 28 ) 29 30 func TestHasPrefix(t *testing.T) { 31 cases := map[string]struct { 32 giveSlice []int 33 givePrefix []int 34 wantHasPrefix bool 35 }{ 36 "nil slice nil prefix": { 37 giveSlice: nil, 38 givePrefix: nil, 39 wantHasPrefix: true, 40 }, 41 "non-nil slice nil prefix": { 42 giveSlice: []int{0}, 43 givePrefix: nil, 44 wantHasPrefix: true, 45 }, 46 "not prefixed": { 47 giveSlice: []int{0, 1, 2}, 48 givePrefix: []int{1, 2}, 49 wantHasPrefix: false, 50 }, 51 "prefixed": { 52 giveSlice: []int{0, 1, 2}, 53 givePrefix: []int{0, 1}, 54 wantHasPrefix: true, 55 }, 56 "equal": { 57 giveSlice: []int{0, 1, 2}, 58 givePrefix: []int{0, 1, 2}, 59 wantHasPrefix: true, 60 }, 61 "prefix too long": { 62 giveSlice: []int{0, 1}, 63 givePrefix: []int{0, 1, 2}, 64 wantHasPrefix: false, 65 }, 66 } 67 68 for name, tt := range cases { 69 t.Run(name, func(t *testing.T) { 70 require.Equal( 71 t, 72 tt.wantHasPrefix, 73 slices.HasPrefix(tt.giveSlice, tt.givePrefix), 74 ) 75 }) 76 } 77 }