package main import ( "encoding/json" "fmt" "log" "os" "regexp" "strings" ) const definition_file = "tests.json" type TestDefinition struct { TestExpression *Regexp `json:"test_expression"` FailCandidates []string `json:"fail_candidates"` SucceedCandidates []string `json:"succeed_candidates"` } type Regexp struct { *regexp.Regexp } func (r *Regexp) UnmarshalJSON(b []byte) error { rg, e := regexp.Compile(strings.Trim(string(b), "\"")) if e != nil { return e } r.Regexp = rg return nil } func main() { var test_definition TestDefinition b, e := os.ReadFile(definition_file) if e != nil { log.Fatalf("Unable to read test definition file: %s\n", e) } e = json.Unmarshal(b, &test_definition) if e != nil { log.Fatalf("Unable to parse test definition file: %s\n", e) } all_passed := true for _, item := range test_definition.FailCandidates { if test_definition.TestExpression.MatchString(item) == true { all_passed = false fmt.Printf("FAILED on input %s: should fail to match\n", item) } } for _, item := range test_definition.SucceedCandidates { if test_definition.TestExpression.MatchString(item) == false { all_passed = false fmt.Printf("FAILED on input %s: should match\n", item) } } if all_passed { fmt.Println("All tests pass") } }