forked from beego/beego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
176 lines (160 loc) · 4.12 KB
/
utils_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package beego
import (
"html/template"
"net/url"
"testing"
"time"
)
func TestWebTime(t *testing.T) {
ts := "Fri, 26 Jul 2013 12:27:42 CST"
l, _ := time.LoadLocation("GST")
tt, _ := time.ParseInLocation(time.RFC1123, ts, l)
if ts != webTime(tt) {
t.Error("should be equal")
}
if "Fri, 26 Jul 2013 12:27:42 GMT" != webTime(tt.UTC()) {
t.Error("should be equal")
}
}
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)
if DateFormat(tt, "2006-01-02 15:04:05") != "2013-07-01 13:27:42" {
t.Error("should be equal")
}
}
func TestDate(t *testing.T) {
ts := "Mon, 01 Jul 2013 13:27:42 CST"
tt, _ := time.Parse(time.RFC1123, ts)
if Date(tt, "Y-m-d H:i:s") != "2013-07-01 13:27:42" {
t.Error("should be equal")
}
if Date(tt, "y-n-j h:i:s A") != "13-7-1 01:27:42 PM" {
t.Error("should be equal")
}
if Date(tt, "D, d M Y g:i:s a") != "Mon, 01 Jul 2013 1:27:42 pm" {
t.Error("should be equal")
}
if Date(tt, "l, d F Y G:i:s") != "Monday, 01 July 2013 13:27:42" {
t.Error("should be equal")
}
}
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")
}
}
func TestInSlice(t *testing.T) {
sl := []string{"A", "b"}
if !inSlice("A", sl) {
t.Error("should be true")
}
if inSlice("B", sl) {
t.Error("should be false")
}
}
func TestParseForm(t *testing.T) {
type user struct {
Id int `form:"-"`
tag string `form:"tag"`
Name interface{} `form:"username"`
Age int `form:"age,text"`
Email string
Intro string `form:",textarea"`
}
u := user{}
form := url.Values{
"Id": []string{"1"},
"-": []string{"1"},
"tag": []string{"no"},
"username": []string{"test"},
"age": []string{"40"},
"Email": []string{"[email protected]"},
"Intro": []string{"I am an engineer!"},
}
if err := ParseForm(form, u); err == nil {
t.Fatal("nothing will be changed")
}
if err := ParseForm(form, &u); err != nil {
t.Fatal(err)
}
if u.Id != 0 {
t.Errorf("Id should equal 0 but got %v", u.Id)
}
if len(u.tag) != 0 {
t.Errorf("tag's length should equal 0 but got %v", len(u.tag))
}
if u.Name.(string) != "test" {
t.Errorf("Name should equal `test` but got `%v`", u.Name.(string))
}
if u.Age != 40 {
t.Errorf("Age should equal 40 but got %v", u.Age)
}
if u.Email != "[email protected]" {
t.Errorf("Email should equal `[email protected]` 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)
}
}
func TestRenderForm(t *testing.T) {
type user struct {
Id int `form:"-"`
tag string `form:"tag"`
Name interface{} `form:"username"`
Age int `form:"age,text,年龄:"`
Sex string
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(
`Name: <input name="username" type="text" value="test"></br>` +
`年龄:<input name="age" type="text" value="0"></br>` +
`Sex: <input name="Sex" type="text" value=""></br>` +
`Intro: <input name="Intro" type="textarea" value="">`)
if output != result {
t.Errorf("output should equal `%v` but got `%v`", result, output)
}
}