-
Notifications
You must be signed in to change notification settings - Fork 217
/
validator_test.go
164 lines (134 loc) · 5.64 KB
/
validator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package eos
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type ruleTestCase struct {
name string
value interface{}
expectedError string
}
func TestEOSBlockNumRule(t *testing.T) {
tag := "eos_block_num"
validator := func(field string, value interface{}) error {
return EOSBlockNumRule(field, tag, "", value)
}
tests := []ruleTestCase{
{"should be a string", true, "The test field must be a string"},
{"should not contains invalid characters", "!", "The test field must be a valid EOS block num"},
{"valid block num", "10", ""},
}
runRuleTestCases(t, tag, tests, validator)
}
func TestEOSNameRule(t *testing.T) {
tag := "eos_name"
validator := func(field string, value interface{}) error {
return EOSNameRule(field, tag, "", value)
}
tests := []ruleTestCase{
{"should be a string", true, "The test field is not a known type for an EOS name"},
{"should not contains invalid characters", "6", "The test field must be a valid EOS name"},
{"should not be longer than 13", "abcdefghigklma", "The test field must be a valid EOS name"},
{"valid empty", "", ""},
{"valid single", "e", ""},
{"valid limit", "5", ""},
{"valid with dots and 13 chars", "eosio.tokenfl", ""},
{"valid eos.Name", Name("eosio"), ""},
{"valid eos.PermissionName", PermissionName("eosio"), ""},
{"valid eos.ActionName", ActionName("eosio"), ""},
{"valid eos.AccountName", AccountName("eosio"), ""},
{"valid eos.TableName", TableName("eosio"), ""},
}
runRuleTestCases(t, tag, tests, validator)
}
func TestEOSExtendedNameRule(t *testing.T) {
tag := "eos_extended_name"
validator := func(field string, value interface{}) error {
return EOSExtendedNameRule(field, tag, "", value)
}
tests := []ruleTestCase{
{"should be a string", true, "The test field is not a known type for an EOS name"},
{"should not contains invalid characters", "6", "The test field must be a valid EOS name"},
{"should not be longer than 13", "abcdefghigklma", "The test field must be a valid EOS name"},
{"valid empty", "", ""},
{"valid single", "e", ""},
{"valid limit", "5", ""},
{"valid with dots and 13 chars", "eosio.tokenfl", ""},
{"valid with whem symbol", "4,EOS", ""},
{"valid with whem symbol code", "EOS", ""},
{"valid eos.Name", Name("eosio"), ""},
{"valid eos.PermissionName", PermissionName("eosio"), ""},
{"valid eos.ActionName", ActionName("eosio"), ""},
{"valid eos.AccountName", AccountName("eosio"), ""},
{"valid eos.TableName", TableName("eosio"), ""},
}
runRuleTestCases(t, tag, tests, validator)
}
func TestEOSNamesListRule(t *testing.T) {
tag := "eos_names_list"
rule := EOSNamesListRuleFactory("|", 2)
validator := func(field string, value interface{}) error {
return rule(field, tag, "", value)
}
tests := []ruleTestCase{
{"should be a string", true, "The test field must be a string"},
{"should have at least 1 element", "", "The test field must have at least 1 element"},
{"should have at max macCount element", "eos|eos|eos", "The test field must have at most 2 elements"},
{"should fail on single error", "6", "The test[0] field must be a valid EOS name"},
{"should fail if any element error", "ab|6", "The test[1] field must be a valid EOS name"},
{"valid single", "ab", ""},
{"valid multiple", "ded|eos", ""},
}
runRuleTestCases(t, tag, tests, validator)
}
func TestEOSExtendedNamesListRule(t *testing.T) {
tag := "eos_extended_names_list"
rule := EOSExtendedNamesListRuleFactory("|", 3)
validator := func(field string, value interface{}) error {
return rule(field, tag, "", value)
}
tests := []ruleTestCase{
{"should be a string", true, "The test field must be a string"},
{"should have at least 1 element", "", "The test field must have at least 1 element"},
{"should have at max macCount element", "eos|eos|eos|eos", "The test field must have at most 3 elements"},
{"should fail on single error", "6", "The test[0] field must be a valid EOS name"},
{"should fail if any element error", "ab|6", "The test[1] field must be a valid EOS name"},
{"valid single", "ab", ""},
{"valid multiple", "ded|eos", ""},
{"valid multiple symbol", "ded|eos|EOS", ""},
{"valid multiple symbol code", "ded|4,EOS", ""},
{"valid multiple mixed", "ded|EOS|4,EOS", ""},
}
runRuleTestCases(t, tag, tests, validator)
}
func TestEOSTrxIDRule(t *testing.T) {
tag := "eos_trx_id"
validator := func(field string, value interface{}) error {
return EOSTrxIDRule(field, tag, "", value)
}
tests := []ruleTestCase{
{"should be a string", true, "The test field must be a string"},
{"should contains something", "", "The test field must be a valid hexadecimal"},
{"should contains a least two characters", "a", "The test field must be a valid hexadecimal"},
{"should not contains invalid characters", "az", "The test field must be a valid hexadecimal"},
{"should be a multple of 2", "ab01020", "The test field must be a valid hexadecimal"},
{"should be long enough", "d8fe02221408fbcc221d1207c1b8cc67e0d9b3ca1c6005a36ea10428dd7fd1", "The test field must have exactly 64 characters"},
{"valid", "d8fe02221408fbcc221d1207c1b8cc67e0d9b3ca1c6005a36ea10428dd7fd148", ""},
{"valid", "D8FE02221408FBCC221D1207C1B8CC67E0D9B3CA1C6005A36EA10428DD7FD148", ""},
}
runRuleTestCases(t, tag, tests, validator)
}
func runRuleTestCases(t *testing.T, tag string, tests []ruleTestCase, validator func(field string, value interface{}) error) {
for _, test := range tests {
t.Run(fmt.Sprintf("%s_%s", tag, test.name), func(t *testing.T) {
err := validator("test", test.value)
if test.expectedError == "" {
assert.NoError(t, err)
} else {
assert.Equal(t, errors.New(test.expectedError), err)
}
})
}
}