Skip to content

Commit fc6b3ea

Browse files
committed
suite.Run: refactor handling of stats
Refactor handling of stats in suite.Run with the goal of reducing indented blocks to improve readability. To achieve this, the SuiteInformation methods now handle being called with a nil receiver to work as noop. This allows to call them from suite.Run without nil checks blocks, so with improved readability.
1 parent 7c2bbf9 commit fc6b3ea

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

suite/stats.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,30 @@ type TestInformation struct {
1616
}
1717

1818
func newSuiteInformation() *SuiteInformation {
19-
testStats := make(map[string]*TestInformation)
20-
2119
return &SuiteInformation{
22-
TestStats: testStats,
20+
TestStats: make(map[string]*TestInformation),
2321
}
2422
}
2523

26-
func (s SuiteInformation) start(testName string) {
24+
func (s *SuiteInformation) start(testName string) {
25+
if s == nil {
26+
return
27+
}
2728
s.TestStats[testName] = &TestInformation{
2829
TestName: testName,
2930
Start: time.Now(),
3031
}
3132
}
3233

33-
func (s SuiteInformation) end(testName string, passed bool) {
34+
func (s *SuiteInformation) end(testName string, passed bool) {
35+
if s == nil {
36+
return
37+
}
3438
s.TestStats[testName].End = time.Now()
3539
s.TestStats[testName].Passed = passed
3640
}
3741

38-
func (s SuiteInformation) Passed() bool {
42+
func (s *SuiteInformation) Passed() bool {
3943
for _, stats := range s.TestStats {
4044
if !stats.Passed {
4145
return false

suite/suite.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,7 @@ func Run(t *testing.T, suite TestingSuite) {
175175

176176
r := recover()
177177

178-
if stats != nil {
179-
passed := !t.Failed() && r == nil
180-
stats.end(method.Name, passed)
181-
}
178+
stats.end(method.Name, !t.Failed() && r == nil)
182179

183180
if afterTestSuite, ok := suite.(AfterTest); ok {
184181
afterTestSuite.AfterTest(suiteName, method.Name)
@@ -199,9 +196,7 @@ func Run(t *testing.T, suite TestingSuite) {
199196
beforeTestSuite.BeforeTest(methodFinder.Elem().Name(), method.Name)
200197
}
201198

202-
if stats != nil {
203-
stats.start(method.Name)
204-
}
199+
stats.start(method.Name)
205200

206201
method.Func.Call([]reflect.Value{reflect.ValueOf(suite)})
207202
},

0 commit comments

Comments
 (0)