Skip to content

Commit 45369ac

Browse files
authored
Visualize child scopes (#403)
visualize child scopes
1 parent a772ad7 commit 45369ac

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

visualize.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,19 @@ func (c *Container) createGraph() *dot.Graph {
174174
func (s *Scope) createGraph() *dot.Graph {
175175
dg := dot.NewGraph()
176176

177+
s.addNodes(dg)
178+
179+
return dg
180+
}
181+
182+
func (s *Scope) addNodes(dg *dot.Graph) {
177183
for _, n := range s.nodes {
178184
dg.AddCtor(newDotCtor(n), n.paramList.DotParam(), n.resultList.DotResult())
179185
}
180186

181-
return dg
187+
for _, cs := range s.childScopes {
188+
cs.addNodes(dg)
189+
}
182190
}
183191

184192
func newDotCtor(n *constructorNode) *dot.Ctor {

visualize_test.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestDotGraph(t *testing.T) {
116116
assertCtorsEqual(t, expected, dg.Ctors)
117117
})
118118

119-
t.Run("create graph with multple constructors", func(t *testing.T) {
119+
t.Run("create graph with multiple constructors", func(t *testing.T) {
120120
expected := []*dot.Ctor{
121121
{
122122
Params: []*dot.Param{p1},
@@ -141,6 +141,62 @@ func TestDotGraph(t *testing.T) {
141141
assertCtorsEqual(t, expected, dg.Ctors)
142142
})
143143

144+
t.Run("create graph with scope", func(t *testing.T) {
145+
expected := []*dot.Ctor{
146+
{
147+
Params: []*dot.Param{p1},
148+
Results: []*dot.Result{r2},
149+
},
150+
{
151+
Params: []*dot.Param{p1},
152+
Results: []*dot.Result{r3},
153+
},
154+
{
155+
Params: []*dot.Param{p2},
156+
Results: []*dot.Result{r4},
157+
},
158+
}
159+
160+
c := digtest.New(t)
161+
c.Provide(func(A t1) t2 { return t2{} })
162+
163+
s := c.Scope("test")
164+
s.Provide(func(A t1) t3 { return t3{} })
165+
s.Provide(func(A t2) t4 { return t4{} })
166+
167+
dg := c.CreateGraph()
168+
assertCtorsEqual(t, expected, dg.Ctors)
169+
})
170+
171+
t.Run("create graph with child scope", func(t *testing.T) {
172+
expected := []*dot.Ctor{
173+
{
174+
Params: []*dot.Param{p1},
175+
Results: []*dot.Result{r2},
176+
},
177+
{
178+
Params: []*dot.Param{p1},
179+
Results: []*dot.Result{r3},
180+
},
181+
{
182+
Params: []*dot.Param{p2},
183+
Results: []*dot.Result{r4},
184+
},
185+
}
186+
187+
c := digtest.New(t)
188+
c.Provide(func(A t1) t2 { return t2{} })
189+
190+
s := c.Scope("parent_scope")
191+
s.Provide(func(A t1) t3 { return t3{} })
192+
193+
cs := s.Scope("child_scope")
194+
cs.Provide(func(A t2) t4 { return t4{} })
195+
196+
dg := c.CreateGraph()
197+
assertCtorsEqual(t, expected, dg.Ctors)
198+
})
199+
144200
t.Run("constructor with multiple params and results", func(t *testing.T) {
145201
expected := []*dot.Ctor{
146202
{

0 commit comments

Comments
 (0)