Skip to content

Commit

Permalink
test: errors/errorsutil: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Dec 27, 2024
1 parent 8aab37c commit dc2b4ba
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions errors/errorsutil/error_with_location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var errorWithLocationTests = []struct {
fn func() error
errMsgSuffix string
}{
{func() error { _, err := time.Parse(time.RFC3339, "Mon, 2024-12-31"); return err }, "github.com/grokify/mogo/errors/errorsutil/error_with_location_test.go:22"},
{func() error { _, err := time.Parse(time.RFC3339, "Mon, 2024-12-31"); return err }, `github.com/grokify/mogo/errors/errorsutil/error_with_location_test.go:22)`},
}

func TestErrorWithLocation(t *testing.T) {
Expand All @@ -20,15 +20,8 @@ func TestErrorWithLocation(t *testing.T) {
panic("no error")
}
tryWithLocation := NewErrorWithLocation(try.Error())
idx, idxCalc, ok := isSuffixOnly(tryWithLocation.Error(), tt.errMsgSuffix)
if !ok {
t.Errorf("errorsutil.NewErrorWithLocation(\"%s\"): mismatch want suffix (%s) got (%s), idx (%d) idxCalc (%d)", tryWithLocation.Error(), tt.errMsgSuffix, tryWithLocation.Error(), idx, idxCalc)
if !strings.HasSuffix(tryWithLocation.Error(), tt.errMsgSuffix) {
t.Errorf("errorsutil.NewErrorWithLocation(\"%s\"): mismatch want suffix [%s] got [%s]", try.Error(), tt.errMsgSuffix, tryWithLocation.Error())
}
}
}

func isSuffixOnly(s, substr string) (int, int, bool) {
idx := strings.Index(s, substr)
idxCalc := len(s) - len(substr) - 1
return idx, idxCalc, idx == idxCalc
}

0 comments on commit dc2b4ba

Please sign in to comment.