-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidator_test.go
More file actions
92 lines (79 loc) · 2.14 KB
/
validator_test.go
File metadata and controls
92 lines (79 loc) · 2.14 KB
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
package validator
import (
"gopkg.in/go-playground/validator.v9"
"testing"
)
func TestLcFirst(t *testing.T) {
var tests = []struct {
input string
expected string
}{
{"Roshan", "roshan"},
{"Rana", "rana"},
{"Test", "test"},
}
for _, test := range tests {
if output := LcFirst(test.input); output != test.expected {
t.Error("Test Failed: {} inputted, {} expected, received: {}", test.input, test.expected, output)
}
}
}
func TestUcFirst(t *testing.T) {
var tests = []struct {
input string
expected string
}{
{"roshan", "Roshan"},
{"rana", "Rana"},
{"test", "Test"},
}
for _, test := range tests {
if output := UcFirst(test.input); output != test.expected {
t.Error("Test Failed: {} inputted, {} expected, received: {}", test.input, test.expected, output)
}
}
}
func TestSplit(t *testing.T) {
var tests = []struct {
input string
expected string
}{
{"myName", "My name"},
{"testDriven", "Test driven"},
{"myOutput", "My output"},
}
for _, test := range tests {
if output := Split(test.input); output != test.expected {
t.Error("Test Failed: {} inputted, {} expected, received: {}", test.input, test.expected, output)
}
}
}
type FirstNameValidationTestStruct struct {
FirstName string `validate:"required" json:"firstName"`
}
type ThisTestVlaidationStruct struct {
ThisTest string `validate:"required" json:"thisTest"`
}
func TestValidationErrorToText(t *testing.T) {
extraValidator := []ExtraValidation{
{Tag: "required", Message: "%s is passed!"},
}
MakeExtraValidation(extraValidator)
validates := validator.New()
err := validates.Struct(FirstNameValidationTestStruct{})
error1 := err.(validator.ValidationErrors)
err2 := validates.Struct(ThisTestVlaidationStruct{})
error2 := err2.(validator.ValidationErrors)
var tests = []struct {
input validator.ValidationErrors
expected string
}{
{error1, "First name is passed!"},
{error2, "This test is passed!"},
}
for _, test := range tests {
if output := ValidationErrorToText(test.input[0]); output != test.expected {
t.Error("Test Failed: {} inputted, {} expected, received: {}", test.input, test.expected, output)
}
}
}