2014-04-12 05:18:18 +00:00
|
|
|
// Beego (http://beego.me/)
|
|
|
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
|
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
|
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
|
|
|
// @authors astaxie
|
|
|
|
|
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"
|
|
|
|
"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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-30 19:12:21 +00:00
|
|
|
|
|
|
|
if ss := DateFormat(tt, "2006-01-02 15:04:05"); ss != "2013-07-01 14:27:42" {
|
|
|
|
t.Errorf("2013-07-01 14: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-30 19:12:21 +00:00
|
|
|
|
|
|
|
if ss := Date(tt, "Y-m-d H:i:s"); ss != "2013-07-01 14:27:42" {
|
|
|
|
t.Errorf("2013-07-01 14:27:42 does not equal %v", ss)
|
2013-07-26 08:29:00 +00:00
|
|
|
}
|
2014-05-30 19:12:21 +00:00
|
|
|
if ss := Date(tt, "y-n-j h:i:s A"); ss != "13-7-1 02:27:42 PM" {
|
|
|
|
t.Errorf("13-7-1 02:27:42 PM does not equal %v", ss)
|
2013-07-26 08:29:00 +00:00
|
|
|
}
|
2014-05-30 19:12:21 +00:00
|
|
|
if ss := Date(tt, "D, d M Y g:i:s a"); ss != "Mon, 01 Jul 2013 2:27:42 pm" {
|
|
|
|
t.Errorf("Mon, 01 Jul 2013 2:27:42 pm does not equal %v", ss)
|
2013-07-26 08:29:00 +00:00
|
|
|
}
|
2014-05-30 19:12:21 +00:00
|
|
|
if ss := Date(tt, "l, d F Y G:i:s"); ss != "Monday, 01 July 2013 14:27:42" {
|
|
|
|
t.Errorf("Monday, 01 July 2013 14:27:42 does not equal %v", ss)
|
2013-07-26 08:29:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompare(t *testing.T) {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHtmlquote(t *testing.T) {
|
|
|
|
h := `<' ”“&">`
|
|
|
|
s := `<' ”“&">`
|
|
|
|
if Htmlquote(s) != h {
|
|
|
|
t.Error("should be equal")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHtmlunquote(t *testing.T) {
|
|
|
|
h := `<' ”“&">`
|
|
|
|
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 {
|
2013-08-11 14:20:05 +00:00
|
|
|
Id int `form:"-"`
|
|
|
|
tag string `form:"tag"`
|
2013-07-25 14:25:12 +00:00
|
|
|
Name interface{} `form:"username"`
|
2013-08-10 03:42:25 +00:00
|
|
|
Age int `form:"age,text"`
|
2013-07-25 14:25:12 +00:00
|
|
|
Email string
|
2013-08-10 03:42:25 +00:00
|
|
|
Intro string `form:",textarea"`
|
2013-07-25 14:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u := user{}
|
|
|
|
form := url.Values{
|
2013-08-11 14:20:05 +00:00
|
|
|
"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"},
|
2013-08-10 03:42:25 +00:00
|
|
|
"Intro": []string{"I am an engineer!"},
|
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 {
|
2013-08-10 03:42:25 +00:00
|
|
|
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" {
|
2013-08-10 03:42:25 +00:00
|
|
|
t.Errorf("Name should equal `test` but got `%v`", u.Name.(string))
|
2013-07-25 14:25:12 +00:00
|
|
|
}
|
|
|
|
if u.Age != 40 {
|
2013-08-10 03:42:25 +00:00
|
|
|
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" {
|
2013-08-10 03:42:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2013-08-10 08:33:46 +00:00
|
|
|
|
|
|
|
func TestRenderForm(t *testing.T) {
|
|
|
|
type user struct {
|
2013-08-11 14:42:35 +00:00
|
|
|
Id int `form:"-"`
|
|
|
|
tag string `form:"tag"`
|
2013-08-10 08:33:46 +00:00
|
|
|
Name interface{} `form:"username"`
|
2013-08-11 22:46:40 +00:00
|
|
|
Age int `form:"age,text,年龄:"`
|
2013-08-11 14:42:35 +00:00
|
|
|
Sex string
|
2013-08-10 08:33:46 +00:00
|
|
|
Email []string
|
|
|
|
Intro string `form:",textarea"`
|
|
|
|
}
|
|
|
|
|
|
|
|
u := user{Name: "test"}
|
|
|
|
output := RenderForm(u)
|
|
|
|
if output != template.HTML("") {
|
|
|
|
t.Errorf("output should be empty but got %v", output)
|
|
|
|
}
|
|
|
|
output = RenderForm(&u)
|
|
|
|
result := template.HTML(
|
2013-08-11 14:42:35 +00:00
|
|
|
`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>` +
|
2013-08-11 14:42:35 +00:00
|
|
|
`Sex: <input name="Sex" type="text" value=""></br>` +
|
2013-08-10 08:33:46 +00:00
|
|
|
`Intro: <input name="Intro" type="textarea" value="">`)
|
|
|
|
if output != result {
|
|
|
|
t.Errorf("output should equal `%v` but got `%v`", result, output)
|
|
|
|
}
|
|
|
|
}
|