File: context_test.go

package info (click to toggle)
golang-github-maxatome-go-testdeep 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,416 kB
  • sloc: perl: 1,012; yacc: 130; makefile: 2
file content (283 lines) | stat: -rw-r--r-- 7,207 bytes parent folder | download | duplicates (2)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (c) 2018, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

package ctxerr_test

import (
	"testing"

	"github.com/maxatome/go-testdeep/internal/ctxerr"
	"github.com/maxatome/go-testdeep/internal/location"
	"github.com/maxatome/go-testdeep/internal/test"
)

func TestContext(t *testing.T) {
	for _, maxErrors := range []int{0, 1} {
		ctx := ctxerr.Context{
			MaxErrors: maxErrors,
		}

		ctx.InitErrors()
		if ctx.Errors != nil {
			t.Errorf("Errors is non-nil for MaxErrors %d", maxErrors)
		}
	}

	for _, maxErrors := range []int{-1, 2} {
		ctx := ctxerr.Context{
			MaxErrors: maxErrors,
		}

		ctx.InitErrors()
		if ctx.Errors == nil {
			t.Errorf("Errors is nil for MaxErrors %d", maxErrors)
			continue
		}

		*ctx.Errors = append(*ctx.Errors, &ctxerr.Error{})

		newc := ctx.ResetErrors()
		if newc.Errors == nil {
			t.Errorf("after ResetErrors, new Errors is nil for MaxErrors %d",
				maxErrors)
			continue
		}
		if len(*newc.Errors) > 0 {
			t.Errorf("after ResetErrors, new Errors is not empty for MaxErrors %d",
				maxErrors)
		}
		if ctx.Errors == nil {
			t.Errorf("after ResetErrors, old Errors is nil for MaxErrors %d",
				maxErrors)
			continue
		}
	}
}

type MyGetLocationer struct{}

func (g MyGetLocationer) GetLocation() location.Location {
	return location.Location{
		File: "context_test.go",
		Func: "MyFunc",
		Line: 42,
	}
}

func TestContextMergeErrors(t *testing.T) {
	// No errors to merge
	ctx := ctxerr.Context{}
	if ctx.MergeErrors() != nil {
		t.Error("ctx.MergeErrors() returned a *Error")
	}

	errors := []*ctxerr.Error{}
	ctx = ctxerr.Context{
		Errors: &errors,
	}
	if ctx.MergeErrors() != nil {
		t.Error("ctx.MergeErrors() returned a *Error")
	}

	// Only 1 error to merge => itself
	firstErr := &ctxerr.Error{}
	errors = []*ctxerr.Error{firstErr}
	ctx = ctxerr.Context{
		Errors: &errors,
	}
	if ctx.MergeErrors() != firstErr {
		t.Error("ctx.MergeErrors() did not return the only one error")
	}

	// Several errors to merge
	secondErr, thirdErr := &ctxerr.Error{}, &ctxerr.Error{}
	errors = []*ctxerr.Error{firstErr, secondErr, thirdErr}
	ctx = ctxerr.Context{
		Errors: &errors,
	}
	if ctx.MergeErrors() != firstErr {
		t.Error("ctx.MergeErrors() did not return the first error")
		return
	}
	if firstErr.Next != secondErr {
		t.Error("ctx.MergeErrors() second error is not linked to first one")
		return
	}
	if secondErr.Next != thirdErr {
		t.Error("ctx.MergeErrors() third error is not linked to second one")
		return
	}
	if thirdErr.Next != nil {
		t.Error("ctx.MergeErrors() third error has a non-nil Next!")
	}
}

