-
Notifications
You must be signed in to change notification settings - Fork 1
/
atomicsetter_test.go
156 lines (146 loc) · 2.95 KB
/
atomicsetter_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
package jaws
import (
"fmt"
"html/template"
"reflect"
"sync/atomic"
"testing"
"time"
)
var _ BoolSetter = (atomicSetter{})
var _ FloatSetter = (atomicSetter{})
var _ StringSetter = (atomicSetter{})
var _ TimeSetter = (atomicSetter{})
var _ HtmlGetter = (atomicSetter{})
func Test_atomicSetter_UninitializedDefaults(t *testing.T) {
var av atomic.Value
g := atomicSetter{v: &av}
if g.JawsGetBool(nil) != false {
t.Fail()
}
if g.JawsGetFloat(nil) != 0 {
t.Fail()
}
if g.JawsGetString(nil) != "" {
t.Fail()
}
if !g.JawsGetTime(nil).IsZero() {
t.Fail()
}
if g.JawsGetHtml(nil) != "" {
t.Fail()
}
}
func Test_atomicSetter_bool(t *testing.T) {
var av atomic.Value
g := atomicSetter{v: &av}
val := true
if err := g.JawsSetBool(nil, val); err != nil {
t.Error(err)
}
if g.JawsGetBool(nil) != val {
t.Fail()
}
if err := g.JawsSetBool(nil, val); err != ErrValueUnchanged {
t.Error(err)
}
}
func Test_atomicSetter_float64(t *testing.T) {
var av atomic.Value
g := atomicSetter{v: &av}
val := float64(1.2)
if err := g.JawsSetFloat(nil, val); err != nil {
t.Error(err)
}
if g.JawsGetFloat(nil) != val {
t.Fail()
}
if err := g.JawsSetFloat(nil, val); err != ErrValueUnchanged {
t.Error(err)
}
}
func Test_atomicSetter_string(t *testing.T) {
var av atomic.Value
g := atomicSetter{v: &av}
val := "str"
if err := g.JawsSetString(nil, val); err != nil {
t.Error(err)
}
if g.JawsGetString(nil) != val {
t.Fail()
}
if err := g.JawsSetString(nil, val); err != ErrValueUnchanged {
t.Error(err)
}
}
func Test_atomicSetter_time(t *testing.T) {
var av atomic.Value
g := atomicSetter{v: &av}
val := time.Now()
if err := g.JawsSetTime(nil, val); err != nil {
t.Error(err)
}
if g.JawsGetTime(nil) != val {
t.Fail()
}
if err := g.JawsSetTime(nil, val); err != ErrValueUnchanged {
t.Error(err)
}
}
func Test_atomicSetter_any(t *testing.T) {
var av atomic.Value
g := atomicSetter{v: &av}
val := "str"
if err := g.JawsSetAny(nil, val); err != nil {
t.Error(err)
}
if g.JawsGetAny(nil) != val {
t.Fail()
}
if err := g.JawsSetAny(nil, val); err != ErrValueUnchanged {
t.Error(err)
}
}
func Test_atomicSetter_JawsGetHtml(t *testing.T) {
tests := []struct {
name string
av atomic.Value
v any
want template.HTML
}{
{
name: "html",
v: template.HTML("html"),
want: "html",
},
{
name: "bool",
v: bool(true),
want: "true",
},
{
name: "float64",
v: float64(1.2),
want: "1.2",
},
{
name: "time.Time",
v: time.Now().Round(time.Minute),
want: template.HTML(fmt.Sprint(time.Now().Round(time.Minute))),
},
{
name: "html-escaped string",
v: "<span>",
want: "<span>",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.av.Store(tt.v)
g := atomicSetter{v: &tt.av}
if got := g.JawsGetHtml(nil); !reflect.DeepEqual(got, tt.want) {
t.Errorf("atomicGetter.JawsGetHtml() for %#v = %v, want %v", tt.v, got, tt.want)
}
})
}
}