Skip to content

Commit 7508b52

Browse files
committed
Add unit tests for custom errors
1 parent 876aff9 commit 7508b52

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type ErrTemplateNotFound struct {
88
}
99

1010
func (e ErrTemplateNotFound) Error() string {
11-
return fmt.Sprintf("template %s not found", e.Name)
11+
return fmt.Sprintf("template '%s' not found", e.Name)
1212
}
1313

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

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

2424
func (e ErrTemplateExecution) Unwrap() error {

errors_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package templator
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestErrTemplateNotFound_Error(t *testing.T) {
11+
t.Parallel()
12+
13+
e := ErrTemplateNotFound{Name: "foo"}
14+
15+
got := e.Error()
16+
assert.Equal(t, "template 'foo' not found", got)
17+
}
18+
19+
func TestErrTemplateExecution_Error(t *testing.T) {
20+
t.Parallel()
21+
22+
e := ErrTemplateExecution{
23+
Name: "foo",
24+
Err: errors.New("bar"),
25+
}
26+
27+
got := e.Error()
28+
assert.Equal(t, "failed to execute template 'foo': 'bar'", got)
29+
}

template_validator_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ func Test_validateField(t *testing.T) {
9292

9393
for _, tc := range testCases {
9494
t.Run(tc.name, func(t *testing.T) {
95+
t.Parallel()
96+
9597
err := validateField(tc.typ, tc.fieldPath)
9698

9799
if tc.wantErr {

0 commit comments

Comments
 (0)