Skip to content

Commit

Permalink
chore: move constants into a separate pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmoraes committed Nov 14, 2020
1 parent dcd7266 commit d9e1528
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 92 deletions.
47 changes: 24 additions & 23 deletions Graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/wwmoraes/dot/attributes"
"github.com/wwmoraes/dot/constants"
"github.com/wwmoraes/dot/dottest"
)

Expand All @@ -22,7 +23,7 @@ func TestGraphBehavior(t *testing.T) {
t.Fatal("graph is nil, expected a valid instance")
}

graph.Node("n1").Edge(graph.Node("n2")).SetAttributeString(attributes.KeyLabel, "uses")
graph.Node("n1").Edge(graph.Node("n2")).SetAttributeString(constants.KeyLabel, "uses")

expected := `digraph {"n1";"n2";"n1"->"n2"[label="uses"];}`

Expand All @@ -36,7 +37,7 @@ func TestGraphBehavior(t *testing.T) {
t.Fatal("graph is nil, expected a valid instance")
}

graph.Node("n1").Edge(graph.Node("n2")).SetAttributeString(attributes.KeyLabel, "uses")
graph.Node("n1").Edge(graph.Node("n2")).SetAttributeString(constants.KeyLabel, "uses")

expected := `digraph {"n1";"n2";"n1"->"n2"[label="uses"];}`

Expand All @@ -50,7 +51,7 @@ func TestGraphBehavior(t *testing.T) {
t.Fatal("graph is nil, expected a valid instance")
}

graph.Node("n1").Edge(graph.Node("n2")).SetAttributeString(attributes.KeyLabel, "uses")
graph.Node("n1").Edge(graph.Node("n2")).SetAttributeString(constants.KeyLabel, "uses")

expected := `graph {"n1";"n2";"n1"--"n2"[label="uses"];}`

