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