Skip to content

Commit e096df7

Browse files
committed
test: 100% test coverage.
1 parent dba118e commit e096df7

File tree

5 files changed

+258
-104
lines changed

5 files changed

+258
-104
lines changed

equal_test.go

Lines changed: 116 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package assert
22

33
import (
4-
"sync"
54
"testing"
5+
6+
"github.com/ghosind/go-assert/internal"
67
)
78

89
type testStruct struct {
@@ -34,43 +35,84 @@ func TestDeepEqualAndNotDeepEqual(t *testing.T) {
3435
}
3536

3637
func testDeepEqualAndNotDeepEqual(t *testing.T, assertion *Assertion, v1, v2 any, isEqual bool) {
38+
testDeepEqual(t, assertion, v1, v2, isEqual)
39+
40+
testNotDeepEqual(t, assertion, v1, v2, isEqual)
41+
42+
testDeepEqualNow(t, assertion, v1, v2, isEqual)
43+
44+
testNotDeepEqualNow(t, assertion, v1, v2, isEqual)
45+
}
46+
47+
func testDeepEqual(t *testing.T, assertion *Assertion, v1, v2 any, isEqual bool) {
3748
err := assertion.DeepEqual(v1, v2)
3849
if isEqual && err != nil {
3950
t.Errorf("Equal(%v, %v) = %v, want = nil", v1, v2, err)
4051
} else if !isEqual && err == nil {
4152
t.Errorf("Equal(%v, %v) = nil, want = error", v1, v2)
4253
}
4354

44-
err = assertion.NotDeepEqual(v1, v2)
55+
err = DeepEqual(assertion.T, v1, v2)
56+
if isEqual && err != nil {
57+
t.Errorf("Equal(%v, %v) = %v, want = nil", v1, v2, err)
58+
} else if !isEqual && err == nil {
59+
t.Errorf("Equal(%v, %v) = nil, want = error", v1, v2)
60+
}
61+
}
62+
63+
func testNotDeepEqual(t *testing.T, assertion *Assertion, v1, v2 any, isEqual bool) {
64+
err := assertion.NotDeepEqual(v1, v2)
4565
if isEqual && err == nil {
4666
t.Errorf("NotEqual(%v, %v) = nil, want = error", v1, v2)
4767
} else if !isEqual && err != nil {
4868
t.Errorf("NotEqual(%v, %v) = %v, want = nil", v1, v2, err)
4969
}
5070

51-
isTerminated := true
52-
wg := sync.WaitGroup{}
53-
wg.Add(1)
54-
go func() {
55-
defer wg.Done()
71+
err = NotDeepEqual(assertion.T, v1, v2)
72+
if isEqual && err == nil {
73+
t.Errorf("NotEqual(%v, %v) = nil, want = error", v1, v2)
74+
} else if !isEqual && err != nil {
75+
t.Errorf("NotEqual(%v, %v) = %v, want = nil", v1, v2, err)
76+
}
77+
}
78+
79+
func testDeepEqualNow(t *testing.T, assertion *Assertion, v1, v2 any, isEqual bool) {
80+
isTerminated := internal.CheckTermination(func() {
5681
assertion.DeepEqualNow(v1, v2)
57-
isTerminated = false
58-
}()
59-
wg.Wait()
82+
})
83+
if isEqual && isTerminated {
84+
t.Error("execution stopped, want do not stop")
85+
} else if !isEqual && !isTerminated {
86+
t.Error("execution do not stopped, want stop")
87+
}
88+
89+
isTerminated = internal.CheckTermination(func() {
90+
DeepEqualNow(assertion.T, v1, v2)
91+
})
6092
if isEqual && isTerminated {
6193
t.Error("execution stopped, want do not stop")
94+
} else if !isEqual && !isTerminated {
95+
t.Error("execution do not stopped, want stop")
6296
}
97+
}
6398

64-
isTerminated = true
65-
wg.Add(1)
66-
go func() {
67-
defer wg.Done()
99+
func testNotDeepEqualNow(t *testing.T, assertion *Assertion, v1, v2 any, isEqual bool) {
100+
isTerminated := internal.CheckTermination(func() {
68101
assertion.NotDeepEqualNow(v1, v2)
69-
isTerminated = false
70-
}()
71-
wg.Wait()
102+
})
103+
if !isEqual && isTerminated {
104+
t.Error("execution stopped, want do not stop")
105+
} else if isEqual && !isTerminated {
106+
t.Error("execution do not stopped, want stop")
107+
}
108+
109+
isTerminated = internal.CheckTermination(func() {
110+
NotDeepEqualNow(assertion.T, v1, v2)
111+
})
72112
if !isEqual && isTerminated {
73113
t.Error("execution stopped, want do not stop")
114+
} else if isEqual && !isTerminated {
115+
t.Error("execution do not stopped, want stop")
74116
}
75117
}
76118

@@ -87,42 +129,83 @@ func TestNilAndNotNil(t *testing.T) {
87129
}
88130

89131
func testNilAndNotNil(t *testing.T, assertion *Assertion, v any, isNil bool) {
132+
testNil(t, assertion, v, isNil)
133+
134+
testNotNil(t, assertion, v, isNil)
135+
136+
testNilNow(t, assertion, v, isNil)
137+
138+
testNotNilNow(t, assertion, v, isNil)
139+
}
140+
141+
func testNil(t *testing.T, assertion *Assertion, v any, isNil bool) {
90142
err := assertion.Nil(v)
91143
if isNil && err != nil {
92144
t.Errorf("Nil(%v) = %v, want nil", v, err)
93145
} else if !isNil && err == nil {
94146
t.Errorf("Nil(%v) = nil, want error", v)
95147
}
96148

97-
err = assertion.NotNil(v)
149+
err = Nil(assertion.T, v)
150+
if isNil && err != nil {
151+
t.Errorf("Nil(%v) = %v, want nil", v, err)
152+
} else if !isNil && err == nil {
153+
t.Errorf("Nil(%v) = nil, want error", v)
154+
}
155+
}
156+
157+
func testNotNil(t *testing.T, assertion *Assertion, v any, isNil bool) {
158+
err := assertion.NotNil(v)
159+
if isNil && err == nil {
160+
t.Errorf("Nil(%v) = nil, want error", v)
161+
} else if !isNil && err != nil {
162+
t.Errorf("Nil(%v) = %v, want nil", v, err)
163+
}
164+
165+
err = NotNil(assertion.T, v)
98166
if isNil && err == nil {
99167
t.Errorf("Nil(%v) = nil, want error", v)
100168
} else if !isNil && err != nil {
101169
t.Errorf("Nil(%v) = %v, want nil", v, err)
102170
}
171+
}
103172

104-
isTerminated := true
105-
wg := sync.WaitGroup{}
106-
wg.Add(1)
107-
go func() {
108-
defer wg.Done()
173+
func testNilNow(t *testing.T, assertion *Assertion, v any, isNil bool) {
174+
isTerminated := internal.CheckTermination(func() {
109175
assertion.NilNow(v)
110-
isTerminated = false
111-
}()
112-
wg.Wait()
176+
})
177+
if isNil && isTerminated {
178+
t.Error("execution stopped, want do not stop")
179+
} else if !isNil && !isTerminated {
180+
t.Error("execution do not stopped, want stop")
181+
}
182+
183+
isTerminated = internal.CheckTermination(func() {
184+
NilNow(assertion.T, v)
185+
})
113186
if isNil && isTerminated {
114187
t.Error("execution stopped, want do not stop")
188+
} else if !isNil && !isTerminated {
189+
t.Error("execution do not stopped, want stop")
115190
}
191+
}
116192

117-
isTerminated = true
118-
wg.Add(1)
119-
go func() {
120-
defer wg.Done()
193+
func testNotNilNow(t *testing.T, assertion *Assertion, v any, isNil bool) {
194+
isTerminated := internal.CheckTermination(func() {
121195
assertion.NotNilNow(v)
122-
isTerminated = false
123-
}()
124-
wg.Wait()
196+
})
197+
if !isNil && isTerminated {
198+
t.Error("execution stopped, want do not stop")
199+
} else if isNil && !isTerminated {
200+
t.Error("execution do not stopped, want stop")
201+
}
202+
203+
isTerminated = internal.CheckTermination(func() {
204+
NotNilNow(assertion.T, v)
205+
})
125206
if !isNil && isTerminated {
126207
t.Error("execution stopped, want do not stop")
208+
} else if isNil && !isTerminated {
209+
t.Error("execution do not stopped, want stop")
127210
}
128211
}

internal/termination_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package internal
2+
3+
import "testing"
4+
5+
func TestCheckTermination(t *testing.T) {
6+
mockT := new(testing.T)
7+
8+
isTerminated := CheckTermination(func() {
9+
// no panic
10+
})
11+
if isTerminated {
12+
t.Error("CheckTermination() = true, want = false")
13+
}
14+
15+
isTerminated = CheckTermination(func() {
16+
mockT.FailNow()
17+
})
18+
if !isTerminated {
19+
t.Error("CheckTermination() = false, want = true")
20+
}
21+
}

internal/termintion.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package internal
2+
3+
import "sync"
4+
5+
// CheckTermination runs the function `fn` with a goroutine, and tries to get the termination
6+
// status that the function terminates the execution by some panic.
7+
//
8+
// This function is for testing XXXNow assertions, and those functions will set `failed` status
9+
// and call `runtime.Goexit()` to stopping the execution of test.
10+
func CheckTermination(fn func()) bool {
11+
wg := sync.WaitGroup{}
12+
wg.Add(1)
13+
14+
isTerminated := true
15+
16+
go func() {
17+
defer wg.Done()
18+
19+
fn()
20+
21+
isTerminated = false
22+
}()
23+
24+
wg.Wait()
25+
26+
return isTerminated
27+
}

0 commit comments

Comments
 (0)