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
|
package vars
import "testing"
func TestFromSetGet(t *testing.T) {
getCalled := false
get := func() any {
getCalled = true
return "cb"
}
var setCalledWith any
set := func(v any) error {
setCalledWith = v
return nil
}
v := FromSetGet(set, get)
if v.Get() != "cb" {
t.Errorf("cbVariable doesn't return value from callback")
}
if !getCalled {
t.Errorf("cbVariable doesn't call callback")
}
v.Set("setting")
if setCalledWith != "setting" {
t.Errorf("cbVariable.Set doesn't call setter with value")
}
}
func TestFromGet(t *testing.T) {
getCalled := false
get := func() any {
getCalled = true
return "cb"
}
v := FromGet(get)
if v.Get() != "cb" {
t.Errorf("roCbVariable doesn't return value from callback")
}
if !getCalled {
t.Errorf("roCbVariable doesn't call callback")
}
if v.Set("lala") == nil {
t.Errorf("roCbVariable.Set doesn't error")
}
}
|