@@ -3175,11 +3175,13 @@ func parseLabeledOutput(output string) []labeledContent {
31753175}
31763176
31773177type captureTestingT struct {
3178- msg string
3178+ failed bool
3179+ msg string
31793180}
31803181
31813182func (ctt * captureTestingT ) Errorf (format string , args ... interface {}) {
31823183 ctt .msg = fmt .Sprintf (format , args ... )
3184+ ctt .failed = true
31833185}
31843186
31853187func (ctt * captureTestingT ) checkResultAndErrMsg (t * testing.T , expectedRes , res bool , expectedErrMsg string ) {
@@ -3188,6 +3190,10 @@ func (ctt *captureTestingT) checkResultAndErrMsg(t *testing.T, expectedRes, res
31883190 t .Errorf ("Should return %t" , expectedRes )
31893191 return
31903192 }
3193+ if res == ctt .failed {
3194+ t .Errorf ("The test result (%t) should be reflected in the testing.T type (%t)" , res , ! ctt .failed )
3195+ return
3196+ }
31913197 contents := parseLabeledOutput (ctt .msg )
31923198 if res == true {
31933199 if contents != nil {
@@ -3348,50 +3354,82 @@ func TestNotErrorIs(t *testing.T) {
33483354
33493355func TestErrorAs (t * testing.T ) {
33503356 tests := []struct {
3351- err error
3352- result bool
3357+ err error
3358+ result bool
3359+ resultErrMsg string
33533360 }{
3354- {fmt .Errorf ("wrap: %w" , & customError {}), true },
3355- {io .EOF , false },
3356- {nil , false },
3361+ {
3362+ err : fmt .Errorf ("wrap: %w" , & customError {}),
3363+ result : true ,
3364+ },
3365+ {
3366+ err : io .EOF ,
3367+ result : false ,
3368+ resultErrMsg : "" +
3369+ "Should be in error chain:\n " +
3370+ "expected: *assert.customError\n " +
3371+ "in chain: \" EOF\" (*errors.errorString)\n " ,
3372+ },
3373+ {
3374+ err : nil ,
3375+ result : false ,
3376+ resultErrMsg : "" +
3377+ "Should be in error chain:\n " +
3378+ "expected: *assert.customError\n " +
3379+ "in chain: \n " ,
3380+ },
3381+ {
3382+ err : fmt .Errorf ("abc: %w" , errors .New ("def" )),
3383+ result : false ,
3384+ resultErrMsg : "" +
3385+ "Should be in error chain:\n " +
3386+ "expected: *assert.customError\n " +
3387+ "in chain: \" abc: def\" (*fmt.wrapError)\n " +
3388+ "\t \" def\" (*errors.errorString)\n " ,
3389+ },
33573390 }
33583391 for _ , tt := range tests {
33593392 tt := tt
33603393 var target * customError
33613394 t .Run (fmt .Sprintf ("ErrorAs(%#v,%#v)" , tt .err , target ), func (t * testing.T ) {
3362- mockT := new (testing. T )
3395+ mockT := new (captureTestingT )
33633396 res := ErrorAs (mockT , tt .err , & target )
3364- if res != tt .result {
3365- t .Errorf ("ErrorAs(%#v,%#v) should return %t" , tt .err , target , tt .result )
3366- }
3367- if res == mockT .Failed () {
3368- t .Errorf ("The test result (%t) should be reflected in the testing.T type (%t)" , res , ! mockT .Failed ())
3369- }
3397+ mockT .checkResultAndErrMsg (t , tt .result , res , tt .resultErrMsg )
33703398 })
33713399 }
33723400}
33733401
33743402func TestNotErrorAs (t * testing.T ) {
33753403 tests := []struct {
3376- err error
3377- result bool
3404+ err error
3405+ result bool
3406+ resultErrMsg string
33783407 }{
3379- {fmt .Errorf ("wrap: %w" , & customError {}), false },
3380- {io .EOF , true },
3381- {nil , true },
3408+ {
3409+ err : fmt .Errorf ("wrap: %w" , & customError {}),
3410+ result : false ,
3411+ resultErrMsg : "" +
3412+ "Target error should not be in err chain:\n " +
3413+ "found: *assert.customError\n " +
3414+ "in chain: \" wrap: fail\" (*fmt.wrapError)\n " +
3415+ "\t \" fail\" (*assert.customError)\n " ,
3416+ },
3417+ {
3418+ err : io .EOF ,
3419+ result : true ,
3420+ },
3421+ {
3422+ err : nil ,
3423+ result : true ,
3424+ },
33823425 }
33833426 for _ , tt := range tests {
33843427 tt := tt
33853428 var target * customError
33863429 t .Run (fmt .Sprintf ("NotErrorAs(%#v,%#v)" , tt .err , target ), func (t * testing.T ) {
3387- mockT := new (testing. T )
3430+ mockT := new (captureTestingT )
33883431 res := NotErrorAs (mockT , tt .err , & target )
3389- if res != tt .result {
3390- t .Errorf ("NotErrorAs(%#v,%#v) should not return %t" , tt .err , target , tt .result )
3391- }
3392- if res == mockT .Failed () {
3393- t .Errorf ("The test result (%t) should be reflected in the testing.T type (%t)" , res , ! mockT .Failed ())
3394- }
3432+ mockT .checkResultAndErrMsg (t , tt .result , res , tt .resultErrMsg )
33953433 })
33963434 }
33973435}
0 commit comments