-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestsetter_test.go
126 lines (113 loc) · 2.04 KB
/
testsetter_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
package jaws
import (
"github.com/linkdata/deadlock"
)
type testSetter[T comparable] struct {
mu deadlock.Mutex
val T
err error
setCount int
setCalled chan struct{}
getCount int
getCalled chan struct{}
}
func newTestSetter[T comparable](val T) *testSetter[T] {
return &testSetter[T]{
val: val,
setCalled: make(chan struct{}),
getCalled: make(chan struct{}),
}
}
func (ts *testSetter[T]) Get() (s T) {
ts.mu.Lock()
s = ts.val
ts.mu.Unlock()
return
}
func (ts *testSetter[T]) Set(s T) {
ts.mu.Lock()
ts.val = s
ts.mu.Unlock()
}
func (ts *testSetter[T]) SetCount() (n int) {
ts.mu.Lock()
n = ts.setCount
ts.mu.Unlock()
return
}
func (ts *testSetter[T]) GetCount() (n int) {
ts.mu.Lock()
n = ts.getCount
ts.mu.Unlock()
return
}
func (ts *testSetter[T]) JawsGet(e *Element) (val T) {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.getCount++
if ts.getCount == 1 {
close(ts.getCalled)
}
val = ts.val
return
}
func (ts *testSetter[T]) JawsSet(e *Element, val T) (err error) {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.setCount++
if ts.setCount == 1 {
close(ts.setCalled)
}
if err = ts.err; err == nil {
if ts.val == val {
err = ErrValueUnchanged
}
ts.val = val
}
return
}
func (ts *testSetter[string]) JawsGetString(e *Element) (val string) {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.getCount++
if ts.getCount == 1 {
close(ts.getCalled)
}
val = ts.val
return
}
func (ts *testSetter[any]) JawsGetAny(e *Element) (val any) {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.getCount++
if ts.getCount == 1 {
close(ts.getCalled)
}
val = ts.val
return
}
func (ts *testSetter[any]) JawsSetAny(e *Element, val any) (err error) {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.setCount++
if ts.setCount == 1 {
close(ts.setCalled)
}
if err = ts.err; err == nil {
if ts.val == val {
err = ErrValueUnchanged
}
ts.val = val
}
return
}
func (ts *testSetter[T]) JawsGetHTML(e *Element) (val T) {
ts.mu.Lock()
defer ts.mu.Unlock()
ts.getCount++
if ts.getCount == 1 {
close(ts.getCalled)
}
val = ts.val
return
}