Skip to content

Commit 79cde0b

Browse files
committed
Use checkResultAndErrMsg in TestErrorIs/TestNotErrorIs
1 parent 1546b81 commit 79cde0b

File tree

1 file changed

+1
-177
lines changed

1 file changed

+1
-177
lines changed

assert/assertions_test.go

Lines changed: 1 addition & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -3117,78 +3117,6 @@ func TestNotErrorIs(t *testing.T) {
31173117
}
31183118
}
31193119

3120-
func parseLabeledOutput(output string) []labeledContent {
3121-
labelPattern := regexp.MustCompile(`\t([^\t]*): *\t(.*)$`)
3122-
contentPattern := regexp.MustCompile(`\t *\t(.*)$`)
3123-
var contents []labeledContent
3124-
lines := strings.Split(output, "\n")
3125-
i := -1
3126-
for _, line := range lines {
3127-
if line == "" {
3128-
// skip blank lines
3129-
continue
3130-
}
3131-
matches := labelPattern.FindStringSubmatch(line)
3132-
if len(matches) == 3 {
3133-
// a label
3134-
contents = append(contents, labeledContent{
3135-
label: matches[1],
3136-
content: matches[2] + "\n",
3137-
})
3138-
i++
3139-
continue
3140-
}
3141-
matches = contentPattern.FindStringSubmatch(line)
3142-
if len(matches) == 2 {
3143-
// just content
3144-
if i >= 0 {
3145-
contents[i].content += matches[1] + "\n"
3146-
continue
3147-
}
3148-
}
3149-
// Couldn't parse output
3150-
return nil
3151-
}
3152-
return contents
3153-
}
3154-
3155-
type captureTestingT struct {
3156-
msg string
3157-
}
3158-
3159-
func (ctt *captureTestingT) Errorf(format string, args ...interface{}) {
3160-
ctt.msg = fmt.Sprintf(format, args...)
3161-
}
3162-
3163-
func checkResultAndErrMsg(t *testing.T, expectedRes, res bool, expectedErrMsg, rawErrOutput string) {
3164-
t.Helper()
3165-
if res != expectedRes {
3166-
t.Errorf("Should return %t", expectedRes)
3167-
return
3168-
}
3169-
contents := parseLabeledOutput(rawErrOutput)
3170-
if res == true {
3171-
if contents != nil {
3172-
t.Errorf("Should not log an error")
3173-
}
3174-
return
3175-
}
3176-
if contents == nil {
3177-
t.Errorf("Should log an error. Log output: %v", rawErrOutput)
3178-
return
3179-
}
3180-
for _, content := range contents {
3181-
if content.label == "Error" {
3182-
if expectedErrMsg == content.content {
3183-
return
3184-
}
3185-
t.Errorf("Logged Error: %v", content.content)
3186-
}
3187-
}
3188-
t.Errorf("Should log Error: %v", expectedErrMsg)
3189-
3190-
}
3191-
31923120
func TestErrorAs(t *testing.T) {
31933121
tests := []struct {
31943122
err error
@@ -3228,7 +3156,7 @@ func TestErrorAs(t *testing.T) {
32283156
var target *customError
32293157
t.Run(fmt.Sprintf("ErrorAs(%#v,%#v)", tt.err, target), func(t *testing.T) {
32303158
res := ErrorAs(mockT, tt.err, &target)
3231-
checkResultAndErrMsg(t, tt.result, res, tt.resultErrMsg, mockT.msg)
3159+
mockT.checkResultAndErrMsg(t, tt.result, res, tt.resultErrMsg)
32323160
})
32333161
}
32343162
}
@@ -3239,107 +3167,3 @@ func TestIsNil(t *testing.T) {
32393167
t.Fatal("fail")
32403168
}
32413169
}
3242-
3243-
type myErrType struct{}
3244-
3245-
func (myErrType) Error() string {
3246-
return "myErr"
3247-
}
3248-
3249-
func TestUnwrapAll(t *testing.T) {
3250-
var myErr myErrType
3251-
testCases := []struct {
3252-
in error
3253-
out []error
3254-
}{
3255-
{
3256-
in: fmt.Errorf("abc: %w", fmt.Errorf("def: %w", fmt.Errorf("ghi"))),
3257-
out: []error{
3258-
errors.New("abc: def: ghi"),
3259-
errors.New("def: ghi"),
3260-
errors.New("ghi"),
3261-
},
3262-
},
3263-
{
3264-
in: fmt.Errorf("abc: %w", myErr),
3265-
out: []error{
3266-
fmt.Errorf("abc: %w", myErr),
3267-
myErr,
3268-
},
3269-
},
3270-
{
3271-
in: nil,
3272-
out: []error{
3273-
nil,
3274-
},
3275-
},
3276-
}
3277-
3278-
for _, tc := range testCases {
3279-
out := unwrapAll(tc.in)
3280-
if !Equal(t, len(tc.out), len(out)) {
3281-
t.FailNow()
3282-
}
3283-
for i := range out {
3284-
if tc.out[i] == nil {
3285-
Nil(t, out[i])
3286-
} else {
3287-
if !NotNil(t, out[i]) {
3288-
t.FailNow()
3289-
}
3290-
EqualError(t, tc.out[i], out[i].Error())
3291-
}
3292-
}
3293-
}
3294-
}
3295-
3296-
func TestBuildErrorChainString(t *testing.T) {
3297-
var myErr myErrType
3298-
testCases := []struct {
3299-
err error
3300-
withType bool
3301-
out string
3302-
}{
3303-
{
3304-
err: fmt.Errorf("abc: %w", fmt.Errorf("def: %w", fmt.Errorf("ghi"))),
3305-
withType: false,
3306-
out: "\"abc: def: ghi\"\n" +
3307-
"\t\"def: ghi\"\n" +
3308-
"\t\"ghi\"",
3309-
},
3310-
{
3311-
err: fmt.Errorf("abc: %w", fmt.Errorf("def: %w", fmt.Errorf("ghi"))),
3312-
withType: true,
3313-
out: "\"abc: def: ghi\" (*fmt.wrapError)\n" +
3314-
"\t\"def: ghi\" (*fmt.wrapError)\n" +
3315-
"\t\"ghi\" (*errors.errorString)",
3316-
},
3317-
{
3318-
err: fmt.Errorf("abc: %w", myErr),
3319-
withType: false,
3320-
out: "\"abc: myErr\"\n" +
3321-
"\t\"myErr\"",
3322-
},
3323-
{
3324-
err: fmt.Errorf("abc: %w", myErr),
3325-
withType: true,
3326-
out: "\"abc: myErr\" (*fmt.wrapError)\n" +
3327-
"\t\"myErr\" (assert.myErrType)",
3328-
},
3329-
{
3330-
err: nil,
3331-
withType: false,
3332-
out: "",
3333-
},
3334-
{
3335-
err: nil,
3336-
withType: true,
3337-
out: "",
3338-
},
3339-
}
3340-
3341-
for _, tc := range testCases {
3342-
out := buildErrorChainString(tc.err, tc.withType)
3343-
Equal(t, tc.out, out)
3344-
}
3345-
}

0 commit comments

Comments
 (0)