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