github.com/mitranim/gg@v0.1.17/lazy_initer_test.go (about) 1 package gg_test 2 3 import ( 4 "testing" 5 6 "github.com/mitranim/gg" 7 "github.com/mitranim/gg/gtest" 8 ) 9 10 type IniterStr string 11 12 func (self *IniterStr) Init() { 13 if *self != `` { 14 panic(`redundant init`) 15 } 16 *self = `inited` 17 } 18 19 // Incomplete: doesn't verify concurrency safety. 20 func TestLazyIniter(t *testing.T) { 21 defer gtest.Catch(t) 22 23 var tar gg.LazyIniter[IniterStr, *IniterStr] 24 25 gtest.Eq(tar.Get(), `inited`) 26 gtest.Eq(tar.Get(), tar.Get()) 27 28 // Note: `gg.CastUnsafe[string](tar)` would have been simpler, but 29 // technically involves passing `tar` by value, which is invalid due 30 // to inner mutex. Wouldn't actually matter. 31 gtest.Eq(*gg.CastUnsafe[*string](&tar), `inited`) 32 33 gtest.Eq(tar.Ptr(), gg.CastUnsafe[*IniterStr](&tar)) 34 gtest.Eq(tar.Ptr(), tar.Ptr()) 35 }