go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/assert/it_has_suffix.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 ItHasSuffix(t *testing.T, corpus string, suffix any, message ...any) { 18 t.Helper() 19 if !hasSuffix(corpus, suffix) { 20 Fatalf(t, "expected %v to have suffix %v", []any{corpus, suffix}, message) 21 } 22 } 23 24 // ItContains a test helper to verify that a string does not contain a given string. 25 func ItNotHasSuffix(t *testing.T, corpus string, suffix any, message ...any) { 26 t.Helper() 27 if hasSuffix(corpus, suffix) { 28 Fatalf(t, "expected %v not to have suffix %v", []any{corpus, suffix}, message) 29 } 30 } 31 32 func hasSuffix(corpus string, suffix any) bool { 33 switch typed := suffix.(type) { 34 case string: 35 return strings.HasSuffix(corpus, typed) 36 case *string: 37 if typed != nil { 38 return strings.HasSuffix(corpus, *typed) 39 } 40 return false 41 default: 42 return strings.HasSuffix(corpus, fmt.Sprint(typed)) 43 } 44 }