| 1 |
// Licensed under the MIT license, see LICENSE file for details. |
| 2 |
|
| 3 |
/* |
| 4 |
Package qtsuite allows quicktest to run test suites. |
| 5 |
|
| 6 |
A test suite is a value with one or more test methods. |
| 7 |
For example, the following code defines a suite of test functions that starts |
| 8 |
an HTTP server before running each test, and tears it down afterwards: |
| 9 |
|
| 10 |
type suite struct { |
| 11 |
url string |
| 12 |
} |
| 13 |
|
| 14 |
func (s *suite) Init(t *testing.T) { |
| 15 |
hnd := func(w http.ResponseWriter, req *http.Request) { |
| 16 |
fmt.Fprintf(w, "%s %s", req.Method, req.URL.Path) |
| 17 |
} |
| 18 |
srv := httptest.NewServer(http.HandlerFunc(hnd)) |
| 19 |
t.Cleanup(srv.Close) |
| 20 |
s.url = srv.URL |
| 21 |
} |
| 22 |
|
| 23 |
func (s *suite) TestGet(t *testing.T) { |
| 24 |
t.Parallel() |
| 25 |
resp, err := http.Get(s.url) |
| 26 |
qt.Assert(t, qt.IsNil(err)) |
| 27 |
defer resp.Body.Close() |
| 28 |
b, err := ioutil.ReadAll(resp.Body) |
| 29 |
qt.Assert(t, qt.IsNil(err)) |
| 30 |
qt.Assert(t, qt.Equals(string(b), "GET /")) |
| 31 |
} |
| 32 |
|
| 33 |
func (s *suite) TestHead(t *testing.T) { |
| 34 |
t.Parallel() |
| 35 |
resp, err := http.Head(s.url + "/path") |
| 36 |
qt.Assert(t, qt.IsNil(err)) |
| 37 |
defer resp.Body.Close() |
| 38 |
b, err := ioutil.ReadAll(resp.Body) |
| 39 |
qt.Assert(t, qt.IsNil(err)) |
| 40 |
qt.Assert(t, qt.Equals(string(b), "")) |
| 41 |
qt.Assert(t, qt.Equals(resp.ContentLength, 10)) |
| 42 |
} |
| 43 |
|
| 44 |
The above code could be invoked from a test function like this: |
| 45 |
|
| 46 |
func TestHTTPMethods(t *testing.T) { |
| 47 |
qtsuite.Run(t, &suite{}) |
| 48 |
} |
| 49 |
*/ |
| 50 |
package qtsuite |
| 51 |
|
| 52 |
import ( |
| 53 |
"reflect" |
| 54 |
"strings" |
| 55 |
"testing" |
| 56 |
"unicode" |
| 57 |
"unicode/utf8" |
| 58 |
) |
| 59 |
|
| 60 |
// Run runs each test method defined on the given value as a separate |
| 61 |
// subtest. A test is a method of the form |
| 62 |
// |
| 63 |
// func (T) TestXxx(*testing.T) |
| 64 |
// |
| 65 |
// where Xxx does not start with a lowercase letter. |
| 66 |
// |
| 67 |
// If suite is a pointer, the value pointed to is copied before any |
| 68 |
// methods are invoked on it: a new copy is made for each test. This |
| 69 |
// means that it is OK for tests to modify fields in suite concurrently |
| 70 |
// if desired - it's OK to call t.Parallel(). |
| 71 |
// |
| 72 |
// If suite has a method of the form |
| 73 |
// |
| 74 |
// func (T) Init(*testing.T) |
| 75 |
// |
| 76 |
// this method will be invoked before each test run. |
| 77 |
func Run(t *testing.T, suite any) { |
| 78 |
sv := reflect.ValueOf(suite) |
| 79 |
st := sv.Type() |
| 80 |
init, hasInit := st.MethodByName("Init") |
| 81 |
if hasInit && !isValidMethod(init) { |
| 82 |
t.Fatal("wrong signature for Init, must be Init(*testing.T)") |
| 83 |
} |
| 84 |
for i := 0; i < st.NumMethod(); i++ { |
| 85 |
m := st.Method(i) |
| 86 |
if !isTestMethod(m) { |
| 87 |
continue |
| 88 |
} |
| 89 |
t.Run(m.Name, func(t *testing.T) { |
| 90 |
if !isValidMethod(m) { |
| 91 |
t.Fatalf("wrong signature for %s, must be %s(*testing.T)", m.Name, m.Name) |
| 92 |
} |
| 93 |
|
| 94 |
sv := sv |
| 95 |
if st.Kind() == reflect.Pointer { |
| 96 |
sv1 := reflect.New(st.Elem()) |
| 97 |
sv1.Elem().Set(sv.Elem()) |
| 98 |
sv = sv1 |
| 99 |
} |
| 100 |
args := []reflect.Value{sv, reflect.ValueOf(t)} |
| 101 |
if hasInit { |
| 102 |
init.Func.Call(args) |
| 103 |
} |
| 104 |
m.Func.Call(args) |
| 105 |
}) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
var tType = reflect.TypeOf((*testing.T)(nil)) |
| 110 |
|
| 111 |
func isTestMethod(m reflect.Method) bool { |
| 112 |
if !strings.HasPrefix(m.Name, "Test") { |
| 113 |
return false |
| 114 |
} |
| 115 |
r, n := utf8.DecodeRuneInString(m.Name[4:]) |
| 116 |
return n == 0 || !unicode.IsLower(r) |
| 117 |
} |
| 118 |
|
| 119 |
func isValidMethod(m reflect.Method) bool { |
| 120 |
return m.Type.NumIn() == 2 && m.Type.NumOut() == 0 && m.Type.In(1) == tType |
| 121 |
} |