forked from thedevsaddam/govalidator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
100 lines (89 loc) · 1.95 KB
/
utils_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
package govalidator
import (
"reflect"
"strings"
"testing"
)
func Test_isContainRequiredField(t *testing.T) {
if !isContainRequiredField([]string{"required", "email"}) {
t.Error("isContainRequiredField failed!")
}
if isContainRequiredField([]string{"numeric", "min:5"}) {
t.Error("isContainRequiredField failed!")
}
}
func Benchmark_isContainRequiredField(b *testing.B) {
for n := 0; n < b.N; n++ {
isContainRequiredField([]string{"required", "email"})
}
}
type person struct{}
func (person) Details() string {
return "John Doe"
}
func (person) Age(age string) string {
return "Age: " + age
}
func Test_isRuleExist(t *testing.T) {
if !isRuleExist("required") {
t.Error("isRuleExist failed for valid rule")
}
if isRuleExist("not exist") {
t.Error("isRuleExist failed for invalid rule")
}
if !isRuleExist("mime") {
t.Error("extended rules failed")
}
}
func Test_toString(t *testing.T) {
Int := 100
str := toString(Int)
typ := reflect.ValueOf(str).Kind()
if typ != reflect.String {
t.Error("toString failed!")
}
}
func Test_isEmpty(t *testing.T) {
var Int int
var Int8 int
var Float32 float32
var Str string
var Slice []int
var e interface{}
list := map[string]interface{}{
"_int": Int,
"_int8": Int8,
"_float32": Float32,
"_str": Str,
"_slice": Slice,
"_empty_interface": e,
}
for k, v := range list {
if !isEmpty(v) {
t.Errorf("%v failed", k)
}
}
}
func Test_getFileInfo(t *testing.T) {
req, err := buildMocFormReq()
if err != nil {
t.Error("request failed", err)
}
fExist, fn, ext, mime, size, _ := getFileInfo(req, "file")
if !fExist {
t.Error("file does not exist")
}
if fn != "BENCHMARK.md" {
t.Error("failed to get file name")
}
if ext != "md" {
t.Error("failed to get file extension")
}
if !strings.Contains(mime, "text/plain") {
t.Log(mime)
t.Error("failed to get file mime")
}
if size <= 0 {
t.Error("failed to get file size")
}
}