-
Notifications
You must be signed in to change notification settings - Fork 189
/
func_map_test.go
94 lines (80 loc) · 2.42 KB
/
func_map_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
package admin
import (
"fmt"
"html/template"
"testing"
"github.com/fatih/color"
"github.com/jinzhu/gorm"
"github.com/qor/qor"
)
type rawTestCase struct {
HTML string
ExpectResult string
}
func TestFuncMaps(t *testing.T) {
rawTestCases := []rawTestCase{
{HTML: "<a href='#'>Hello</a>", ExpectResult: "Hello"},
{HTML: "<a href='http://www.google.com'>Hello</a>", ExpectResult: "<a href=\"http://www.google.com\" rel=\"nofollow\">Hello</a>"},
{HTML: "<a href='http://www.google.com' data-hint='Hello'>Hello</a>", ExpectResult: "<a href=\"http://www.google.com\" rel=\"nofollow\">Hello</a>"},
}
unsafeRawTestCases := []rawTestCase{
{HTML: "<a href='http://g.cn'>Hello</a>", ExpectResult: "<a href='http://g.cn'>Hello</a>"},
{HTML: "<a href='#' data-hint='Hello'>Hello</a>", ExpectResult: "<a href='#' data-hint='Hello'>Hello</a>"},
}
context := Context{
Admin: New(&qor.Config{}),
}
funcMaps := context.FuncMap()
for i, testcase := range rawTestCases {
result := funcMaps["raw"].((func(string) template.HTML))(testcase.HTML)
var hasError bool
if result != template.HTML(testcase.ExpectResult) {
t.Errorf(color.RedString(fmt.Sprintf("Admin FuncMap raw #%v: expect get %v, but got '%v'", i+1, testcase.ExpectResult, result)))
hasError = true
}
if !hasError {
fmt.Printf(color.GreenString(fmt.Sprintf("Admin FuncMap raw #%v: Success\n", i+1)))
}
}
for i, testcase := range unsafeRawTestCases {
result := funcMaps["unsafe_raw"].((func(string) template.HTML))(testcase.HTML)
var hasError bool
if result != template.HTML(testcase.ExpectResult) {
t.Errorf(color.RedString(fmt.Sprintf("Admin FuncMap unsafe_raw #%v: expect get %v, but got '%v'", i+1, testcase.ExpectResult, result)))
hasError = true
}
if !hasError {
fmt.Printf(color.GreenString(fmt.Sprintf("Admin FuncMap unsafe_raw #%v: Success\n", i+1)))
}
}
}
type FakeStruct struct {
gorm.Model
Name string
}
func TestIsEqual(t *testing.T) {
c1 := FakeStruct{Name: "c1"}
c1.ID = 1
c2 := FakeStruct{Name: "c2"}
c2.ID = 1
context := Context{
Admin: New(&qor.Config{}),
}
if !context.isEqual(c1, c2) {
t.Error("same primary key is not equal")
}
c1.ID = 2
if context.isEqual(c1, c2) {
t.Error("different primary key is equal")
}
a := "a test"
b := "another one"
if context.isEqual(a, b) {
t.Error("different string is equal")
}
c := 11
d := 11
if !context.isEqual(c, d) {
t.Error("same int is not equal")
}
}