Fix graphviz_test with test table
The tests were explicitly checking order where the underlying implementation is `map[string]*BasicNode`. This PR addresses the order which is not known and within the test I create a test table to allow multiple tests (2 in this PR rather than 1).
This commit is contained in:
parent
d8dec878da
commit
c1ba945c7c
|
@ -1,44 +1,101 @@
|
||||||
package digraph
|
package digraph
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"strings"
|
"reflect"
|
||||||
"testing"
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var testTable = []struct {
|
||||||
|
BasicData string // Basic graph data
|
||||||
|
ExpectedDigraphData string // Digraph data which should be generated from BasicData
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
`a -> b ; foo
|
||||||
|
a -> c
|
||||||
|
b -> d
|
||||||
|
b -> e
|
||||||
|
`,
|
||||||
|
`digraph {
|
||||||
|
"a";
|
||||||
|
"a" -> "b" [label="foo"];
|
||||||
|
"a" -> "c" [label="Edge"];
|
||||||
|
"b";
|
||||||
|
"b" -> "d" [label="Edge"];
|
||||||
|
"b" -> "e" [label="Edge"];
|
||||||
|
"c";
|
||||||
|
"d";
|
||||||
|
"e";
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`a -> c ; foo
|
||||||
|
a -> d
|
||||||
|
b -> c
|
||||||
|
b -> e
|
||||||
|
a -> f
|
||||||
|
`,
|
||||||
|
`digraph {
|
||||||
|
"a";
|
||||||
|
"a" -> "c" [label="foo"];
|
||||||
|
"a" -> "f" [label="Edge"];
|
||||||
|
"a" -> "d" [label="Edge"];
|
||||||
|
"b";
|
||||||
|
"b" -> "c" [label="Edge"];
|
||||||
|
"b" -> "e" [label="Edge"];
|
||||||
|
"c";
|
||||||
|
"d";
|
||||||
|
"e";
|
||||||
|
"f";
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nieve normalizer. Takes a string, splits it and sorts it.
|
||||||
|
func normalize(input string) []string {
|
||||||
|
out := strings.Split(input, "\n")
|
||||||
|
|
||||||
|
// trim each line
|
||||||
|
for i, str := range out {
|
||||||
|
out[i] = strings.TrimSpace(str)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(out)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriteDot(t *testing.T) {
|
func TestWriteDot(t *testing.T) {
|
||||||
nodes := ParseBasic(`a -> b ; foo
|
// Build []Node from BasicNode map
|
||||||
a -> c
|
var buildNodes = func(nodes map[string]*BasicNode) []Node {
|
||||||
b -> d
|
var nlist []Node
|
||||||
b -> e
|
for _, n := range nodes {
|
||||||
`)
|
nlist = append(nlist, n)
|
||||||
var nlist []Node
|
}
|
||||||
for _, n := range nodes {
|
return nlist
|
||||||
nlist = append(nlist, n)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
buf := bytes.NewBuffer(nil)
|
// Get a normalized string representation of the file
|
||||||
if err := WriteDot(buf, nlist); err != nil {
|
var writeFile = func(nlist []Node) string {
|
||||||
t.Fatalf("err: %s", err)
|
buf := bytes.NewBuffer(nil)
|
||||||
}
|
if err := WriteDot(buf, nlist); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(string(buf.Bytes()))
|
||||||
|
}
|
||||||
|
|
||||||
actual := strings.TrimSpace(string(buf.Bytes()))
|
// For each entry in the test table construct an
|
||||||
expected := strings.TrimSpace(writeDotStr)
|
// actual and expected values and compare.
|
||||||
if actual != expected {
|
for _, data := range testTable {
|
||||||
t.Fatalf("bad: %s", actual)
|
nodes := buildNodes(ParseBasic(data.BasicData))
|
||||||
}
|
actual := normalize(writeFile(nodes))
|
||||||
|
expected := normalize(strings.TrimSpace(data.ExpectedDigraphData))
|
||||||
|
|
||||||
|
// Deep equal the array values
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Logf("Expected:\n%s", expected)
|
||||||
|
t.Fatalf("Bad:\n%s", actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const writeDotStr = `
|
|
||||||
digraph {
|
|
||||||
"a";
|
|
||||||
"a" -> "b" [label="foo"];
|
|
||||||
"a" -> "c" [label="Edge"];
|
|
||||||
"b";
|
|
||||||
"b" -> "d" [label="Edge"];
|
|
||||||
"b" -> "e" [label="Edge"];
|
|
||||||
"c";
|
|
||||||
"d";
|
|
||||||
"e";
|
|
||||||
}
|
|
||||||
`
|
|
||||||
|
|
Loading…
Reference in New Issue