| 1 | package graph |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestDirected(t *testing.T) { |
| 6 | tests := map[string]struct { |
| 7 | expected *Traits |
| 8 | }{ |
| 9 | "directed graph": { |
| 10 | expected: &Traits{ |
| 11 | IsDirected: true, |
| 12 | }, |
| 13 | }, |
| 14 | } |
| 15 | |
| 16 | for name, test := range tests { |
| 17 | p := &Traits{} |
| 18 | |
| 19 | Directed()(p) |
| 20 | |
| 21 | if !traitsAreEqual(test.expected, p) { |
| 22 | t.Errorf("%s: trait expectation doesn't match: expected %v, got %v", name, test.expected, p) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestAcyclic(t *testing.T) { |
| 28 | tests := map[string]struct { |
| 29 | expected *Traits |
| 30 | }{ |
| 31 | "acyclic graph": { |
| 32 | expected: &Traits{ |
| 33 | IsAcyclic: true, |
| 34 | }, |
| 35 | }, |
| 36 | } |
| 37 | |
| 38 | for name, test := range tests { |
| 39 | p := &Traits{} |
| 40 | |
| 41 | Acyclic()(p) |
| 42 | |
| 43 | if !traitsAreEqual(test.expected, p) { |
| 44 | t.Errorf("%s: trait expectation doesn't match: expected %v, got %v", name, test.expected, p) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestWeighted(t *testing.T) { |
| 50 | tests := map[string]struct { |
| 51 | expected *Traits |
| 52 | }{ |
| 53 | "weighted graph": { |
| 54 | expected: &Traits{ |
| 55 | IsWeighted: true, |
| 56 | }, |
| 57 | }, |
| 58 | } |
| 59 | |
| 60 | for name, test := range tests { |
| 61 | p := &Traits{} |
| 62 | |
| 63 | Weighted()(p) |
| 64 | |
| 65 | if !traitsAreEqual(test.expected, p) { |
| 66 | t.Errorf("%s: trait expectation doesn't match: expected %v, got %v", name, test.expected, p) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestRooted(t *testing.T) { |
| 72 | tests := map[string]struct { |
| 73 | expected *Traits |
| 74 | }{ |
| 75 | "rooted graph": { |
| 76 | expected: &Traits{ |
| 77 | IsRooted: true, |
| 78 | }, |
| 79 | }, |
| 80 | } |
| 81 | |
| 82 | for name, test := range tests { |
| 83 | p := &Traits{} |
| 84 | |
| 85 | Rooted()(p) |
| 86 | |
| 87 | if !traitsAreEqual(test.expected, p) { |
| 88 | t.Errorf("%s: trait expectation doesn't match: expected %v, got %v", name, test.expected, p) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestTree(t *testing.T) { |
| 94 | tests := map[string]struct { |
| 95 | expected *Traits |
| 96 | }{ |
| 97 | "tree graph": { |
| 98 | expected: &Traits{ |
| 99 | IsAcyclic: true, |
| 100 | IsRooted: true, |
| 101 | }, |
| 102 | }, |
| 103 | } |
| 104 | |
| 105 | for name, test := range tests { |
| 106 | p := &Traits{} |
| 107 | |
| 108 | Tree()(p) |
| 109 | |
| 110 | if !traitsAreEqual(test.expected, p) { |
| 111 | t.Errorf("%s: trait expectation doesn't match: expected %v, got %v", name, test.expected, p) |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func TestPreventCycles(t *testing.T) { |
| 117 | tests := map[string]struct { |
| 118 | expected *Traits |
| 119 | }{ |
| 120 | "prevent cycles": { |
| 121 | expected: &Traits{ |
| 122 | IsAcyclic: true, |
| 123 | PreventCycles: true, |
| 124 | }, |
| 125 | }, |
| 126 | } |
| 127 | |
| 128 | for name, test := range tests { |
| 129 | p := &Traits{} |
| 130 | |
| 131 | PreventCycles()(p) |
| 132 | |
| 133 | if !traitsAreEqual(test.expected, p) { |
| 134 | t.Errorf("%s: trait expectation doesn't match: expected %v, got %v", name, test.expected, p) |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func traitsAreEqual(a, b *Traits) bool { |
| 140 | return a.IsAcyclic == b.IsAcyclic && |
| 141 | a.IsDirected == b.IsDirected && |
| 142 | a.IsRooted == b.IsRooted && |
| 143 | a.IsWeighted == b.IsWeighted && |
| 144 | a.PreventCycles == b.PreventCycles |
| 145 | } |