...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package validate
16
17 import (
18 "errors"
19 "testing"
20
21 "github.com/stretchr/testify/assert"
22 "github.com/stretchr/testify/require"
23 )
24
25 func TestHelpers_addPointerError(t *testing.T) {
26 res := new(Result)
27 r := errorHelp.addPointerError(res, errors.New("my error"), "my ref", "path")
28 require.NotEmpty(t, r.Errors)
29 msg := r.Errors[0].Error()
30 assert.Contains(t, msg, "could not resolve reference in path to $ref my ref: my error")
31 }
32
33 func integerFactory(base int) []interface{} {
34 return []interface{}{
35 base,
36 int8(base),
37 int16(base),
38 int32(base),
39 int64(base),
40 uint(base),
41 uint8(base),
42 uint16(base),
43 uint32(base),
44 uint64(base),
45 float32(base),
46 float64(base),
47 }
48 }
49
50
51 func TestHelpers_asInt64(t *testing.T) {
52 for _, v := range integerFactory(3) {
53 assert.Equal(t, int64(3), valueHelp.asInt64(v))
54 }
55
56
57 if assert.NotPanics(t, func() {
58 valueHelp.asInt64("123")
59 }) {
60 assert.Equal(t, int64(0), valueHelp.asInt64("123"))
61 }
62 }
63
64
65 func TestHelpers_asUint64(t *testing.T) {
66 for _, v := range integerFactory(3) {
67 assert.Equal(t, uint64(3), valueHelp.asUint64(v))
68 }
69
70
71 if assert.NotPanics(t, func() {
72 valueHelp.asUint64("123")
73 }) {
74 assert.Equal(t, uint64(0), valueHelp.asUint64("123"))
75 }
76 }
77
78
79 func TestHelpers_asFloat64(t *testing.T) {
80 const epsilon = 1e-9
81
82 for _, v := range integerFactory(3) {
83 assert.InDelta(t, float64(3), valueHelp.asFloat64(v), epsilon)
84 }
85
86
87 if assert.NotPanics(t, func() {
88 valueHelp.asFloat64("123")
89 }) {
90 assert.InDelta(t, float64(0), valueHelp.asFloat64("123"), epsilon)
91 }
92 }
93
View as plain text