This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
forked from kirves/go-form-it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms_test.go
159 lines (135 loc) · 4.5 KB
/
forms_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
package forms
import (
"github.com/APTrust/go-form-it/common"
"github.com/APTrust/go-form-it/fields"
"testing"
"time"
)
const (
style = formcommon.BOOTSTRAP
)
var (
txt, psw, btn fields.FieldInterface
)
func TestFieldRender(t *testing.T) {
field := fields.TextField("test")
field.AddClass("test").AddClass("class").SetId("testId").SetParam("param1", "val1").AddCss("css1", "val1").SetStyle(style).Disabled()
field.AddLabelClass("LABEL")
field.SetLabel("This is a label")
field.AddError("ERROR")
t.Log("Rendered field:", field.Render())
txt = field
}
func TestPasswordRender(t *testing.T) {
field := fields.PasswordField("test")
field.AddClass("test")
field.AddClass("class")
field.SetId("testId")
field.SetStyle(style)
field.SetValue("asd")
t.Log("Rendered field:", field.Render())
psw = field
}
func TestButtonRender(t *testing.T) {
field := fields.SubmitButton("btn", "Click me!")
field.SetStyle(style)
t.Log("Rendered button:", field.Render())
btn = field
}
func TestRadioButtonRender(t *testing.T) {
field := fields.RadioField("radio", []fields.InputChoice{
fields.InputChoice{Id: "choice1", Val: "value1"},
fields.InputChoice{Id: "choice2", Val: "value2"},
})
field.SetStyle(style)
t.Log("Rendered radio:", field.Render())
}
func TestSelectRender(t *testing.T) {
field := fields.SelectField("select", map[string][]fields.InputChoice{
"": []fields.InputChoice{
fields.InputChoice{"choice1", "value1"},
fields.InputChoice{"choice2", "value2"},
},
}).MultipleChoice().SetLabel("asd").AddSelected("choice1", "choice2")
field.SetStyle(style).SetValue("choice1")
t.Log("Rendered select:", field.Render())
}
func TestHiddenRender(t *testing.T) {
field := fields.HiddenField("hidden")
field.SetStyle(style)
t.Log("Rendered hidden:", field.Render())
}
func TestNumberRender(t *testing.T) {
field := fields.NumberField("number")
field.SetStyle(style)
t.Log("Rendered number:", field.Render())
}
func TestFormRender(t *testing.T) {
form := BootstrapForm(POST, "")
form.Elements(&FieldSetType{}, txt, psw, btn)
// form.AddField(psw)
// form.AddField(btn)
t.Log("Rendered form:", form.Render())
}
func TestFormFromSimpleModel(t *testing.T) {
type User struct {
Username string
Password1 string `form_widget:"password" form_label:"Password 1"`
Password2 string `form_widget:"password" form_label:"Password 2"`
SkipThis int `form_options:"skip"`
}
u := User{}
form := BaseFormFromModel(u, POST, "/action.html")
t.Log("Rendered form:", form.Render())
}
func TestFormFromModel(t *testing.T) {
type Model struct {
User string `form_label:"User label test"`
password string `form_widget:"password"`
Id int `form_min:"0" form_max:"5"`
Ts time.Time `form_min:"2013-04-22T15:00"`
RadioTest string `form_widget:"select" form_choices:"|A|Option A|G1|B|Option B" form_value:"A"`
BoolTest bool //`form_options:"checked"`
}
form := BaseFormFromModel(Model{"asd", "lol", 20, time.Now(), "B", false}, POST, "")
t.Log("Rendered form:", form.Render())
}
func TestBSFormFromModel(t *testing.T) {
type Model struct {
User string `form_label:"User label test"`
password string `form_widget:"password"`
Id int `form_min:"0" form_max:"5"`
Ts time.Time `form_min:"2013-04-22T15:00"`
RadioTest string `form_widget:"select" form_choices:"|A|Option A|G1|B|Option B" form_value:"A"`
BoolTest bool //`form_options:"checked"`
}
form := BootstrapFormFromModel(Model{"asd", "lol", 20, time.Now(), "B", false}, POST, "")
t.Log("Rendered form:", form.Render())
}
func TestInlineCreation(t *testing.T) {
form := BaseForm(POST, "/action.html").Elements(
fields.TextField("text_field").SetLabel("Username"),
FieldSet("psw_fieldset", "Passwords",
fields.PasswordField("psw1").AddClass("password_class").SetLabel("Password 1"),
fields.PasswordField("psw2").AddClass("password_class").SetLabel("Password 2"),
),
fields.SubmitButton("btn1", "Submit"),
)
t.Log("Rendered form:", form.Render())
}
func TestPizzaCreation(t *testing.T) {
type Ingredient struct {
IngredientId int `db_key:"true" db_autoincrement:"true"`
Name string `db_size:"30"`
}
type Pizza struct {
Id int `db_key:"true" db_autoincrement:"true"`
Name string `db_size:"30"`
// Ingredients []int
Price float32 `form_widget:"number" form_min:"0"`
// Transient
Ingrs []Ingredient `db_transient:"true" form_options:"skip"`
}
form := BootstrapFormFromModel(Pizza{Price: 2.2}, POST, "")
t.Log("Rendered form:", form.Render())
}