Skip to content

Commit

Permalink
add logf to T() interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed May 10, 2023
1 parent 9e868cd commit 9c8d6f2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
19 changes: 18 additions & 1 deletion env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ type testMock struct {

m sync.Mutex
cleanups []func()
fatals []struct {
logs []struct {
format string
args []interface{}
resultString string
}
fatals []struct {
format string
args []interface{}
resultString string
Expand Down Expand Up @@ -69,6 +74,17 @@ func (t *testMock) Fatalf(format string, args ...interface{}) {
}
}

func (t *testMock) Logf(format string, args ...interface{}) {
t.m.Lock()
defer t.m.Unlock()

t.logs = append(t.logs, struct {
format string
args []interface{}
resultString string
}{format: format, args: args, resultString: fmt.Sprintf(format, args...)})
}

func (t *testMock) Name() string {
if t.TestName == "" {
return "mock"
Expand Down Expand Up @@ -157,6 +173,7 @@ func Test_Env_Cache(t *testing.T) {
cntF := func() int {
res := e.Cache(nil, nil, func() (res interface{}, err error) {
val++
e.T().Logf("val: ", val)
return val, nil
})
return res.(int)
Expand Down
2 changes: 2 additions & 0 deletions examples/simple/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func counter(e fixenv.Env) int {
return fixenv.Cache(e, nil, nil, func() (res int, err error) {
globalCounter++
e.T().Logf("increment globalCounter to: ")
return globalCounter, nil
})
}
Expand Down Expand Up @@ -43,6 +44,7 @@ func counterTestAndSubtest(e fixenv.Env) int {
Scope: fixenv.ScopeTestAndSubtests,
}, func() (res int, err error) {
globalTestAndSubtestCounter++
e.T().Logf("increment globalTestAndSubtestCounter to: ")
return globalTestAndSubtestCounter, nil
})
}
Expand Down
7 changes: 7 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ type T interface {
// (which then runs all deferred calls in the current goroutine). Execution will continue at the next test or benchmark. FailNow must be called from the goroutine running the test or benchmark function, not from other goroutines created during the test. Calling FailNow does not stop those other goroutines.
Fatalf(format string, args ...interface{})

// Logf formats its arguments according to the format, analogous to Printf, and
// records the text in the error log. A final newline is added if not provided. For
// tests, the text will be printed only if the test fails or the -test.v flag is
// set. For benchmarks, the text is always printed to avoid having performance
// depend on the value of the -test.v flag.
Logf(format string, args ...interface{})

// Name returns the name of the running (sub-) test or benchmark.
//
// The name will include the name of the test along with the names
Expand Down
5 changes: 5 additions & 0 deletions maintest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fixenv

import (
"fmt"
"log"
"sync"
)

Expand Down Expand Up @@ -90,6 +91,10 @@ func (t *virtualTest) Fatalf(format string, args ...interface{}) {
t.fatalf(format, args...)
}

func (t *virtualTest) Logf(format string, args ...interface{}) {
log.Printf(format, args...)
}

func (t *virtualTest) Name() string {
return packageScopeName
}
Expand Down
1 change: 1 addition & 0 deletions maintest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestCreateMainTestEnv(t *testing.T) {
t.Run("simple", func(t *testing.T) {
at := assert.New(t)
e, cancel := CreateMainTestEnv(nil)
e.T().Logf("env created")
at.Equal(packageScopeName, e.t.Name())
at.NotNil(globalScopeInfo[packageScopeName])
cancel()
Expand Down

0 comments on commit 9c8d6f2

Please sign in to comment.