-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmem_test.go
158 lines (119 loc) · 3.67 KB
/
mem_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
157
158
package gg_test
import (
"testing"
"github.com/mitranim/gg"
"github.com/mitranim/gg/gtest"
)
type IniterStrs []string
func (self *IniterStrs) Init() {
if *self != nil {
panic(`redundant init`)
}
*self = []string{`one`, `two`, `three`}
}
func TestMem(t *testing.T) {
defer gtest.Catch(t)
{
// Note: `IniterStr` panics if init is called on a non-zero value.
// This is part of our contract.
var mem gg.Mem[gg.DurSecond, IniterStr, *IniterStr]
test := func() {
testPeekerZero[IniterStr](&mem)
gtest.Eq(mem.Get(), `inited`)
gtest.Eq(mem.Get(), `inited`)
gtest.Eq(mem.Peek(), `inited`)
gtest.Eq(mem.Peek(), `inited`)
testPeekerNotZero[IniterStr](&mem)
}
test()
mem.Clear()
test()
}
{
// Note: `IniterStrs` panics if init is called on a non-zero value.
// This is part of our contract.
var mem gg.Mem[gg.DurSecond, IniterStrs, *IniterStrs]
test := func() {
testPeekerZero[IniterStrs](&mem)
prev := mem.Get()
gtest.Equal(prev, []string{`one`, `two`, `three`})
gtest.SliceIs(mem.Get(), prev)
gtest.SliceIs(mem.Get(), prev)
testPeekerNotZero[IniterStrs](&mem)
}
test()
prev := mem.Get()
mem.Clear()
test()
next := mem.Get()
gtest.NotSliceIs(next, prev)
}
}
/*
Technical note: because `Mem` contains a synchronization primitive, its method
`.MarshalJSON` cannot be safely implemented on the value type, and we cannot
safely pass the value to JSON-encoding functions. This is a known inconsistency
with other types that implement JSON encoding.
*/
func TestMem_MarshalJSON(t *testing.T) {
defer gtest.Catch(t)
t.Run(`primitive`, func(t *testing.T) {
defer gtest.Catch(t)
type Type = gg.Mem[gg.DurSecond, IniterStr, *IniterStr]
gtest.Eq(gg.ToString(gg.Try1((*Type)(nil).MarshalJSON())), `null`)
gtest.Eq(gg.JsonString((*Type)(nil)), `null`)
var mem Type
gtest.Eq(gg.JsonString(&mem), `null`)
mem.Get()
gtest.Eq(gg.JsonString(&mem), `"inited"`)
mem.Clear()
gtest.Eq(gg.JsonString(&mem), `null`)
})
t.Run(`slice`, func(t *testing.T) {
defer gtest.Catch(t)
type Type = gg.Mem[gg.DurSecond, IniterStrs, *IniterStrs]
gtest.Eq(gg.ToString(gg.Try1((*Type)(nil).MarshalJSON())), `null`)
gtest.Eq(gg.JsonString((*Type)(nil)), `null`)
var mem Type
gtest.Eq(gg.JsonString(&mem), `null`)
mem.Get()
gtest.Eq(gg.JsonString(&mem), `["one","two","three"]`)
mem.Clear()
gtest.Eq(gg.JsonString(&mem), `null`)
})
}
func TestMem_UnmarshalJSON(t *testing.T) {
defer gtest.Catch(t)
t.Run(`primitive`, func(t *testing.T) {
defer gtest.Catch(t)
var mem gg.Mem[gg.DurSecond, IniterStr, *IniterStr]
gtest.NoErr(mem.UnmarshalJSON([]byte(`null`)))
gtest.Zero(mem.Peek())
gtest.NoErr(mem.UnmarshalJSON([]byte(`""`)))
gtest.Eq(mem.Peek(), ``)
gtest.NoErr(mem.UnmarshalJSON([]byte(`"test"`)))
gtest.Eq(mem.Peek(), `test`)
gtest.ErrStr(`invalid character 'i'`, mem.UnmarshalJSON([]byte(`invalid`)))
gtest.Eq(mem.Peek(), `test`)
})
t.Run(`slice`, func(t *testing.T) {
defer gtest.Catch(t)
var mem gg.Mem[gg.DurSecond, IniterStrs, *IniterStrs]
gtest.NoErr(mem.UnmarshalJSON([]byte(`null`)))
gtest.Zero(mem.Peek())
gtest.NoErr(mem.UnmarshalJSON([]byte(`[]`)))
gtest.Equal(mem.Peek(), IniterStrs{})
gtest.NoErr(mem.UnmarshalJSON([]byte(`["four","five","six"]`)))
gtest.Equal(mem.Peek(), IniterStrs{`four`, `five`, `six`})
gtest.ErrStr(`invalid character 'i'`, mem.UnmarshalJSON([]byte(`invalid`)))
gtest.Equal(mem.Peek(), IniterStrs{`four`, `five`, `six`})
})
}
func testPeekerZero[Tar any, Src gg.Peeker[Tar]](src Src) {
gtest.Zero(src.Peek())
gtest.Zero(src.Peek())
}
func testPeekerNotZero[Tar any, Src gg.Peeker[Tar]](src Src) {
gtest.NotZero(src.Peek())
gtest.NotZero(src.Peek())
}