-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_map_test.go
57 lines (49 loc) · 1.82 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
package admin
import (
"fmt"
"html/template"
"testing"
"github.com/fatih/color"
"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)))
}
}
}