Skip to content

Instantly share code, notes, and snippets.

@axw
Created September 15, 2015 02:54
Show Gist options
  • Save axw/2bed535305537ae3a078 to your computer and use it in GitHub Desktop.
Save axw/2bed535305537ae3a078 to your computer and use it in GitHub Desktop.
gocheck checker for multiple single-line regexps operating on multiple line output
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