Skip to content

Commit

Permalink
Fix handling of nil data and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DerAndereAndi committed Dec 30, 2023
1 parent 1940014 commit 1bdad4f
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
12 changes: 10 additions & 2 deletions spine/model/commandframe_additions.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (f *FilterData) SelectorMatch(item any) bool {

// Get the field for a given functionType
func (f *FilterType) SetDataForFunction(tagType EEBusTagTypeType, fct FunctionType, data any) {
if data == nil || reflect.ValueOf(data).Kind() != reflect.Ptr {
return
}

v := reflect.ValueOf(*f)
dv := reflect.ValueOf(f).Elem()
for i := 0; i < v.NumField(); i++ {
Expand Down Expand Up @@ -99,7 +103,7 @@ func (f *FilterType) SetDataForFunction(tagType EEBusTagTypeType, fct FunctionTy
break
}

if data == nil || (reflect.ValueOf(data).Kind() == reflect.Ptr && reflect.ValueOf(data).IsNil()) {
if reflect.ValueOf(data).IsNil() {
typ := reflect.TypeOf(data).Elem()
ff.Set(reflect.New(typ))
return
Expand Down Expand Up @@ -177,6 +181,10 @@ type CmdData struct {

// Get the field for a given functionType
func (cmd *CmdType) SetDataForFunction(fct FunctionType, data any) {
if data == nil || reflect.ValueOf(data).Kind() != reflect.Ptr {
return
}

v := reflect.ValueOf(*cmd)
dv := reflect.ValueOf(cmd).Elem()
for i := 0; i < v.NumField(); i++ {
Expand Down Expand Up @@ -208,7 +216,7 @@ func (cmd *CmdType) SetDataForFunction(fct FunctionType, data any) {
break
}

if data == nil || (reflect.ValueOf(data).Kind() == reflect.Ptr && reflect.ValueOf(data).IsNil()) {
if reflect.ValueOf(data).IsNil() {
typ := reflect.TypeOf(data).Elem()
ff.Set(reflect.New(typ))
return
Expand Down
81 changes: 81 additions & 0 deletions spine/model/commandframe_additions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,75 @@ import (
"testing"

"github.com/enbility/eebus-go/spine/model"
"github.com/enbility/eebus-go/util"
"github.com/stretchr/testify/assert"
)

func TestFilterType_Selector_Data(t *testing.T) {
data := &model.ElectricalConnectionDescriptionListDataSelectorsType{
ElectricalConnectionId: util.Ptr(model.ElectricalConnectionIdType(1)),
ScopeType: util.Ptr(model.ScopeTypeTypeACPower),
}

sut := &model.FilterType{
ElectricalConnectionDescriptionListDataSelectors: data,
}

// Act
cmdData, err := sut.Data()
assert.Nil(t, err)
assert.NotNil(t, cmdData)
assert.Equal(t, model.FunctionTypeElectricalConnectionDescriptionListData, *cmdData.Function)
assert.Equal(t, data, cmdData.Selector)
}

func TestFilterType_Selector_SetDataForFunction(t *testing.T) {
cmd := model.FilterType{}
cmd.SetDataForFunction(model.EEBusTagTypeTypeSelector, model.FunctionTypeElectricalConnectionDescriptionListData, &model.ElectricalConnectionDescriptionListDataSelectorsType{})
assert.NotNil(t, cmd.ElectricalConnectionDescriptionListDataSelectors)

cmd = model.FilterType{}
cmd.SetDataForFunction(model.EEBusTagTypeTypeSelector, model.FunctionTypeElectricalConnectionDescriptionListData, nil)
assert.Nil(t, cmd.ElectricalConnectionDescriptionListDataSelectors)

var test *model.ElectricalConnectionDescriptionListDataSelectorsType
cmd = model.FilterType{}
cmd.SetDataForFunction(model.EEBusTagTypeTypeSelector, model.FunctionTypeElectricalConnectionDescriptionListData, test)
assert.NotNil(t, cmd.ElectricalConnectionDescriptionListDataSelectors)
}

func TestFilterType_Elements_Data(t *testing.T) {
data := &model.ElectricalConnectionDescriptionDataElementsType{
ElectricalConnectionId: util.Ptr(model.ElementTagType{}),
}

sut := &model.FilterType{
ElectricalConnectionDescriptionDataElements: data,
}

// Act
cmdData, err := sut.Data()
assert.Nil(t, err)
assert.NotNil(t, cmdData)
assert.Equal(t, model.FunctionTypeElectricalConnectionDescriptionListData, *cmdData.Function)
assert.Equal(t, data, cmdData.Elements)
}

func TestFilterType_Elements_SetDataForFunction(t *testing.T) {
cmd := model.FilterType{}
cmd.SetDataForFunction(model.EEbusTagTypeTypeElements, model.FunctionTypeElectricalConnectionDescriptionListData, &model.ElectricalConnectionDescriptionDataElementsType{})
assert.NotNil(t, cmd.ElectricalConnectionDescriptionDataElements)

cmd = model.FilterType{}
cmd.SetDataForFunction(model.EEbusTagTypeTypeElements, model.FunctionTypeElectricalConnectionDescriptionListData, nil)
assert.Nil(t, cmd.ElectricalConnectionDescriptionDataElements)

var test *model.ElectricalConnectionDescriptionDataElementsType
cmd = model.FilterType{}
cmd.SetDataForFunction(model.EEbusTagTypeTypeElements, model.FunctionTypeElectricalConnectionDescriptionListData, test)
assert.NotNil(t, cmd.ElectricalConnectionDescriptionDataElements)
}

func TestCmdType_Data(t *testing.T) {
data := &model.NodeManagementDetailedDiscoveryDataType{
SpecificationVersionList: &model.NodeManagementSpecificationVersionListType{[]model.SpecificationVersionDataType{model.SpecificationVersionDataType("dummy")}},
Expand All @@ -25,6 +91,21 @@ func TestCmdType_Data(t *testing.T) {
assert.Equal(t, data, cmdData.Value)
}

func TestCmdType_SetDataForFunction(t *testing.T) {
cmd := model.CmdType{}
cmd.SetDataForFunction(model.FunctionTypeElectricalConnectionDescriptionListData, &model.ElectricalConnectionDescriptionListDataType{})
assert.NotNil(t, cmd.ElectricalConnectionDescriptionListData)

cmd = model.CmdType{}
cmd.SetDataForFunction(model.FunctionTypeElectricalConnectionDescriptionListData, nil)
assert.Nil(t, cmd.ElectricalConnectionDescriptionListData)

var test *model.ElectricalConnectionDescriptionListDataType
cmd = model.CmdType{}
cmd.SetDataForFunction(model.FunctionTypeElectricalConnectionDescriptionListData, test)
assert.NotNil(t, cmd.ElectricalConnectionDescriptionListData)
}

func TestCmdType_ExtractFilter_NoFilter(t *testing.T) {
sut := &model.CmdType{
NodeManagementDetailedDiscoveryData: &model.NodeManagementDetailedDiscoveryDataType{},
Expand Down

0 comments on commit 1bdad4f

Please sign in to comment.