func TestContextCollectError(t *testing.T) {
	//
	// Only one error kept
	ctx := ctxerr.Context{}

	if ctx.CollectError(nil) != nil {
		t.Error("ctx.CollectError(nil) returned non-nil *Error")
	}

	err := ctxerr.Context{BooleanError: true}.CollectError(&ctxerr.Error{})
	if err != ctxerr.BooleanError {
		t.Error("boolean-ctx.CollectError(X) did not return BooleanError")
	}

	// !err.Location.IsInitialized() + ctx.CurOperator == nil
	origErr := &ctxerr.Error{}
	err = ctx.CollectError(origErr)
	if err != origErr {
		t.Error("ctx.CollectError(err) != err")
	}

	// !err.Location.IsInitialized() + ctx.CurOperator != nil
	ctx.CurOperator = MyGetLocationer{}
	origErr = &ctxerr.Error{}
	err = ctx.CollectError(origErr)
	if err != origErr {
		t.Error("ctx.CollectError(err) != err")
	}
	test.EqualInt(t, err.Location.Line, 42, // see MyGetLocationer.GetLocation()
		"ctx.CollectError(err) initialized err.Location")

	// err.Location.IsInitialized()
	origErr = &ctxerr.Error{
		Location: location.Location{
			File: "zz.go",
			Func: "ErrFunc",
			Line: 24,
		},
	}
	err = ctx.CollectError(origErr)
	if err != origErr {
		t.Error("ctx.CollectError(err) != err")
	}
	test.EqualInt(t, err.Location.Line, 24,
		"ctx.CollectError(err) did not touch err.Location")

	//
	// 2 errors kept max
	errors := []*ctxerr.Error{}
	ctx = ctxerr.Context{
		Errors:    &errors,
		MaxErrors: 2,
	}
	origErr = &ctxerr.Error{}
	if ctx.CollectError(origErr) != nil { // 1st error is accumulated
		t.Error("ctx.CollectError(err) != nil")
		return
	}

	secondErr := &ctxerr.Error{}
	if ctx.CollectError(secondErr) != origErr {
		t.Error("ctx.CollectError(err) != origErr")
		return
	}

	if origErr.Next != secondErr {
		t.Error("origErr.Next != secondErr")
		return
	}

	if secondErr.Next != ctxerr.ErrTooManyErrors {
		t.Error("secondErr.Next != ErrTooManyErrors")
		return
	}

	//
	// All errors kept
	errors = nil
	ctx = ctxerr.Context{
		Errors:    &errors,
		MaxErrors: -1,
	}
	for i := 0; i < 100; i++ {
		if ctx.CollectError(&ctxerr.Error{}) != nil { // 1st error is accumulated
			t.Errorf("#%d: ctx.CollectError(err) != nil", i)
			return
		}
	}
	if len(errors) != 100 {
		t.Errorf("Only %d errors accumulated instead of 100", len(errors))
	}

	//
	// Do not collect 2 times the same error
	errors = nil
	ctx = ctxerr.Context{
		Errors:    &errors,
		MaxErrors: -1,
	}
	ctx.CollectError(&ctxerr.Error{}) //nolint: errcheck
	x := &ctxerr.Error{}
	ctx.CollectError(x)               //nolint: errcheck
	ctx.CollectError(&ctxerr.Error{}) //nolint: errcheck
	ctx.CollectError(x)               //nolint: errcheck
	ctx.CollectError(x)               //nolint: errcheck
	ctx.CollectError(&ctxerr.Error{}) //nolint: errcheck
	if len(errors) != 4 {
		t.Errorf("%d errors accumulated instead of 4", len(errors))
	}
}

func TestCannotCompareError(t *testing.T) {
	ctx := ctxerr.Context{BooleanError: true}

	err := ctx.CannotCompareError()
	if err != ctxerr.BooleanError {
		t.Error("CannotCompareError does not return ctxerr.BooleanError")
	}

	ctx = ctxerr.Context{}
	err = ctx.CannotCompareError()
	test.EqualStr(t, err.Message, "cannot compare")
}

func TestContextPath(t *testing.T) {
	ctx := ctxerr.Context{Path: ctxerr.NewPath("DATA")}
	ctx = ctx.AddField("field")
	test.EqualStr(t, ctx.Path.String(), "DATA.field")
	test.EqualInt(t, ctx.Depth, 1)

	ctx = ctx.AddPtr(2)
	test.EqualStr(t, ctx.Path.String(), "**DATA.field")
	test.EqualInt(t, ctx.Depth, 2)

	ctx = ctx.AddField("another")
	test.EqualStr(t, ctx.Path.String(), "(*DATA.field).another")
	test.EqualInt(t, ctx.Depth, 3)

	ctx = ctx.AddCustomLevel("→cust")
	test.EqualStr(t, ctx.Path.String(), "(*DATA.field).another→cust")
	test.EqualInt(t, ctx.Depth, 4)

	ctx = ctxerr.Context{Path: ctxerr.NewPath("DATA")}
	ctx = ctx.AddArrayIndex(18)
	test.EqualStr(t, ctx.Path.String(), "DATA[18]")
	test.EqualInt(t, ctx.Depth, 1)

	ctx = ctxerr.Context{Path: ctxerr.NewPath("DATA")}
	ctx = ctx.AddMapKey("foo")
	test.EqualStr(t, ctx.Path.String(), `DATA["foo"]`) // special case of util.ToString()
	test.EqualInt(t, ctx.Depth, 1)

	ctx = ctxerr.Context{Path: ctxerr.NewPath("DATA")}
	ctx = ctx.AddMapKey(12)
	test.EqualStr(t, ctx.Path.String(), `DATA[12]`)
	test.EqualInt(t, ctx.Depth, 1)

	ctx = ctxerr.Context{Path: ctxerr.NewPath("DATA")}
	ctx = ctx.AddFunctionCall("foobar")
	test.EqualStr(t, ctx.Path.String(), "foobar(DATA)")
	test.EqualInt(t, ctx.Depth, 1)

	ctx = ctx.ResetPath("NEW")
	test.EqualStr(t, ctx.Path.String(), "NEW")
	test.EqualInt(t, ctx.Depth, 2)
}