1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-30 00:11:28 +00:00
Beego/templatefunc_test.go

324 lines
8.7 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-07-25 14:25:12 +00:00
package beego
import (
2013-08-10 08:33:46 +00:00
"html/template"
2013-07-25 14:25:12 +00:00
"net/url"
2014-07-03 15:40:21 +00:00
"reflect"
2013-07-25 14:25:12 +00:00
"testing"
2013-07-26 08:29:00 +00:00
"time"
2013-07-25 14:25:12 +00:00
)
2013-07-26 08:29:00 +00:00
func TestSubstr(t *testing.T) {
s := `012345`
if Substr(s, 0, 2) != "01" {
t.Error("should be equal")
}
if Substr(s, 0, 100) != "012345" {
t.Error("should be equal")
}
if Substr(s, 12, 100) != "012345" {
t.Error("should be equal")
}
2013-07-26 08:29:00 +00:00
}
func TestHtml2str(t *testing.T) {
h := `<HTML><style></style><script>x<x</script></HTML><123> 123\n
\n`
if Html2str(h) != "123\\n\n\\n" {
t.Error("should be equal")
}
}
func TestDateFormat(t *testing.T) {
ts := "Mon, 01 Jul 2013 13:27:42 CST"
tt, _ := time.Parse(time.RFC1123, ts)
2014-05-31 06:28:44 +00:00
if ss := DateFormat(tt, "2006-01-02 15:04:05"); ss != "2013-07-01 13:27:42" {
t.Errorf("2013-07-01 13:27:42 does not equal %v", ss)
2013-07-26 08:29:00 +00:00
}
}
func TestDate(t *testing.T) {
ts := "Mon, 01 Jul 2013 13:27:42 CST"
tt, _ := time.Parse(time.RFC1123, ts)
2014-05-31 06:28:44 +00:00
if ss := Date(tt, "Y-m-d H:i:s"); ss != "2013-07-01 13:27:42" {
t.Errorf("2013-07-01 13:27:42 does not equal %v", ss)
2013-07-26 08:29:00 +00:00
}
2014-05-31 06:28:44 +00:00
if ss := Date(tt, "y-n-j h:i:s A"); ss != "13-7-1 01:27:42 PM" {
t.Errorf("13-7-1 01:27:42 PM does not equal %v", ss)
2013-07-26 08:29:00 +00:00
}
2014-05-31 06:28:44 +00:00
if ss := Date(tt, "D, d M Y g:i:s a"); ss != "Mon, 01 Jul 2013 1:27:42 pm" {
t.Errorf("Mon, 01 Jul 2013 1:27:42 pm does not equal %v", ss)
2013-07-26 08:29:00 +00:00
}
2014-05-31 06:28:44 +00:00
if ss := Date(tt, "l, d F Y G:i:s"); ss != "Monday, 01 July 2013 13:27:42" {
t.Errorf("Monday, 01 July 2013 13:27:42 does not equal %v", ss)
2013-07-26 08:29:00 +00:00
}
}
func TestCompareRelated(t *testing.T) {
2013-07-26 08:29:00 +00:00
if !Compare("abc", "abc") {
t.Error("should be equal")
}
if Compare("abc", "aBc") {
t.Error("should be not equal")
}
if !Compare("1", 1) {
t.Error("should be equal")
}
if CompareNot("abc", "abc") {
t.Error("should be equal")
}
if !CompareNot("abc", "aBc") {
t.Error("should be not equal")
}
if !NotNil("a string") {
t.Error("should not be nil")
}
2013-07-26 08:29:00 +00:00
}
func TestHtmlquote(t *testing.T) {
h := `&lt;&#39;&nbsp;&rdquo;&ldquo;&amp;&quot;&gt;`
s := `<' ”“&">`
if Htmlquote(s) != h {
t.Error("should be equal")
}
}
func TestHtmlunquote(t *testing.T) {
h := `&lt;&#39;&nbsp;&rdquo;&ldquo;&amp;&quot;&gt;`
s := `<' ”“&">`
if Htmlunquote(h) != s {
t.Error("should be equal")
}
}
2013-07-25 14:25:12 +00:00
func TestParseForm(t *testing.T) {
type user struct {
2014-11-04 08:39:17 +00:00
Id int `form:"-"`
tag string `form:"tag"`
Name interface{} `form:"username"`
Age int `form:"age,text"`
Email string
Intro string `form:",textarea"`
StrBool bool `form:"strbool"`
Date time.Time `form:"date,2006-01-02"`
2013-07-25 14:25:12 +00:00
}
u := user{}
form := url.Values{
"Id": []string{"1"},
"-": []string{"1"},
2013-07-25 14:25:12 +00:00
"tag": []string{"no"},
"username": []string{"test"},
"age": []string{"40"},
"Email": []string{"test@gmail.com"},
"Intro": []string{"I am an engineer!"},
2014-11-04 08:39:17 +00:00
"strbool": []string{"yes"},
"date": []string{"2014-11-12"},
2013-07-25 14:25:12 +00:00
}
if err := ParseForm(form, u); err == nil {
t.Fatal("nothing will be changed")
}
if err := ParseForm(form, &u); err != nil {
t.Fatal(err)
}
2013-07-25 14:50:29 +00:00
if u.Id != 0 {
2013-08-10 02:29:29 +00:00
t.Errorf("Id should equal 0 but got %v", u.Id)
2013-07-25 14:50:29 +00:00
}
if len(u.tag) != 0 {
t.Errorf("tag's length should equal 0 but got %v", len(u.tag))
2013-07-25 14:50:29 +00:00
}
2013-07-25 14:25:12 +00:00
if u.Name.(string) != "test" {
t.Errorf("Name should equal `test` but got `%v`", u.Name.(string))
2013-07-25 14:25:12 +00:00
}
if u.Age != 40 {
t.Errorf("Age should equal 40 but got %v", u.Age)
2013-07-25 14:25:12 +00:00
}
if u.Email != "test@gmail.com" {
t.Errorf("Email should equal `test@gmail.com` but got `%v`", u.Email)
}
if u.Intro != "I am an engineer!" {
t.Errorf("Intro should equal `I am an engineer!` but got `%v`", u.Intro)
2013-07-25 14:25:12 +00:00
}
2014-11-04 08:39:17 +00:00
if u.StrBool != true {
t.Errorf("strboll should equal `true`, but got `%v`", u.StrBool)
}
y, m, d := u.Date.Date()
if y != 2014 || m.String() != "November" || d != 12 {
t.Errorf("Date should equal `2014-11-12`, but got `%v`", u.Date.String())
}
2013-07-25 14:25:12 +00:00
}
2013-08-10 08:33:46 +00:00
func TestRenderForm(t *testing.T) {
type user struct {
2014-07-03 15:40:21 +00:00
Id int `form:"-"`
tag string `form:"tag"`
Name interface{} `form:"username"`
Age int `form:"age,text,年龄:"`
Sex string
Email []string
Intro string `form:",textarea"`
Ignored string `form:"-"`
2013-08-10 08:33:46 +00:00
}
2014-07-03 15:40:21 +00:00
u := user{Name: "test", Intro: "Some Text"}
2013-08-10 08:33:46 +00:00
output := RenderForm(u)
if output != template.HTML("") {
t.Errorf("output should be empty but got %v", output)
}
output = RenderForm(&u)
result := template.HTML(
`Name: <input name="username" type="text" value="test"></br>` +
2013-08-11 22:46:40 +00:00
`年龄:<input name="age" type="text" value="0"></br>` +
`Sex: <input name="Sex" type="text" value=""></br>` +
`Intro: <textarea name="Intro">Some Text</textarea>`)
2013-08-10 08:33:46 +00:00
if output != result {
t.Errorf("output should equal `%v` but got `%v`", result, output)
}
}
func TestRenderFormField(t *testing.T) {
2014-10-20 14:23:29 +00:00
html := renderFormField("Label: ", "Name", "text", "Value", "", "")
2014-07-03 15:40:21 +00:00
if html != `Label: <input name="Name" type="text" value="Value">` {
t.Errorf("Wrong html output for input[type=text]: %v ", html)
}
2014-10-20 14:23:29 +00:00
html = renderFormField("Label: ", "Name", "textarea", "Value", "", "")
2014-07-03 15:40:21 +00:00
if html != `Label: <textarea name="Name">Value</textarea>` {
t.Errorf("Wrong html output for textarea: %v ", html)
}
}
func TestParseFormTag(t *testing.T) {
2014-07-03 15:40:21 +00:00
// create struct to contain field with different types of struct-tag `form`
type user struct {
All int `form:"name,text,年龄:"`
NoName int `form:",hidden,年龄:"`
2014-07-03 15:40:21 +00:00
OnlyLabel int `form:",,年龄:"`
2014-10-20 14:23:29 +00:00
OnlyName int `form:"name" id:"name" class:"form-name"`
2014-07-03 15:40:21 +00:00
Ignored int `form:"-"`
}
objT := reflect.TypeOf(&user{}).Elem()
2014-10-20 14:23:29 +00:00
label, name, fType, id, class, ignored := parseFormTag(objT.Field(0))
2014-07-03 15:40:21 +00:00
if !(name == "name" && label == "年龄:" && fType == "text" && ignored == false) {
t.Errorf("Form Tag with name, label and type was not correctly parsed.")
2014-07-03 15:40:21 +00:00
}
2014-10-20 14:23:29 +00:00
label, name, fType, id, class, ignored = parseFormTag(objT.Field(1))
2014-07-03 15:40:21 +00:00
if !(name == "NoName" && label == "年龄:" && fType == "hidden" && ignored == false) {
t.Errorf("Form Tag with label and type but without name was not correctly parsed.")
2014-07-03 15:40:21 +00:00
}
2014-10-20 14:23:29 +00:00
label, name, fType, id, class, ignored = parseFormTag(objT.Field(2))
2014-07-03 15:40:21 +00:00
if !(name == "OnlyLabel" && label == "年龄:" && fType == "text" && ignored == false) {
t.Errorf("Form Tag containing only label was not correctly parsed.")
2014-07-03 15:40:21 +00:00
}
2014-10-20 14:23:29 +00:00
label, name, fType, id, class, ignored = parseFormTag(objT.Field(3))
if !(name == "name" && label == "OnlyName: " && fType == "text" && ignored == false &&
id == "name" && class == "form-name") {
2014-07-03 15:40:21 +00:00
t.Errorf("Form Tag containing only name was not correctly parsed.")
}
2014-10-20 14:23:29 +00:00
label, name, fType, id, class, ignored = parseFormTag(objT.Field(4))
2014-07-03 15:40:21 +00:00
if ignored == false {
t.Errorf("Form Tag that should be ignored was not correctly parsed.")
}
}
2015-08-20 14:04:43 +00:00
func TestMapGet(t *testing.T) {
// test one level map
m1 := map[string]int64{
"a": 1,
"1": 2,
}
if res, err := MapGet(m1, "a"); err == nil {
if res.(int64) != 1 {
t.Errorf("Should return 1, but return %v", res)
}
} else {
t.Errorf("Error happens %v", err)
}
if res, err := MapGet(m1, "1"); err == nil {
if res.(int64) != 2 {
t.Errorf("Should return 2, but return %v", res)
}
} else {
t.Errorf("Error happens %v", err)
}
if res, err := MapGet(m1, 1); err == nil {
if res.(int64) != 2 {
t.Errorf("Should return 2, but return %v", res)
}
} else {
t.Errorf("Error happens %v", err)
}
// test 2 level map
m2 := map[string]interface{}{
"1": map[string]float64{
"2": 3.5,
},
}
if res, err := MapGet(m2, 1, 2); err == nil {
if res.(float64) != 3.5 {
t.Errorf("Should return 3.5, but return %v", res)
}
} else {
t.Errorf("Error happens %v", err)
}
// test 5 level map
m5 := map[string]interface{}{
"1": map[string]interface{}{
"2": map[string]interface{}{
"3": map[string]interface{}{
"4": map[string]interface{}{
"5": 1.2,
},
},
},
},
}
if res, err := MapGet(m5, 1, 2, 3, 4, 5); err == nil {
if res.(float64) != 1.2 {
t.Errorf("Should return 1.2, but return %v", res)
}
} else {
t.Errorf("Error happens %v", err)
}
// check whether element not exists in map
if res, err := MapGet(m5, 5, 4, 3, 2, 1); err == nil {
if res != nil {
t.Errorf("Should return nil, but return %v", res)
}
} else {
t.Errorf("Error happens %v", err)
}
}