From 1d67fb694bc1a95487f7f74e9286db1b877ce251 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 9 Oct 2024 12:18:56 +0300 Subject: [PATCH] use regex for vm output and expected content --- driver/testdriver.go | 26 ++++++++++++++ internal/testutil/TestEngine.go | 2 +- test_engine/menu_traversal_test.go | 57 +++++++++++++++++++++++------- 3 files changed, 72 insertions(+), 13 deletions(-) diff --git a/driver/testdriver.go b/driver/testdriver.go index 67f534b..c6d0a69 100644 --- a/driver/testdriver.go +++ b/driver/testdriver.go @@ -4,6 +4,7 @@ import ( "encoding/json" "log" "os" + "regexp" ) type Step struct { @@ -11,6 +12,19 @@ type Step struct { ExpectedContent string `json:"expectedContent"` } +func (s *Step) MatchesExpectedContent(content []byte) (bool, error) { + pattern := `.*\?.*|.*` + re, err := regexp.Compile(pattern) + if err != nil { + return false, err + } + // Check if the content matches the regex pattern + if re.Match(content) { + return true, nil + } + return false, nil +} + // Group represents a group of steps type Group struct { Name string `json:"name"` @@ -23,6 +37,18 @@ type TestCase struct { ExpectedContent string } +func (s *TestCase) MatchesExpectedContent(content []byte) (bool, error) { + re, err := regexp.Compile(s.ExpectedContent) + if err != nil { + return false, err + } + // Check if the content matches the regex pattern + if re.Match(content) { + return true, nil + } + return false, nil +} + // DataGroup represents the overall structure of the JSON. type DataGroup struct { Groups []Group `json:"groups"` diff --git a/internal/testutil/TestEngine.go b/internal/testutil/TestEngine.go index e7320f3..16502e8 100644 --- a/internal/testutil/TestEngine.go +++ b/internal/testutil/TestEngine.go @@ -97,6 +97,6 @@ func TestEngine(sessionId string) (engine.Engine, func()) { logg.Infof("testengine storage closed") } - en = en.WithDebug(nil) + //en = en.WithDebug(nil) return en, cleanFn } diff --git a/test_engine/menu_traversal_test.go b/test_engine/menu_traversal_test.go index 96f216a..9298bbf 100644 --- a/test_engine/menu_traversal_test.go +++ b/test_engine/menu_traversal_test.go @@ -76,7 +76,11 @@ func TestAccountCreationSuccessful(t *testing.T) { t.Fatalf("Test case '%s' failed during Flush: %v", group.Name, err) } b := w.Bytes() - if !bytes.Equal(b, []byte(step.ExpectedContent)) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) } } @@ -116,7 +120,11 @@ func TestAccountRegistrationRejectTerms(t *testing.T) { } b := w.Bytes() - if !bytes.Equal(b, []byte(step.ExpectedContent)) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) } } @@ -153,8 +161,11 @@ func TestSendWithInvalidInputs(t *testing.T) { // Replace placeholder {public_key} with the actual dynamic public key expectedContent := bytes.Replace([]byte(step.ExpectedContent), []byte("{public_key}"), []byte(publicKey), -1) - - if !bytes.Equal(b, expectedContent) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expectedContent, b) } } @@ -185,7 +196,11 @@ func TestMyAccount_Check_My_Balance(t *testing.T) { t.Errorf("Test case '%s' failed during Flush: %v", group.Name, err) } b := w.Bytes() - if !bytes.Equal(b, []byte(step.ExpectedContent)) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) } } @@ -216,7 +231,11 @@ func TestMainMenuHelp(t *testing.T) { } b := w.Bytes() - if !bytes.Equal(b, []byte(step.ExpectedContent)) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) } } @@ -247,7 +266,11 @@ func TestMainMenuQuit(t *testing.T) { } b := w.Bytes() - if !bytes.Equal(b, []byte(step.ExpectedContent)) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) } } @@ -278,7 +301,11 @@ func TestMyAccount_Check_Community_Balance(t *testing.T) { t.Errorf("Test case '%s' failed during Flush: %v", group.Name, err) } b := w.Bytes() - if !bytes.Equal(b, []byte(step.ExpectedContent)) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) } } @@ -311,10 +338,12 @@ func TestMyAccount_MyAddress(t *testing.T) { b := w.Bytes() publicKey := extractPublicKey(b) - expectedContent := bytes.Replace([]byte(step.ExpectedContent), []byte("{public_key}"), []byte(publicKey), -1) - - if !bytes.Equal(b, expectedContent) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expectedContent, b) } } @@ -347,7 +376,11 @@ func TestGroups(t *testing.T) { t.Errorf("Test case '%s' failed during Flush: %v", tt.Name, err) } b := w.Bytes() - if !bytes.Equal(b, []byte(tt.ExpectedContent)) { + match, err := tt.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", tt.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", tt.ExpectedContent, b) }