-
Notifications
You must be signed in to change notification settings - Fork 1
/
makehtmlgetter_test.go
107 lines (100 loc) · 2.28 KB
/
makehtmlgetter_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
package jaws
import (
"html"
"html/template"
"reflect"
"sync/atomic"
"testing"
)
type testAnySetter struct {
Value any
}
func (ag *testAnySetter) JawsGetAny(*Element) any {
return ag.Value
}
func (ag *testAnySetter) JawsSetAny(e *Element, v any) error {
ag.Value = v
return nil
}
func Test_MakeHTMLGetter(t *testing.T) {
untypedText := "<span>"
typedText := template.HTML(untypedText)
escapedTypedText := template.HTML(html.EscapeString(untypedText))
var avUntyped, avTyped atomic.Value
avUntyped.Store(untypedText)
avTyped.Store(typedText)
stringer := testStringer{}
getterString := testGetterString{}
getterAny := &testAnySetter{Value: untypedText}
tests := []struct {
name string
v any
want HTMLGetter
out template.HTML
tag any
}{
{
name: "HTMLGetter",
v: htmlGetter{typedText},
want: htmlGetter{typedText},
out: typedText,
tag: nil,
},
{
name: "Getter[string]",
v: getterString,
want: htmlGetterString{getterString},
out: template.HTML(html.EscapeString(getterString.JawsGet(nil))),
tag: getterString,
},
{
name: "Getter[any]",
v: getterAny,
want: htmlGetterAny{getterAny},
out: escapedTypedText,
tag: getterAny,
},
{
name: "fmt.Stringer",
v: stringer,
want: htmlStringerGetter{stringer},
out: template.HTML(html.EscapeString(stringer.String())),
tag: stringer,
},
{
name: "template.HTML",
v: typedText,
want: htmlGetter{typedText},
out: typedText,
tag: nil,
},
{
name: "string",
v: untypedText,
want: htmlGetter{template.HTML(untypedText)},
out: template.HTML(untypedText),
tag: nil,
},
{
name: "int",
v: 123,
want: htmlGetter{template.HTML("123")},
out: template.HTML("123"),
tag: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := MakeHTMLGetter(tt.v)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MakeHTMLGetter(%s)\n got %#v\n want %#v", tt.name, got, tt.want)
}
if txt := got.JawsGetHTML(nil); txt != tt.out {
t.Errorf("MakeHTMLGetter(%s).JawsGetHTML() = %v, want %v", tt.name, txt, tt.out)
}
if tag := got.(TagGetter).JawsGetTag(nil); tag != tt.tag {
t.Errorf("MakeHTMLGetter(%s).JawsGetTag() = %v, want %v", tt.name, tag, tt.tag)
}
})
}
}