Expand Down Expand Up @@ -245,7 +246,7 @@ func TestGraph_Initializers(t *testing.T) {
t.Run("graph with node initializer", func(t *testing.T) {
graph, err := NewGraph(
WithNodeInitializer(func(nodeInstance Node) {
nodeInstance.SetAttributeString(attributes.KeyClass, "test-class")
nodeInstance.SetAttributeString(constants.KeyClass, "test-class")
}),
)
if err != nil {
Expand All @@ -261,7 +262,7 @@ func TestGraph_Initializers(t *testing.T) {
t.Run("graph with edge initializer", func(t *testing.T) {
graph, err := NewGraph(
WithEdgeInitializer(func(edgeInstance StyledEdge) {
edgeInstance.SetAttributeString(attributes.KeyClass, "test-class")
edgeInstance.SetAttributeString(constants.KeyClass, "test-class")
}),
)
if err != nil {
Expand Down Expand Up @@ -651,8 +652,8 @@ func TestEmptyWithIDAndAttributes(t *testing.T) {
t.Fatal("graph is nil, expected a valid instance")
}

di.SetAttribute(attributes.KeyStyle, attributes.NewString("filled"))
di.SetAttribute(attributes.KeyColor, attributes.NewString("lightgrey"))
di.SetAttribute(constants.KeyStyle, attributes.NewString("filled"))
di.SetAttribute(constants.KeyColor, attributes.NewString("lightgrey"))
if got, want := dottest.MustGetFlattenSerializableString(t, di), `digraph {graph [color="lightgrey",style="filled"];}`; got != want {
t.Errorf("got [\n%v\n] want [\n%v\n]", got, want)
}
Expand All @@ -664,7 +665,7 @@ func TestEmptyWithHTMLLabel(t *testing.T) {
t.Fatal("graph is nil, expected a valid instance")
}

di.SetAttribute(attributes.KeyLabel, attributes.NewHTML("<B>Hi</B>"))
di.SetAttribute(constants.KeyLabel, attributes.NewHTML("<B>Hi</B>"))
if got, want := dottest.MustGetFlattenSerializableString(t, di), `digraph {graph [label=<<B>Hi</B>>];}`; got != want {
t.Errorf("got [\n%v\n] want [\n%v\n]", got, want)
}
Expand All @@ -676,7 +677,7 @@ func TestEmptyWithLiteralValueLabel(t *testing.T) {
t.Fatal("graph is nil, expected a valid instance")
}

di.SetAttribute(attributes.KeyLabel, attributes.NewLiteral(`"left-justified text\l"`))
di.SetAttribute(constants.KeyLabel, attributes.NewLiteral(`"left-justified text\l"`))
if got, want := dottest.MustGetFlattenSerializableString(t, di), `digraph {graph [label="left-justified text\l"];}`; got != want {
t.Errorf("got [\n%v\n] want [\n%v\n]", got, want)
}
Expand Down Expand Up @@ -710,7 +711,7 @@ func TestTwoConnectedNodesAcrossSubgraphs(t *testing.T) {

n2 := sub.Node("B")
edge := di.Edge(n1, n2)
edge.SetAttributeString(attributes.KeyLabel, "cross-graph")
edge.SetAttributeString(constants.KeyLabel, "cross-graph")

// test graph-level edge finding
{
Expand Down Expand Up @@ -777,11 +778,11 @@ func TestSubgraph(t *testing.T) {
t.Fatal("graph is nil, expected a valid instance")
}

sub.SetAttributeString(attributes.KeyStyle, "filled")
sub.SetAttributeString(constants.KeyStyle, "filled")
if got, want := dottest.MustGetFlattenSerializableString(t, di), fmt.Sprintf(`digraph {subgraph "%s" {graph [style="filled"];}}`, sub.ID()); got != want {
t.Errorf("got [\n%v\n] want [\n%v\n]", got, want)
}
sub.SetAttributeString(attributes.KeyLabel, "new-label")
sub.SetAttributeString(constants.KeyLabel, "new-label")
if got, want := dottest.MustGetFlattenSerializableString(t, di), fmt.Sprintf(`digraph {subgraph "%s" {graph [label="new-label",style="filled"];}}`, sub.ID()); got != want {
t.Errorf("got [\n%v\n] want [\n%v\n]", got, want)
}
Expand Down Expand Up @@ -839,8 +840,8 @@ func TestNode(t *testing.T) {

node := graph.Node("")
node.SetAttributesString(attributes.MapString{
attributes.KeyLabel: "test",
attributes.KeyShape: "box",
constants.KeyLabel: "test",
constants.KeyShape: "box",
})

if node.ID() == "" {
Expand Down Expand Up @@ -874,7 +875,7 @@ func TestEdgeLabel(t *testing.T) {
n1 := di.Node("e1")
n2 := di.Node("e2")
attr := attributes.NewAttributes()
attr.SetAttributeString(attributes.KeyLabel, "what")
attr.SetAttributeString(constants.KeyLabel, "what")
n1.EdgeWithAttributes(n2, attr)
if got, want := dottest.MustGetFlattenSerializableString(t, di), `digraph {"e1";"e2";"e1"->"e2"[label="what"];}`; got != want {
t.Errorf("got [\n%v\n] want [\n%v\n]", got, want)
Expand Down Expand Up @@ -913,7 +914,7 @@ func TestCluster(t *testing.T) {
t.Fatal("graph is nil, expected a valid instance")
}

clusterA.SetAttributeString(attributes.KeyLabel, "Cluster A")
clusterA.SetAttributeString(constants.KeyLabel, "Cluster A")
insideOne := clusterA.Node("one")
insideTwo := clusterA.Node("two")
clusterB, err := di.Subgraph(
Expand All @@ -924,7 +925,7 @@ func TestCluster(t *testing.T) {
t.Fatal("graph is nil, expected a valid instance")
}

clusterB.SetAttributeString(attributes.KeyLabel, "Cluster B")
clusterB.SetAttributeString(constants.KeyLabel, "Cluster B")
insideThree := clusterB.Node("three")
insideFour := clusterB.Node("four")
outside.Edge(insideFour).Edge(insideOne).Edge(insideTwo).Edge(insideThree).Edge(outside)
Expand All @@ -941,7 +942,7 @@ func TestDeleteLabel(t *testing.T) {
}

n := g.Node("my-id")
n.DeleteAttribute(attributes.KeyLabel)
n.DeleteAttribute(constants.KeyLabel)
if got, want := dottest.MustGetFlattenSerializableString(t, g), `digraph {"my-id";}`; got != want {
t.Errorf("got [\n%v\n] want [\n%v\n]", got, want)
}
Expand Down Expand Up @@ -1039,7 +1040,7 @@ func TestLabelWithEscaping(t *testing.T) {
}

n := di.Node("without-linefeed")
n.SetAttribute(attributes.KeyLabel, attributes.NewLiteral(`"with \l linefeed"`))
n.SetAttribute(constants.KeyLabel, attributes.NewLiteral(`"with \l linefeed"`))
if got, want := dottest.MustGetFlattenSerializableString(t, di), `digraph {"without-linefeed"[label="with \l linefeed"];}`; got != want {
t.Errorf("got [\n%v\n] want [\n%v\n]", got, want)
}
Expand All @@ -1048,15 +1049,15 @@ func TestLabelWithEscaping(t *testing.T) {
func TestGraphNodeInitializer(t *testing.T) {
di, err := NewGraph(
WithNodeInitializer(func(n Node) {
n.SetAttribute(attributes.KeyLabel, attributes.NewString("test"))
n.SetAttribute(constants.KeyLabel, attributes.NewString("test"))
}),
)
if err != nil {
t.Fatal("graph is nil, expected a valid instance")
}

n := di.Node("A")
gotAttr, gotOk := n.GetAttribute(attributes.KeyLabel)
gotAttr, gotOk := n.GetAttribute(constants.KeyLabel)
if !gotOk {
t.Error("attribute not found")
}
Expand All @@ -1068,15 +1069,15 @@ func TestGraphNodeInitializer(t *testing.T) {
func TestGraphEdgeInitializer(t *testing.T) {
di, err := NewGraph(
WithEdgeInitializer(func(e StyledEdge) {
e.SetAttribute(attributes.KeyLabel, attributes.NewString("test"))
e.SetAttribute(constants.KeyLabel, attributes.NewString("test"))
}),
)
if err != nil {
t.Fatal("graph is nil, expected a valid instance")
}

e := di.Node("A").Edge(di.Node("B"))
gotAttr, gotOk := e.GetAttribute(attributes.KeyLabel)
gotAttr, gotOk := e.GetAttribute(constants.KeyLabel)
if !gotOk {
t.Error("attribute not found")
}
Expand Down
6 changes: 4 additions & 2 deletions attributes/Reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package attributes

import (
"fmt"

"github.com/wwmoraes/dot/constants"
)

// Reader is implemented by attribute-based values that allows reading them
type Reader interface {
// GetAttribute returns the attribute value or nil if unset
GetAttribute(key Key) (fmt.Stringer, bool)
GetAttribute(key constants.Key) (fmt.Stringer, bool)
// GetAttributeString returns the attribute as string or an empty string if unset
GetAttributeString(key Key) string
GetAttributeString(key constants.Key) string
// GetAttributes returns a copy of all attributes
GetAttributes() Map
// HasAttributes returns true if there's any attribute set
Expand Down
12 changes: 7 additions & 5 deletions attributes/Writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package attributes

import (
"fmt"

"github.com/wwmoraes/dot/constants"
)

// Writer is implemented by attribute-based values that allows mutating them
type Writer interface {
// SetAttribute sets the value for the attribute Key
SetAttribute(key Key, value fmt.Stringer)
SetAttribute(key constants.Key, value fmt.Stringer)
// SetAttributeString sets the string value for the attribute Key
SetAttributeString(key Key, value string)
SetAttributeString(key constants.Key, value string)
// SetAttributeLiteral sets the literal value for the attribute Key
SetAttributeLiteral(key Key, value string)
SetAttributeLiteral(key constants.Key, value string)
// SetAttributeHTML sets the HTML value for the attribute Key
SetAttributeHTML(key Key, value string)
SetAttributeHTML(key constants.Key, value string)
// SetAttributes sets the value for multiple attributes
SetAttributes(attributeMap Map)
// SetAttributesString sets the string value for multiple attributes
Expand All @@ -23,5 +25,5 @@ type Writer interface {
// SetAttributesHTML sets the HTML value for multiple attributes
SetAttributesHTML(attributeMap MapString)
// DeleteAttribute unset the attribute Key
DeleteAttribute(key Key)
DeleteAttribute(key constants.Key)
}
22 changes: 12 additions & 10 deletions attributes/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"io"
"sort"
"strings"

"github.com/wwmoraes/dot/constants"
)

// Map collection of dot component attributes
type Map map[Key]fmt.Stringer
type Map map[constants.Key]fmt.Stringer

// MapString collection of dot component attributes as primitive strings
type MapString map[Key]string
type MapString map[constants.Key]string

// Attributes dot component attributes data
type Attributes struct {
Expand Down Expand Up @@ -42,13 +44,13 @@ func (dotObjectData *Attributes) getAttributes() Map {
}

// GetAttribute returns the attribute value or nil if unset
func (dotObjectData *Attributes) GetAttribute(key Key) (fmt.Stringer, bool) {
func (dotObjectData *Attributes) GetAttribute(key constants.Key) (fmt.Stringer, bool) {
attr, found := dotObjectData.attributes[key]
return attr, found
}

// GetAttributeString returns the attribute as string or an empty string if unset
func (dotObjectData *Attributes) GetAttributeString(key Key) string {
func (dotObjectData *Attributes) GetAttributeString(key constants.Key) string {
attr, ok := dotObjectData.GetAttribute(key)

if !ok {
Expand All @@ -68,22 +70,22 @@ func (dotObjectData *Attributes) GetAttributes() Map {
}

// SetAttribute sets the value for the attribute Key
func (dotObjectData *Attributes) SetAttribute(key Key, value fmt.Stringer) {
func (dotObjectData *Attributes) SetAttribute(key constants.Key, value fmt.Stringer) {
dotObjectData.attributes[key] = value
}

// SetAttributeString sets the string value for the attribute Key
func (dotObjectData *Attributes) SetAttributeString(key Key, value string) {
func (dotObjectData *Attributes) SetAttributeString(key constants.Key, value string) {
dotObjectData.attributes[key] = NewString(value)
}

// SetAttributeLiteral sets the literal value for the attribute Key
func (dotObjectData *Attributes) SetAttributeLiteral(key Key, value string) {
func (dotObjectData *Attributes) SetAttributeLiteral(key constants.Key, value string) {
dotObjectData.attributes[key] = NewLiteral(value)
}

// SetAttributeHTML sets the HTML value for the attribute Key
func (dotObjectData *Attributes) SetAttributeHTML(key Key, value string) {
func (dotObjectData *Attributes) SetAttributeHTML(key constants.Key, value string) {
dotObjectData.attributes[key] = NewHTML(value)
}

Expand Down Expand Up @@ -116,7 +118,7 @@ func (dotObjectData *Attributes) SetAttributesHTML(attributeMap MapString) {
}

// DeleteAttribute unset the attribute Key
func (dotObjectData *Attributes) DeleteAttribute(key Key) {
func (dotObjectData *Attributes) DeleteAttribute(key constants.Key) {
delete(dotObjectData.attributes, key)
}

Expand All @@ -127,7 +129,7 @@ func (dotObjectData *Attributes) WriteTo(writer io.Writer) (n int64, err error)
}

// first collect keys
keys := []Key{}
keys := []constants.Key{}
for k := range dotObjectData.attributes {
keys = append(keys, k)
}
Expand Down
Loading

0 comments on commit d9e1528

Please sign in to comment.