| 1 |
package draw |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/dominikbraun/graph" |
| 9 |
) |
| 10 |
|
| 11 |
func TestGenerateDOT(t *testing.T) { |
| 12 |
tests := map[string]struct { |
| 13 |
graph graph.Graph[string, string] |
| 14 |
attributes map[string]string |
| 15 |
vertices []string |
| 16 |
vertexProperties map[string]graph.VertexProperties |
| 17 |
edges []graph.Edge[string] |
| 18 |
expected description |
| 19 |
}{ |
| 20 |
"3-vertex directed graph": { |
| 21 |
graph: graph.New(graph.StringHash, graph.Directed()), |
| 22 |
attributes: map[string]string{}, |
| 23 |
vertices: []string{"1", "2", "3"}, |
| 24 |
edges: []graph.Edge[string]{ |
| 25 |
{Source: "1", Target: "2"}, |
| 26 |
{Source: "1", Target: "3"}, |
| 27 |
}, |
| 28 |
expected: description{ |
| 29 |
GraphType: "digraph", |
| 30 |
Attributes: map[string]string{}, |
| 31 |
EdgeOperator: "->", |
| 32 |
Statements: []statement{ |
| 33 |
{Source: "1", Target: "2"}, |
| 34 |
{Source: "1", Target: "3"}, |
| 35 |
{Source: "1"}, |
| 36 |
{Source: "2"}, |
| 37 |
{Source: "3"}, |
| 38 |
}, |
| 39 |
}, |
| 40 |
}, |
| 41 |
"3-vertex directed, weighted graph with weights and attributes": { |
| 42 |
graph: graph.New(graph.StringHash, graph.Directed(), graph.Weighted()), |
| 43 |
attributes: map[string]string{}, |
| 44 |
vertices: []string{"1", "2", "3"}, |
| 45 |
edges: []graph.Edge[string]{ |
| 46 |
{ |
| 47 |
Source: "1", |
| 48 |
Target: "2", |
| 49 |
Properties: graph.EdgeProperties{ |
| 50 |
Weight: 10, |
| 51 |
Attributes: map[string]string{ |
| 52 |
"color": "red", |
| 53 |
}, |
| 54 |
}, |
| 55 |
}, |
| 56 |
{Source: "1", Target: "3"}, |
| 57 |
}, |
| 58 |
expected: description{ |
| 59 |
GraphType: "digraph", |
| 60 |
Attributes: map[string]string{}, |
| 61 |
EdgeOperator: "->", |
| 62 |
Statements: []statement{ |
| 63 |
{ |
| 64 |
Source: "1", |
| 65 |
Target: "2", |
| 66 |
EdgeWeight: 10, |
| 67 |
EdgeAttributes: map[string]string{ |
| 68 |
"color": "red", |
| 69 |
}, |
| 70 |
}, |
| 71 |
{Source: "1", Target: "3"}, |
| 72 |
{Source: "1"}, |
| 73 |
{Source: "2"}, |
| 74 |
{Source: "3"}, |
| 75 |
}, |
| 76 |
}, |
| 77 |
}, |
| 78 |
"vertices with attributes": { |
| 79 |
graph: graph.New(graph.StringHash, graph.Directed(), graph.Weighted()), |
| 80 |
attributes: map[string]string{}, |
| 81 |
vertices: []string{"1", "2"}, |
| 82 |
vertexProperties: map[string]graph.VertexProperties{ |
| 83 |
"1": { |
| 84 |
Attributes: map[string]string{ |
| 85 |
"color": "red", |
| 86 |
}, |
| 87 |
Weight: 10, |
| 88 |
}, |
| 89 |
"2": { |
| 90 |
Attributes: map[string]string{ |
| 91 |
"color": "blue", |
| 92 |
}, |
| 93 |
Weight: 20, |
| 94 |
}, |
| 95 |
}, |
| 96 |
edges: []graph.Edge[string]{ |
| 97 |
{Source: "1", Target: "2"}, |
| 98 |
}, |
| 99 |
expected: description{ |
| 100 |
GraphType: "digraph", |
| 101 |
Attributes: map[string]string{}, |
| 102 |
EdgeOperator: "->", |
| 103 |
Statements: []statement{ |
| 104 |
{ |
| 105 |
Source: "1", |
| 106 |
SourceAttributes: map[string]string{ |
| 107 |
"color": "red", |
| 108 |
}, |
| 109 |
SourceWeight: 10, |
| 110 |
}, |
| 111 |
{ |
| 112 |
Source: "2", |
| 113 |
SourceAttributes: map[string]string{ |
| 114 |
"color": "blue", |
| 115 |
}, |
| 116 |
SourceWeight: 20, |
| 117 |
}, |
| 118 |
{ |
| 119 |
Source: "1", |
| 120 |
Target: "2", |
| 121 |
}, |
| 122 |
}, |
| 123 |
}, |
| 124 |
}, |
| 125 |
"directed graph with attributes": { |
| 126 |
graph: graph.New(graph.StringHash, graph.Directed()), |
| 127 |
attributes: map[string]string{ |
| 128 |
"label": "my-graph", |
| 129 |
"normalize": "true", |
| 130 |
"compound": "false", |
| 131 |
}, |
| 132 |
vertices: []string{"1", "2"}, |
| 133 |
edges: []graph.Edge[string]{ |
| 134 |
{Source: "1", Target: "2"}, |
| 135 |
}, |
| 136 |
expected: description{ |
| 137 |
GraphType: "digraph", |
| 138 |
Attributes: map[string]string{ |
| 139 |
"label": "my-graph", |
| 140 |
"normalize": "true", |
| 141 |
"compound": "false", |
| 142 |
}, |
| 143 |
EdgeOperator: "->", |
| 144 |
Statements: []statement{ |
| 145 |
{ |
| 146 |
Source: "1", |
| 147 |
}, |
| 148 |
{ |
| 149 |
Source: "2", |
| 150 |
}, |
| 151 |
{ |
| 152 |
Source: "1", |
| 153 |
Target: "2", |
| 154 |
}, |
| 155 |
}, |
| 156 |
}, |
| 157 |
}, |
| 158 |
} |
| 159 |
|
| 160 |
for name, test := range tests { |
| 161 |
for _, vertex := range test.vertices { |
| 162 |
if test.vertexProperties == nil { |
| 163 |
_ = test.graph.AddVertex(vertex) |
| 164 |
continue |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
for key, value := range test.vertexProperties[vertex].Attributes { |
| 170 |
weight := test.vertexProperties[vertex].Weight |
| 171 |
|
| 172 |
_ = test.graph.AddVertex(vertex, graph.VertexWeight(weight), graph.VertexAttribute(key, value)) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
for _, edge := range test.edges { |
| 177 |
var err error |
| 178 |
if len(edge.Properties.Attributes) == 0 { |
| 179 |
err = test.graph.AddEdge(edge.Source, edge.Target, graph.EdgeWeight(edge.Properties.Weight)) |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
for key, value := range edge.Properties.Attributes { |
| 185 |
err = test.graph.AddEdge(edge.Source, edge.Target, graph.EdgeWeight(edge.Properties.Weight), graph.EdgeAttribute(key, value)) |
| 186 |
} |
| 187 |
if err != nil { |
| 188 |
t.Fatalf("%s: failed to add edge: %s", name, err.Error()) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
desc, _ := generateDOT(test.graph) |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
desc.Attributes = test.attributes |
| 197 |
|
| 198 |
if desc.GraphType != test.expected.GraphType { |
| 199 |
t.Errorf("%s: graph type expectancy doesn't match: expected %v, got %v", name, test.expected.GraphType, desc.GraphType) |
| 200 |
} |
| 201 |
|
| 202 |
if desc.EdgeOperator != test.expected.EdgeOperator { |
| 203 |
t.Errorf("%s: edge operator expectancy doesn't match: expected %v, got %v", name, test.expected.EdgeOperator, desc.EdgeOperator) |
| 204 |
} |
| 205 |
|
| 206 |
if !slicesAreEqual(desc.Statements, test.expected.Statements, statementsAreEqual) { |
| 207 |
t.Errorf("%s: statements expectancy doesn't match: expected %v, got %v", name, test.expected.Statements, desc.Statements) |
| 208 |
} |
| 209 |
|
| 210 |
stringsAreEqual := func(a, b string) bool { |
| 211 |
return a == b |
| 212 |
} |
| 213 |
|
| 214 |
if !mapsAreEqual(desc.Attributes, test.expected.Attributes, stringsAreEqual) { |
| 215 |
t.Errorf("%s: attributes expectancy doesn't match: expected %v, got %v", name, test.expected.Attributes, desc.Attributes) |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
func TestRenderDOT(t *testing.T) { |
| 221 |
tests := map[string]struct { |
| 222 |
description description |
| 223 |
expected string |
| 224 |
}{ |
| 225 |
"3-vertex directed graph": { |
| 226 |
description: description{ |
| 227 |
GraphType: "digraph", |
| 228 |
Attributes: map[string]string{}, |
| 229 |
EdgeOperator: "->", |
| 230 |
Statements: []statement{ |
| 231 |
{Source: 1, Target: 2}, |
| 232 |
{Source: 1, Target: 3}, |
| 233 |
{Source: 1}, |
| 234 |
{Source: 2}, |
| 235 |
{Source: 3}, |
| 236 |
}, |
| 237 |
}, |
| 238 |
expected: `strict digraph { |
| 239 |
"1" -> "2" [ weight=0 ]; |
| 240 |
"1" -> "3" [ weight=0 ]; |
| 241 |
"1" [ weight=0 ]; |
| 242 |
"2" [ weight=0 ]; |
| 243 |
"3" [ weight=0 ]; |
| 244 |
}`, |
| 245 |
}, |
| 246 |
"custom edge attributes": { |
| 247 |
description: description{ |
| 248 |
GraphType: "digraph", |
| 249 |
Attributes: map[string]string{}, |
| 250 |
EdgeOperator: "->", |
| 251 |
Statements: []statement{ |
| 252 |
{ |
| 253 |
Source: 1, |
| 254 |
Target: 2, |
| 255 |
EdgeAttributes: map[string]string{ |
| 256 |
"color": "red", |
| 257 |
}, |
| 258 |
}, |
| 259 |
{ |
| 260 |
Source: 1, |
| 261 |
Target: 3, |
| 262 |
EdgeAttributes: map[string]string{ |
| 263 |
"color": "blue", |
| 264 |
}, |
| 265 |
}, |
| 266 |
{Source: 1}, |
| 267 |
{Source: 2}, |
| 268 |
{Source: 3}, |
| 269 |
}, |
| 270 |
}, |
| 271 |
expected: `strict digraph { |
| 272 |
"1" -> "2" [ color="red", weight=0 ]; |
| 273 |
"1" -> "3" [ color="blue", weight=0 ]; |
| 274 |
"1" [ weight=0 ]; |
| 275 |
"2" [ weight=0 ]; |
| 276 |
"3" [ weight=0 ]; |
| 277 |
}`, |
| 278 |
}, |
| 279 |
"vertices containing special characters": { |
| 280 |
description: description{ |
| 281 |
GraphType: "digraph", |
| 282 |
Attributes: map[string]string{}, |
| 283 |
EdgeOperator: "->", |
| 284 |
Statements: []statement{ |
| 285 |
{Source: "/home", Target: "projects/graph"}, |
| 286 |
{Source: "/home", Target: ".config"}, |
| 287 |
{Source: ".config", Target: "my file.txt"}, |
| 288 |
}, |
| 289 |
}, |
| 290 |
expected: `strict digraph { |
| 291 |
"/home" -> "projects/graph" [ weight=0 ]; |
| 292 |
"/home" -> ".config" [ weight=0 ]; |
| 293 |
".config" -> "my file.txt" [ weight=0 ]; |
| 294 |
}`, |
| 295 |
}, |
| 296 |
"vertices with attributes": { |
| 297 |
description: description{ |
| 298 |
GraphType: "digraph", |
| 299 |
Attributes: map[string]string{}, |
| 300 |
EdgeOperator: "->", |
| 301 |
Statements: []statement{ |
| 302 |
{ |
| 303 |
Source: "1", |
| 304 |
SourceAttributes: map[string]string{ |
| 305 |
"color": "red", |
| 306 |
}, |
| 307 |
SourceWeight: 10, |
| 308 |
}, |
| 309 |
{ |
| 310 |
Source: "2", |
| 311 |
SourceAttributes: map[string]string{ |
| 312 |
"color": "blue", |
| 313 |
}, |
| 314 |
SourceWeight: 20, |
| 315 |
}, |
| 316 |
{ |
| 317 |
Source: "1", |
| 318 |
Target: "2", |
| 319 |
}, |
| 320 |
}, |
| 321 |
}, |
| 322 |
expected: `strict digraph { |
| 323 |
"1" [ color="red", weight=10 ]; |
| 324 |
"2" [ color="blue", weight=20 ]; |
| 325 |
"1" -> "2" [ weight=0 ]; |
| 326 |
}`, |
| 327 |
}, |
| 328 |
"3-vertex directed graph with attributes": { |
| 329 |
description: description{ |
| 330 |
GraphType: "digraph", |
| 331 |
Attributes: map[string]string{ |
| 332 |
"label": "my-graph", |
| 333 |
}, |
| 334 |
EdgeOperator: "->", |
| 335 |
Statements: []statement{ |
| 336 |
{Source: 1, Target: 2}, |
| 337 |
{Source: 1, Target: 3}, |
| 338 |
{Source: 1}, |
| 339 |
{Source: 2}, |
| 340 |
{Source: 3}, |
| 341 |
}, |
| 342 |
}, |
| 343 |
expected: `strict digraph { |
| 344 |
label="my-graph"; |
| 345 |
"1" -> "2" [ weight=0 ]; |
| 346 |
"1" -> "3" [ weight=0 ]; |
| 347 |
"1" [ weight=0 ]; |
| 348 |
"2" [ weight=0 ]; |
| 349 |
"3" [ weight=0 ]; |
| 350 |
}`, |
| 351 |
}, |
| 352 |
} |
| 353 |
|
| 354 |
for name, test := range tests { |
| 355 |
buf := new(bytes.Buffer) |
| 356 |
_ = renderDOT(buf, test.description) |
| 357 |
|
| 358 |
output := normalizeOutput(buf.String()) |
| 359 |
expected := normalizeOutput(test.expected) |
| 360 |
|
| 361 |
if output != expected { |
| 362 |
t.Errorf("%s: DOT output expectancy doesn't match: expected %v, got %v", name, expected, output) |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
func TestGraphAttribute(t *testing.T) { |
| 368 |
tests := map[string]struct { |
| 369 |
attribute [2]string |
| 370 |
expected *description |
| 371 |
}{ |
| 372 |
"label attribute": { |
| 373 |
attribute: [2]string{"label", "my-graph"}, |
| 374 |
expected: &description{ |
| 375 |
Attributes: map[string]string{ |
| 376 |
"label": "my-graph", |
| 377 |
}, |
| 378 |
}, |
| 379 |
}, |
| 380 |
} |
| 381 |
|
| 382 |
for name, test := range tests { |
| 383 |
d := &description{ |
| 384 |
Attributes: make(map[string]string), |
| 385 |
} |
| 386 |
|
| 387 |
GraphAttribute(test.attribute[0], test.attribute[1])(d) |
| 388 |
|
| 389 |
stringsAreEqual := func(a, b string) bool { |
| 390 |
return a == b |
| 391 |
} |
| 392 |
|
| 393 |
if !mapsAreEqual(test.expected.Attributes, d.Attributes, stringsAreEqual) { |
| 394 |
t.Errorf("%s: graph attribute expectation doesn't match: expected %v, got %v", name, test.expected.Attributes, d.Attributes) |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
} |
| 399 |
|
| 400 |
func slicesAreEqual[T any](a, b []T, equals func(a, b T) bool) bool { |
| 401 |
if len(a) != len(b) { |
| 402 |
return false |
| 403 |
} |
| 404 |
|
| 405 |
for _, aValue := range a { |
| 406 |
found := false |
| 407 |
for _, bValue := range b { |
| 408 |
if equals(aValue, bValue) { |
| 409 |
found = true |
| 410 |
} |
| 411 |
} |
| 412 |
if !found { |
| 413 |
return false |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
return true |
| 418 |
} |
| 419 |
|
| 420 |
func mapsAreEqual[K comparable, V any](a, b map[K]V, equals func(a, b V) bool) bool { |
| 421 |
if len(a) != len(b) { |
| 422 |
return false |
| 423 |
} |
| 424 |
|
| 425 |
for aKey, aValue := range a { |
| 426 |
bValue, ok := b[aKey] |
| 427 |
if !ok { |
| 428 |
return false |
| 429 |
} |
| 430 |
if !equals(aValue, bValue) { |
| 431 |
return false |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
return true |
| 436 |
} |
| 437 |
|
| 438 |
func normalizeOutput(output string) string { |
| 439 |
replacer := strings.NewReplacer(" ", "", "\n", "", "\t", "") |
| 440 |
|
| 441 |
return replacer.Replace(output) |
| 442 |
} |
| 443 |
|
| 444 |
func statementsAreEqual(a, b statement) bool { |
| 445 |
if len(a.EdgeAttributes) != len(b.EdgeAttributes) { |
| 446 |
return false |
| 447 |
} |
| 448 |
|
| 449 |
for aKey, aValue := range a.EdgeAttributes { |
| 450 |
bValue, ok := b.EdgeAttributes[aKey] |
| 451 |
if !ok { |
| 452 |
return false |
| 453 |
} |
| 454 |
if aValue != bValue { |
| 455 |
return false |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
if len(a.SourceAttributes) != len(b.SourceAttributes) { |
| 460 |
return false |
| 461 |
} |
| 462 |
|
| 463 |
for aKey, aValue := range a.SourceAttributes { |
| 464 |
bValue, ok := b.SourceAttributes[aKey] |
| 465 |
if !ok { |
| 466 |
return false |
| 467 |
} |
| 468 |
if aValue != bValue { |
| 469 |
return false |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
return a.Source == b.Source && |
| 474 |
a.Target == b.Target && |
| 475 |
a.EdgeWeight == b.EdgeWeight && |
| 476 |
a.SourceWeight == b.SourceWeight |
| 477 |
} |