/
field_test.go
134 lines (101 loc) · 2.72 KB
/
field_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
package forms
import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
// Testing the output of a text field
func TestFieldOutputText(t *testing.T) {
field := NewField()
field.Name = "foo"
field.Type = "text"
tag := field.output()
assert.Equal(t, "<input type=\"text\" name=\"foo\" id=\"foo\" value=\"\" />", tag)
}
// Testing the output of a checkbox/radio
func TestFieldOutputBox(t *testing.T) {
field := NewField()
field.Name = "foo"
field.Type = "checkbox"
tag := field.output()
assert.Equal(t, "<input type=\"checkbox\" name=\"foo\" id=\"foo\" value=\"\" />", tag)
}
// Testing the output of a textarea field
func TestFieldOutputTextArea(t *testing.T) {
field := NewField()
field.Name = "foo"
field.Type = "textarea"
tag := field.output()
assert.Equal(t, "<textarea name=\"foo\" id=\"foo\"></textarea>", tag)
}
// Testing the output of a select field
func TestFieldOutputSelect(t *testing.T) {
field := NewField()
field.Name = "foo"
field.Type = "select"
field.Values = []string{"test"}
tag := field.output()
assert.Equal(t, "<select name=\"foo\" id=\"foo\"><option value=\"test\">test</option></select>", tag)
}
// Testing the output of a label
func TestFieldOutputLabel(t *testing.T) {
field := NewField()
field.Name = "foo"
field.Label = "Foo"
assert.Equal(t, "<label for=\"foo\">Foo</label>", field.label())
}
// Testing the add error method
func TestFieldAddError(t *testing.T) {
field := NewField()
field.addError("A test error")
assert.Equal(t, 1, len(field.Errors))
}
// Testing validation on fields
func TestFieldValidateRequired(t *testing.T) {
req := &Required{
Err: "This field is required",
}
field := NewField()
field.Name = "test"
field.Value = ""
field.Type = "text"
field.addValidation(req)
field.validate()
assert.Equal(t, 1, len(field.Errors))
}
func TestFieldValidateLength(t *testing.T) {
req := &LengthValidation{
Err: "This field should be 5 characters",
Len: 5,
}
field := NewField()
field.Name = "test"
field.Value = ""
field.Type = "text"
field.addValidation(req)
field.validate()
assert.Equal(t, 1, len(field.Errors))
}
func TestFieldValidationRequired(t *testing.T) {
req := &RegexValidation{
Err: "An error",
Regex: "^([A-Z])$",
}
field := NewField()
field.Name = "test"
field.Value = "test"
field.addValidation(req)
field.validate()
assert.Equal(t, 1, len(field.Errors))
}
// Testing class output
func TestAddingHtmlProperties(t *testing.T) {
m := make(map[string]string)
m["class"] = "testclass"
m["data-id"] = "3"
field := NewField()
field.Props = m
prop := field.getHtmlProperties()
assert.Equal(t, true, strings.ContainsAny(prop, "class=\"testclass\""))
assert.Equal(t, true, strings.ContainsAny(prop, "data-id=\"3\""))
}