1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 09:50:55 +00:00

fix the test fun for pull request 873

This commit is contained in:
astaxie 2014-10-20 22:23:29 +08:00
parent dbf944adce
commit 710f5b6234
2 changed files with 14 additions and 13 deletions

View File

@ -381,18 +381,18 @@ func RenderForm(obj interface{}) template.HTML {
// renderFormField returns a string containing HTML of a single form field. // renderFormField returns a string containing HTML of a single form field.
func renderFormField(label, name, fType string, value interface{}, id string, class string) string { func renderFormField(label, name, fType string, value interface{}, id string, class string) string {
if id != "" { if id != "" {
id = "id=\"" + id + "\"" id = " id=\"" + id + "\""
} }
if class != "" { if class != "" {
class = "class=\"" + class + "\"" class = " class=\"" + class + "\""
} }
if isValidForInput(fType) { if isValidForInput(fType) {
return fmt.Sprintf(`%v<input %v %v name="%v" type="%v" value="%v">`, label, id, class, name, fType, value) return fmt.Sprintf(`%v<input%v%v name="%v" type="%v" value="%v">`, label, id, class, name, fType, value)
} }
return fmt.Sprintf(`%v<%v %v %v name="%v">%v</%v>`, label, fType, id, class, name, value, fType) return fmt.Sprintf(`%v<%v%v%v name="%v">%v</%v>`, label, fType, id, class, name, value, fType)
} }
// isValidForInput checks if fType is a valid value for the `type` property of an HTML input element. // isValidForInput checks if fType is a valid value for the `type` property of an HTML input element.

View File

@ -175,12 +175,12 @@ func TestRenderForm(t *testing.T) {
} }
func TestRenderFormField(t *testing.T) { func TestRenderFormField(t *testing.T) {
html := renderFormField("Label: ", "Name", "text", "Value") html := renderFormField("Label: ", "Name", "text", "Value", "", "")
if html != `Label: <input name="Name" type="text" value="Value">` { if html != `Label: <input name="Name" type="text" value="Value">` {
t.Errorf("Wrong html output for input[type=text]: %v ", html) t.Errorf("Wrong html output for input[type=text]: %v ", html)
} }
html = renderFormField("Label: ", "Name", "textarea", "Value") html = renderFormField("Label: ", "Name", "textarea", "Value", "", "")
if html != `Label: <textarea name="Name">Value</textarea>` { if html != `Label: <textarea name="Name">Value</textarea>` {
t.Errorf("Wrong html output for textarea: %v ", html) t.Errorf("Wrong html output for textarea: %v ", html)
} }
@ -192,33 +192,34 @@ func TestParseFormTag(t *testing.T) {
All int `form:"name,text,年龄:"` All int `form:"name,text,年龄:"`
NoName int `form:",hidden,年龄:"` NoName int `form:",hidden,年龄:"`
OnlyLabel int `form:",,年龄:"` OnlyLabel int `form:",,年龄:"`
OnlyName int `form:"name"` OnlyName int `form:"name" id:"name" class:"form-name"`
Ignored int `form:"-"` Ignored int `form:"-"`
} }
objT := reflect.TypeOf(&user{}).Elem() objT := reflect.TypeOf(&user{}).Elem()
label, name, fType, ignored := parseFormTag(objT.Field(0)) label, name, fType, id, class, ignored := parseFormTag(objT.Field(0))
if !(name == "name" && label == "年龄:" && fType == "text" && ignored == false) { if !(name == "name" && label == "年龄:" && fType == "text" && ignored == false) {
t.Errorf("Form Tag with name, label and type was not correctly parsed.") t.Errorf("Form Tag with name, label and type was not correctly parsed.")
} }
label, name, fType, ignored = parseFormTag(objT.Field(1)) label, name, fType, id, class, ignored = parseFormTag(objT.Field(1))
if !(name == "NoName" && label == "年龄:" && fType == "hidden" && ignored == false) { if !(name == "NoName" && label == "年龄:" && fType == "hidden" && ignored == false) {
t.Errorf("Form Tag with label and type but without name was not correctly parsed.") t.Errorf("Form Tag with label and type but without name was not correctly parsed.")
} }
label, name, fType, ignored = parseFormTag(objT.Field(2)) label, name, fType, id, class, ignored = parseFormTag(objT.Field(2))
if !(name == "OnlyLabel" && label == "年龄:" && fType == "text" && ignored == false) { if !(name == "OnlyLabel" && label == "年龄:" && fType == "text" && ignored == false) {
t.Errorf("Form Tag containing only label was not correctly parsed.") t.Errorf("Form Tag containing only label was not correctly parsed.")
} }
label, name, fType, ignored = parseFormTag(objT.Field(3)) label, name, fType, id, class, ignored = parseFormTag(objT.Field(3))
if !(name == "name" && label == "OnlyName: " && fType == "text" && ignored == false) { if !(name == "name" && label == "OnlyName: " && fType == "text" && ignored == false &&
id == "name" && class == "form-name") {
t.Errorf("Form Tag containing only name was not correctly parsed.") t.Errorf("Form Tag containing only name was not correctly parsed.")
} }
label, name, fType, ignored = parseFormTag(objT.Field(4)) label, name, fType, id, class, ignored = parseFormTag(objT.Field(4))
if ignored == false { if ignored == false {
t.Errorf("Form Tag that should be ignored was not correctly parsed.") t.Errorf("Form Tag that should be ignored was not correctly parsed.")
} }