Skip to content

Commit

Permalink
Add unit tests & documentation for RandomSelector and RandomSequence
Browse files Browse the repository at this point in the history
  • Loading branch information
meniku committed Jun 24, 2019
1 parent f90ce17 commit c2b5795
Show file tree
Hide file tree
Showing 7 changed files with 583 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Editor/Tests/Composite/ParallelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public void StopLowerPriorityChildrenForChild_WithoutImmediateRestart_ShouldThro

sut.StopLowerPriorityChildrenForChild(firstChild, false);
}
catch(Exception e)
catch(Exception)
{
bExceptionCought = true;
}
Expand Down
219 changes: 219 additions & 0 deletions Editor/Tests/Composite/RandomSelectorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
using NUnit.Framework;
namespace NPBehave
{

public class RandomSelectorTest : Test
{
[Test]
public void ShouldFail_WhenSingleChildFails()
{
MockNode failingChild = new MockNode();
RandomSelector sut = new RandomSelector( failingChild );
TestRoot behaviorTree = CreateBehaviorTree( sut );

behaviorTree.Start();

Assert.AreEqual( Node.State.ACTIVE, sut.CurrentState );

failingChild.Finish( false );

Assert.AreEqual( Node.State.INACTIVE, sut.CurrentState );
Assert.IsTrue( behaviorTree.DidFinish );
Assert.IsFalse( behaviorTree.WasSuccess );
}

[Test]
public void ShouldSucceed_WhenSingleChildSucceeds()
{
MockNode succeedingChild = new MockNode();
RandomSelector sut = new RandomSelector( succeedingChild );
TestRoot behaviorTree = CreateBehaviorTree( sut );

behaviorTree.Start();

Assert.AreEqual( Node.State.ACTIVE, sut.CurrentState );

succeedingChild.Finish( true );

Assert.AreEqual( Node.State.INACTIVE, sut.CurrentState );
Assert.IsTrue( behaviorTree.DidFinish );
Assert.IsTrue( behaviorTree.WasSuccess );
}

[Test]
public void ShouldFail_WhenStoppedExplicitly()
{
MockNode failingChild = new MockNode( false );
RandomSelector sut = new RandomSelector( failingChild );
TestRoot behaviorTree = CreateBehaviorTree( sut );

behaviorTree.Start();

Assert.AreEqual( Node.State.ACTIVE, sut.CurrentState );

sut.Stop();

Assert.AreEqual( Node.State.INACTIVE, sut.CurrentState );
Assert.IsTrue( behaviorTree.DidFinish );
Assert.IsFalse( behaviorTree.WasSuccess );
}

[Test]
public void ShouldSucceed_WhenStoppedExplicitlyButChildStillFinishesSuccessfully()
{
MockNode succeedingChild = new MockNode( true );
RandomSelector sut = new RandomSelector( succeedingChild );
TestRoot behaviorTree = CreateBehaviorTree( sut );

behaviorTree.Start();

Assert.AreEqual( Node.State.ACTIVE, sut.CurrentState );

sut.Stop();

Assert.AreEqual( Node.State.INACTIVE, sut.CurrentState );
Assert.IsTrue( behaviorTree.DidFinish );
Assert.True( behaviorTree.WasSuccess );
}

[Test]
public void ShouldSucceed_WhenFirstChildSuccessful()
{
MockNode firstChild = new MockNode();
MockNode secondChild = new MockNode();
RandomSelector sut = new RandomSelector( firstChild, secondChild );
TestRoot behaviorTree = CreateBehaviorTree( sut );

behaviorTree.Start();

MockNode firstActiveChild = sut.DebugGetActiveChild() as MockNode;
MockNode inactiveChild = firstActiveChild == firstChild ? secondChild : firstChild;
Assert.IsNotNull( firstActiveChild );

Assert.AreEqual( Node.State.ACTIVE, sut.CurrentState );
Assert.AreEqual( Node.State.ACTIVE, firstActiveChild.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, inactiveChild.CurrentState );

firstActiveChild.Finish( true );

Assert.AreEqual( Node.State.INACTIVE, sut.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, firstChild.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, secondChild.CurrentState );
Assert.IsTrue( behaviorTree.DidFinish );
Assert.IsTrue( behaviorTree.WasSuccess );
}

[Test]
public void ShouldProcceedToSecondChild_WhenFirstChildFailed()
{
MockNode firstChild = new MockNode();
MockNode secondChild = new MockNode();
RandomSelector sut = new RandomSelector( firstChild, secondChild );
TestRoot behaviorTree = CreateBehaviorTree( sut );

behaviorTree.Start();

MockNode firstActiveChild = sut.DebugGetActiveChild() as MockNode;
MockNode secondActiveChild = firstActiveChild == firstChild ? secondChild : firstChild;
Assert.IsNotNull( firstActiveChild );

Assert.AreEqual( Node.State.ACTIVE, sut.CurrentState );
Assert.AreEqual( Node.State.ACTIVE, firstActiveChild.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, secondActiveChild.CurrentState );

firstActiveChild.Finish( false );

Assert.AreEqual( Node.State.ACTIVE, sut.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, firstActiveChild.CurrentState );
Assert.AreEqual( Node.State.ACTIVE, secondActiveChild.CurrentState );

secondActiveChild.Finish( false );

Assert.AreEqual( Node.State.INACTIVE, sut.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, firstActiveChild.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, secondActiveChild.CurrentState );
Assert.IsTrue( behaviorTree.DidFinish );
Assert.IsFalse( behaviorTree.WasSuccess );
}

[Test]
public void StopLowerPriorityChildrenForChild_WithoutImmediateRestart_ShouldCancelSecondChild()
{
MockNode firstChild = new MockNode();
MockNode secondChild = new MockNode();
RandomSelector sut = new RandomSelector( firstChild, secondChild );
TestRoot behaviorTree = CreateBehaviorTree( sut );

// TODO: will we keep the priority or will we switch to the priority defined by the randomized children?
RandomSelector.DebugSetSeed( 2 );

behaviorTree.Start();
firstChild.Finish( false );

Assert.AreEqual( Node.State.ACTIVE, sut.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, firstChild.CurrentState );
Assert.AreEqual( Node.State.ACTIVE, secondChild.CurrentState );

sut.StopLowerPriorityChildrenForChild( firstChild, false );

Assert.AreEqual( Node.State.INACTIVE, sut.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, firstChild.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, secondChild.CurrentState );
Assert.IsTrue( behaviorTree.DidFinish );
Assert.IsFalse( behaviorTree.WasSuccess );
}

[Test]
public void StopLowerPriorityChildrenForChild_WithImmediateRestart_ShouldRestartFirstChild_WhenSecondChildFails()
{
MockNode firstChild = new MockNode();
MockNode secondChild = new MockNode( false );
RandomSelector sut = new RandomSelector( firstChild, secondChild );
TestRoot behaviorTree = CreateBehaviorTree( sut );

// TODO: will we keep the priority or will we switch to the priority defined by the randomized children?
RandomSelector.DebugSetSeed( 2 );

behaviorTree.Start();
firstChild.Finish( false );

Assert.AreEqual( Node.State.ACTIVE, sut.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, firstChild.CurrentState );
Assert.AreEqual( Node.State.ACTIVE, secondChild.CurrentState );

sut.StopLowerPriorityChildrenForChild( firstChild, true );

Assert.AreEqual( Node.State.ACTIVE, sut.CurrentState );
Assert.AreEqual( Node.State.ACTIVE, firstChild.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, secondChild.CurrentState );
Assert.IsFalse( behaviorTree.DidFinish );
}

[Test]
public void StopLowerPriorityChildrenForChild_WithImmediateRestart_ShouldNotRestartFirstChild_WhenSecondChildSucceeds()
{
MockNode firstChild = new MockNode();
MockNode secondChild = new MockNode( true );
RandomSelector sut = new RandomSelector( firstChild, secondChild );
TestRoot behaviorTree = CreateBehaviorTree( sut );

// TODO: will we keep the priority or will we switch to the priority defined by the randomized children?
RandomSelector.DebugSetSeed( 2 );

behaviorTree.Start();
firstChild.Finish( false );

Assert.AreEqual( Node.State.ACTIVE, sut.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, firstChild.CurrentState );
Assert.AreEqual( Node.State.ACTIVE, secondChild.CurrentState );

sut.StopLowerPriorityChildrenForChild( firstChild, true );

Assert.AreEqual( Node.State.INACTIVE, sut.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, firstChild.CurrentState );
Assert.AreEqual( Node.State.INACTIVE, secondChild.CurrentState );
Assert.IsTrue( behaviorTree.DidFinish );
Assert.IsTrue( behaviorTree.WasSuccess );
}
}
}
Loading

0 comments on commit c2b5795

Please sign in to comment.