util/strs: add new package for string utility funcs

Updates #5309

Change-Id: I677cc6e01050b6e10d8d6907d961b11c7a787a05
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2022-08-05 15:01:41 -07:00
committed by Brad Fitzpatrick
parent 622d80c007
commit 01e8ef7293
2 changed files with 66 additions and 0 deletions

30
util/strs/strs_test.go Normal file
View File

@@ -0,0 +1,30 @@
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package strs
import "testing"
func TestCut(t *testing.T) {
tests := []struct {
fn func(string, string) (string, bool)
in1, in2 string
want string
wantOK bool
}{
{CutPrefix, "foo", "fo", "o", true},
{CutPrefix, "bar", "fo", "bar", false},
{CutSuffix, "foo", "o", "fo", true},
{CutSuffix, "bar", "fo", "bar", false},
}
for i, tt := range tests {
got, gotOK := tt.fn(tt.in1, tt.in2)
if got != tt.want {
t.Errorf("%d. got %q; want %q", i, got, tt.want)
}
if gotOK != tt.wantOK {
t.Errorf("%d. got %v; want %v", i, gotOK, tt.wantOK)
}
}
}