Created
September 15, 2015 02:54
-
-
Save axw/2bed535305537ae3a078 to your computer and use it in GitHub Desktop.
gocheck checker for multiple single-line regexps operating on multiple line output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var LinesMatch gc.Checker = &linesMatchChecker{ | |
&gc.CheckerInfo{Name: "LinesMatch", Params: []string{"obtained", "regexps"}}, | |
} | |
type linesMatchChecker struct { | |
*gc.CheckerInfo | |
} | |
func (linesMatchChecker) Check(params []interface{}, names []string) (result bool, error string) { | |
obtainedLines := strings.Split(params[0].(string), "\n") | |
expectedLines := strings.Split(params[1].(string), "\n") | |
if len(obtainedLines) != len(expectedLines) { | |
return false, fmt.Sprintf( | |
"obtained value contains %d lines, expected %d", | |
len(obtainedLines), len(expectedLines), | |
) | |
} | |
for i, reStr := range expectedLines { | |
obtainedLine := obtainedLines[i] | |
matches, err := regexp.MatchString("^"+reStr+"$", obtainedLine) | |
if err != nil { | |
return false, "Can't compile regex: " + err.Error() | |
} | |
if !matches { | |
return false, fmt.Sprintf( | |
"mismatch at line %d: %q does not match %q", | |
i+1, obtainedLine, reStr, | |
) | |
} | |
} | |
return true, "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment