1717package testcase_test
1818
1919import (
20- "errors"
21- "fmt"
2220 "testing"
2321
22+ "github.com/go-spring/stdlib/errutil"
2423 "github.com/go-spring/stdlib/testing/assert"
2524 "github.com/go-spring/stdlib/testing/internal"
2625 "github.com/go-spring/stdlib/testing/require"
@@ -44,20 +43,20 @@ func TestError_Nil(t *testing.T) {
4443
4544 // Test with non-nil error - should fail
4645 m .Reset ()
47- assert .Error (m , errors . New ( "this is an error" )).Nil ()
46+ assert .Error (m , errutil . Explain ( nil , "this is an error" )).Nil ()
4847 assert .String (t , m .String ()).Equal (`error# Assertion failed: expected error to be nil, but it is not
4948 actual: (*errors.errorString) "this is an error"` )
5049
5150 // Test with Require mode - should fatal
5251 m .Reset ()
53- require .Error (m , errors . New ( "this is an error" )).Nil ("index is 0" )
52+ require .Error (m , errutil . Explain ( nil , "this is an error" )).Nil ("index is 0" )
5453 assert .String (t , m .String ()).Equal (`fatal# Assertion failed: expected error to be nil, but it is not
5554 actual: (*errors.errorString) "this is an error"
5655 message: "index is 0"` )
5756
5857 // Test with custom message
5958 m .Reset ()
60- assert .Error (m , errors . New ( "test error" )).Nil ("expected no error in this operation" )
59+ assert .Error (m , errutil . Explain ( nil , "test error" )).Nil ("expected no error in this operation" )
6160 assert .String (t , m .String ()).Equal (`error# Assertion failed: expected error to be nil, but it is not
6261 actual: (*errors.errorString) "test error"
6362 message: "expected no error in this operation"` )
@@ -68,7 +67,7 @@ func TestError_NotNil(t *testing.T) {
6867
6968 // Test with non-nil error - should pass
7069 m .Reset ()
71- assert .Error (m , errors . New ( "this is an error" )).NotNil ()
70+ assert .Error (m , errutil . Explain ( nil , "this is an error" )).NotNil ()
7271 assert .String (t , m .String ()).Equal ("" )
7372
7473 // Test with nil error - should fail
@@ -91,7 +90,7 @@ func TestError_NotNil(t *testing.T) {
9190
9291func TestError_Is (t * testing.T ) {
9392 m := new (internal.MockTestingT )
94- err := errors . New ( "this is an error" )
93+ err := errutil . Explain ( nil , "this is an error" )
9594
9695 // Test successful case - error is the same as target
9796 m .Reset ()
@@ -100,23 +99,23 @@ func TestError_Is(t *testing.T) {
10099
101100 // Test failed case - different errors
102101 m .Reset ()
103- assert .Error (m , err ).Is (errors . New ( "another error" ))
102+ assert .Error (m , err ).Is (errutil . Explain ( nil , "another error" ))
104103 assert .String (t , m .String ()).Equal (`error# Assertion failed: expected error to be target (according to errors.Is), but they are different
105104 actual: this is an error
106105expected: another error` )
107106
108107 // Test failed case with Require - should fatal
109108 m .Reset ()
110- require .Error (m , err ).Is (errors . New ( "another error" ), "index is 0" )
109+ require .Error (m , err ).Is (errutil . Explain ( nil , "another error" ), "index is 0" )
111110 assert .String (t , m .String ()).Equal (`fatal# Assertion failed: expected error to be target (according to errors.Is), but they are different
112111 actual: this is an error
113112expected: another error
114113 message: "index is 0"` )
115114
116115 // Test with wrapped error - should not match the root error (because we're checking Is in wrong direction)
117116 m .Reset ()
118- rootErr := errors . New ( "root error" )
119- wrappedErr := fmt . Errorf ( "level 1: %w" , fmt . Errorf ( "level 2: %w" , rootErr ) )
117+ rootErr := errutil . Explain ( nil , "root error" )
118+ wrappedErr := errutil . Explain ( errutil . Explain ( rootErr , "level 2" ), "level 1" )
120119 assert .Error (m , wrappedErr ).Is (rootErr )
121120 assert .String (t , m .String ()).Equal ("" )
122121
@@ -135,7 +134,7 @@ expected: this is an error`)
135134
136135 // Test with custom message on failure
137136 m .Reset ()
138- assert .Error (m , errors . New ( "some error" )).Is (errors . New ( "other error" ), "expected errors to match" )
137+ assert .Error (m , errutil . Explain ( nil , "some error" )).Is (errutil . Explain ( nil , "other error" ), "expected errors to match" )
139138 assert .String (t , m .String ()).Equal (`error# Assertion failed: expected error to be target (according to errors.Is), but they are different
140139 actual: some error
141140expected: other error
@@ -144,11 +143,11 @@ expected: other error
144143
145144func TestError_NotIs (t * testing.T ) {
146145 m := new (internal.MockTestingT )
147- err := errors . New ( "this is an error" )
146+ err := errutil . Explain ( nil , "this is an error" )
148147
149148 // Test successful case - different errors
150149 m .Reset ()
151- assert .Error (m , err ).NotIs (errors . New ( "another error" ))
150+ assert .Error (m , err ).NotIs (errutil . Explain ( nil , "another error" ))
152151 assert .String (t , m .String ()).Equal ("" )
153152
154153 // Test failed case - same errors
@@ -168,8 +167,8 @@ expected: this is an error
168167
169168 // Test with wrapped error - wrapped error contains root error, so NotIs should fail
170169 m .Reset ()
171- rootErr := errors . New ( "root error" )
172- wrappedErr := fmt . Errorf ( "level 1: %w" , fmt . Errorf ( "level 2: %w" , rootErr ) )
170+ rootErr := errutil . Explain ( nil , "root error" )
171+ wrappedErr := errutil . Explain ( errutil . Explain ( rootErr , "level 2" ), "level 1" )
173172 assert .Error (m , rootErr ).NotIs (wrappedErr )
174173 assert .String (t , m .String ()).Equal ("" )
175174
@@ -195,7 +194,7 @@ expected: this is an error
195194
196195func TestError_String (t * testing.T ) {
197196 m := new (internal.MockTestingT )
198- err := errors . New ( "this is an error" )
197+ err := errutil . Explain ( nil , "this is an error" )
199198
200199 // Test successful case - error is the same as target
201200 m .Reset ()
@@ -219,8 +218,8 @@ expected: "another error"
219218
220219 // Test with wrapped error - should not match the root error (because we're checking Is in wrong direction)
221220 m .Reset ()
222- rootErr := errors . New ( "root error" )
223- wrappedErr := fmt . Errorf ( "level 1: %w" , fmt . Errorf ( "level 2: %w" , rootErr ) )
221+ rootErr := errutil . Explain ( nil , "root error" )
222+ wrappedErr := errutil . Explain ( errutil . Explain ( rootErr , "level 2" ), "level 1" )
224223 assert .Error (m , wrappedErr ).String ("level 1: level 2: root error" )
225224 assert .String (t , m .String ()).Equal ("" )
226225
@@ -237,7 +236,7 @@ expected: "another error"
237236
238237 // Test with custom message on failure
239238 m .Reset ()
240- assert .Error (m , errors . New ( "some error" )).String ("other error" , "expected errors to match" )
239+ assert .Error (m , errutil . Explain ( nil , "some error" )).String ("other error" , "expected errors to match" )
241240 assert .String (t , m .String ()).Equal (`error# Assertion failed: expected strings to be equal, but they are not
242241 actual: "some error"
243242expected: "other error"
@@ -249,12 +248,12 @@ func TestError_Matches(t *testing.T) {
249248
250249 // Test successful case - simple string match
251250 m .Reset ()
252- assert .Error (m , errors . New ( "this is an error" )).Matches ("an error" )
251+ assert .Error (m , errutil . Explain ( nil , "this is an error" )).Matches ("an error" )
253252 assert .String (t , m .String ()).Equal ("" )
254253
255254 // Test invalid regex pattern
256255 m .Reset ()
257- assert .Error (m , errors . New ( "there's no error" )).Matches (`an error \` )
256+ assert .Error (m , errutil . Explain ( nil , "there's no error" )).Matches (`an error \` )
258257 assert .String (t , m .String ()).Equal ("error# Assertion failed: invalid pattern" )
259258
260259 // Test with nil error - should fail
@@ -270,28 +269,28 @@ func TestError_Matches(t *testing.T) {
270269
271270 // Test failed match with Require - should fatal
272271 m .Reset ()
273- require .Error (m , errors . New ( "there's no error" )).Matches ("an error" )
272+ require .Error (m , errutil . Explain ( nil , "there's no error" )).Matches ("an error" )
274273 assert .String (t , m .String ()).Equal (`fatal# Assertion failed: got "there's no error" which does not match "an error"` )
275274
276275 // Test failed match with Require and custom message
277276 m .Reset ()
278- require .Error (m , errors . New ( "there's no error" )).Matches ("an error" , "index is 0" )
277+ require .Error (m , errutil . Explain ( nil , "there's no error" )).Matches ("an error" , "index is 0" )
279278 assert .String (t , m .String ()).Equal (`fatal# Assertion failed: got "there's no error" which does not match "an error"
280279 message: "index is 0"` )
281280
282281 // Test with regex pattern that matches
283282 m .Reset ()
284- assert .Error (m , errors . New ( "error code 123" )).Matches (`error code \d+` )
283+ assert .Error (m , errutil . Explain ( nil , "error code 123" )).Matches (`error code \d+` )
285284 assert .String (t , m .String ()).Equal ("" )
286285
287286 // Test with regex pattern that does not match
288287 m .Reset ()
289- assert .Error (m , errors . New ( "error code abc" )).Matches (`error code \d+` )
288+ assert .Error (m , errutil . Explain ( nil , "error code abc" )).Matches (`error code \d+` )
290289 assert .String (t , m .String ()).Equal (`error# Assertion failed: got "error code abc" which does not match "error code \\d+"` )
291290
292291 // Test with complex error message
293292 m .Reset ()
294- assert .Error (m , fmt . Errorf ( " database connection failed: %w" , errors . New ( "timeout" ) )).Matches ("connection failed" )
293+ assert .Error (m , errutil . Explain ( errutil . Explain ( nil , "timeout" ), " database connection failed" )).Matches ("connection failed" )
295294 assert .String (t , m .String ()).Equal ("" )
296295
297296 // Test with custom error type
@@ -301,7 +300,7 @@ func TestError_Matches(t *testing.T) {
301300
302301 // Test with custom message on failure
303302 m .Reset ()
304- assert .Error (m , errors . New ( "some error" )).Matches ("nonexistent" , "expected error to match pattern" )
303+ assert .Error (m , errutil . Explain ( nil , "some error" )).Matches ("nonexistent" , "expected error to match pattern" )
305304 assert .String (t , m .String ()).Equal (`error# Assertion failed: got "some error" which does not match "nonexistent"
306305 message: "expected error to match pattern"` )
307306}
0 commit comments