use regex for vm output and expected content

This commit is contained in:
Carlosokumu 2024-10-09 12:18:56 +03:00
parent 3d409a7a59
commit 1d67fb694b
Signed by: carlos
GPG Key ID: 7BD6BC8160A5C953
3 changed files with 72 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"os" "os"
"regexp"
) )
type Step struct { type Step struct {
@ -11,6 +12,19 @@ type Step struct {
ExpectedContent string `json:"expectedContent"` 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 // Group represents a group of steps
type Group struct { type Group struct {
Name string `json:"name"` Name string `json:"name"`
@ -23,6 +37,18 @@ type TestCase struct {
ExpectedContent string 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. // DataGroup represents the overall structure of the JSON.
type DataGroup struct { type DataGroup struct {
Groups []Group `json:"groups"` Groups []Group `json:"groups"`

View File

@ -97,6 +97,6 @@ func TestEngine(sessionId string) (engine.Engine, func()) {
logg.Infof("testengine storage closed") logg.Infof("testengine storage closed")
} }
en = en.WithDebug(nil) //en = en.WithDebug(nil)
return en, cleanFn return en, cleanFn
} }

View File

@ -76,7 +76,11 @@ func TestAccountCreationSuccessful(t *testing.T) {
t.Fatalf("Test case '%s' failed during Flush: %v", group.Name, err) t.Fatalf("Test case '%s' failed during Flush: %v", group.Name, err)
} }
b := w.Bytes() 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) 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() 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) 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 // Replace placeholder {public_key} with the actual dynamic public key
expectedContent := bytes.Replace([]byte(step.ExpectedContent), []byte("{public_key}"), []byte(publicKey), -1) expectedContent := bytes.Replace([]byte(step.ExpectedContent), []byte("{public_key}"), []byte(publicKey), -1)
match, err := step.MatchesExpectedContent(b)
if !bytes.Equal(b, expectedContent) { 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) 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) t.Errorf("Test case '%s' failed during Flush: %v", group.Name, err)
} }
b := w.Bytes() 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) 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() 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) 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() 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) 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) t.Errorf("Test case '%s' failed during Flush: %v", group.Name, err)
} }
b := w.Bytes() 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) 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() b := w.Bytes()
publicKey := extractPublicKey(b) publicKey := extractPublicKey(b)
expectedContent := bytes.Replace([]byte(step.ExpectedContent), []byte("{public_key}"), []byte(publicKey), -1) expectedContent := bytes.Replace([]byte(step.ExpectedContent), []byte("{public_key}"), []byte(publicKey), -1)
match, err := step.MatchesExpectedContent(b)
if !bytes.Equal(b, expectedContent) { 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) 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) t.Errorf("Test case '%s' failed during Flush: %v", tt.Name, err)
} }
b := w.Bytes() 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) t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", tt.ExpectedContent, b)
} }