(t *testing.T)
| 5 | ) |
| 6 | |
| 7 | func TestGraph(t *testing.T) { |
| 8 | var ( |
| 9 | root = NewNode("root", nil) |
| 10 | rootChild1 = NewNode("rootChild1", nil) |
| 11 | rootChild2 = NewNode("rootChild2", nil) |
| 12 | rootChild3 = NewNode("rootChild3", nil) |
| 13 | rootChild2Child1 = NewNode("rootChild2Child1", nil) |
| 14 | rootChild2Child1Child1 = NewNode("rootChild2Child1Child1", nil) |
| 15 | |
| 16 | testGraph = NewGraph(root) |
| 17 | ) |
| 18 | |
| 19 | _, err := testGraph.InsertNodeAt("does not exits", rootChild1.ID, nil) |
| 20 | if err == nil { |
| 21 | t.Fatal("InsertNodeAt error expected") |
| 22 | } |
| 23 | |
| 24 | _, _ = testGraph.InsertNodeAt(root.ID, rootChild1.ID, nil) |
| 25 | _, _ = testGraph.InsertNodeAt(root.ID, rootChild2.ID, nil) |
| 26 | _, _ = testGraph.InsertNodeAt(root.ID, rootChild3.ID, nil) |
| 27 | |
| 28 | _, _ = testGraph.InsertNodeAt(rootChild2.ID, rootChild2Child1.ID, nil) |
| 29 | _, _ = testGraph.InsertNodeAt(rootChild2Child1.ID, rootChild2Child1Child1.ID, nil) |
| 30 | _, _ = testGraph.InsertNodeAt(rootChild3.ID, rootChild2.ID, nil) |
| 31 | |
| 32 | // Cyclic graph error |
| 33 | _, err = testGraph.InsertNodeAt(rootChild2Child1Child1.ID, rootChild3.ID, nil) |
| 34 | if err == nil { |
| 35 | t.Fatal("Cyclic error expected") |
| 36 | } else { |
| 37 | errMsg := `Cyclic dependency found: |
| 38 | rootChild2Child1Child1 |
| 39 | rootChild3 |
| 40 | rootChild2 |
| 41 | rootChild2Child1 |
| 42 | rootChild2Child1Child1` |
| 43 | |
| 44 | if err.Error() != errMsg { |
| 45 | t.Fatalf("Expected %s, got %s", errMsg, err.Error()) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // Find first path |
| 50 | path := findFirstPath(rootChild1, rootChild2) |
| 51 | if path != nil { |
| 52 | t.Fatalf("Wrong path found: %#+v", path) |
| 53 | } |
| 54 | |
| 55 | // Find first path |
| 56 | path = findFirstPath(root, rootChild2Child1Child1) |
| 57 | if len(path) != 4 || path[0].ID != root.ID || path[1].ID != rootChild2.ID || path[2].ID != rootChild2Child1.ID || path[3].ID != rootChild2Child1Child1.ID { |
| 58 | t.Fatalf("Wrong path found: %#+v", path) |
| 59 | } |
| 60 | |
| 61 | // Get leaf node |
| 62 | leaf := testGraph.GetNextLeaf(root) |
| 63 | if leaf.ID != rootChild1.ID { |
| 64 | t.Fatalf("GetLeaf1: Got id %s, expected %s", leaf.ID, rootChild1.ID) |
nothing calls this directly
no test coverage detected