From 33fda8605a7e46597651edc2834c2dae618ed6ee Mon Sep 17 00:00:00 2001 From: James Shubin Date: Tue, 4 May 2021 04:27:33 -0400 Subject: [PATCH] lang: types: Add new ValueOf tests Hopefully this makes all of this a bit more obvious. --- lang/types/value_test.go | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/lang/types/value_test.go b/lang/types/value_test.go index 63041b5d..2b3487b2 100644 --- a/lang/types/value_test.go +++ b/lang/types/value_test.go @@ -632,15 +632,68 @@ func TestValueOf0(t *testing.T) { val, err := ValueOf(reflect.ValueOf(gotyp)) if err != nil { t.Errorf("function ValueOf(%+v) returned err %s", gotyp, err) + continue } // use string representation comparison as maps are non-deterministic in order // and cmp doesn't work as the pointers differ if val.String() != value.String() { t.Errorf("function ValueOf(%+v) gave %+v and doesn't match expected %+v", gotyp, val, value) + continue } } } +func TestValueOf1(t *testing.T) { + str := "abc" + pstr := &str + value := &StrValue{V: "abc"} + + val, err := ValueOf(reflect.ValueOf(pstr)) + if err != nil { + t.Errorf("function ValueOf(%+v) returned err %s", pstr, err) + return + } + if val.String() != value.String() { + t.Errorf("function ValueOf(%+v) gave %+v and doesn't match expected %+v", pstr, val, value) + return + } +} + +func TestValueOf2(t *testing.T) { + str := "point" + pstr := &str + + str2 := "point2" + pstr2 := &str2 + ppstr2 := &pstr2 + st := struct { + Num int `lang:"num"` + Name string `lang:"name"` + Ptr *string `lang:"ptr"` + Ptr2 **string `lang:"ptr2"` + }{42, "mgmt", pstr, ppstr2} + + value := &StructValue{ + T: NewType("struct{num int; name str; ptr str; ptr2 str}"), + V: map[string]Value{ + "num": &IntValue{V: 42}, + "name": &StrValue{V: "mgmt"}, + "ptr": &StrValue{V: "point"}, + "ptr2": &StrValue{V: "point2"}, + }, + } + + val, err := ValueOf(reflect.ValueOf(st)) + if err != nil { + t.Errorf("function ValueOf(%+v) returned err %s", st, err) + return + } + if val.String() != value.String() { + t.Errorf("function ValueOf(%+v) gave %+v and doesn't match expected %+v", st, val, value) + return + } +} + func TestValueInto0(t *testing.T) { // converts a Go variable to a types.Value, or panics if any error mustValue := func(v interface{}) Value {