710f5b62 by astaxie

fix the test fun for pull request 873

1 parent dbf944ad
...@@ -381,18 +381,18 @@ func RenderForm(obj interface{}) template.HTML { ...@@ -381,18 +381,18 @@ func RenderForm(obj interface{}) template.HTML {
381 // renderFormField returns a string containing HTML of a single form field. 381 // renderFormField returns a string containing HTML of a single form field.
382 func renderFormField(label, name, fType string, value interface{}, id string, class string) string { 382 func renderFormField(label, name, fType string, value interface{}, id string, class string) string {
383 if id != "" { 383 if id != "" {
384 id = "id=\"" + id + "\"" 384 id = " id=\"" + id + "\""
385 } 385 }
386 386
387 if class != "" { 387 if class != "" {
388 class = "class=\"" + class + "\"" 388 class = " class=\"" + class + "\""
389 } 389 }
390 390
391 if isValidForInput(fType) { 391 if isValidForInput(fType) {
392 return fmt.Sprintf(`%v<input %v %v name="%v" type="%v" value="%v">`, label, id, class, name, fType, value) 392 return fmt.Sprintf(`%v<input%v%v name="%v" type="%v" value="%v">`, label, id, class, name, fType, value)
393 } 393 }
394 394
395 return fmt.Sprintf(`%v<%v %v %v name="%v">%v</%v>`, label, fType, id, class, name, value, fType) 395 return fmt.Sprintf(`%v<%v%v%v name="%v">%v</%v>`, label, fType, id, class, name, value, fType)
396 } 396 }
397 397
398 // isValidForInput checks if fType is a valid value for the `type` property of an HTML input element. 398 // isValidForInput checks if fType is a valid value for the `type` property of an HTML input element.
......
...@@ -175,12 +175,12 @@ func TestRenderForm(t *testing.T) { ...@@ -175,12 +175,12 @@ func TestRenderForm(t *testing.T) {
175 } 175 }
176 176
177 func TestRenderFormField(t *testing.T) { 177 func TestRenderFormField(t *testing.T) {
178 html := renderFormField("Label: ", "Name", "text", "Value") 178 html := renderFormField("Label: ", "Name", "text", "Value", "", "")
179 if html != `Label: <input name="Name" type="text" value="Value">` { 179 if html != `Label: <input name="Name" type="text" value="Value">` {
180 t.Errorf("Wrong html output for input[type=text]: %v ", html) 180 t.Errorf("Wrong html output for input[type=text]: %v ", html)
181 } 181 }
182 182
183 html = renderFormField("Label: ", "Name", "textarea", "Value") 183 html = renderFormField("Label: ", "Name", "textarea", "Value", "", "")
184 if html != `Label: <textarea name="Name">Value</textarea>` { 184 if html != `Label: <textarea name="Name">Value</textarea>` {
185 t.Errorf("Wrong html output for textarea: %v ", html) 185 t.Errorf("Wrong html output for textarea: %v ", html)
186 } 186 }
...@@ -192,33 +192,34 @@ func TestParseFormTag(t *testing.T) { ...@@ -192,33 +192,34 @@ func TestParseFormTag(t *testing.T) {
192 All int `form:"name,text,年龄:"` 192 All int `form:"name,text,年龄:"`
193 NoName int `form:",hidden,年龄:"` 193 NoName int `form:",hidden,年龄:"`
194 OnlyLabel int `form:",,年龄:"` 194 OnlyLabel int `form:",,年龄:"`
195 OnlyName int `form:"name"` 195 OnlyName int `form:"name" id:"name" class:"form-name"`
196 Ignored int `form:"-"` 196 Ignored int `form:"-"`
197 } 197 }
198 198
199 objT := reflect.TypeOf(&user{}).Elem() 199 objT := reflect.TypeOf(&user{}).Elem()
200 200
201 label, name, fType, ignored := parseFormTag(objT.Field(0)) 201 label, name, fType, id, class, ignored := parseFormTag(objT.Field(0))
202 if !(name == "name" && label == "年龄:" && fType == "text" && ignored == false) { 202 if !(name == "name" && label == "年龄:" && fType == "text" && ignored == false) {
203 t.Errorf("Form Tag with name, label and type was not correctly parsed.") 203 t.Errorf("Form Tag with name, label and type was not correctly parsed.")
204 } 204 }
205 205
206 label, name, fType, ignored = parseFormTag(objT.Field(1)) 206 label, name, fType, id, class, ignored = parseFormTag(objT.Field(1))
207 if !(name == "NoName" && label == "年龄:" && fType == "hidden" && ignored == false) { 207 if !(name == "NoName" && label == "年龄:" && fType == "hidden" && ignored == false) {
208 t.Errorf("Form Tag with label and type but without name was not correctly parsed.") 208 t.Errorf("Form Tag with label and type but without name was not correctly parsed.")
209 } 209 }
210 210
211 label, name, fType, ignored = parseFormTag(objT.Field(2)) 211 label, name, fType, id, class, ignored = parseFormTag(objT.Field(2))
212 if !(name == "OnlyLabel" && label == "年龄:" && fType == "text" && ignored == false) { 212 if !(name == "OnlyLabel" && label == "年龄:" && fType == "text" && ignored == false) {
213 t.Errorf("Form Tag containing only label was not correctly parsed.") 213 t.Errorf("Form Tag containing only label was not correctly parsed.")
214 } 214 }
215 215
216 label, name, fType, ignored = parseFormTag(objT.Field(3)) 216 label, name, fType, id, class, ignored = parseFormTag(objT.Field(3))
217 if !(name == "name" && label == "OnlyName: " && fType == "text" && ignored == false) { 217 if !(name == "name" && label == "OnlyName: " && fType == "text" && ignored == false &&
218 id == "name" && class == "form-name") {
218 t.Errorf("Form Tag containing only name was not correctly parsed.") 219 t.Errorf("Form Tag containing only name was not correctly parsed.")
219 } 220 }
220 221
221 label, name, fType, ignored = parseFormTag(objT.Field(4)) 222 label, name, fType, id, class, ignored = parseFormTag(objT.Field(4))
222 if ignored == false { 223 if ignored == false {
223 t.Errorf("Form Tag that should be ignored was not correctly parsed.") 224 t.Errorf("Form Tag that should be ignored was not correctly parsed.")
224 } 225 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!