ussd/driver/groupdriver.go

112 lines
2.3 KiB
Go
Raw Permalink Normal View History

2024-10-07 22:35:41 +02:00
package driver
import (
"encoding/json"
2024-10-09 15:44:51 +02:00
"log"
2024-10-07 22:35:41 +02:00
"os"
2024-10-09 15:44:51 +02:00
"regexp"
2024-10-07 22:35:41 +02:00
)
2024-10-09 15:44:51 +02:00
type Step struct {
2024-10-07 22:35:41 +02:00
Input string `json:"input"`
ExpectedContent string `json:"expectedContent"`
}
2024-10-09 15:44:51 +02:00
func (s *Step) MatchesExpectedContent(content []byte) (bool, error) {
2024-10-09 21:21:11 +02:00
pattern := regexp.QuoteMeta(s.ExpectedContent)
re, err := regexp.Compile(pattern)
if err != nil {
return false, err
}
2024-10-09 21:21:11 +02:00
if re.Match([]byte(content)) {
2024-10-09 15:44:51 +02:00
return true, nil
}
return false, nil
}
// Group represents a group of steps
type Group struct {
2024-10-07 22:35:41 +02:00
Name string `json:"name"`
Steps []Step `json:"steps"`
}
2024-10-09 15:44:51 +02:00
type TestCase struct {
Name string
Input string
ExpectedContent string
}
func (s *TestCase) MatchesExpectedContent(content []byte) (bool, error) {
2024-10-09 21:21:11 +02:00
pattern := regexp.QuoteMeta(s.ExpectedContent)
re, err := regexp.Compile(pattern)
2024-10-09 15:44:51 +02:00
if err != nil {
return false, err
}
// Check if the content matches the regex pattern
if re.Match(content) {
return true, nil
}
return false, nil
}
2024-10-07 22:35:41 +02:00
// DataGroup represents the overall structure of the JSON.
type DataGroup struct {
2024-10-09 15:44:51 +02:00
Groups []Group `json:"groups"`
}
type Session struct {
Name string `json:"name"`
Groups []Group `json:"groups"`
}
func ReadData() []Session {
data, err := os.ReadFile("test_setup.json")
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
// Unmarshal JSON data
var sessions []Session
err = json.Unmarshal(data, &sessions)
if err != nil {
log.Fatalf("Failed to unmarshal JSON: %v", err)
}
return sessions
}
func FilterGroupsByName(groups []Group, name string) []Group {
var filteredGroups []Group
for _, group := range groups {
if group.Name == name {
filteredGroups = append(filteredGroups, group)
}
}
return filteredGroups
2024-10-07 22:35:41 +02:00
}
func LoadTestGroups(filePath string) (DataGroup, error) {
var sessionsData DataGroup
data, err := os.ReadFile(filePath)
if err != nil {
return sessionsData, err
}
err = json.Unmarshal(data, &sessionsData)
return sessionsData, err
}
2024-10-09 15:44:51 +02:00
func CreateTestCases(group DataGroup) []TestCase {
var tests []TestCase
2024-10-07 22:35:41 +02:00
for _, group := range group.Groups {
for _, step := range group.Steps {
// Create a test case for each group
2024-10-09 15:44:51 +02:00
tests = append(tests, TestCase{
2024-10-07 22:35:41 +02:00
Name: group.Name,
Input: step.Input,
ExpectedContent: step.ExpectedContent,
})
}
}
return tests
}