Skip to content

Commit

Permalink
Use checkResultAndErrMsg in TestErrorIs/TestNotErrorIs
Browse files Browse the repository at this point in the history
  • Loading branch information
craig65535 committed Nov 9, 2023
1 parent 11f7b61 commit f56991b
Showing 1 changed file with 1 addition and 177 deletions.
178 changes: 1 addition & 177 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3117,78 +3117,6 @@ 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) {
tests := []struct {
err error
Expand Down Expand Up @@ -3228,7 +3156,7 @@ func TestErrorAs(t *testing.T) {
var target *customError
t.Run(fmt.Sprintf("ErrorAs(%#v,%#v)", tt.err, target), func(t *testing.T) {
res := ErrorAs(mockT, tt.err, &target)
checkResultAndErrMsg(t, tt.result, res, tt.resultErrMsg, mockT.msg)
mockT.checkResultAndErrMsg(t, tt.result, res, tt.resultErrMsg)
})
}
}
Expand All @@ -3239,107 +3167,3 @@ func TestIsNil(t *testing.T) {
t.Fatal("fail")
}
}

type myErrType struct{}

func (myErrType) Error() string {
return "myErr"
}

func TestUnwrapAll(t *testing.T) {
var myErr myErrType
testCases := []struct {
in error
out []error
}{
{
in: fmt.Errorf("abc: %w", fmt.Errorf("def: %w", fmt.Errorf("ghi"))),
out: []error{
errors.New("abc: def: ghi"),
errors.New("def: ghi"),
errors.New("ghi"),
},
},
{
in: fmt.Errorf("abc: %w", myErr),
out: []error{
fmt.Errorf("abc: %w", myErr),
myErr,
},
},
{
in: nil,
out: []error{
nil,
},
},
}

for _, tc := range testCases {
out := unwrapAll(tc.in)
if !Equal(t, len(tc.out), len(out)) {
t.FailNow()
}
for i := range out {
if tc.out[i] == nil {
Nil(t, out[i])
} else {
if !NotNil(t, out[i]) {
t.FailNow()
}
EqualError(t, tc.out[i], out[i].Error())
}
}
}
}

func TestBuildErrorChainString(t *testing.T) {
var myErr myErrType
testCases := []struct {
err error
withType bool
out string
}{
{
err: fmt.Errorf("abc: %w", fmt.Errorf("def: %w", fmt.Errorf("ghi"))),
withType: false,
out: "\"abc: def: ghi\"\n" +
"\t\"def: ghi\"\n" +
"\t\"ghi\"",
},
{
err: fmt.Errorf("abc: %w", fmt.Errorf("def: %w", fmt.Errorf("ghi"))),
withType: true,
out: "\"abc: def: ghi\" (*fmt.wrapError)\n" +
"\t\"def: ghi\" (*fmt.wrapError)\n" +
"\t\"ghi\" (*errors.errorString)",
},
{
err: fmt.Errorf("abc: %w", myErr),
withType: false,
out: "\"abc: myErr\"\n" +
"\t\"myErr\"",
},
{
err: fmt.Errorf("abc: %w", myErr),
withType: true,
out: "\"abc: myErr\" (*fmt.wrapError)\n" +
"\t\"myErr\" (assert.myErrType)",
},
{
err: nil,
withType: false,
out: "",
},
{
err: nil,
withType: true,
out: "",
},
}

for _, tc := range testCases {
out := buildErrorChainString(tc.err, tc.withType)
Equal(t, tc.out, out)
}
}

0 comments on commit f56991b

Please sign in to comment.