forked from urdt/ussd
use one test driver
This commit is contained in:
parent
3afe35b44f
commit
810dd74e43
@ -2,23 +2,86 @@ package driver
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StepTest struct {
|
type Step struct {
|
||||||
Input string `json:"input"`
|
Input string `json:"input"`
|
||||||
ExpectedContent string `json:"expectedContent"`
|
ExpectedContent string `json:"expectedContent"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group represents a group of steps.
|
func (s *Step) MatchesExpectedContent(content []byte) (bool, error) {
|
||||||
type GroupTest struct {
|
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"`
|
Name string `json:"name"`
|
||||||
Steps []Step `json:"steps"`
|
Steps []Step `json:"steps"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TestCase struct {
|
||||||
|
Name string
|
||||||
|
Input 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 []GroupTest `json:"groups"`
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadTestGroups(filePath string) (DataGroup, error) {
|
func LoadTestGroups(filePath string) (DataGroup, error) {
|
||||||
@ -31,24 +94,12 @@ func LoadTestGroups(filePath string) (DataGroup, error) {
|
|||||||
return sessionsData, err
|
return sessionsData, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateTestCases(group DataGroup) []struct {
|
func CreateTestCases(group DataGroup) []TestCase {
|
||||||
Name string
|
var tests []TestCase
|
||||||
Input string
|
|
||||||
ExpectedContent string
|
|
||||||
} {
|
|
||||||
var tests []struct {
|
|
||||||
Name string
|
|
||||||
Input string
|
|
||||||
ExpectedContent string
|
|
||||||
}
|
|
||||||
for _, group := range group.Groups {
|
for _, group := range group.Groups {
|
||||||
for _, step := range group.Steps {
|
for _, step := range group.Steps {
|
||||||
// Create a test case for each group
|
// Create a test case for each group
|
||||||
tests = append(tests, struct {
|
tests = append(tests, TestCase{
|
||||||
Name string
|
|
||||||
Input string
|
|
||||||
ExpectedContent string
|
|
||||||
}{
|
|
||||||
Name: group.Name,
|
Name: group.Name,
|
||||||
Input: step.Input,
|
Input: step.Input,
|
||||||
ExpectedContent: step.ExpectedContent,
|
ExpectedContent: step.ExpectedContent,
|
||||||
|
Loading…
Reference in New Issue
Block a user