| 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(c *qt.C) { |
| 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 |
c.Cleanup(srv.Close) |
| 20 |
s.url = srv.URL |
| 21 |
} |
| 22 |
|
| 23 |
func (s *suite) TestGet(c *qt.C) { |
| 24 |
c.Parallel() |
| 25 |
resp, err := http.Get(s.url) |
| 26 |
c.Assert(err, qt.Equals, nil) |
| 27 |
defer resp.Body.Close() |
| 28 |
b, err := ioutil.ReadAll(resp.Body) |
| 29 |
c.Assert(err, qt.Equals, nil) |
| 30 |
c.Assert(string(b), qt.Equals, "GET /") |
| 31 |
} |
| 32 |
|
| 33 |
func (s *suite) TestHead(c *qt.C) { |
| 34 |
c.Parallel() |
| 35 |
resp, err := http.Head(s.url + "/path") |
| 36 |
c.Assert(err, qt.Equals, nil) |
| 37 |
defer resp.Body.Close() |
| 38 |
b, err := ioutil.ReadAll(resp.Body) |
| 39 |
c.Assert(err, qt.Equals, nil) |
| 40 |
c.Assert(string(b), qt.Equals, "") |
| 41 |
c.Assert(resp.ContentLength, qt.Equals, int64(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(qt.New(t), &suite{"http://example.com"}) |
| 48 |
} |
| 49 |
*/ |
| 50 |
package qtsuite |
| 51 |
|
| 52 |
import ( |
| 53 |
"reflect" |
| 54 |
"strings" |
| 55 |
"unicode" |
| 56 |
"unicode/utf8" |
| 57 |
|
| 58 |
qt "github.com/frankban/quicktest" |
| 59 |
) |
| 60 |
|
| 61 |
// Run runs each test method defined on the given value as a separate |
| 62 |
// subtest. A test is a method of the form |
| 63 |
// |
| 64 |
// func (T) TestXxx(*quicktest.C) |
| 65 |
// |
| 66 |
// where Xxx does not start with a lowercase letter. |
| 67 |
// |
| 68 |
// If suite is a pointer, the value pointed to is copied before any |
| 69 |
// methods are invoked on it; a new copy is made for each test. This |
| 70 |
// means that it is OK for tests to modify fields in suite concurrently |
| 71 |
// if desired - it's OK to call c.Parallel(). |
| 72 |
// |
| 73 |
// If suite has a method of the form |
| 74 |
// |
| 75 |
// func (T) Init(*quicktest.C) |
| 76 |
// |
| 77 |
// this method will be invoked before each test run. |
| 78 |
func Run(c *qt.C, suite interface{}) { |
| 79 |
sv := reflect.ValueOf(suite) |
| 80 |
st := sv.Type() |
| 81 |
init, hasInit := st.MethodByName("Init") |
| 82 |
if hasInit && !isValidMethod(init) { |
| 83 |
c.Fatal("wrong signature for Init, must be Init(*quicktest.C)") |
| 84 |
} |
| 85 |
for i := 0; i < st.NumMethod(); i++ { |
| 86 |
m := st.Method(i) |
| 87 |
if !isTestMethod(m) { |
| 88 |
continue |
| 89 |
} |
| 90 |
c.Run(m.Name, func(c *qt.C) { |
| 91 |
if !isValidMethod(m) { |
| 92 |
c.Fatalf("wrong signature for %s, must be %s(*quicktest.C)", m.Name, m.Name) |
| 93 |
} |
| 94 |
|
| 95 |
sv := sv |
| 96 |
if st.Kind() == reflect.Ptr { |
| 97 |
sv1 := reflect.New(st.Elem()) |
| 98 |
sv1.Elem().Set(sv.Elem()) |
| 99 |
sv = sv1 |
| 100 |
} |
| 101 |
args := []reflect.Value{sv, reflect.ValueOf(c)} |
| 102 |
if hasInit { |
| 103 |
init.Func.Call(args) |
| 104 |
} |
| 105 |
m.Func.Call(args) |
| 106 |
}) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
var cType = reflect.TypeOf(&qt.C{}) |
| 111 |
|
| 112 |
func isTestMethod(m reflect.Method) bool { |
| 113 |
if !strings.HasPrefix(m.Name, "Test") { |
| 114 |
return false |
| 115 |
} |
| 116 |
r, n := utf8.DecodeRuneInString(m.Name[4:]) |
| 117 |
return n == 0 || !unicode.IsLower(r) |
| 118 |
} |
| 119 |
|
| 120 |
func isValidMethod(m reflect.Method) bool { |
| 121 |
return m.Type.NumIn() == 2 && m.Type.NumOut() == 0 && m.Type.In(1) == cType |
| 122 |
} |