Skip to content

Commit 960a0df

Browse files
committed
refact
1 parent 816f3e6 commit 960a0df

File tree

6 files changed

+33
-12
lines changed

6 files changed

+33
-12
lines changed

T_test.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func TestT_Defer_failNowWillNotHang(t *testing.T) {
131131
s := testcase.NewSpec(&doubles.TB{})
132132
s.Test(``, func(t *testcase.T) {
133133
t.Defer(func() { t.FailNow() })
134-
134+
135135
panic(`die`)
136136
})
137137
})
@@ -176,6 +176,26 @@ func TestT_Defer_withArguments(t *testing.T) {
176176
assert.Must(t).Equal(expected, actually)
177177
}
178178

179+
func TestT_Defer_runsOnlyAfterTestIsdone(t *testing.T) {
180+
s := testcase.NewSpec(t)
181+
182+
CTX := testcase.Let(s, func(t *testcase.T) func() context.Context {
183+
return func() context.Context {
184+
ctx, cancel := context.WithCancel(context.Background())
185+
t.Cleanup(cancel)
186+
t.Defer(cancel)
187+
return ctx
188+
}
189+
})
190+
191+
s.Before(func(t *testcase.T) {
192+
t.Cleanup(func() { t.Must.NoError(CTX.Get(t)().Err()) })
193+
t.Defer(func() { t.Must.NoError(CTX.Get(t)().Err()) })
194+
})
195+
196+
s.Test("", func(t *testcase.T) {})
197+
}
198+
179199
func TestT_Defer_withArgumentsButArgumentCountMismatch(t *testing.T) {
180200
s := testcase.NewSpec(t)
181201

assert/Asserter.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -717,12 +717,12 @@ func (a Asserter) containExactlySlice(exp reflect.Value, act reflect.Value, msg
717717
Message: msg,
718718
Values: []fmterror.Value{
719719
{
720-
Label: "actual:",
721-
Value: act.Interface(),
720+
Label: "expected:",
721+
Value: exp.Len(),
722722
},
723723
{
724-
Label: "value",
725-
Value: exp.Interface(),
724+
Label: "actual:",
725+
Value: act.Len(),
726726
},
727727
},
728728
})

assert/Diff.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ type diffFn func(value, othValue any) string
66

77
// DiffFunc is the function that will be used to print out two object if they are not equal.
88
// You can use your preferred diff implementation if you are not happy with the pretty print diff format.
9-
var DiffFunc diffFn = pp.DiffFormat
9+
var DiffFunc diffFn = pp.DiffFormat[any]

internal/fmterror/Message.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Message struct {
1616

1717
type Value struct {
1818
Label string
19-
Value interface{}
19+
Value any
2020
}
2121

2222
type Raw string

internal/teardown/Teardown.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ type Teardown struct {
3232
// even in tests where this is not needed.
3333
//
3434
// e.g.:
35-
// - mock initialization with mock controller, where the mock controller #Finish function must be executed after each testCase suite.
36-
// - sql.DB / sql.Tx
37-
// - basically anything that has the io.Closer interface
35+
// - mock initialization with mock controller, where the mock controller #Finish function must be executed after each testCase suite.
36+
// - sql.DB / sql.Tx
37+
// - basically anything that has the io.Closer interface
3838
//
3939
// https://github.com/golang/go/issues/41891
4040
func (td *Teardown) Defer(fn interface{}, args ...interface{}) {
@@ -142,6 +142,7 @@ func (td *Teardown) run() {
142142
td.fns = nil
143143
td.mutex.Unlock()
144144
for _, cu := range fns {
145+
//goland:noinspection GoDeferInLoop
145146
defer cu()
146147
}
147148
}

pp/Diff.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
)
1010

1111
// Diff will pretty print two value and show side-by-side the difference between them.
12-
func Diff(v1, v2 any) {
12+
func Diff[T any](v1, v2 T) {
1313
_, _ = defaultWriter.Write([]byte(DiffString(Format(v1), Format(v2))))
1414
}
1515

1616
// DiffFormat format the values in pp.Format and compare the results line by line in a side-by-side style.
17-
func DiffFormat(v1, v2 any) string {
17+
func DiffFormat[T any](v1, v2 T) string {
1818
return DiffString(Format(v1), Format(v2))
1919
}
2020

0 commit comments

Comments
 (0)