OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / stretchr / testify / suite / suite.go
1 package suite
2
3 import (
4         "flag"
5         "fmt"
6         "os"
7         "reflect"
8         "regexp"
9         "testing"
10
11         "github.com/stretchr/testify/assert"
12         "github.com/stretchr/testify/require"
13 )
14
15 var allTestsFilter = func(_, _ string) (bool, error) { return true, nil }
16 var matchMethod = flag.String("testify.m", "", "regular expression to select tests of the testify suite to run")
17
18 // Suite is a basic testing suite with methods for storing and
19 // retrieving the current *testing.T context.
20 type Suite struct {
21         *assert.Assertions
22         require *require.Assertions
23         t       *testing.T
24 }
25
26 // T retrieves the current *testing.T context.
27 func (suite *Suite) T() *testing.T {
28         return suite.t
29 }
30
31 // SetT sets the current *testing.T context.
32 func (suite *Suite) SetT(t *testing.T) {
33         suite.t = t
34         suite.Assertions = assert.New(t)
35         suite.require = require.New(t)
36 }
37
38 // Require returns a require context for suite.
39 func (suite *Suite) Require() *require.Assertions {
40         if suite.require == nil {
41                 suite.require = require.New(suite.T())
42         }
43         return suite.require
44 }
45
46 // Assert returns an assert context for suite.  Normally, you can call
47 // `suite.NoError(expected, actual)`, but for situations where the embedded
48 // methods are overridden (for example, you might want to override
49 // assert.Assertions with require.Assertions), this method is provided so you
50 // can call `suite.Assert().NoError()`.
51 func (suite *Suite) Assert() *assert.Assertions {
52         if suite.Assertions == nil {
53                 suite.Assertions = assert.New(suite.T())
54         }
55         return suite.Assertions
56 }
57
58 // Run takes a testing suite and runs all of the tests attached
59 // to it.
60 func Run(t *testing.T, suite TestingSuite) {
61         suite.SetT(t)
62
63         if setupAllSuite, ok := suite.(SetupAllSuite); ok {
64                 setupAllSuite.SetupSuite()
65         }
66         defer func() {
67                 if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
68                         tearDownAllSuite.TearDownSuite()
69                 }
70         }()
71
72         methodFinder := reflect.TypeOf(suite)
73         tests := []testing.InternalTest{}
74         for index := 0; index < methodFinder.NumMethod(); index++ {
75                 method := methodFinder.Method(index)
76                 ok, err := methodFilter(method.Name)
77                 if err != nil {
78                         fmt.Fprintf(os.Stderr, "testify: invalid regexp for -m: %s\n", err)
79                         os.Exit(1)
80                 }
81                 if ok {
82                         test := testing.InternalTest{
83                                 Name: method.Name,
84                                 F: func(t *testing.T) {
85                                         parentT := suite.T()
86                                         suite.SetT(t)
87                                         if setupTestSuite, ok := suite.(SetupTestSuite); ok {
88                                                 setupTestSuite.SetupTest()
89                                         }
90                                         if beforeTestSuite, ok := suite.(BeforeTest); ok {
91                                                 beforeTestSuite.BeforeTest(methodFinder.Elem().Name(), method.Name)
92                                         }
93                                         defer func() {
94                                                 if afterTestSuite, ok := suite.(AfterTest); ok {
95                                                         afterTestSuite.AfterTest(methodFinder.Elem().Name(), method.Name)
96                                                 }
97                                                 if tearDownTestSuite, ok := suite.(TearDownTestSuite); ok {
98                                                         tearDownTestSuite.TearDownTest()
99                                                 }
100                                                 suite.SetT(parentT)
101                                         }()
102                                         method.Func.Call([]reflect.Value{reflect.ValueOf(suite)})
103                                 },
104                         }
105                         tests = append(tests, test)
106                 }
107         }
108         runTests(t, tests)
109 }
110
111 func runTests(t testing.TB, tests []testing.InternalTest) {
112         r, ok := t.(runner)
113         if !ok { // backwards compatibility with Go 1.6 and below
114                 if !testing.RunTests(allTestsFilter, tests) {
115                         t.Fail()
116                 }
117                 return
118         }
119
120         for _, test := range tests {
121                 r.Run(test.Name, test.F)
122         }
123 }
124
125 // Filtering method according to set regular expression
126 // specified command-line argument -m
127 func methodFilter(name string) (bool, error) {
128         if ok, _ := regexp.MatchString("^Test", name); !ok {
129                 return false, nil
130         }
131         return regexp.MatchString(*matchMethod, name)
132 }
133
134 type runner interface {
135         Run(name string, f func(t *testing.T)) bool
136 }