From b8682b9772e0485f9e43e2ac309e5cb56cd4f628 Mon Sep 17 00:00:00 2001 From: gezimbll Date: Tue, 18 Oct 2022 10:49:02 -0400 Subject: [PATCH] daily tests --- utils/map_test.go | 1 - utils/reflect_test.go | 103 ++++++++++++++++++++++++++++++++++++ utils/stringset_test.go | 33 ++++++++++++ utils/value_formula_test.go | 42 +++++++++++++++ 4 files changed, 178 insertions(+), 1 deletion(-) diff --git a/utils/map_test.go b/utils/map_test.go index 84e5f5f00..677d2d55e 100644 --- a/utils/map_test.go +++ b/utils/map_test.go @@ -425,7 +425,6 @@ func TestFlagsWithParamsClone(t *testing.T) { } } -// my test func TestStringMapFieldAsInterfaceNotNil(t *testing.T) { sm := StringMap{} fldPath := []string{"first", "second"} diff --git a/utils/reflect_test.go b/utils/reflect_test.go index 8bd04b8ef..c34fb3f1e 100644 --- a/utils/reflect_test.go +++ b/utils/reflect_test.go @@ -1889,3 +1889,106 @@ func TestIfaceAsTFloat64(t *testing.T) { t.Errorf("expected: <%+v>, \nreceived: <%+v>", experr, err) } } + +func TestIfaceAsTInt(t *testing.T) { + eInt := 3 + + val := interface{}(3) + + if intConvert, err := IfaceAsTInt(val); err != nil { + + t.Error(err) + } else if intConvert != eInt { + t.Errorf("received %+v", intConvert) + } + val = interface{}(time.Duration(3)) + + if intConvert, err := IfaceAsTInt(val); err != nil { + + t.Error(err) + } else if intConvert != eInt { + t.Errorf("received %+v ", intConvert) + } + val = interface{}(int32(3)) + if intConvert, err := IfaceAsTInt(val); err != nil { + + t.Error(err) + } else if intConvert != eInt { + t.Errorf("received %+v ", intConvert) + } + val = interface{}(int64(3)) + + if intConvert, err := IfaceAsTInt(val); err != nil { + t.Error(err) + } else if intConvert != eInt { + t.Errorf("received %+v", intConvert) + } + + val = interface{}(3.122) + if intConvert, err := IfaceAsTInt(val); err != nil { + t.Error(err) + } else if intConvert != eInt { + t.Errorf("received %+v", intConvert) + } + val = interface{}(float32(3.11)) + if intConvert, err := IfaceAsTInt(val); err != nil { + t.Error(err) + } else if intConvert != eInt { + t.Errorf("received %+v", intConvert) + } + val = interface{}("3") + if intConvert, err := IfaceAsTInt(val); err != nil { + + t.Error(err) + } else if intConvert != eInt { + t.Errorf("received %+v", intConvert) + } + val = interface{}(nil) + if _, err := IfaceAsTInt(val); err == nil { + + t.Error("expecting error") + } +} + +func TestIfaceStringInterface(t *testing.T) { + + s := "" + + if strItem := StringToInterface(s); strItem != s { + t.Error("Return empty") + } + s = "3" + + if _, ok := StringToInterface(s).(int64); !ok { + + t.Errorf("received type %T", s) + } + s = "false" + + if _, ok := StringToInterface(s).(bool); !ok { + + t.Errorf("received type %T ", s) + } + s = "4.566" + + if _, ok := StringToInterface(s).(float64); !ok { + + t.Errorf("received type %T", s) + } + s = "*daily" + + if _, ok := StringToInterface(s).(time.Time); !ok { + t.Errorf("received %T", s) + } + s = "3s" + if _, ok := StringToInterface(s).(time.Duration); !ok { + t.Errorf("received type %T", s) + } + s = "A string" + + if _, ok := StringToInterface(s).(string); !ok { + t.Error("Should return string") + + } + +} diff --git a/utils/stringset_test.go b/utils/stringset_test.go index da1c00d4e..47616a616 100644 --- a/utils/stringset_test.go +++ b/utils/stringset_test.go @@ -265,3 +265,36 @@ func TestStringSetJoin(t *testing.T) { t.Errorf("Expected %+v, received %+v", ToJSON(expected), ToJSON(rcv)) } } + +func TestStringFieldAsInterface(t *testing.T) { + var s StringSet + fldPath := []string{"test1", "test2", "test3"} + if val, _ := s.FieldAsInterface(fldPath); val != nil { + t.Error("expected error") + } + + fldPath = []string{"test1"} + s = StringSet{ + "test2": struct{}{}, + } + if val, _ := s.FieldAsInterface(fldPath); val != nil { + t.Errorf("expected error") + } + fldPath = []string{"test2"} + if val, err := s.FieldAsInterface(fldPath); err != nil { + t.Errorf("expected %v", val) + } +} + +func TestStringFieldAsString(t *testing.T) { + s := StringSet{} + fldPath := []string{"test1"} + if _, err := s.FieldAsString(fldPath); err == nil { + t.Error("expected error") + } + exp := "{}" + s["test1"] = struct{}{} + if _, err := s.FieldAsString(fldPath); err != nil { + t.Errorf("expected %v got error", exp) + } +} diff --git a/utils/value_formula_test.go b/utils/value_formula_test.go index 871850834..598893702 100644 --- a/utils/value_formula_test.go +++ b/utils/value_formula_test.go @@ -289,3 +289,45 @@ func TestValueFormulaCover(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", expected, received) } } + +func TestValueFieldAsInterface(t *testing.T) { + vf := &ValueFormula{} + fldPath := make([]string, 0) + if val, _ := vf.FieldAsInterface(fldPath); val != nil { + t.Errorf("expected error") + } + fldPath = []string{"test1"} + vf = &ValueFormula{ + Method: "Method", + Params: map[string]interface{}{}, + Static: 22, + } + if val, _ := vf.FieldAsInterface(fldPath); val != nil { + t.Error("expected error") + } + fldPath = []string{"Method"} + if _, err := vf.FieldAsInterface(fldPath); err != nil { + t.Errorf("expected %v ", vf.Method) + } + fldPath = []string{"Method", "second"} + if val, _ := vf.FieldAsInterface(fldPath); val != nil { + t.Error("expected error") + } + fldPath = []string{"Static"} + if _, err := vf.FieldAsInterface(fldPath); err != nil { + t.Errorf("expected %v", vf.Static) + } + fldPath = []string{"Static", "second"} + if val, _ := vf.FieldAsInterface(fldPath); val != nil { + t.Error("expected error") + } + fldPath = []string{"Params"} + if _, err := vf.FieldAsInterface(fldPath); err != nil { + t.Errorf("expected %v", vf.Params) + } + fldPath = []string{"Params", "second"} + if val, _ := vf.FieldAsInterface(fldPath); val != nil { + t.Error("expected error") + } + +}