From fefda1abda0b8b7ada7be0c47594528770c4c162 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Mon, 21 Oct 2024 11:59:22 -0400 Subject: [PATCH] internal/texttest: remove Run and Bench helpers Versions of Go whose testing package doesn't yet support subtests and sub-benchmarks are no longer supported. Drop an intermediate layer of compatibility from the ./internal/texttest package. The initial version of this change was git-generate'd with the script: rm internal/testtext/go1_{6,7}.go gofmt -r 'testtext.Run(t, n, f) -> t.Run(n, f)' -w . gofmt -r 'testtext.Bench(b, n, f) -> b.Run(n, f)' -w . goimports -w . Unfortunately it seems gofmt -r dropped inner comments inside f, and also added some spurious blank lines. So it was manually amended not to include those changes. Change-Id: I5bcb553b90ce392aada7896d576194be479f2616 Reviewed-on: https://go-review.googlesource.com/c/text/+/621575 Auto-Submit: Dmitri Shuralyov LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Pratt Reviewed-by: Dmitri Shuralyov --- cases/context_test.go | 3 +-- cases/icu_test.go | 3 +-- cases/map_test.go | 16 ++++++++-------- internal/export/idna/idna_test.go | 2 +- internal/number/number_test.go | 3 +-- internal/testtext/go1_6.go | 23 ----------------------- internal/testtext/go1_7.go | 17 ----------------- language/display/display_test.go | 5 ++--- runes/runes_test.go | 4 ++-- secure/bidirule/bench_test.go | 4 +--- secure/bidirule/bidirule_test.go | 3 +-- secure/precis/benchmark_test.go | 4 +--- secure/precis/enforce_test.go | 2 +- secure/precis/profile_test.go | 3 +-- transform/transform_test.go | 14 +++++++------- unicode/cldr/collate_test.go | 8 ++++---- unicode/norm/normalize_test.go | 5 ++--- width/transform_test.go | 2 +- 18 files changed, 35 insertions(+), 86 deletions(-) delete mode 100644 internal/testtext/go1_6.go delete mode 100644 internal/testtext/go1_7.go diff --git a/cases/context_test.go b/cases/context_test.go index eb4d69321..db84ca25f 100644 --- a/cases/context_test.go +++ b/cases/context_test.go @@ -9,7 +9,6 @@ import ( "testing" "unicode" - "golang.org/x/text/internal/testtext" "golang.org/x/text/language" "golang.org/x/text/transform" "golang.org/x/text/unicode/norm" @@ -214,7 +213,7 @@ func TestCCC(t *testing.T) { func TestWordBreaks(t *testing.T) { for _, tt := range breakTest { desc := norm.NFC.String(tt) - testtext.Run(t, desc, func(t *testing.T) { + t.Run(desc, func(t *testing.T) { parts := strings.Split(tt, "|") want := "" for _, s := range parts { diff --git a/cases/icu_test.go b/cases/icu_test.go index 6d9703501..2c5cdc0b4 100644 --- a/cases/icu_test.go +++ b/cases/icu_test.go @@ -11,7 +11,6 @@ import ( "strings" "testing" - "golang.org/x/text/internal/testtext" "golang.org/x/text/language" "golang.org/x/text/unicode/norm" ) @@ -83,7 +82,7 @@ func TestICUConformance(t *testing.T) { if exclude(c, tag, s) { continue } - testtext.Run(t, path.Join(c, tag, s), func(t *testing.T) { + t.Run(path.Join(c, tag, s), func(t *testing.T) { want := doICU(tag, c, s) got := doGo(tag, c, s) if norm.NFC.String(got) != norm.NFC.String(want) { diff --git a/cases/map_test.go b/cases/map_test.go index 8cfb89e59..bea6c085f 100644 --- a/cases/map_test.go +++ b/cases/map_test.go @@ -205,7 +205,7 @@ func TestAlloc(t *testing.T) { // func() Caser { return Title(language.Und) }, // func() Caser { return Title(language.Und, HandleFinalSigma(false)) }, } { - testtext.Run(t, "", func(t *testing.T) { + t.Run("", func(t *testing.T) { var c Caser v := testtext.AllocsPerRun(10, func() { c = f() @@ -234,7 +234,7 @@ func testHandover(t *testing.T, c Caser, src string) { // Test handover for each substring of the prefix. for i := 0; i < pSrc; i++ { - testtext.Run(t, fmt.Sprint("interleave/", i), func(t *testing.T) { + t.Run(fmt.Sprint("interleave/", i), func(t *testing.T) { dst := make([]byte, 4*len(src)) c.Reset() nSpan, _ := c.Span([]byte(src[:i]), false) @@ -299,7 +299,7 @@ func TestHandover(t *testing.T) { "'", "n bietje", }} for _, tc := range testCases { - testtext.Run(t, tc.desc, func(t *testing.T) { + t.Run(tc.desc, func(t *testing.T) { src := tc.first + tc.second want := tc.t.String(src) tc.t.Reset() @@ -601,7 +601,7 @@ func init() { func TestShortBuffersAndOverflow(t *testing.T) { for i, tt := range bufferTests { - testtext.Run(t, tt.desc, func(t *testing.T) { + t.Run(tt.desc, func(t *testing.T) { buf := make([]byte, tt.dstSize) got := []byte{} var nSrc, nDst int @@ -827,7 +827,7 @@ func TestSpan(t *testing.T) { err: transform.ErrEndOfSpan, t: Title(language.Afrikaans), }} { - testtext.Run(t, tt.desc, func(t *testing.T) { + t.Run(tt.desc, func(t *testing.T) { for p := 0; p < len(tt.want); p += utf8.RuneLen([]rune(tt.src[p:])[0]) { tt.t.Reset() n, err := tt.t.Span([]byte(tt.src[:p]), false) @@ -901,7 +901,7 @@ func BenchmarkCasers(b *testing.B) { {"title", bytes.ToTitle}, {"upper", bytes.ToUpper}, } { - testtext.Bench(b, path.Join(s.name, "bytes", f.name), func(b *testing.B) { + b.Run(path.Join(s.name, "bytes", f.name), func(b *testing.B) { b.SetBytes(int64(len(src))) for i := 0; i < b.N; i++ { f.fn(src) @@ -921,7 +921,7 @@ func BenchmarkCasers(b *testing.B) { } { c := Caser{t.caser} dst := make([]byte, len(src)) - testtext.Bench(b, path.Join(s.name, t.name, "transform"), func(b *testing.B) { + b.Run(path.Join(s.name, t.name, "transform"), func(b *testing.B) { b.SetBytes(int64(len(src))) for i := 0; i < b.N; i++ { c.Reset() @@ -934,7 +934,7 @@ func BenchmarkCasers(b *testing.B) { continue } spanSrc := c.Bytes(src) - testtext.Bench(b, path.Join(s.name, t.name, "span"), func(b *testing.B) { + b.Run(path.Join(s.name, t.name, "span"), func(b *testing.B) { c.Reset() if n, _ := c.Span(spanSrc, true); n < len(spanSrc) { b.Fatalf("spanner is not recognizing text %q as done (at %d)", spanSrc, n) diff --git a/internal/export/idna/idna_test.go b/internal/export/idna/idna_test.go index a13b67348..543fe527b 100644 --- a/internal/export/idna/idna_test.go +++ b/internal/export/idna/idna_test.go @@ -81,7 +81,7 @@ func doTest(t *testing.T, f func(string) (string, error), name, input, want, err in = strings.Replace(in, `\U`, "#", -1) name = fmt.Sprintf("%s/%s/%s", name, in, test) - testtext.Run(t, name, func(t *testing.T) { + t.Run(name, func(t *testing.T) { got, err := f(input) if err != nil { diff --git a/internal/number/number_test.go b/internal/number/number_test.go index cbc28ab49..6fe81f790 100644 --- a/internal/number/number_test.go +++ b/internal/number/number_test.go @@ -8,7 +8,6 @@ import ( "fmt" "testing" - "golang.org/x/text/internal/testtext" "golang.org/x/text/language" ) @@ -93,7 +92,7 @@ func TestFormats(t *testing.T) { {"zgh", "#,##0 %", tagToPercent}, } for _, tc := range testCases { - testtext.Run(t, tc.lang, func(t *testing.T) { + t.Run(tc.lang, func(t *testing.T) { got := formatForLang(language.MustParse(tc.lang), tc.index) want, _ := ParsePattern(tc.pattern) if *got != *want { diff --git a/internal/testtext/go1_6.go b/internal/testtext/go1_6.go deleted file mode 100644 index d7b4947e4..000000000 --- a/internal/testtext/go1_6.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build !go1.7 - -package testtext - -import "testing" - -func Run(t *testing.T, name string, fn func(t *testing.T)) bool { - t.Logf("Running %s...", name) - fn(t) - return t.Failed() -} - -// Bench runs the given benchmark function. This pre-1.7 implementation renders -// the measurement useless, but allows the code to be compiled at least. -func Bench(b *testing.B, name string, fn func(b *testing.B)) bool { - b.Logf("Running %s...", name) - fn(b) - return b.Failed() -} diff --git a/internal/testtext/go1_7.go b/internal/testtext/go1_7.go deleted file mode 100644 index 8153af67c..000000000 --- a/internal/testtext/go1_7.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.7 - -package testtext - -import "testing" - -func Run(t *testing.T, name string, fn func(t *testing.T)) bool { - return t.Run(name, fn) -} - -func Bench(b *testing.B, name string, fn func(b *testing.B)) bool { - return b.Run(name, fn) -} diff --git a/language/display/display_test.go b/language/display/display_test.go index f26bace14..94a1cfce1 100644 --- a/language/display/display_test.go +++ b/language/display/display_test.go @@ -11,7 +11,6 @@ import ( "testing" "unicode" - "golang.org/x/text/internal/testtext" "golang.org/x/text/language" "golang.org/x/text/message" ) @@ -40,7 +39,7 @@ func TestValues(t *testing.T) { // checkDefined checks that a value exists in a Namer. checkDefined := func(x interface{}, namers []testcase) { for _, n := range namers { - testtext.Run(t, fmt.Sprintf("%s.Name(%s)", n.kind, x), func(t *testing.T) { + t.Run(fmt.Sprintf("%s.Name(%s)", n.kind, x), func(t *testing.T) { if n.n.Name(x) == "" { // As of version 28 there is no data for az-Arab in English, // although there is useful data in other languages. @@ -449,7 +448,7 @@ func TestLanguage(t *testing.T) { {"en", "sr-Latn-ME", "Serbo-Croatian"}, // See comments in TestTag. } for _, tt := range tests { - testtext.Run(t, tt.dict+"/"+tt.tag, func(t *testing.T) { + t.Run(tt.dict+"/"+tt.tag, func(t *testing.T) { name, fmtName := splitName(tt.name) dict := language.MustParse(tt.dict) tag := language.Raw.MustParse(tt.tag) diff --git a/runes/runes_test.go b/runes/runes_test.go index 23c5bc952..b5d8c6e1d 100644 --- a/runes/runes_test.go +++ b/runes/runes_test.go @@ -626,7 +626,7 @@ func doBench(b *testing.B, t Transformer) { dst := make([]byte, 2*len(bc.data)) src := []byte(bc.data) - testtext.Bench(b, bc.name+"/transform", func(b *testing.B) { + b.Run(bc.name+"/transform", func(b *testing.B) { b.SetBytes(int64(len(src))) for i := 0; i < b.N; i++ { t.Transform(dst, src, true) @@ -634,7 +634,7 @@ func doBench(b *testing.B, t Transformer) { }) src = t.Bytes(src) t.Reset() - testtext.Bench(b, bc.name+"/span", func(b *testing.B) { + b.Run(bc.name+"/span", func(b *testing.B) { b.SetBytes(int64(len(src))) for i := 0; i < b.N; i++ { t.Span(src, true) diff --git a/secure/bidirule/bench_test.go b/secure/bidirule/bench_test.go index 2db922bfd..34ed5b8e8 100644 --- a/secure/bidirule/bench_test.go +++ b/secure/bidirule/bench_test.go @@ -6,8 +6,6 @@ package bidirule import ( "testing" - - "golang.org/x/text/internal/testtext" ) var benchData = []struct{ name, data string }{ @@ -18,7 +16,7 @@ var benchData = []struct{ name, data string }{ func doBench(b *testing.B, fn func(b *testing.B, data string)) { for _, d := range benchData { - testtext.Bench(b, d.name, func(b *testing.B) { fn(b, d.data) }) + b.Run(d.name, func(b *testing.B) { fn(b, d.data) }) } } diff --git a/secure/bidirule/bidirule_test.go b/secure/bidirule/bidirule_test.go index e1bc11cef..e797eba3b 100644 --- a/secure/bidirule/bidirule_test.go +++ b/secure/bidirule/bidirule_test.go @@ -8,7 +8,6 @@ import ( "fmt" "testing" - "golang.org/x/text/internal/testtext" "golang.org/x/text/unicode/bidi" "golang.org/x/text/unicode/norm" ) @@ -57,7 +56,7 @@ func doTests(t *testing.T, fn func(t *testing.T, tc ruleTest)) { for rule, cases := range testCases { for i, tc := range cases { name := fmt.Sprintf("%d/%d:%+q:%[3]s", rule, i, norm.NFC.String(tc.in)) - testtext.Run(t, name, func(t *testing.T) { + t.Run(name, func(t *testing.T) { fn(t, tc) }) } diff --git a/secure/precis/benchmark_test.go b/secure/precis/benchmark_test.go index 4abebd85b..f8d05ae5d 100644 --- a/secure/precis/benchmark_test.go +++ b/secure/precis/benchmark_test.go @@ -8,8 +8,6 @@ package precis import ( "testing" - - "golang.org/x/text/internal/testtext" ) var benchData = []struct{ name, str string }{ @@ -33,7 +31,7 @@ var benchProfiles = []struct { func doBench(b *testing.B, f func(b *testing.B, p *Profile, s string)) { for _, bp := range benchProfiles { for _, d := range benchData { - testtext.Bench(b, bp.name+"/"+d.name, func(b *testing.B) { + b.Run(bp.name+"/"+d.name, func(b *testing.B) { f(b, bp.p, d.str) }) } diff --git a/secure/precis/enforce_test.go b/secure/precis/enforce_test.go index ac2aad2c6..0ced110a9 100644 --- a/secure/precis/enforce_test.go +++ b/secure/precis/enforce_test.go @@ -24,7 +24,7 @@ func doTests(t *testing.T, fn func(t *testing.T, p *Profile, tc testCase)) { for _, g := range enforceTestCases { for i, tc := range g.cases { name := fmt.Sprintf("%s:%d:%+q", g.name, i, tc.input) - testtext.Run(t, name, func(t *testing.T) { + t.Run(name, func(t *testing.T) { fn(t, g.p, tc) }) } diff --git a/secure/precis/profile_test.go b/secure/precis/profile_test.go index 4edb28a76..cf922a7a6 100644 --- a/secure/precis/profile_test.go +++ b/secure/precis/profile_test.go @@ -10,7 +10,6 @@ import ( "testing" "unicode" - "golang.org/x/text/internal/testtext" "golang.org/x/text/transform" ) @@ -114,7 +113,7 @@ func doCompareTests(t *testing.T, fn func(t *testing.T, p *Profile, tc compareTe for _, g := range compareTestCases { for i, tc := range g.cases { name := fmt.Sprintf("%s:%d:%+q", g.name, i, tc.a) - testtext.Run(t, name, func(t *testing.T) { + t.Run(name, func(t *testing.T) { fn(t, g.p, tc) }) } diff --git a/transform/transform_test.go b/transform/transform_test.go index 62fad2bc9..1ce36c812 100644 --- a/transform/transform_test.go +++ b/transform/transform_test.go @@ -642,7 +642,7 @@ var testCases = []testCase{ func TestReader(t *testing.T) { for _, tc := range testCases { - testtext.Run(t, tc.desc, func(t *testing.T) { + t.Run(tc.desc, func(t *testing.T) { r := NewReader(strings.NewReader(tc.src), tc.t) // Differently sized dst and src buffers are not part of the // exported API. We override them manually. @@ -665,7 +665,7 @@ func TestWriter(t *testing.T) { sizes = []int{tc.ioSize} } for _, sz := range sizes { - testtext.Run(t, fmt.Sprintf("%s/%d", tc.desc, sz), func(t *testing.T) { + t.Run(fmt.Sprintf("%s/%d", tc.desc, sz), func(t *testing.T) { bb := &bytes.Buffer{} w := NewWriter(bb, tc.t) // Differently sized dst and src buffers are not part of the @@ -1149,7 +1149,7 @@ func testString(t *testing.T, f func(Transformer, string) (string, int, error)) // The result string will be different. continue } - testtext.Run(t, tt.desc, func(t *testing.T) { + t.Run(tt.desc, func(t *testing.T) { got, n, err := f(tt.t, tt.src) if tt.wantErr != err { t.Errorf("error: got %v; want %v", err, tt.wantErr) @@ -1193,7 +1193,7 @@ func TestAppend(t *testing.T) { } func TestString(t *testing.T) { - testtext.Run(t, "transform", func(t *testing.T) { testString(t, String) }) + t.Run("transform", func(t *testing.T) { testString(t, String) }) // Overrun the internal destination buffer. for i, s := range []string{ @@ -1211,7 +1211,7 @@ func TestString(t *testing.T) { aaa[:1*initialBufSize+0] + "A", aaa[:1*initialBufSize+1] + "A", } { - testtext.Run(t, fmt.Sprint("dst buffer test using lower/", i), func(t *testing.T) { + t.Run(fmt.Sprint("dst buffer test using lower/", i), func(t *testing.T) { got, _, _ := String(lowerCaseASCII{}, s) if want := strings.ToLower(s); got != want { t.Errorf("got %s (%d); want %s (%d)", got, len(got), want, len(want)) @@ -1228,7 +1228,7 @@ func TestString(t *testing.T) { aaa[:2*initialBufSize+0], aaa[:2*initialBufSize+1], } { - testtext.Run(t, fmt.Sprint("src buffer test using rleEncode/", i), func(t *testing.T) { + t.Run(fmt.Sprint("src buffer test using rleEncode/", i), func(t *testing.T) { got, _, _ := String(rleEncode{}, s) if want := fmt.Sprintf("%da", len(s)); got != want { t.Errorf("got %s (%d); want %s (%d)", got, len(got), want, len(want)) @@ -1246,7 +1246,7 @@ func TestString(t *testing.T) { aaa[:initialBufSize+1], aaa[:10*initialBufSize], } { - testtext.Run(t, fmt.Sprint("alloc/", i), func(t *testing.T) { + t.Run(fmt.Sprint("alloc/", i), func(t *testing.T) { if n := testtext.AllocsPerRun(5, func() { String(&lowerCaseASCIILookahead{}, s) }); n > 1 { t.Errorf("#allocs was %f; want 1", n) } diff --git a/unicode/cldr/collate_test.go b/unicode/cldr/collate_test.go index f6721639a..e5237416b 100644 --- a/unicode/cldr/collate_test.go +++ b/unicode/cldr/collate_test.go @@ -164,13 +164,13 @@ func TestRuleProcessor(t *testing.T) { }, { desc: "err empty anchor", - in: " & ", - out: "E:1: missing string", + in: " & ", + out: "E:1: missing string", }, { desc: "err anchor invalid special 1", - in: " & [ foo ", - out: "E:1: unmatched bracket", + in: " & [ foo ", + out: "E:1: unmatched bracket", }, { desc: "err anchor invalid special 2", diff --git a/unicode/norm/normalize_test.go b/unicode/norm/normalize_test.go index 678ca403d..855e7b595 100644 --- a/unicode/norm/normalize_test.go +++ b/unicode/norm/normalize_test.go @@ -18,7 +18,6 @@ import ( "testing" "unicode/utf8" - "golang.org/x/text/internal/testtext" "golang.org/x/text/transform" ) @@ -467,7 +466,7 @@ var quickSpanNFCTests = []spanTest{ func runSpanTests(t *testing.T, name string, f Form, testCases []spanTest) { for i, tc := range testCases { s := fmt.Sprintf("Bytes/%s/%d=%+q/atEOF=%v", name, i, pc(tc.input), tc.atEOF) - ok := testtext.Run(t, s, func(t *testing.T) { + ok := t.Run(s, func(t *testing.T) { n, err := f.Span([]byte(tc.input), tc.atEOF) if n != tc.n || err != tc.err { t.Errorf("\n got %d, %v;\nwant %d, %v", n, err, tc.n, tc.err) @@ -477,7 +476,7 @@ func runSpanTests(t *testing.T, name string, f Form, testCases []spanTest) { continue // Don't do the String variant if the Bytes variant failed. } s = fmt.Sprintf("String/%s/%d=%+q/atEOF=%v", name, i, pc(tc.input), tc.atEOF) - testtext.Run(t, s, func(t *testing.T) { + t.Run(s, func(t *testing.T) { n, err := f.SpanString(tc.input, tc.atEOF) if n != tc.n || err != tc.err { t.Errorf("\n got %d, %v;\nwant %d, %v", n, err, tc.n, tc.err) diff --git a/width/transform_test.go b/width/transform_test.go index f9122d6db..f14c7c66d 100644 --- a/width/transform_test.go +++ b/width/transform_test.go @@ -65,7 +65,7 @@ type transformTest struct { } func (tc *transformTest) doTest(t *testing.T, tr Transformer) { - testtext.Run(t, tc.desc, func(t *testing.T) { + t.Run(tc.desc, func(t *testing.T) { b := make([]byte, tc.nBuf) nDst, nSrc, err := tr.Transform(b, []byte(tc.src), tc.atEOF) if got := string(b[:nDst]); got != tc.dst[:nDst] {