From 01198533440cf1120dc70ee7b84c6787ef5948c2 Mon Sep 17 00:00:00 2001 From: Tristan Cartledge Date: Thu, 14 Dec 2023 14:46:20 +0000 Subject: [PATCH] chore: add more test coverage --- datamodel/high/v3/path_item_test.go | 38 +++++++++++++++++++++++++++++ datamodel/low/model_builder.go | 11 +++------ datamodel/low/model_builder_test.go | 15 ++++++++++++ 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/datamodel/high/v3/path_item_test.go b/datamodel/high/v3/path_item_test.go index 9a03e01..36619ba 100644 --- a/datamodel/high/v3/path_item_test.go +++ b/datamodel/high/v3/path_item_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/pb33f/libopenapi/datamodel/low" + lowV3 "github.com/pb33f/libopenapi/datamodel/low/v3" v3 "github.com/pb33f/libopenapi/datamodel/low/v3" "github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/orderedmap" @@ -159,3 +160,40 @@ parameters: assert.Equal(t, desired, strings.TrimSpace(string(rend))) } + +func TestPathItem_GetOperations_NoLow(t *testing.T) { + pi := &PathItem{ + Delete: &Operation{}, + Post: &Operation{}, + Get: &Operation{}, + } + ops := pi.GetOperations() + + expectedOrderOfOps := []string{"get", "post", "delete"} + actualOrder := []string{} + + for pair := orderedmap.First(ops); pair != nil; pair = pair.Next() { + actualOrder = append(actualOrder, pair.Key()) + } + + assert.Equal(t, expectedOrderOfOps, actualOrder) +} + +func TestPathItem_GetOperations_LowWithUnsetOperations(t *testing.T) { + pi := &PathItem{ + Delete: &Operation{}, + Post: &Operation{}, + Get: &Operation{}, + low: &lowV3.PathItem{}, + } + ops := pi.GetOperations() + + expectedOrderOfOps := []string{"get", "post", "delete"} + actualOrder := []string{} + + for pair := orderedmap.First(ops); pair != nil; pair = pair.Next() { + actualOrder = append(actualOrder, pair.Key()) + } + + assert.Equal(t, expectedOrderOfOps, actualOrder) +} diff --git a/datamodel/low/model_builder.go b/datamodel/low/model_builder.go index a396365..4e74a61 100644 --- a/datamodel/low/model_builder.go +++ b/datamodel/low/model_builder.go @@ -55,10 +55,7 @@ func BuildModel(node *yaml.Node, model interface{}) error { switch kind { case reflect.Struct, reflect.Slice, reflect.Map, reflect.Pointer: vn = utils.NodeAlias(vn) - err := SetField(&field, vn, kn) - if err != nil { - return err - } + SetField(&field, vn, kn) default: return fmt.Errorf("unable to parse unsupported type: %v", kind) } @@ -71,9 +68,9 @@ func BuildModel(node *yaml.Node, model interface{}) error { // SetField accepts a field reflection value, a yaml.Node valueNode and a yaml.Node keyNode. Using reflection, the // function will attempt to set the value of the field based on the key and value nodes. This method is only useful // for low-level models, it has no value to high-level ones. -func SetField(field *reflect.Value, valueNode *yaml.Node, keyNode *yaml.Node) error { +func SetField(field *reflect.Value, valueNode *yaml.Node, keyNode *yaml.Node) { if valueNode == nil { - return nil + return } switch field.Type() { @@ -451,7 +448,7 @@ func SetField(field *reflect.Value, valueNode *yaml.Node, keyNode *yaml.Node) er // we want to ignore everything else, each model handles its own complex types. break } - return nil + return } // BuildModelAsync is a convenience function for calling BuildModel from a goroutine, requires a sync.WaitGroup diff --git a/datamodel/low/model_builder_test.go b/datamodel/low/model_builder_test.go index 1c1b3c0..cd4ef19 100644 --- a/datamodel/low/model_builder_test.go +++ b/datamodel/low/model_builder_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/pb33f/libopenapi/orderedmap" + "github.com/pb33f/libopenapi/utils" "github.com/stretchr/testify/assert" "gopkg.in/yaml.v3" ) @@ -407,3 +408,17 @@ func TestBuildModelAsync(t *testing.T) { wg.Wait() assert.Equal(t, 3, orderedmap.Len(ins.Thing.Value)) } + +func TestSetField_NilValueNode(t *testing.T) { + assert.NotPanics(t, func() { + SetField(nil, nil, nil) + }) +} + +func TestBuildModelAsync_HandlesError(t *testing.T) { + errs := []error{} + wg := sync.WaitGroup{} + wg.Add(1) + BuildModelAsync(utils.CreateStringNode("cake"), "cake", &wg, &errs) + assert.NotEmpty(t, errs) +}