aboutsummaryrefslogtreecommitdiffstats
path: root/assert_test.go
blob: 59645a68e6287de265ffbb23e279cdba99bcdc98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package checkmate

import (
	"fmt"
	"strings"
	"testing"
)

// MockT is a mock testing.T structure for capturing test outputs.
type MockT struct {
	TestingT

	HelperCalled  bool
	FailNowCalled bool
	Logs          []string
}

func (m *MockT) Helper() {
	m.HelperCalled = true
}

func (m *MockT) FailNow() {
	m.FailNowCalled = true
}

func (m *MockT) Log(args ...any) {
	m.Logs = append(m.Logs, fmt.Sprint(args...))
}

func TestCheck(t *testing.T) {
	t.Run("True", func(t *testing.T) {
		mockT := &MockT{}
		Check(mockT, true)
		if mockT.FailNowCalled {
			t.Fatal("Check should not be failing any tests")
		}
		if len(mockT.Logs) > 0 {
			t.Fatal("Check should not emit any logs on success")
		}
	})

	t.Run("False", func(t *testing.T) {
		mockT := &MockT{}
		Check(mockT, false)
		if mockT.FailNowCalled {
			t.Fatal("Check should not be failing any tests")
		}
		if len(mockT.Logs) == 0 {
			t.Fatal("Check should be emiting a logs on failure")
		}
	})
}

func TestAssert(t *testing.T) {
	t.Run("True", func(t *testing.T) {
		mockT := &MockT{}
		Assert(mockT, true)
		if mockT.FailNowCalled {
			t.Fatal("Assert failed when it should have passed")
		}
	})

	t.Run("False", func(t *testing.T) {
		mockT := &MockT{}
		Assert(mockT, false)
		if !mockT.FailNowCalled {
			t.Fatal("Assert passed when it should have failed")
		}
		if len(mockT.Logs) == 0 {
			t.Fatal("Assert should have emitted a failure log")
		}
	})
}

func TestAssertEqual(t *testing.T) {
	t.Run("Equal", func(t *testing.T) {
		mockT := &MockT{}
		AssertEqual(mockT, 5, 5)
		if mockT.FailNowCalled {
			t.Fatal("AssertEqual failed when it should have passed")
		}
	})

	t.Run("Not equal", func(t *testing.T) {
		mockT := &MockT{}
		AssertEqual(mockT, 5, 10)
		if !mockT.FailNowCalled {
			t.Fatal("AssertEqual passed when it should have failed")
		}
	})
}

func TestAssertNotEqual(t *testing.T) {
	t.Run("Equal", func(t *testing.T) {
		mockT := &MockT{}
		AssertNotEqual(mockT, 5, 5)
		if !mockT.FailNowCalled {
			t.Fatal("AssertNotEqual failed when it should have passed")
		}
	})

	t.Run("Not equal", func(t *testing.T) {
		mockT := &MockT{}
		AssertNotEqual(mockT, 5, 10)
		if mockT.FailNowCalled {
			t.Fatal("AssertNotEqual passed when it should have failed")
		}
	})
}

func TestAssertLenEqual(t *testing.T) {
	t.Run("Equal lengths", func(t *testing.T) {
		mockT := &MockT{}
		AssertLenEqual(mockT, []int{1, 2, 3}, 3)
		if mockT.FailNowCalled {
			t.Error("AssertLenEqual failed when it should have passed")
		}
	})

	t.Run("Lengths aren't equal", func(t *testing.T) {
		mockT := &MockT{}
		AssertLenEqual(mockT, []int{1, 2, 3}, 2)
		if !mockT.FailNowCalled {
			t.Error("AssertLenEqual passed when it should have failed")
		}
	})
}

func TestAssertDeepEqual(t *testing.T) {
	type TestStruct struct {
		Name string
		Age  int
	}

	t.Run("Equal structs", func(t *testing.T) {
		mockT := &MockT{}
		AssertDeepEqual(mockT, TestStruct{"Alice", 30}, TestStruct{"Alice", 30})
		if mockT.FailNowCalled {
			t.Error("AssertDeepEqual failed when it should have passed")
		}
	})

	t.Run("Not equal structs", func(t *testing.T) {
		mockT := &MockT{}
		AssertDeepEqual(mockT, TestStruct{"Alice", 30}, TestStruct{"Bob", 30})
		if !mockT.FailNowCalled {
			t.Error("AssertDeepEqual passed when it should have failed")
		}
		if len(mockT.Logs) == 0 || !containsDiffMessage(mockT.Logs[0]) {
			t.Error("AssertDeepEqual did not log the correct diff message")
		}
	})
}

func containsDiffMessage(log string) bool {
	return strings.Contains(log, "(-expected +actual)")
}