Skip to content

Commit 69656eb

Browse files
committed
Completion of decorator inverter
1 parent 42c07d4 commit 69656eb

File tree

3 files changed

+142
-14
lines changed

3 files changed

+142
-14
lines changed

Assets/FluidBehaviorTree/Editor/Testing/Decorators/InverterTest.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Adnc.FluidBT.Decorators;
2+
using Adnc.FluidBT.Tasks;
23
using NSubstitute;
34
using NUnit.Framework;
45

@@ -35,5 +36,109 @@ public void Returns_false_if_child_is_set_but_set_to_false () {
3536
Assert.IsFalse(inverter.Enabled);
3637
}
3738
}
39+
40+
public class UpdateMethod {
41+
[Test]
42+
public void Returns_failure_when_child_returns_success () {
43+
var inverter = new Inverter {
44+
child = A.TaskStub().Build()
45+
};
46+
47+
Assert.AreEqual(TaskStatus.Failure, inverter.Update());
48+
}
49+
50+
[Test]
51+
public void Returns_true_when_child_returns_false () {
52+
var inverter = new Inverter {
53+
child = A.TaskStub().WithUpdateStatus(TaskStatus.Failure).Build()
54+
};
55+
56+
Assert.AreEqual(TaskStatus.Success, inverter.Update());
57+
}
58+
59+
[Test]
60+
public void Returns_continue_when_child_returns_continue () {
61+
var inverter = new Inverter {
62+
child = A.TaskStub().WithUpdateStatus(TaskStatus.Continue).Build()
63+
};
64+
65+
Assert.AreEqual(TaskStatus.Continue, inverter.Update());
66+
}
67+
68+
[Test]
69+
public void Does_not_crash_if_no_child () {
70+
var inverter = new Inverter();
71+
72+
inverter.Update();
73+
}
74+
75+
[Test]
76+
public void Sets_last_status_to_inverted_value () {
77+
var inverter = new Inverter {
78+
child = A.TaskStub().Build()
79+
};
80+
81+
var status = inverter.Update();
82+
83+
Assert.AreEqual(status, inverter.LastStatus);
84+
}
85+
}
86+
87+
public class GetAbortConditionMethod {
88+
[Test]
89+
public void Returns_itself_as_an_abort_condition_if_child_returns () {
90+
var task = A.TaskStub()
91+
.WithAbortConditionSelf(true)
92+
.Build();
93+
94+
var inverter = new Inverter { child = task };
95+
96+
Assert.AreEqual(inverter, inverter.GetAbortCondition());
97+
}
98+
99+
[Test]
100+
public void Does_not_return_an_abort_condition_if_child_does_not_return () {
101+
var inverter = new Inverter { child = A.TaskStub().Build() };
102+
103+
Assert.AreEqual(null, inverter.GetAbortCondition());
104+
}
105+
}
106+
107+
public class GetAbortStatusMethod {
108+
[Test]
109+
public void Returns_status_from_update () {
110+
var task = A.TaskStub()
111+
.WithAbortConditionSelf(true)
112+
.Build();
113+
114+
var inverter = new Inverter { child = task };
115+
116+
Assert.AreEqual(TaskStatus.Failure, inverter.GetAbortStatus());
117+
}
118+
}
119+
120+
public class EndMethod {
121+
[Test]
122+
public void Calls_end_on_child () {
123+
var task = A.TaskStub().Build();
124+
var inverter = new Inverter { child = task };
125+
126+
inverter.End();
127+
128+
task.Received(1).End();
129+
}
130+
}
131+
132+
public class ResetMethod {
133+
[Test]
134+
public void Runs_reset_on_child () {
135+
var task = A.TaskStub().Build();
136+
var inverter = new Inverter { child = task };
137+
138+
inverter.Reset();
139+
140+
task.Received(1).Reset();
141+
}
142+
}
38143
}
39144
}

Assets/FluidBehaviorTree/Scripts/TaskParents/Decorators/Inverter.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,50 @@ public bool Enabled {
99
}
1010

1111
public bool IsLowerPriority { get; } = false;
12-
public TaskStatus LastStatus { get; }
12+
public TaskStatus LastStatus { get; private set; }
1313
public ITask child;
1414

1515
public TaskStatus Update () {
16-
throw new System.NotImplementedException();
16+
if (child == null) {
17+
return TaskStatus.Success;
18+
}
19+
20+
var childStatus = child.Update();
21+
var status = childStatus;
22+
23+
switch (childStatus) {
24+
case TaskStatus.Success:
25+
status = TaskStatus.Failure;
26+
break;
27+
case TaskStatus.Failure:
28+
status = TaskStatus.Success;
29+
break;
30+
}
31+
32+
// @TODO Can be moved to base class
33+
LastStatus = status;
34+
35+
return status;
1736
}
1837

1938
public ITask GetAbortCondition () {
20-
throw new System.NotImplementedException();
39+
if (child.GetAbortCondition() != null) {
40+
return this;
41+
}
42+
43+
return null;
2144
}
2245

2346
public void End () {
24-
throw new System.NotImplementedException();
47+
child.End();
2548
}
2649

2750
public void Reset (bool hardReset = false) {
28-
throw new System.NotImplementedException();
51+
child.Reset();
2952
}
3053

3154
public TaskStatus GetAbortStatus () {
32-
throw new System.NotImplementedException();
55+
return Update();
3356
}
3457
}
3558
}

Assets/FluidBehaviorTree/Scripts/Tasks/ITask.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public interface ITask {
2727
/// <returns></returns>
2828
ITask GetAbortCondition ();
2929

30+
/// <summary>
31+
/// Query this parent task to get the correct corresponding abort type
32+
/// Example selector tries all valid conditions until success (or returns failure), while
33+
/// sequence returns first item
34+
/// </summary>
35+
/// <returns></returns>
36+
TaskStatus GetAbortStatus ();
37+
3038
/// <summary>
3139
/// Forcibly end this task. Firing all necessary completion logic
3240
/// </summary>
@@ -38,13 +46,5 @@ public interface ITask {
3846
/// </summary>
3947
/// <param name="hardReset"></param>
4048
void Reset (bool hardReset = false);
41-
42-
/// <summary>
43-
/// Query this parent task to get the correct corresponding abort type
44-
/// Example selector tries all valid conditions until success (or returns failure), while
45-
/// sequence returns first item
46-
/// </summary>
47-
/// <returns></returns>
48-
TaskStatus GetAbortStatus ();
4949
}
5050
}

0 commit comments

Comments
 (0)