github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/digraph/graphviz_test.go (about)

     1  package digraph
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestWriteDot(t *testing.T) {
    10  	nodes := ParseBasic(`a -> b ; foo
    11  a -> c
    12  b -> d
    13  b -> e
    14  `)
    15  	var nlist []Node
    16  	for _, n := range nodes {
    17  		nlist = append(nlist, n)
    18  	}
    19  
    20  	buf := bytes.NewBuffer(nil)
    21  	if err := WriteDot(buf, nlist); err != nil {
    22  		t.Fatalf("err: %s", err)
    23  	}
    24  
    25  	actual := strings.TrimSpace(string(buf.Bytes()))
    26  	expected := strings.TrimSpace(writeDotStr)
    27  
    28  	actualLines := strings.Split(actual, "\n")
    29  	expectedLines := strings.Split(expected, "\n")
    30  
    31  	if actualLines[0] != expectedLines[0] ||
    32  		actualLines[len(actualLines)-1] != expectedLines[len(expectedLines)-1] ||
    33  		len(actualLines) != len(expectedLines) {
    34  		t.Fatalf("bad: %s", actual)
    35  	}
    36  
    37  	count := 0
    38  	for _, el := range expectedLines[1 : len(expectedLines)-1] {
    39  		for _, al := range actualLines[1 : len(actualLines)-1] {
    40  			if el == al {
    41  				count++
    42  				break
    43  			}
    44  		}
    45  	}
    46  
    47  	if count != len(expectedLines)-2 {
    48  		t.Fatalf("bad: %s", actual)
    49  	}
    50  }
    51  
    52  const writeDotStr = `
    53  digraph {
    54  	"a";
    55  	"a" -> "b" [label="foo"];
    56  	"a" -> "c" [label="Edge"];
    57  	"b";
    58  	"b" -> "d" [label="Edge"];
    59  	"b" -> "e" [label="Edge"];
    60  	"c";
    61  	"d";
    62  	"e";
    63  }
    64  `