This commit is contained in:
sajjanjyothi 2025-06-20 15:37:25 -04:00 committed by GitHub
commit dc502927ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 0 deletions

View File

@ -1301,3 +1301,10 @@ func CutPrefix(s, prefix string) (after string, found bool) {
func CutSuffix(s, suffix string) (before string, found bool) {
return stringslite.CutSuffix(s, suffix)
}
// StringPointerFrom returns a string pointer for the given string constant.
// This is specifically useful when we are writing test cases,
// such as passing a string pointer to a struct field from a string constant.
func StringPointerFrom(s string) *string {
return &s
}

View File

@ -1868,6 +1868,39 @@ func TestCutSuffix(t *testing.T) {
}
}
func TestStringPointerFrom(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
{
name: "valid string",
args: args{
s: "test",
},
want: "test",
},
{
name: "empty string",
args: args{
s: "",
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := StringPointerFrom(tt.args.s); *got != tt.want {
t.Errorf("StringPointerFrom() = %v, want %v", *got, tt.want)
}
})
}
}
func makeBenchInputHard() string {
tokens := [...]string{
"<a>", "<p>", "<b>", "<strong>",