Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jul 19, 2015
1 parent f2b175f commit 13f1cf2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/main/java/net/citizensnpcs/api/ai/tree/Selector.java
Expand Up @@ -34,8 +34,9 @@ public Function<List<Behavior>, Behavior> getSelectionFunction() {
@Override
public void reset() {
super.reset();
if (executing != null)
if (executing != null) {
stopExecution(executing);
}
executing = null;
}

Expand Down
33 changes: 16 additions & 17 deletions src/test/java/net/citizensnpcs/api/ai/BehaviorTreeTest.java
Expand Up @@ -5,26 +5,24 @@

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import com.google.common.base.Function;

import net.citizensnpcs.api.ai.tree.Behavior;
import net.citizensnpcs.api.ai.tree.BehaviorGoalAdapter;
import net.citizensnpcs.api.ai.tree.BehaviorStatus;
import net.citizensnpcs.api.ai.tree.Selector;
import net.citizensnpcs.api.ai.tree.Sequence;

import org.junit.Before;
import org.junit.Test;

import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

public class BehaviorTreeTest {
private GoalController test;

@Test
public void failureSelector() {
final CountedBehavior goal = new CountedBehavior(Suppliers.ofInstance(BehaviorStatus.SUCCESS));
CountedBehavior goal2 = new CountedBehavior(Suppliers.ofInstance(BehaviorStatus.FAILURE));
final CountedBehavior goal = new CountedBehavior(BehaviorStatus.SUCCESS);
CountedBehavior goal2 = new CountedBehavior(BehaviorStatus.FAILURE);
Selector p = Selector.selecting(goal2, goal).selectionFunction(new Function<List<Behavior>, Behavior>() {
int idx;

Expand All @@ -48,11 +46,12 @@ public Behavior apply(List<Behavior> input) {

@Test
public void failureSequence() {
CountedBehavior goal = new CountedBehavior(Suppliers.ofInstance(BehaviorStatus.FAILURE));
CountedBehavior goal2 = new CountedBehavior(Suppliers.ofInstance(BehaviorStatus.SUCCESS));
CountedBehavior goal = new CountedBehavior(BehaviorStatus.FAILURE);
CountedBehavior goal2 = new CountedBehavior(BehaviorStatus.SUCCESS);
Sequence p = Sequence.createRetryingSequence(goal, goal2);
test.addGoal(p, 1);
test.run();
test.run();
assertThat("Reset count", goal.resetCount, is(1));
assertThat("Run count", goal.runCount, is(1));
assertThat("Should execute count", goal.shouldExecuteCount, is(1));
Expand All @@ -68,7 +67,7 @@ public void setUp() {

@Test
public void singleSelector() {
CountedBehavior goal = new CountedBehavior(Suppliers.ofInstance(BehaviorStatus.SUCCESS));
CountedBehavior goal = new CountedBehavior(BehaviorStatus.SUCCESS);
Selector p = Selector.selecting(goal).build();
test.addGoal(p, 1);
test.run();
Expand All @@ -79,7 +78,7 @@ public void singleSelector() {

@Test
public void singleSequence() {
CountedBehavior goal = new CountedBehavior(Suppliers.ofInstance(BehaviorStatus.SUCCESS));
CountedBehavior goal = new CountedBehavior(BehaviorStatus.SUCCESS);
Sequence p = Sequence.createSequence(goal);
test.addGoal(p, 1);
test.run();
Expand All @@ -91,11 +90,11 @@ public void singleSequence() {
private static class CountedBehavior extends BehaviorGoalAdapter {
public int loggingTag = 0;
private int resetCount;
private final Supplier<BehaviorStatus> ret;
private final BehaviorStatus ret;
private int runCount;
private int shouldExecuteCount;

private CountedBehavior(Supplier<BehaviorStatus> ret) {
private CountedBehavior(BehaviorStatus ret) {
this.ret = ret;
}

Expand All @@ -109,9 +108,9 @@ public void reset() {
@Override
public BehaviorStatus run() {
if (loggingTag > 0)
System.err.println(loggingTag + ": run " + ret.get());
System.err.println(loggingTag + ": run " + ret);
runCount++;
return ret.get();
return ret;
}

@Override
Expand Down

0 comments on commit 13f1cf2

Please sign in to comment.