Skip to content

Commit 87101a6

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 39b912b commit 87101a6

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
@@ -161,10 +161,7 @@ func Run(t *testing.T, suite TestingSuite) {
161161

162162
r := recover()
163163

164-
if stats != nil {
165-
passed := !t.Failed() && r == nil
166-
stats.end(method.Name, passed)
167-
}
164+
stats.end(method.Name, !t.Failed() && r == nil)
168165

169166
if afterTestSuite, ok := suite.(AfterTest); ok {
170167
afterTestSuite.AfterTest(suiteName, method.Name)
@@ -185,9 +182,7 @@ func Run(t *testing.T, suite TestingSuite) {
185182
beforeTestSuite.BeforeTest(methodFinder.Elem().Name(), method.Name)
186183
}
187184

188-
if stats != nil {
189-
stats.start(method.Name)
190-
}
185+
stats.start(method.Name)
191186

192187
method.Func.Call([]reflect.Value{reflect.ValueOf(suite)})
193188
},

0 commit comments

Comments
 (0)