Skip to content

Commit

Permalink
internal/texttest: remove Run and Bench helpers
Browse files Browse the repository at this point in the history
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 <dmitshur@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
  • Loading branch information
dmitshur authored and gopherbot committed Oct 21, 2024
1 parent a457f47 commit fefda1a
Show file tree
Hide file tree
Showing 18 changed files with 35 additions and 86 deletions.
3 changes: 1 addition & 2 deletions cases/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions cases/icu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"
"testing"

"golang.org/x/text/internal/testtext"
"golang.org/x/text/language"
"golang.org/x/text/unicode/norm"
)
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 8 additions & 8 deletions cases/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/export/idna/idna_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions internal/number/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"testing"

"golang.org/x/text/internal/testtext"
"golang.org/x/text/language"
)

Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 0 additions & 23 deletions internal/testtext/go1_6.go

This file was deleted.

17 changes: 0 additions & 17 deletions internal/testtext/go1_7.go

This file was deleted.

5 changes: 2 additions & 3 deletions language/display/display_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"testing"
"unicode"

"golang.org/x/text/internal/testtext"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions runes/runes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,15 +626,15 @@ 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)
}
})
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)
Expand Down
4 changes: 1 addition & 3 deletions secure/bidirule/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package bidirule

import (
"testing"

"golang.org/x/text/internal/testtext"
)

var benchData = []struct{ name, data string }{
Expand All @@ -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) })
}
}

Expand Down
3 changes: 1 addition & 2 deletions secure/bidirule/bidirule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
})
}
Expand Down
4 changes: 1 addition & 3 deletions secure/precis/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ package precis

import (
"testing"

"golang.org/x/text/internal/testtext"
)

var benchData = []struct{ name, str string }{
Expand All @@ -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)
})
}
Expand Down
2 changes: 1 addition & 1 deletion secure/precis/enforce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down
3 changes: 1 addition & 2 deletions secure/precis/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"testing"
"unicode"

"golang.org/x/text/internal/testtext"
"golang.org/x/text/transform"
)

Expand Down Expand Up @@ -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)
})
}
Expand Down
14 changes: 7 additions & 7 deletions transform/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions unicode/cldr/collate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit fefda1a

Please sign in to comment.