Skip to content

Commit

Permalink
Add test for draw.GraphAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikbraun committed Apr 12, 2023
1 parent f5e1473 commit 40286f7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.17.0] - 2023-04-10
## [0.17.0] - 2023-04-12

### Added
* Added the `draw.GraphAttributes` functional option for `draw.DOT` for rendering graph attributes.
Expand Down
6 changes: 3 additions & 3 deletions draw/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ type statement struct {
//
// go run main.go | dot -Tsvg > output.svg
//
// DOT also accepts the [draw.GraphAttribute] functional option, which can be
// used to add global attributes when rendering the graph:
// DOT also accepts the [GraphAttribute] functional option, which can be used to
// add global attributes when rendering the graph:
//
// _ = draw.DOT(g, file, draw.GraphAttribute("label", "my-graph"))
func DOT[K comparable, T any](g graph.Graph[K, T], w io.Writer, options ...func(*description)) error {
Expand All @@ -79,7 +79,7 @@ func DOT[K comparable, T any](g graph.Graph[K, T], w io.Writer, options ...func(
return renderDOT(w, desc)
}

// GraphAttribute is a functional option for the [draw.DOT] method.
// GraphAttribute is a functional option for the [DOT] method.
func GraphAttribute(key, value string) func(*description) {
return func(d *description) {
d.Attributes[key] = value
Expand Down
33 changes: 33 additions & 0 deletions draw/draw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,39 @@ func TestRenderDOT(t *testing.T) {
}
}

func TestGraphAttribute(t *testing.T) {
tests := map[string]struct {
attribute [2]string
expected *description
}{
"label attribute": {
attribute: [2]string{"label", "my-graph"},
expected: &description{
Attributes: map[string]string{
"label": "my-graph",
},
},
},
}

for name, test := range tests {
d := &description{
Attributes: make(map[string]string),
}

GraphAttribute(test.attribute[0], test.attribute[1])(d)

stringsAreEqual := func(a, b string) bool {
return a == b
}

if !mapsAreEqual(test.expected.Attributes, d.Attributes, stringsAreEqual) {
t.Errorf("%s: graph attribute expectation doesn't match: expected %v, got %v", name, test.expected.Attributes, d.Attributes)
}
}

}

func slicesAreEqual[T any](a, b []T, equals func(a, b T) bool) bool {
if len(a) != len(b) {
return false
Expand Down

0 comments on commit 40286f7

Please sign in to comment.