Passed
Push — main ( 2a46cd...12059c )
by Igor
01:41
created

test.mockValidatableStruct.Validate   A

Complexity

Conditions 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
dl 0
loc 20
c 0
b 0
f 0
rs 9.55
nop 1
1
package test
2
3
import (
4
	"github.com/muonsoft/validation"
5
	"github.com/muonsoft/validation/it"
6
	"golang.org/x/text/language"
7
8
	"time"
9
)
10
11
var (
12
	nilBool    *bool
13
	nilInt     *int64
14
	nilUint    *uint64
15
	nilFloat   *float64
16
	nilString  *string
17
	nilTime    *time.Time
18
	emptyArray [0]string
19
	emptySlice []string
20
	emptyMap   map[string]string
21
	emptyTime  time.Time
22
)
23
24
func boolValue(b bool) *bool {
25
	return &b
26
}
27
28
func intValue(i int64) *int64 {
29
	return &i
30
}
31
32
func uintValue(u uint64) *uint64 {
33
	return &u
34
}
35
36
func floatValue(f float64) *float64 {
37
	return &f
38
}
39
40
func stringValue(s string) *string {
41
	return &s
42
}
43
44
func timeValue(t time.Time) *time.Time {
45
	return &t
46
}
47
48
type mockViolation struct {
49
	err             string
50
	code            string
51
	message         string
52
	messageTemplate string
53
	parameters      map[string]string
54
	propertyPath    validation.PropertyPath
55
}
56
57
func (mock *mockViolation) Error() string {
58
	return mock.err
59
}
60
61
func (mock *mockViolation) Code() string {
62
	return mock.code
63
}
64
65
func (mock *mockViolation) Message() string {
66
	return mock.message
67
}
68
69
func (mock *mockViolation) MessageTemplate() string {
70
	return mock.messageTemplate
71
}
72
73
func (mock *mockViolation) Parameters() map[string]string {
74
	return mock.parameters
75
}
76
77
func (mock *mockViolation) PropertyPath() validation.PropertyPath {
78
	return mock.propertyPath
79
}
80
81
func mockNewViolationFunc() validation.ViolationFactory {
82
	return validation.NewViolationFunc(func(
83
		code, messageTemplate string,
84
		pluralCount int,
85
		parameters map[string]string,
86
		propertyPath validation.PropertyPath,
87
		lang language.Tag,
88
	) validation.Violation {
89
		return &mockViolation{
90
			code:            code,
91
			messageTemplate: messageTemplate,
92
			parameters:      parameters,
93
			propertyPath:    propertyPath,
94
		}
95
	})
96
}
97
98
type mockValidatableString struct {
99
	value string
100
}
101
102
func (mock mockValidatableString) Validate(validator *validation.Validator) error {
103
	return validator.Validate(
104
		validation.String(
105
			&mock.value,
106
			validation.PropertyName("value"),
107
			it.IsNotBlank(),
108
		),
109
	)
110
}
111
112
type mockValidatableStruct struct {
113
	intValue    int64
114
	floatValue  float64
115
	stringValue string
116
	structValue mockValidatableString
117
}
118
119
func (mock mockValidatableStruct) Validate(validator *validation.Validator) error {
120
	return validator.Validate(
121
		validation.Number(
122
			mock.intValue,
123
			validation.PropertyName("intValue"),
124
			it.IsNotBlank(),
125
		),
126
		validation.Number(
127
			mock.floatValue,
128
			validation.PropertyName("floatValue"),
129
			it.IsNotBlank(),
130
		),
131
		validation.String(
132
			&mock.stringValue,
133
			validation.PropertyName("stringValue"),
134
			it.IsNotBlank(),
135
		),
136
		validation.Value(
137
			&mock.structValue,
138
			validation.PropertyName("structValue"),
139
		),
140
	)
141
}
142