forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: Add
testfork.RunTest
to run multiple tests in one function (ping…
…cap#35746) close pingcap#35747
- Loading branch information
1 parent
27e7bbd
commit 0d9e02b
Showing
8 changed files
with
264 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "testfork", | ||
srcs = ["fork.go"], | ||
importpath = "github.com/pingcap/tidb/testkit/testfork", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@com_github_cockroachdb_errors//:errors", | ||
"@com_github_stretchr_testify//require", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "testfork_test", | ||
srcs = ["fork_test.go"], | ||
embed = [":testfork"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package testfork | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cockroachdb/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type pickStack struct { | ||
stack [][]any | ||
pos int | ||
valid bool | ||
} | ||
|
||
func newPickStack() *pickStack { | ||
return &pickStack{ | ||
valid: true, | ||
} | ||
} | ||
|
||
func (s *pickStack) NextStack() { | ||
for len(s.stack) > 0 { | ||
lastIndex := len(s.stack) - 1 | ||
s.stack[lastIndex] = s.stack[lastIndex][1:] | ||
if len(s.stack[lastIndex]) > 0 { | ||
break | ||
} | ||
s.stack = s.stack[:lastIndex] | ||
} | ||
|
||
s.pos = 0 | ||
s.valid = len(s.stack) > 0 | ||
} | ||
|
||
func (s *pickStack) PickValue(values []any) (any, error) { | ||
if len(values) == 0 { | ||
return nil, errors.New("values should not be empty") | ||
} | ||
|
||
stackLen := len(s.stack) | ||
if s.pos > stackLen { | ||
return nil, errors.Newf("illegal state %d > %d", s.pos, stackLen) | ||
} | ||
|
||
defer func() { | ||
s.pos++ | ||
}() | ||
|
||
if s.pos == stackLen { | ||
s.stack = append(s.stack, values) | ||
} | ||
return s.stack[s.pos][0], nil | ||
} | ||
|
||
func (s *pickStack) Values() []any { | ||
values := make([]any, 0) | ||
for _, v := range s.stack { | ||
values = append(values, v[0]) | ||
} | ||
return values | ||
} | ||
|
||
func (s *pickStack) ValuesText() string { | ||
values := s.Values() | ||
strValues := make([]string, len(values)) | ||
for i, value := range values { | ||
switch v := value.(type) { | ||
case string: | ||
strValues[i] = fmt.Sprintf(`"%s"`, v) | ||
default: | ||
strValues[i] = fmt.Sprintf("%v", v) | ||
} | ||
} | ||
return "[" + strings.Join(strValues, " ") + "]" | ||
} | ||
|
||
func (s *pickStack) Valid() bool { | ||
return s.valid | ||
} | ||
|
||
// T is used by for test | ||
type T struct { | ||
*testing.T | ||
stack *pickStack | ||
} | ||
|
||
// RunTest runs the test function `f` multiple times util all the values in `Pick` are tested. | ||
func RunTest(t *testing.T, f func(t *T)) { | ||
idx := 0 | ||
for stack := newPickStack(); stack.Valid(); stack.NextStack() { | ||
success := t.Run("", func(t *testing.T) { | ||
f(&T{T: t, stack: stack}) | ||
}) | ||
|
||
if !success { | ||
_, err := fmt.Fprintf(os.Stderr, "SubTest #%v failed, failed values: %s\n", idx, stack.ValuesText()) | ||
require.NoError(t, err) | ||
} | ||
idx++ | ||
} | ||
} | ||
|
||
// Pick returns a value from the values list | ||
func Pick[E any](t *T, values []E) E { | ||
slice := make([]any, len(values)) | ||
for i, item := range values { | ||
slice[i] = item | ||
} | ||
value, err := t.stack.PickValue(slice) | ||
require.NoError(t, err) | ||
return value.(E) | ||
} | ||
|
||
// PickEnum returns a value from the value enums | ||
func PickEnum[E any](t *T, item E, other ...E) E { | ||
return Pick(t, append([]E{item}, other...)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package testfork | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestForkSubTest(t *testing.T) { | ||
var values [][]any | ||
RunTest(t, func(t *T) { | ||
x := Pick(t, []int{1, 2, 3}) | ||
y := PickEnum(t, "a", "b") | ||
var z any | ||
if x == 2 { | ||
z = PickEnum(t, 10, 11) | ||
} else { | ||
z = Pick(t, []string{"g", "h"}) | ||
} | ||
values = append(values, []any{x, y, z}) | ||
}) | ||
require.Equal(t, [][]any{ | ||
{1, "a", "g"}, | ||
{1, "a", "h"}, | ||
{1, "b", "g"}, | ||
{1, "b", "h"}, | ||
{2, "a", 10}, | ||
{2, "a", 11}, | ||
{2, "b", 10}, | ||
{2, "b", 11}, | ||
{3, "a", "g"}, | ||
{3, "a", "h"}, | ||
{3, "b", "g"}, | ||
{3, "b", "h"}, | ||
}, values) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters