From f6953c99becda23d47306d7b1a31ff970e473d3a Mon Sep 17 00:00:00 2001 From: greeflas Date: Tue, 16 Jan 2024 23:20:12 +0200 Subject: [PATCH] visualize child scopes --- visualize.go | 10 +++++++- visualize_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/visualize.go b/visualize.go index d7ff3248..05a91ee8 100644 --- a/visualize.go +++ b/visualize.go @@ -174,11 +174,19 @@ func (c *Container) createGraph() *dot.Graph { func (s *Scope) createGraph() *dot.Graph { dg := dot.NewGraph() + s.addNodes(dg) + + return dg +} + +func (s *Scope) addNodes(dg *dot.Graph) { for _, n := range s.nodes { dg.AddCtor(newDotCtor(n), n.paramList.DotParam(), n.resultList.DotResult()) } - return dg + for _, cs := range s.childScopes { + cs.addNodes(dg) + } } func newDotCtor(n *constructorNode) *dot.Ctor { diff --git a/visualize_test.go b/visualize_test.go index 07d8e5a9..c626c639 100644 --- a/visualize_test.go +++ b/visualize_test.go @@ -116,7 +116,7 @@ func TestDotGraph(t *testing.T) { assertCtorsEqual(t, expected, dg.Ctors) }) - t.Run("create graph with multple constructors", func(t *testing.T) { + t.Run("create graph with multiple constructors", func(t *testing.T) { expected := []*dot.Ctor{ { Params: []*dot.Param{p1}, @@ -141,6 +141,62 @@ func TestDotGraph(t *testing.T) { assertCtorsEqual(t, expected, dg.Ctors) }) + t.Run("create graph with scope", func(t *testing.T) { + expected := []*dot.Ctor{ + { + Params: []*dot.Param{p1}, + Results: []*dot.Result{r2}, + }, + { + Params: []*dot.Param{p1}, + Results: []*dot.Result{r3}, + }, + { + Params: []*dot.Param{p2}, + Results: []*dot.Result{r4}, + }, + } + + c := digtest.New(t) + c.Provide(func(A t1) t2 { return t2{} }) + + s := c.Scope("test") + s.Provide(func(A t1) t3 { return t3{} }) + s.Provide(func(A t2) t4 { return t4{} }) + + dg := c.CreateGraph() + assertCtorsEqual(t, expected, dg.Ctors) + }) + + t.Run("create graph with child scope", func(t *testing.T) { + expected := []*dot.Ctor{ + { + Params: []*dot.Param{p1}, + Results: []*dot.Result{r2}, + }, + { + Params: []*dot.Param{p1}, + Results: []*dot.Result{r3}, + }, + { + Params: []*dot.Param{p2}, + Results: []*dot.Result{r4}, + }, + } + + c := digtest.New(t) + c.Provide(func(A t1) t2 { return t2{} }) + + s := c.Scope("parent_scope") + s.Provide(func(A t1) t3 { return t3{} }) + + cs := s.Scope("child_scope") + cs.Provide(func(A t2) t4 { return t4{} }) + + dg := c.CreateGraph() + assertCtorsEqual(t, expected, dg.Ctors) + }) + t.Run("constructor with multiple params and results", func(t *testing.T) { expected := []*dot.Ctor{ {