22using Adnc . FluidBT . Tasks ;
33using NSubstitute ;
44using System . Collections . Generic ;
5+ using NSubstitute . ReturnsExtensions ;
56using NUnit . Framework ;
67
78namespace Adnc . FluidBT . Testing {
@@ -14,6 +15,19 @@ public void SetSelector () {
1415 _selector = new Selector ( ) ;
1516 }
1617
18+ public void CheckUpdateCalls ( List < int > updateCalls ) {
19+ for ( var i = 0 ; i < updateCalls . Count ; i ++ ) {
20+ var child = _selector . children [ i ] ;
21+ child . Received ( updateCalls [ i ] ) . Update ( ) ;
22+ }
23+ }
24+
25+ public class NoNodes : UpdateMethod {
26+ public void Returns_success ( ) {
27+ Assert . AreEqual ( TaskStatus . Success , _selector . Update ( ) ) ;
28+ }
29+ }
30+
1731 public class SingleNode : UpdateMethod {
1832 [ Test ]
1933 public void Returns_success_if_a_child_task_returns_success ( ) {
@@ -54,10 +68,7 @@ public void Stops_on_continue () {
5468 _selector . Update ( ) ;
5569
5670 var updateCalls = new List < int > { 1 , 1 , 0 } ;
57- for ( var i = 0 ; i < updateCalls . Count ; i ++ ) {
58- var child = _selector . children [ i ] ;
59- child . Received ( updateCalls [ i ] ) . Update ( ) ;
60- }
71+ CheckUpdateCalls ( updateCalls ) ;
6172 }
6273
6374 [ Test ]
@@ -72,10 +83,7 @@ public void Reruns_the_same_node_if_it_returns_continue () {
7283 _selector . Update ( ) ;
7384
7485 var updateCalls = new List < int > { 1 , 2 } ;
75- for ( var i = 0 ; i < updateCalls . Count ; i ++ ) {
76- var child = _selector . children [ i ] ;
77- child . Received ( updateCalls [ i ] ) . Update ( ) ;
78- }
86+ CheckUpdateCalls ( updateCalls ) ;
7987 }
8088
8189 [ Test ]
@@ -103,15 +111,120 @@ public void Stops_on_first_success_node () {
103111 _selector . Update ( ) ;
104112
105113 var updateCalls = new List < int > { 1 , 1 , 0 } ;
106- for ( var i = 0 ; i < updateCalls . Count ; i ++ ) {
107- var child = _selector . children [ i ] ;
108- child . Received ( updateCalls [ i ] ) . Update ( ) ;
109- }
114+ CheckUpdateCalls ( updateCalls ) ;
110115 }
111116 }
112117
113118 public class ConditionalAbortSelf : UpdateMethod {
119+ public ITask childFailure ;
120+ public ITask childContinue ;
121+
122+ [ SetUp ]
123+ public void CreateChildren ( ) {
124+ _selector . AbortType = AbortType . Self ;
125+
126+ childFailure = A . TaskStub ( )
127+ . WithAbortConditionSelf ( true )
128+ . WithUpdateStatus ( TaskStatus . Failure )
129+ . Build ( ) ;
130+
131+ childContinue = A . TaskStub ( )
132+ . WithUpdateStatus ( TaskStatus . Continue )
133+ . Build ( ) ;
134+ }
135+
136+ [ Test ]
114137 public void Revaultes_first_condition_node_when_it_goes_from_failure_to_success ( ) {
138+ _selector . AddChild ( childFailure ) ;
139+ _selector . AddChild ( childContinue ) ;
140+
141+ _selector . Update ( ) ;
142+ childFailure . Update ( ) . Returns ( TaskStatus . Success ) ;
143+ _selector . Update ( ) ;
144+
145+ var updateCalls = new List < int > { 3 , 1 } ;
146+ CheckUpdateCalls ( updateCalls ) ;
147+ }
148+
149+ [ Test ]
150+ public void Does_not_revaluate_if_no_abort_type ( ) {
151+ _selector . AbortType = AbortType . None ;
152+
153+ _selector . AddChild ( childFailure ) ;
154+ _selector . AddChild ( childContinue ) ;
155+
156+ _selector . Update ( ) ;
157+ childFailure . Update ( ) . Returns ( TaskStatus . Success ) ;
158+ _selector . Update ( ) ;
159+
160+ var updateCalls = new List < int > { 1 , 2 } ;
161+ CheckUpdateCalls ( updateCalls ) ;
162+ }
163+
164+ [ Test ]
165+ public void Does_not_crash_if_self_abort_is_null ( ) {
166+ childFailure . GetAbortCondition ( ) . ReturnsNull ( ) ;
167+
168+ _selector . AddChild ( childFailure ) ;
169+ _selector . AddChild ( childContinue ) ;
170+
171+ _selector . Update ( ) ;
172+ childFailure . Update ( ) . Returns ( TaskStatus . Success ) ;
173+ _selector . Update ( ) ;
174+
175+ var updateCalls = new List < int > { 1 , 2 } ;
176+ CheckUpdateCalls ( updateCalls ) ;
177+ }
178+
179+ [ Test ]
180+ public void Returns_true_when_revaluating_abort_condition ( ) {
181+ _selector . AddChild ( childFailure ) ;
182+ _selector . AddChild ( childContinue ) ;
183+
184+ _selector . Update ( ) ;
185+ childFailure . Update ( ) . Returns ( TaskStatus . Success ) ;
186+
187+ Assert . AreEqual ( TaskStatus . Success , _selector . Update ( ) ) ;
188+ }
189+
190+ [ Test ]
191+ public void Triggers_reset_on_abort ( ) {
192+ _selector . AddChild ( childFailure ) ;
193+ _selector . AddChild ( childContinue ) ;
194+
195+ _selector . Update ( ) ;
196+ childFailure . Update ( ) . Returns ( TaskStatus . Success ) ;
197+ _selector . Update ( ) ;
198+
199+ childFailure . Received ( 1 ) . Reset ( ) ;
200+ childContinue . Received ( 1 ) . Reset ( ) ;
201+ }
202+
203+ [ Test ]
204+ public void Triggers_end_on_current_task_pointer ( ) {
205+ _selector . AddChild ( childFailure ) ;
206+ _selector . AddChild ( childContinue ) ;
207+
208+ _selector . Update ( ) ;
209+ childFailure . Update ( ) . Returns ( TaskStatus . Success ) ;
210+ _selector . Update ( ) ;
211+
212+ childContinue . Received ( 1 ) . End ( ) ;
213+ }
214+
215+ [ Test ]
216+ public void Returns_failure_if_nested_condition_is_followed_by_failure_task ( ) {
217+ var nestedSequence = new Sequence ( )
218+ . AddChild ( childFailure )
219+ . AddChild ( A . TaskStub ( ) . WithUpdateStatus ( TaskStatus . Failure ) . Build ( ) ) ;
220+
221+ _selector . AddChild ( nestedSequence ) ;
222+ _selector . AddChild ( childContinue ) ;
223+
224+ _selector . Update ( ) ;
225+ childFailure . Update ( ) . Returns ( TaskStatus . Success ) ;
226+
227+ Assert . AreEqual ( TaskStatus . Continue , _selector . Update ( ) ) ;
115228 }
116229 }
117230 }
0 commit comments