Skip to content

Commit

Permalink
TestErrorAs: check error contents
Browse files Browse the repository at this point in the history
  • Loading branch information
craig65535 committed Jan 24, 2024
1 parent 755cbd4 commit 73f79f1
Showing 1 changed file with 103 additions and 9 deletions.
112 changes: 103 additions & 9 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3116,24 +3116,118 @@ func TestNotErrorIs(t *testing.T) {
}
}

func parseLabeledOutput(output string) []labeledContent {
labelPattern := regexp.MustCompile(`\t([^\t]*): *\t(.*)$`)
contentPattern := regexp.MustCompile(`\t *\t(.*)$`)
var contents []labeledContent
lines := strings.Split(output, "\n")
i := -1
for _, line := range lines {
if line == "" {
// skip blank lines
continue
}
matches := labelPattern.FindStringSubmatch(line)
if len(matches) == 3 {
// a label
contents = append(contents, labeledContent{
label: matches[1],
content: matches[2] + "\n",
})
i++
continue
}
matches = contentPattern.FindStringSubmatch(line)
if len(matches) == 2 {
// just content
if i >= 0 {
contents[i].content += matches[1] + "\n"
continue
}
}
// Couldn't parse output
return nil
}
return contents
}

type captureTestingT struct {
msg string
}

func (ctt *captureTestingT) Errorf(format string, args ...interface{}) {
ctt.msg = fmt.Sprintf(format, args...)
}

func checkResultAndErrMsg(t *testing.T, expectedRes, res bool, expectedErrMsg, rawErrOutput string) {
t.Helper()
if res != expectedRes {
t.Errorf("Should return %t", expectedRes)
return
}
contents := parseLabeledOutput(rawErrOutput)
if res == true {
if contents != nil {
t.Errorf("Should not log an error")
}
return
}
if contents == nil {
t.Errorf("Should log an error. Log output: %v", rawErrOutput)
return
}
for _, content := range contents {
if content.label == "Error" {
if expectedErrMsg == content.content {
return
}
t.Errorf("Logged Error: %v", content.content)
}
}
t.Errorf("Should log Error: %v", expectedErrMsg)

}

func TestErrorAs(t *testing.T) {
mockT := new(testing.T)
tests := []struct {
err error
result bool
err error
result bool
resultErrMsg string
}{
{fmt.Errorf("wrap: %w", &customError{}), true},
{io.EOF, false},
{nil, false},
{
err: fmt.Errorf("wrap: %w", &customError{}),
result: true,
},
{
err: io.EOF,
result: false,
resultErrMsg: "Should be in error chain:\n" +
"expected: **assert.customError\n" +
"in chain: \"EOF\" (*errors.errorString)\n",
},
{
err: nil,
result: false,
resultErrMsg: "Should be in error chain:\n" +
"expected: **assert.customError\n" +
"in chain: \n",
},
{
err: fmt.Errorf("abc: %w", errors.New("def")),
result: false,
resultErrMsg: "Should be in error chain:\n" +
"expected: **assert.customError\n" +
"in chain: \"abc: def\" (*fmt.wrapError)\n" +
"\t\"def\" (*errors.errorString)\n",
},
}
for _, tt := range tests {
tt := tt
mockT := new(captureTestingT)
var target *customError
t.Run(fmt.Sprintf("ErrorAs(%#v,%#v)", tt.err, target), func(t *testing.T) {
res := ErrorAs(mockT, tt.err, &target)
if res != tt.result {
t.Errorf("ErrorAs(%#v,%#v) should return %t)", tt.err, target, tt.result)
}
checkResultAndErrMsg(t, tt.result, res, tt.resultErrMsg, mockT.msg)
})
}
}

0 comments on commit 73f79f1

Please sign in to comment.