aboutsummaryrefslogtreecommitdiff
path: root/assert/equal_test.go
blob: f3259164001908a14c0935a23604872891706c28 (plain)
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
158
159
160
161
162
163
164
165
166
// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0

package assert_test

import (
	"strings"
	"testing"

	"go.xyrillian.de/gg/assert"
	"go.xyrillian.de/gg/testcapture"
)

func expectErrors(t *testing.T, test func(assert.TestingTB), expected string) {
	t.Helper()
	r := testcapture.Result{Outcome: testcapture.OutcomeFailed}
	for line := range strings.SplitSeq(expected, "\n") {
		line = strings.TrimSpace(line)
		if line != "" {
			r.Messages = append(r.Messages, testcapture.Log(line))
		}
	}
	assert.Equal(t, testcapture.Capture(t.Context(), t.Name(), test), r)
}

func TestEqual(t *testing.T) {
	// basic test for scalars
	assert.Equal(t, true, true)
	expectErrors(t, func(t assert.TestingTB) {
		assert.Equal(t, false, true)
	}, `expected true, but got false`)

	// basic test for slices
	correctSlice := []int{1, 2, 3}
	wrongSlice := []int{1, 42, 3}
	assert.Equal(t, correctSlice, correctSlice)
	expectErrors(t, func(t assert.TestingTB) {
		assert.Equal(t, wrongSlice, correctSlice)
	}, `at actual[1]: expected 2, but got 42`)

	// slices: report the entire slice if it comes out more compact than reporting each different element
	expectErrors(t, func(t assert.TestingTB) {
		var (
			expected = []int{1, 2, 3, 4, 5, 6, 7, 8}
			actual   = []int{2, 3, 4, 5, 6, 7, 8, 9}
		)
		assert.Equal(t, actual, expected)
	}, `expected []int{1, 2, 3, 4, 5, 6, 7, 8}, but got []int{2, 3, 4, 5, 6, 7, 8, 9}`)
	expectErrors(t, func(t assert.TestingTB) {
		var (
			expected = make([]map[string]int, 6)
			actual   = make([]map[string]int, 6)
		)
		for idx := range expected {
			expected[idx] = map[string]int{"foo": 1 + idx, "bar": 2 + idx}
			actual[idx] = map[string]int{"foo": 1 + idx, "bar": 3 + idx}
		}
		assert.Equal(t, actual, expected)
	}, `
		at actual[0]["bar"]: expected 2, but got 3
		at actual[1]["bar"]: expected 3, but got 4
		at actual[2]["bar"]: expected 4, but got 5
		at actual[3]["bar"]: expected 5, but got 6
		at actual[4]["bar"]: expected 6, but got 7
		at actual[5]["bar"]: expected 7, but got 8
	`)

	// basic test for maps
	correctMap := map[string]int{
		"one":   1,
		"two":   2,
		"three": 3,
	}
	wrongMap := map[string]int{
		"one":  1,
		"two":  23,
		"four": 4,
	}
	assert.Equal(t, correctMap, correctMap)
	expectErrors(t, func(t assert.TestingTB) {
		assert.Equal(t, wrongMap, correctMap)
	}, `
		at actual["four"]: expected <missing>, but got 4
		at actual["three"]: expected 3, but got <missing>
		at actual["two"]: expected 2, but got 23
	`)

	// basic test for structs
	type record struct {
		ID      int
		Name    string
		private bool
	}
	correctStruct := record{1, "Alice", false}
	wrongStruct1 := record{2, "Bob", false}
	assert.Equal(t, correctStruct, correctStruct)
	expectErrors(t, func(t assert.TestingTB) {
		assert.Equal(t, wrongStruct1, correctStruct)
	}, `
		at actual.ID: expected 1, but got 2
		at actual.Name: expected "Alice", but got "Bob"
	`)

	// test for structs: diff in private field (we cannot Interface() the values in such fields, so we have to report the entire struct)
	wrongStruct2 := record{1, "Alice", true}
	expectErrors(t, func(t assert.TestingTB) {
		assert.Equal(t, wrongStruct2, correctStruct)
	}, `expected assert_test.record{ID:1, Name:"Alice", private:false}, but got assert_test.record{ID:1, Name:"Alice", private:true}`)

	// basic test for pointers
	assert.Equal(t, (*bool)(nil), (*bool)(nil))
	assert.Equal(t, new(true), new(true))
	expectErrors(t, func(t assert.TestingTB) {
		assert.Equal(t, new(false), new(true))
	}, `at (*actual): expected true, but got false`)
	expectErrors(t, func(t assert.TestingTB) {
		assert.Equal(t, nil, new(true))
	}, `expected pointer to true, but got nil`)
	expectErrors(t, func(t assert.TestingTB) {
		assert.Equal(t, new(false), nil)
	}, `expected nil, but got pointer to false`)

	// basic test for interfaces
	type slot struct {
		Value any
	}
	correctSlot := slot{[]int{1, 2, 3}}
	wrongSlot := slot{[]int{1, 42, 3}}
	mistypedSlot := slot{42}
	assert.Equal(t, correctSlot, correctSlot)
	expectErrors(t, func(t assert.TestingTB) {
		assert.Equal(t, wrongSlot, correctSlot)
	}, `at actual.Value.([]int)[1]: expected 2, but got 42`)
	expectErrors(t, func(t assert.TestingTB) {
		assert.Equal(t, mistypedSlot, correctSlot)
	}, `at actual.Value: expected []int{1, 2, 3}, but got 42`)

	// check that derefences are elided from reported paths, but only if possible
	expectErrors(t, func(t assert.TestingTB) {
		var (
			expected = map[string]*[]int{"foo": {1, 2, 3}}
			actual   = map[string]*[]int{"foo": {1, 42, 3}}
		)
		assert.Equal(t, actual, expected)
	}, `at (*actual["foo"])[1]: expected 2, but got 42`)
	expectErrors(t, func(t assert.TestingTB) {
		var (
			expected = []*map[string]int{{"foo": 1, "bar": 2}}
			actual   = []*map[string]int{{"foo": 1}}
		)
		assert.Equal(t, actual, expected)
	}, `at (*actual[0])["bar"]: expected 2, but got <missing>`)
	expectErrors(t, func(t assert.TestingTB) {
		var (
			expected = []*record{{1, "Alice", false}}
			actual   = []*record{{2, "Bob", false}}
		)
		assert.Equal(t, actual, expected)
	}, `
		at actual[0].ID: expected 1, but got 2
		at actual[0].Name: expected "Alice", but got "Bob"
	`)
}

// TODO: check coverage
// TODO: implement special case for ~[]byte containing valid UTF-8