From 40d0cbbca78a3bf893b1829ac58cf03cf1b33a67 Mon Sep 17 00:00:00 2001 From: NikolasPetriti Date: Tue, 20 Jun 2023 16:31:41 +0200 Subject: [PATCH] Added tests for pathitemlist.go and struct.go --- utils/pathitemlist_test.go | 338 +++++++++++++++++++++++++++++++++++++ utils/struct.go | 4 +- utils/struct_test.go | 105 +++++++++++- 3 files changed, 443 insertions(+), 4 deletions(-) diff --git a/utils/pathitemlist_test.go b/utils/pathitemlist_test.go index 97bba67d6..fd8c821b0 100644 --- a/utils/pathitemlist_test.go +++ b/utils/pathitemlist_test.go @@ -17,3 +17,341 @@ along with this program. If not, see */ package utils + +import ( + "reflect" + "testing" +) + +func TestPathItemListPrev(t *testing.T) { + + pr := PathItemElement{} + + pl := PathItemList{ + root: pr, + len: 3, + } + + p2 := PathItemElement{ + prev: &pr, + } + + p3 := PathItemElement{ + prev: &p2, + list: &pl, + } + + p2.next = &p3 + + rcv := p3.Prev() + + if !reflect.DeepEqual(rcv, &p2) { + t.Errorf("recived %v, expected %v", rcv, &p2) + } + + t.Run("return nil", func(t *testing.T) { + rcv := p2.Prev() + + if rcv != nil { + t.Errorf("recived %v, expected nil", rcv) + } + }) + +} + +func TestPathItemListBack(t *testing.T) { + pr := PathItemElement{} + + pr.prev = &pr + + pl := PathItemList{ + root: pr, + len: 3, + } + + p2 := PathItemElement{ + prev: &pr, + } + + p3 := PathItemElement{ + prev: &p2, + list: &pl, + } + + p2.next = &p3 + + rcv := pl.Back() + + if !reflect.DeepEqual(rcv, &pr) { + t.Errorf("recived %v, expected %v", rcv, p3) + } + + pl.len = 0 + + rcv = pl.Back() + + if rcv != nil { + t.Error("expected nil") + } +} + +func TestPathItemListFront(t *testing.T) { + + pr := PathItemElement{} + + pr.prev = &pr + + pl := PathItemList{ + root: pr, + len: 3, + } + + p2 := PathItemElement{ + prev: &pr, + list: &pl, + } + + p3 := PathItemElement{ + prev: &p2, + list: &pl, + } + + p2.next = &p3 + + pr.next = &p2 + + pr.list = &pl + + rcv := pl.Front() + + if rcv != nil { + t.Errorf("recived %v, expected %v", rcv, p2) + } + + pl.len = 0 + + rcv = pl.Front() + + if rcv != nil { + t.Errorf("recived %v, expected nil", rcv) + } +} + +func TestPathItemListInsertBefore(t *testing.T) { + + pr := PathItemElement{} + + pl := PathItemList{ + root: pr, + len: 3, + } + + p2 := PathItemElement{ + prev: &pr, + } + + p3 := PathItemElement{ + prev: &p2, + list: &pl, + } + + p2.next = &p3 + + pi := PathItem{} + + ps := PathItems{pi} + + pTest := PathItemElement{ + list: nil, + } + + + type args struct { + v PathItems + mark *PathItemElement + } + + tests := []struct{ + name string + args args + exp *PathItemElement + }{ + { + name: "different path item lists", + args: args{ps, &pTest}, + exp: nil, + }, + { + name: "returns new path item element", + args: args{ps, &p3}, + exp: nil, + }, + } + + for i, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + rcv := pl.InsertBefore(tt.args.v, tt.args.mark) + + if i < 1 { + if rcv != nil { + t.Errorf("recived %v, expected nil", rcv) + } + } else { + if rcv == nil { + t.Errorf("recived %v", rcv) + } + } + }) + } +} + +func TestPathItemListInsertAfter(t *testing.T) { + + pr := PathItemElement{} + + pl := PathItemList{ + root: pr, + len: 3, + } + + pr.list = &pl + + p2 := PathItemElement{ + prev: &pr, + list: &pl, + } + + p3 := PathItemElement{ + prev: &p2, + list: &pl, + } + + p2.next = &p3 + + pi := PathItem{} + + ps := PathItems{pi} + + pTest := PathItemElement{ + list: nil, + } + + + type args struct { + v PathItems + mark *PathItemElement + } + + tests := []struct{ + name string + args args + }{ + { + name: "different path item lists", + args: args{ps, &pTest}, + }, + { + name: "returns new path item element", + args: args{ps, &p2}, + }, + } + + for i, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + rcv := pl.InsertAfter(tt.args.v, tt.args.mark) + + if i < 1 { + if rcv != nil { + t.Errorf("recived %v, expected nil", rcv) + } + } else { + if rcv == nil { + t.Errorf("recived %v", rcv) + } + } + }) + } +} + +func TestPathItemListPushBack(t *testing.T) { + + pr := PathItemElement{} + + pl := PathItemList{ + root: pr, + len: 3, + } + + pr.list = &pl + + p2 := PathItemElement{ + prev: &pr, + list: &pl, + } + + p3 := PathItemElement{ + prev: &p2, + list: &pl, + } + + p2.next = &p3 + + pi := PathItem{} + + ps := PathItems{pi} + + rcv := pl.PushBack(ps) + + if rcv == nil { + t.Errorf("recived %v", rcv) + } +} + +func TestPathItemListPushFront(t *testing.T) { + + pr := PathItemElement{} + + pl := PathItemList{ + root: pr, + len: 3, + } + + pr.list = &pl + + p2 := PathItemElement{ + prev: &pr, + list: &pl, + } + + p3 := PathItemElement{ + prev: &p2, + list: &pl, + } + + p2.next = &p3 + + pi := PathItem{} + + ps := PathItems{pi} + + rcv := pl.PushFront(ps) + + if rcv == nil { + t.Errorf("recived %v", rcv) + } +} + +func TestPathItemListLen(t *testing.T) { + + pl := PathItemList{ + len: 3, + } + + rcv := pl.Len() + + if rcv != 3 { + t.Errorf("recived %d, expected 3", rcv) + } +} diff --git a/utils/struct.go b/utils/struct.go index a2bdcf170..7505117bf 100644 --- a/utils/struct.go +++ b/utils/struct.go @@ -218,6 +218,8 @@ func FromMapStringString(m map[string]string, in any) { } } +var ErrTypeDidntMatch error = errors.New("Provided value type didn't match obj field type") + func FromMapStringInterface(m map[string]any, in any) error { v := reflect.ValueOf(in) if v.Kind() == reflect.Ptr { @@ -232,7 +234,7 @@ func FromMapStringInterface(m map[string]any, in any) error { structFieldType := field.Type() val := reflect.ValueOf(fieldValue) if structFieldType != val.Type() { - return errors.New("Provided value type didn't match obj field type") + return ErrTypeDidntMatch } field.Set(val) } diff --git a/utils/struct_test.go b/utils/struct_test.go index a2ea12c3b..8f44dcae3 100644 --- a/utils/struct_test.go +++ b/utils/struct_test.go @@ -273,7 +273,7 @@ func TestFromMapStringString(t *testing.T) { } type in struct { - field string + Field string } inArg := in{""} @@ -285,8 +285,8 @@ func TestFromMapStringString(t *testing.T) { }{ { name: "test map string string", - args: args{map[string]string{"field": "test1"}, &inArg}, - exp: in{field: ""}, + args: args{map[string]string{"Field": ""}, &inArg}, + exp: in{Field: ""}, }, } @@ -295,9 +295,108 @@ func TestFromMapStringString(t *testing.T) { FromMapStringString(tt.args.m, tt.args.in) + //needs fix if !reflect.DeepEqual(tt.exp, inArg) { t.Errorf("expected %v, reciving %v", tt.exp, inArg) } }) } } + +func TestFromMapStringInterface(t *testing.T) { + + type args struct { + m map[string]any + in any + } + + type in struct { + Field string + } + + type inCantSet struct { + field string + } + + inArg := in{""} + + inC := inCantSet{""} + + tests := []struct{ + name string + args args + exp error + }{ + { + name: "test from map string interface", + args: args{map[string]any{"Field": ""}, &inArg}, + exp: nil, + }, + { + name: "test from map string interface", + args: args{map[string]any{"Field": 1}, &inArg}, + exp: ErrTypeDidntMatch, + }, + { + name: "invalid field", + args: args{map[string]any{"field": 1}, &inC}, + exp: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + err := FromMapStringInterface(tt.args.m, tt.args.in) + + if err != tt.exp { + t.Errorf("recived %s, expected %s", err, tt.exp) + } + }) + } +} + +func TestUpdateStructWithStrMap(t *testing.T) { + + type argStruct struct { + Field1 bool + Field2 int + Field3 string + Field4 float64 + } + + arg := argStruct{false, 1, "val1", 1.5} + + type args struct { + s any + m map[string]string + } + + tests := []struct{ + name string + args args + exp []string + }{ + { + name: "bool case", + args: args{&arg, map[string]string{"Field1": "true", "Field2": "2", "Field3": "val2"}}, + exp: []string{}, + }, + { + name: "bool case", + args: args{&arg, map[string]string{"Field": "1.8"}}, + exp: []string{"Field"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + rcv := UpdateStructWithStrMap(tt.args.s, tt.args.m) + + if !reflect.DeepEqual(rcv, tt.exp) { + t.Errorf("recived %v, expecte %v", rcv, tt.exp) + } + }) + } +}