Skip to content

Commit a05c568

Browse files
committed
Add tests for WriteDot() method
1 parent 0b1468f commit a05c568

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

binarytree_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package binarytree_test
22

33
import (
4+
"bytes"
45
"reflect"
6+
"strings"
57
"testing"
68

79
"gopkg.in/dnaeon/go-binarytree.v1"
@@ -349,3 +351,36 @@ func TestNodeAttributes(t *testing.T) {
349351
t.Fatal("node attributes mismatch")
350352
}
351353
}
354+
355+
func TestWriteDot(t *testing.T) {
356+
// Our test tree
357+
//
358+
// 1__
359+
// / \
360+
// 2 3
361+
// / \
362+
// 4 5
363+
root := binarytree.NewNode(1)
364+
root.InsertLeft(2)
365+
three := root.InsertRight(3)
366+
three.InsertLeft(4)
367+
three.InsertRight(5)
368+
369+
var buf bytes.Buffer
370+
if err := root.WriteDot(&buf); err != nil {
371+
t.Fatal(err)
372+
}
373+
374+
output := buf.String()
375+
if output == "" {
376+
t.Fatal("got empty dot representation")
377+
}
378+
379+
if !strings.HasPrefix(output, "digraph {") {
380+
t.Fatal("missing dot prefix")
381+
}
382+
383+
if !strings.HasSuffix(output, "}\n") {
384+
t.Fatal("missing dot suffix")
385+
}
386+
}

0 commit comments

Comments
 (0)