-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlazy_initer_test.go
49 lines (37 loc) · 1.06 KB
/
lazy_initer_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
package gg_test
import (
"testing"
"github.com/mitranim/gg"
"github.com/mitranim/gg/gtest"
)
type IniterStr string
func (self *IniterStr) Init() {
if *self != `` {
panic(`redundant init`)
}
*self = `inited`
}
// Incomplete: doesn't verify concurrency safety.
func TestLazyIniter(t *testing.T) {
defer gtest.Catch(t)
var tar gg.LazyIniter[IniterStr, *IniterStr]
test := func(exp gg.Opt[IniterStr]) {
// Note: `gg.CastUnsafe[A](tar)` would have been simpler, but technically
// involves passing `tar` by value, which is invalid due to inner mutex.
// Wouldn't actually matter.
gtest.Eq(*gg.CastUnsafe[*gg.Opt[IniterStr]](&tar), exp)
}
test(gg.Zero[gg.Opt[IniterStr]]())
gtest.Eq(tar.Get(), `inited`)
gtest.Eq(tar.Get(), tar.Get())
test(gg.OptVal(IniterStr(`inited`)))
tar.Clear()
test(gg.Zero[gg.Opt[IniterStr]]())
gtest.Eq(tar.Get(), `inited`)
gtest.Eq(tar.Get(), tar.Get())
test(gg.OptVal(IniterStr(`inited`)))
tar.Reset(`inited_1`)
gtest.Eq(tar.Get(), `inited_1`)
gtest.Eq(tar.Get(), tar.Get())
test(gg.OptVal(IniterStr(`inited_1`)))
}