From 87101a6e4a5859cee372b6ded7821787b3190cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Thu, 5 Jun 2025 11:47:28 +0200 Subject: [PATCH] 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. --- suite/stats.go | 16 ++++++++++------ suite/suite.go | 9 ++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/suite/stats.go b/suite/stats.go index 261da37f7..be4ccd679 100644 --- a/suite/stats.go +++ b/suite/stats.go @@ -16,26 +16,30 @@ type TestInformation struct { } func newSuiteInformation() *SuiteInformation { - testStats := make(map[string]*TestInformation) - return &SuiteInformation{ - TestStats: testStats, + TestStats: make(map[string]*TestInformation), } } -func (s SuiteInformation) start(testName string) { +func (s *SuiteInformation) start(testName string) { + if s == nil { + return + } s.TestStats[testName] = &TestInformation{ TestName: testName, Start: time.Now(), } } -func (s SuiteInformation) end(testName string, passed bool) { +func (s *SuiteInformation) end(testName string, passed bool) { + if s == nil { + return + } s.TestStats[testName].End = time.Now() s.TestStats[testName].Passed = passed } -func (s SuiteInformation) Passed() bool { +func (s *SuiteInformation) Passed() bool { for _, stats := range s.TestStats { if !stats.Passed { return false diff --git a/suite/suite.go b/suite/suite.go index a7ceba731..8a5c0534e 100644 --- a/suite/suite.go +++ b/suite/suite.go @@ -161,10 +161,7 @@ func Run(t *testing.T, suite TestingSuite) { r := recover() - if stats != nil { - passed := !t.Failed() && r == nil - stats.end(method.Name, passed) - } + stats.end(method.Name, !t.Failed() && r == nil) if afterTestSuite, ok := suite.(AfterTest); ok { afterTestSuite.AfterTest(suiteName, method.Name) @@ -185,9 +182,7 @@ func Run(t *testing.T, suite TestingSuite) { beforeTestSuite.BeforeTest(methodFinder.Elem().Name(), method.Name) } - if stats != nil { - stats.start(method.Name) - } + stats.start(method.Name) method.Func.Call([]reflect.Value{reflect.ValueOf(suite)}) },