From 2945408f0dd4db2a34a255b5114eff2875fe4617 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 16 Jan 2023 13:18:56 +0100 Subject: [PATCH] add test for errWithTrace --- bundle/schema/schema.go | 6 ++---- bundle/schema/schema_test.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/bundle/schema/schema.go b/bundle/schema/schema.go index 9368cfb36..c6ea8cfbf 100644 --- a/bundle/schema/schema.go +++ b/bundle/schema/schema.go @@ -9,7 +9,6 @@ import ( const MaxHistoryOccurances = 3 -// TODO: should omit empty denote non required fields in the json schema? // TODO: add tests for the error cases, forcefully triggering them // TODO: Add support for refs in case of a cycle // TODO: handle case of self referential pointers in structs @@ -83,15 +82,14 @@ func javascriptType(golangType reflect.Type) (JavascriptType, error) { } } -// TODO: add a simple test for this func errWithTrace(prefix string, trace *list.List) error { traceString := "root" curr := trace.Front() - for curr.Next() != nil { + for curr != nil { traceString += " -> " + curr.Value.(string) curr = curr.Next() } - return fmt.Errorf("[ERROR] " + prefix + ". traveral trace: " + traceString) + return fmt.Errorf("[ERROR] " + prefix + ". traversal trace: " + traceString) } // A wrapper over toProperty function with checks for an cycles to avoid being diff --git a/bundle/schema/schema_test.go b/bundle/schema/schema_test.go index f03514fe5..154ad3470 100644 --- a/bundle/schema/schema_test.go +++ b/bundle/schema/schema_test.go @@ -1,6 +1,7 @@ package schema import ( + "container/list" "encoding/json" "reflect" "testing" @@ -338,6 +339,21 @@ func TestEmbeddedStructSchema(t *testing.T) { assert.Equal(t, expected, string(jsonSchema)) } +func TestErrorWithTrace(t *testing.T) { + debugTrace := list.New() + err := errWithTrace("with empty trace", debugTrace) + assert.ErrorContains(t, err, "[ERROR] with empty trace. traversal trace: root") + + debugTrace.PushBack("resources") + err = errWithTrace("with depth = 1", debugTrace) + assert.ErrorContains(t, err, "[ERROR] with depth = 1. traversal trace: root -> resources") + + debugTrace.PushBack("pipelines") + debugTrace.PushBack("datasets") + err = errWithTrace("with depth = 4", debugTrace) + assert.ErrorContains(t, err, "[ERROR] with depth = 4. traversal trace: root -> resources -> pipelines -> datasets") +} + // // Only for testing bundle, will be removed // func TestBundleSchema(t *testing.T) { // elem := config.Root{}