-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_input_gen_test.go
112 lines (105 loc) · 4.21 KB
/
html_input_gen_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
package structvalidator
import (
"testing"
)
func TestGenerateHTML(t *testing.T) {
s := &Test1{}
fieldsHTMLInputs := GenerateHTML(s, &HTMLOptions{
RestrictFields: map[string]bool{
"FirstName": true,
"Age": true,
"PostCode": true,
"Email": true,
"Country": true,
"County": true,
},
IDPrefix: "id_",
NamePrefix: "name_",
})
if fieldsHTMLInputs["FirstName"] != `<input type="text" name="name_FirstName" id="id_FirstName" required minlength="5" maxlength="25"/>` {
t.Fatal("GenerateHTML failed to output HTML for 'FirstName' field")
}
if fieldsHTMLInputs["Age"] != `<input type="number" name="name_Age" id="id_Age" required min="18" max="150"/>` {
t.Fatal("GenerateHTML failed to output HTML for 'Age' field")
}
if fieldsHTMLInputs["PostCode"] != `<input type="text" name="name_PostCode" id="id_PostCode" required pattern="^[0-9][0-9]-[0-9][0-9][0-9]$"/>` {
t.Fatal("GenerateHTML failed to output HTML for 'PostCode' field")
}
if fieldsHTMLInputs["Email"] != `<input type="email" name="name_Email" id="id_Email" required/>` {
t.Fatal("GenerateHTML failed to output HTML for 'Email' field")
}
if fieldsHTMLInputs["Country"] != `<input type="text" name="name_Country" id="id_Country" pattern="^[A-Z][A-Z]$"/>` {
t.Fatal("GenerateHTML failed to output HTML for 'Country' field")
}
if fieldsHTMLInputs["County"] != `<textarea name="name_County" id="id_County" maxlength="40"></textarea>` {
t.Fatal("GenerateHTML failed to output HTML for 'County' field")
}
}
func TestGenerateHTMLWithValues(t *testing.T) {
s := &Test1{}
fieldsHTMLInputs := GenerateHTML(s, &HTMLOptions{
RestrictFields: map[string]bool{
"FirstName": true,
"Age": true,
"PostCode": true,
"Email": true,
"Country": true,
"County": true,
},
IDPrefix: "id_",
NamePrefix: "name_",
OverwriteValues: map[string]string{
"FirstName": `Joe "Joe"`,
"Age": "40",
"Email": "[email protected]",
"Country": "XX",
},
})
if fieldsHTMLInputs["FirstName"] != `<input type="text" name="name_FirstName" id="id_FirstName" required minlength="5" maxlength="25" value="Joe "Joe""/>` {
t.Fatal("GenerateHTML failed to output HTML for 'FirstName' field")
}
if fieldsHTMLInputs["Age"] != `<input type="number" name="name_Age" id="id_Age" required min="18" max="150" value="40"/>` {
t.Fatal("GenerateHTML failed to output HTML for 'Age' field")
}
if fieldsHTMLInputs["PostCode"] != `<input type="text" name="name_PostCode" id="id_PostCode" required pattern="^[0-9][0-9]-[0-9][0-9][0-9]$"/>` {
t.Fatal("GenerateHTML failed to output HTML for 'PostCode' field")
}
if fieldsHTMLInputs["Email"] != `<input type="email" name="name_Email" id="id_Email" required value="[email protected]"/>` {
t.Fatal("GenerateHTML failed to output HTML for 'Email' field")
}
if fieldsHTMLInputs["Country"] != `<input type="text" name="name_Country" id="id_Country" pattern="^[A-Z][A-Z]$" value="XX"/>` {
t.Fatal("GenerateHTML failed to output HTML for 'Country' field")
}
if fieldsHTMLInputs["County"] != `<textarea name="name_County" id="id_County" maxlength="40"></textarea>` {
t.Fatal("GenerateHTML failed to output HTML for 'County' field")
}
}
func TestGenerateHTMLWithFieldValues(t *testing.T) {
s := &Test1{
FirstName: "Joe",
Age: 60,
Email: "[email protected]",
}
fieldsHTMLInputs := GenerateHTML(s, &HTMLOptions{
RestrictFields: map[string]bool{
"FirstName": true,
"Age": true,
"Email": true,
},
IDPrefix: "id_",
NamePrefix: "name_",
OverwriteValues: map[string]string{
"FirstName": `Joe "Joe"`,
},
FieldValues: true,
})
if fieldsHTMLInputs["FirstName"] != `<input type="text" name="name_FirstName" id="id_FirstName" required minlength="5" maxlength="25" value="Joe "Joe""/>` {
t.Fatal("GenerateHTML failed to output HTML for 'FirstName' field")
}
if fieldsHTMLInputs["Age"] != `<input type="number" name="name_Age" id="id_Age" required min="18" max="150" value="60"/>` {
t.Fatal("GenerateHTML failed to output HTML for 'Age' field")
}
if fieldsHTMLInputs["Email"] != `<input type="email" name="name_Email" id="id_Email" required value="[email protected]"/>` {
t.Fatal("GenerateHTML failed to output HTML for 'Email' field")
}
}