...
1 package errors
2
3 import (
4 "os"
5 "testing"
6
7 "github.com/go-playground/validator/v10"
8 "github.com/stretchr/testify/assert"
9 )
10
11 func TestMain(m *testing.M) {
12 os.Exit(m.Run())
13 }
14
15 func TestNewFieldErrorDetail(t *testing.T) {
16 testCases := map[string]struct {
17 err validator.FieldError
18 expected string
19 }{
20 "Required": {
21 err: &testFieldError{
22 field: "example",
23 tag: "required",
24 },
25 expected: NewRequiredFieldMessage("example"),
26 },
27 "HostnameRFC1123": {
28 err: &testFieldError{
29 field: "example",
30 tag: "hostname_rfc1123",
31 },
32 expected: NewInvalidRFC1123HostnameMessage("example"),
33 },
34 }
35
36 for name, tc := range testCases {
37 t.Run(name, func(t *testing.T) {
38 msg := NewFieldErrorDetail(tc.err)
39 assert.Equal(t, tc.expected, msg)
40 })
41 }
42 }
43
44 type testFieldError struct {
45 validator.FieldError
46
47 tag string
48 field string
49 }
50
51 func (fe *testFieldError) Tag() string {
52 return fe.tag
53 }
54
55 func (fe *testFieldError) Field() string {
56 return fe.field
57 }
58
View as plain text