Skip to content

Commit 1fb0664

Browse files
committed
Moved inverter decorate base code to base class
1 parent 69656eb commit 1fb0664

File tree

6 files changed

+166
-136
lines changed

6 files changed

+166
-136
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using Adnc.FluidBT.Decorators;
2+
using Adnc.FluidBT.Tasks;
3+
using NSubstitute;
4+
using NUnit.Framework;
5+
6+
namespace Adnc.FluidBT.Testing {
7+
public class DecoratorTest {
8+
public class DecoratorExample : DecoratorBase {
9+
public TaskStatus status;
10+
11+
protected override TaskStatus OnUpdate () {
12+
return status;
13+
}
14+
}
15+
16+
public class EnabledProperty {
17+
[Test]
18+
public void Returns_true_if_child_is_set () {
19+
var decorator = new DecoratorExample { child = A.TaskStub().Build() };
20+
21+
Assert.IsTrue(decorator.Enabled);
22+
}
23+
24+
[Test]
25+
public void Returns_false_if_child_is_not_set () {
26+
var decorator = new DecoratorExample();
27+
28+
Assert.IsFalse(decorator.Enabled);
29+
}
30+
31+
[Test]
32+
public void Returns_false_if_child_is_disabled () {
33+
var decorator = new DecoratorExample { child = A.TaskStub().Build() };
34+
decorator.child.Enabled.Returns(false);
35+
36+
Assert.IsTrue(decorator.Enabled);
37+
}
38+
39+
[Test]
40+
public void Returns_false_if_child_is_set_but_set_to_false () {
41+
var decorator = new DecoratorExample { child = A.TaskStub().Build() };
42+
decorator.Enabled = false;
43+
44+
Assert.IsFalse(decorator.Enabled);
45+
}
46+
}
47+
48+
public class UpdateMethod {
49+
[Test]
50+
public void Sets_LastUpdate_to_returned_status_value () {
51+
var decorator = new DecoratorExample();
52+
decorator.status = TaskStatus.Failure;
53+
54+
decorator.Update();
55+
56+
Assert.AreEqual(TaskStatus.Failure, decorator.LastStatus);
57+
}
58+
}
59+
60+
public class GetAbortConditionMethod {
61+
[Test]
62+
public void Returns_itself_as_an_abort_condition_if_child_returns () {
63+
var task = A.TaskStub()
64+
.WithAbortConditionSelf(true)
65+
.Build();
66+
67+
var decorator = new DecoratorExample { child = task };
68+
69+
Assert.AreEqual(decorator, decorator.GetAbortCondition());
70+
}
71+
72+
[Test]
73+
public void Does_not_return_an_abort_condition_if_child_does_not_return () {
74+
var decorator = new DecoratorExample { child = A.TaskStub().Build() };
75+
76+
Assert.AreEqual(null, decorator.GetAbortCondition());
77+
}
78+
}
79+
80+
public class GetAbortStatusMethod {
81+
[Test]
82+
public void Returns_status_from_update () {
83+
var decorator = new DecoratorExample { status = TaskStatus.Failure };
84+
85+
Assert.AreEqual(TaskStatus.Failure, decorator.GetAbortStatus());
86+
}
87+
}
88+
89+
public class EndMethod {
90+
[Test]
91+
public void Calls_end_on_child () {
92+
var task = A.TaskStub().Build();
93+
var decorator = new DecoratorExample { child = task };
94+
95+
decorator.End();
96+
97+
task.Received(1).End();
98+
}
99+
}
100+
101+
public class ResetMethod {
102+
[Test]
103+
public void Runs_reset_on_child () {
104+
var task = A.TaskStub().Build();
105+
var decorator = new DecoratorExample { child = task };
106+
107+
decorator.Reset();
108+
109+
task.Received(1).Reset();
110+
}
111+
}
112+
}
113+
}

Assets/FluidBehaviorTree/Editor/Testing/Decorators/DecoratorTest.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,9 @@
11
using Adnc.FluidBT.Decorators;
22
using Adnc.FluidBT.Tasks;
3-
using NSubstitute;
43
using NUnit.Framework;
54

