Skip to content

Commit

Permalink
Add unit tests for custom errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alesr committed Feb 9, 2025
1 parent 876aff9 commit 7508b52
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type ErrTemplateNotFound struct {
}

func (e ErrTemplateNotFound) Error() string {
return fmt.Sprintf("template %s not found", e.Name)
return fmt.Sprintf("template '%s' not found", e.Name)
}

// ErrTemplateExecution is returned when a template fails to execute.
Expand All @@ -18,7 +18,7 @@ type ErrTemplateExecution struct {
}

func (e ErrTemplateExecution) Error() string {
return fmt.Sprintf("failed to execute template %s: %v", e.Name, e.Err)
return fmt.Sprintf("failed to execute template '%s': '%v'", e.Name, e.Err)
}

func (e ErrTemplateExecution) Unwrap() error {
Expand Down
29 changes: 29 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package templator

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestErrTemplateNotFound_Error(t *testing.T) {
t.Parallel()

e := ErrTemplateNotFound{Name: "foo"}

got := e.Error()
assert.Equal(t, "template 'foo' not found", got)
}

func TestErrTemplateExecution_Error(t *testing.T) {
t.Parallel()

e := ErrTemplateExecution{
Name: "foo",
Err: errors.New("bar"),
}

got := e.Error()
assert.Equal(t, "failed to execute template 'foo': 'bar'", got)
}
2 changes: 2 additions & 0 deletions template_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func Test_validateField(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

err := validateField(tc.typ, tc.fieldPath)

if tc.wantErr {
Expand Down

0 comments on commit 7508b52

Please sign in to comment.