From 8dc0d445137a3f2c545535ab2de24ca046a725b4 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Thu, 22 Aug 2024 20:12:59 -0400 Subject: [PATCH] lang: Add an extra fail scenario to our test suite Let's us write tests for Validate failures. --- lang/interpret_test.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/lang/interpret_test.go b/lang/interpret_test.go index f62d1d54..7f96b6f9 100644 --- a/lang/interpret_test.go +++ b/lang/interpret_test.go @@ -1403,6 +1403,7 @@ func TestAstFunc3(t *testing.T) { const magicErrorGraph = "errGraph: " const magicErrorInterpret = "errInterpret: " const magicErrorAutoEdge = "errAutoEdge: " + const magicErrorValidate = "errValidate: " const magicEmpty = "# empty!" dir, err := util.TestDirFull() if err != nil { @@ -1550,6 +1551,7 @@ func TestAstFunc3(t *testing.T) { failGraph := false failInterpret := false failAutoEdge := false + failValidate := false if strings.HasPrefix(expstr, magicError) { errStr = strings.TrimPrefix(expstr, magicError) expstr = errStr @@ -1594,6 +1596,11 @@ func TestAstFunc3(t *testing.T) { expstr = errStr failAutoEdge = true } + if strings.HasPrefix(expstr, magicErrorValidate) { + errStr = strings.TrimPrefix(expstr, magicErrorValidate) + expstr = errStr + failValidate = true + } } fail := errStr != "" @@ -2183,7 +2190,9 @@ func TestAstFunc3(t *testing.T) { return } defer func() { - if err := ge.Shutdown(); err != nil { + // skip errors if failValidate is true since it + // would cause an error here too as well... + if err := ge.Shutdown(); err != nil && !failValidate { // TODO: cause the final exit code to be non-zero t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: engine Shutdown failed with: %+v", index, err) @@ -2197,11 +2206,27 @@ func TestAstFunc3(t *testing.T) { return } - if err := ge.Validate(); err != nil { // validate the new graph + err = ge.Validate() // validate the new graph + if (!fail || !failValidate) && err != nil { t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: error validating the new graph: %+v", index, err) return } + if failValidate && err != nil { + s := err.Error() // convert to string + if s != expstr { + t.Errorf("test #%d: FAIL", index) + t.Errorf("test #%d: expected different error", index) + t.Logf("test #%d: err: %s", index, s) + t.Logf("test #%d: exp: %s", index, expstr) + } + return + } + if failValidate && err == nil { + t.Errorf("test #%d: FAIL", index) + t.Errorf("test #%d: validate passed, expected fail", index) + return + } // TODO: apply the global metaparams to the graph