2018-02-15 17:26:30 +08:00
|
|
|
package vars
|
2018-02-06 14:03:27 +08:00
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
2018-11-18 22:23:31 +08:00
|
|
|
func TestFromPtr(t *testing.T) {
|
2018-02-06 14:03:27 +08:00
|
|
|
i := 10
|
2018-03-08 21:20:31 +08:00
|
|
|
variable := FromPtr(&i)
|
2021-04-04 20:37:38 +08:00
|
|
|
if g := variable.Get(); g != 10 {
|
|
|
|
t.Errorf(`Get -> %v, want 10`, g)
|
2019-08-27 16:55:12 +08:00
|
|
|
}
|
2018-02-06 14:03:27 +08:00
|
|
|
err := variable.Set("20")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf(`Setting ptrVariable with "20" returns error %v`, err)
|
|
|
|
}
|
|
|
|
if i != 20 {
|
|
|
|
t.Errorf(`Setting ptrVariable didn't change underlying value`)
|
|
|
|
}
|
|
|
|
err = variable.Set("x")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Setting ptrVariable with incompatible value returns no error")
|
|
|
|
}
|
|
|
|
}
|
2018-11-18 22:13:03 +08:00
|
|
|
|
2018-11-18 22:23:31 +08:00
|
|
|
func TestFromInit(t *testing.T) {
|
|
|
|
v := FromInit(true)
|
|
|
|
if val := v.Get(); val != true {
|
|
|
|
t.Errorf("Get returned %v, want true", val)
|
|
|
|
}
|
|
|
|
if err := v.Set("233"); err != nil {
|
|
|
|
t.Errorf("Set errors: %v", err)
|
|
|
|
}
|
|
|
|
if val := v.Get(); val != "233" {
|
|
|
|
t.Errorf(`Get returns %v, want "233"`, val)
|
2018-11-18 22:13:03 +08:00
|
|
|
}
|
|
|
|
}
|