-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConditionalTests.cs
More file actions
91 lines (74 loc) · 2.65 KB
/
Copy pathConditionalTests.cs
File metadata and controls
91 lines (74 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using GroveGames.BehaviourTree.Collections;
using GroveGames.BehaviourTree.Nodes;
using GroveGames.BehaviourTree.Nodes.Decorators;
namespace GroveGames.BehaviourTree.Tests.Nodes.Decorators;
public class ConditionalTests
{
private sealed class TestBlackboard : IBlackboard
{
public T? GetValue<T>(string key) => default;
public void SetValue<T>(string key, T value) where T : notnull { }
public void DeleteValue(string key) { }
public void Clear() { }
}
private sealed class TestNode : INode
{
public int EvaluateCount { get; private set; }
public NodeState ReturnState { get; set; } = NodeState.Success;
public NodeState State => ReturnState;
public string Name => string.Empty;
public NodeState Evaluate(float deltaTime)
{
EvaluateCount++;
return ReturnState;
}
public void Reset() { }
public void Abort() { }
public void StartEvaluate() { }
public void EndEvaluate() { }
public void SetParent(IParent parent) { }
public void SetName(string name)
{
}
}
private sealed class TestParent : IParent
{
public IBlackboard Blackboard { get; } = new TestBlackboard();
public IParent Attach(INode node) => this;
public IParent Attach(IChildTree tree) => this;
}
[Fact]
public void Evaluate_ShouldReturnFailureWhenConditionIsFalse()
{
var parent = new TestParent();
var child = new TestNode();
var conditional = new Conditional(() => false);
conditional.Attach(child);
var result = conditional.Evaluate(1.0f);
Assert.Equal(NodeState.Failure, result);
Assert.Equal(NodeState.Failure, conditional.State);
Assert.Equal(0, child.EvaluateCount);
}
[Fact]
public void Evaluate_ShouldCallChildEvaluateWhenConditionIsTrue()
{
var parent = new TestParent();
var child = new TestNode { ReturnState = NodeState.Running };
var conditional = new Conditional(() => true);
conditional.Attach(child);
var result = conditional.Evaluate(1.0f);
Assert.Equal(NodeState.Running, result);
Assert.Equal(NodeState.Running, conditional.State);
Assert.Equal(1, child.EvaluateCount);
}
[Fact]
public void Evaluate_ShouldNotCallChildEvaluateWhenConditionIsFalse()
{
var parent = new TestParent();
var child = new TestNode();
var conditional = new Conditional(() => false);
conditional.Attach(child);
conditional.Evaluate(1.0f);
Assert.Equal(0, child.EvaluateCount);
}
}