go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/assert/it_has_prefix.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package assert 9 10 import ( 11 "fmt" 12 "strings" 13 "testing" 14 ) 15 16 // ItContains a test helper to verify that a string contains a given string. 17 func ItHasPrefix(t *testing.T, corpus string, prefix any, message ...any) { 18 t.Helper() 19 if !hasPrefix(corpus, prefix) { 20 Fatalf(t, "expected %v to have prefix %v", []any{corpus, prefix}, message) 21 } 22 } 23 24 // ItContains a test helper to verify that a string does not contain a given string. 25 func ItNotHasPrefix(t *testing.T, corpus string, prefix any, message ...any) { 26 t.Helper() 27 if hasPrefix(corpus, prefix) { 28 Fatalf(t, "expected %v not to have prefix %v", []any{corpus, prefix}, message) 29 } 30 } 31 32 func hasPrefix(corpus string, prefix any) bool { 33 switch typed := prefix.(type) { 34 case string: 35 return strings.HasPrefix(corpus, typed) 36 case *string: 37 if typed != nil { 38 return strings.HasPrefix(corpus, *typed) 39 } 40 return false 41 default: 42 return strings.HasPrefix(corpus, fmt.Sprint(typed)) 43 } 44 }