Vectrum
Sign In
28 lines · 969 Bytes · Shell
#!/usr/bin/env bash
set -euo pipefail
export GOPROXY=off GOFLAGS=-mod=mod
go test ./... >/dev/null 2>&1 || { echo "the package's own tests fail"; go test ./... 2>&1; exit 1; }
grep -q 'func Truncate' textutil.go || { echo "Truncate not defined in textutil.go"; exit 1; }
grep -qi 'func Test.*Truncate' *_test.go || { echo "no test for Truncate"; exit 1; }
cat > /tmp/verify_truncate_$$_test.go <<'GO'
package textutil

import "testing"

func TestTruncateBehaviour(t *testing.T) {
	cases := []struct{ in string; max int; want string }{
		{"hello", 10, "hello"},
		{"hello", 5, "hello"},
		{"hello world", 8, "hello..."},
		{"ünïcödé text", 9, "ünïcöd..."},
	}
	for _, c := range cases {
		if got := Truncate(c.in, c.max); got != c.want {
			t.Errorf("Truncate(%q, %d) = %q, want %q", c.in, c.max, got, c.want)
		}
	}
}
GO
cp /tmp/verify_truncate_$$_test.go ./zz_verify_test.go
rm -f /tmp/verify_truncate_$$_test.go
go test -run 'TestTruncateBehaviour' ./... 2>&1