refactored test to be more flexible

This commit is contained in:
Nate Jones 2014-04-25 06:47:06 -07:00
parent 4857bfb781
commit d83cfa5ea1

View file

@ -1,24 +1,59 @@
package main package main
import ( import (
"regexp"
"testing" "testing"
) )
func Test_Dot(t *testing.T) { type RunTest struct {
json := `[{ "VirtualSize": 662553464, "Size": 662553464, "RepoTags": [ "<none>:<none>" ], "ParentId": "", "Id": "4c1208b690c68af3476b437e7bc2bcc460f062bda2094d2d8f21a7e70368d358", "Created": 1386114144 }]` json string
regexps []string
expectedResult := `digraph docker {
base -> "4c1208b690c6" [style=invis]
base [style=invisible]
} }
`
im, _ := parseJSON([]byte(json)) func Test_Dot(t *testing.T) {
result := jsonToDot(im) allMatch := []string{
"(?s)digraph docker {.*}",
`(?m) base \[style=invisible\]`,
}
allRegex := compileRegexps(t, allMatch)
if result == expectedResult { dotTests := []RunTest{
t.Log("Pass") RunTest{
} else { json: `[{ "VirtualSize": 662553464, "Size": 662553464, "RepoTags": [ "<none>:<none>" ], "ParentId": "", "Id": "4c1208b690c68af3476b437e7bc2bcc460f062bda2094d2d8f21a7e70368d358", "Created": 1386114144 }]`,
t.Errorf("|%s| and |%s| are different.", result, expectedResult) regexps: []string{
`base -> "4c1208b690c6"`,
},
},
}
for _, dotTest := range dotTests {
im, _ := parseJSON([]byte(dotTest.json))
result := jsonToDot(im)
for _, regexp := range allRegex {
if !regexp.MatchString(result) {
t.Fatalf("images dot content '%s' did not match regexp '%s'", result, regexp)
}
}
for _, regexp := range compileRegexps(t, dotTest.regexps) {
if !regexp.MatchString(result) {
t.Fatalf("images dot content '%s' did not match regexp '%s'", result, regexp)
}
}
} }
} }
func compileRegexps(t *testing.T, regexpStrings []string) []*regexp.Regexp {
compiledRegexps := []*regexp.Regexp{}
for _, regexpString := range regexpStrings {
regexp, err := regexp.Compile(regexpString)
if err != nil {
t.Errorf("Error in regex string '%s': %s", regexpString, err)
}
compiledRegexps = append(compiledRegexps, regexp)
}
return compiledRegexps
}