@@ -152,6 +152,35 @@ public void Listen_RootType()
152152 rootListener . Count . Should ( ) . Equal ( 10 ) ;
153153 }
154154
155+ [ Test ]
156+ public void Listen_ShouldListenToChildren ( )
157+ {
158+ var doListenToChildren = new TestListener < ANode > { ListenToChildren = ( _ , _ ) => true } ;
159+ var doNotListenToChildren = new TestListener < BNode > { ListenToChildren = ( _ , _ ) => false } ;
160+
161+ var tree = new ANode ( new ANode ( new CNode ( ) ) , new BNode ( new BChild ( ) ) , new CNode ( ) ) ;
162+
163+ var listener = CompositeListener < TestContext , TestNode >
164+ . Build ( )
165+ . With ( doListenToChildren )
166+ . With ( doNotListenToChildren )
167+ . ToListener ( ) ;
168+
169+ var context = new TestContext ( ) ;
170+ listener . Listen ( context , tree ) ;
171+
172+ context . Count . Should ( ) . Equal ( 3 ) ;
173+ doListenToChildren . Count . Should ( ) . Equal ( 2 ) ;
174+ doNotListenToChildren . Count . Should ( ) . Equal ( 1 ) ;
175+
176+ // Repeat to ensure cached handlers work.
177+ listener . Listen ( context , tree ) ;
178+
179+ context . Count . Should ( ) . Equal ( 6 ) ;
180+ doListenToChildren . Count . Should ( ) . Equal ( 4 ) ;
181+ doNotListenToChildren . Count . Should ( ) . Equal ( 2 ) ;
182+ }
183+
155184 private class BChild : BNode ;
156185
157186 private class BGrandChild : BChild ;
@@ -172,13 +201,17 @@ protected internal override void ListenToNode(TestContext context, TestNode _)
172201 private sealed class TestListener < TNode > : Listener < TestContext , TestNode , TNode >
173202 where TNode : TestNode
174203 {
204+ public Func < TestContext , TNode , bool > ? ListenToChildren { get ; init ; }
205+
175206 public int Count { get ; private set ; }
176207
177208 protected override void ListenToNode ( TestContext context , TNode _ )
178209 {
179210 context . Count ++ ;
180211 Count ++ ;
181212 }
213+
214+ protected override bool ShouldListenToChildren ( TestContext context , TNode node ) => ListenToChildren ? . Invoke ( context , node ) ?? base . ShouldListenToChildren ( context , node ) ;
182215 }
183216
184217 private sealed class TestContext
0 commit comments