65
namespace Adnc.FluidBT.Testing {
76
public class InverterTest {
8-
public class EnabledProperty {
9-
[Test]
10-
public void Returns_true_if_child_is_set () {
11-
var inverter = new Inverter { child = A.TaskStub().Build() };
12-
13-
Assert.IsTrue(inverter.Enabled);
14-
}
15-
16-
[Test]
17-
public void Returns_false_if_child_is_not_set () {
18-
var inverter = new Inverter();
19-
20-
Assert.IsFalse(inverter.Enabled);
21-
}
22-
23-
[Test]
24-
public void Returns_false_if_child_is_disabled () {
25-
var inverter = new Inverter { child = A.TaskStub().Build() };
26-
inverter.child.Enabled.Returns(false);
27-
28-
Assert.IsTrue(inverter.Enabled);
29-
}
30-
31-
[Test]
32-
public void Returns_false_if_child_is_set_but_set_to_false () {
33-
var inverter = new Inverter { child = A.TaskStub().Build() };
34-
inverter.Enabled = false;
35-
36-
Assert.IsFalse(inverter.Enabled);
37-
}
38-
}
39-
407
public class UpdateMethod {
418
[Test]
429
public void Returns_failure_when_child_returns_success () {
@@ -71,74 +38,6 @@ public void Does_not_crash_if_no_child () {
7138

7239
inverter.Update();
7340
}
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-
}
14241
}
14342
}
14443
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Adnc.FluidBT.Tasks;
2+
3+
namespace Adnc.FluidBT.Decorators {
4+
public abstract class DecoratorBase : ITask {
5+
public ITask child;
6+
7+
private bool _enabled = true;
8+
public bool Enabled {
9+
get { return child != null && _enabled; }
10+
set { _enabled = value; }
11+
}
12+
13+
public bool IsLowerPriority { get; } = false;
14+
public TaskStatus LastStatus { get; private set; }
15+
16+
public TaskStatus Update () {
17+
var status = OnUpdate();
18+
LastStatus = status;
19+
20+
return status;
21+
}
22+
23+
protected abstract TaskStatus OnUpdate ();
24+
25+
public ITask GetAbortCondition () {
26+
if (child.GetAbortCondition() != null) {
27+
return this;
28+
}
29+
30+
return null;
31+
}
32+
33+
public TaskStatus GetAbortStatus () {
34+
return Update();
35+
}
36+
37+
public void End () {
38+
child.End();
39+
}
40+
41+
public void Reset (bool hardReset = false) {
42+
child.Reset();
43+
}
44+
}
45+
}

Assets/FluidBehaviorTree/Scripts/TaskParents/Decorators/DecoratorBase.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
using Adnc.FluidBT.Tasks;
22

33
namespace Adnc.FluidBT.Decorators {
4-
public class Inverter : ITask {
5-
private bool _enabled = true;
6-
public bool Enabled {
7-
get { return child != null && _enabled; }
8-
set { _enabled = value; }
9-
}
10-
11-
public bool IsLowerPriority { get; } = false;
12-
public TaskStatus LastStatus { get; private set; }
13-
public ITask child;
14-
15-
public TaskStatus Update () {
4+
public class Inverter : DecoratorBase {
5+
protected override TaskStatus OnUpdate () {
166
if (child == null) {
177
return TaskStatus.Success;
188
}
@@ -29,30 +19,7 @@ public TaskStatus Update () {
2919
break;
3020
}
3121

32-
// @TODO Can be moved to base class
33-
LastStatus = status;
34-
3522
return status;
3623
}
37-
38-
public ITask GetAbortCondition () {
39-
if (child.GetAbortCondition() != null) {
40-
return this;
41-
}
42-
43-
return null;
44-
}
45-
46-
public void End () {
47-
child.End();
48-
}
49-
50-
public void Reset (bool hardReset = false) {
51-
child.Reset();
52-
}
53-
54-
public TaskStatus GetAbortStatus () {
55-
return Update();
56-
}
5724
}
5825
}

0 commit comments

Comments
 (0)