Skip to content

Commit 88ad97c

Browse files
committed
Adds tests.
Updates README.md.
1 parent 180d558 commit 88ad97c

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

MonoGameStateMachine/Fsm.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,17 @@ private bool CheckAfterEntries(List<Timer<TS>> afterEntries,
9696
{
9797
if (t.ConditionsMet(Current.Identifier))
9898
{
99+
double timerMax = e.Time;
99100
double? r = e.Tick(g.ElapsedGameTime.TotalMilliseconds);
100101
afterEntries[i] = e;
101102
if (r.HasValue)
102103
{
103104
// It triggered.
104105
DoTransition(e.Target, default(TT), t.Pop);
105106
gt.IsRunningSlowly = g.IsRunningSlowly;
106-
gt.TotalGameTime = g.TotalGameTime.Subtract(TimeSpan.FromMilliseconds(r.Value));
107+
gt.TotalGameTime = g.TotalGameTime.Subtract(TimeSpan.FromMilliseconds(timerMax));
107108
gt.ElapsedGameTime =
108-
g.ElapsedGameTime.Subtract(TimeSpan.FromMilliseconds(r.Value));
109+
g.ElapsedGameTime.Subtract(TimeSpan.FromMilliseconds(timerMax));
109110
Update(gt);
110111
return true;
111112
}

MonoGameStateMachine/NUnitTests/FluentTests.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ private class Button
5959
}
6060

6161
[Test]
62-
[Category("MonoGameStateMachine.FluentTests.OnMethods")]
63-
public void OnMethodTests()
62+
[Category("MonoGameStateMachine.FluentTests")]
63+
public void OnMethodHooksShouldBeTriggeredCorrectly()
6464
{
6565
var button = new Button();
6666

@@ -130,5 +130,56 @@ public void OnMethodTests()
130130
// Update was triggered twice over all states.
131131
Assert.That(button.UpdateCounter, Is.EqualTo(3F));
132132
}
133+
134+
[Test]
135+
[Category("MonoGameStateMachine.FluentTests")]
136+
public void WhenMultipleAfterConditionsFireOnASingleUpdateAdvanceStateCorrectly()
137+
{
138+
var m = Fsm<State, Trigger>.Builder(State.IDLE)
139+
.State(State.IDLE)
140+
.TransitionTo(State.OVER).After(10, TimeUnit.MILLISECONDS)
141+
.State(State.OVER)
142+
.TransitionTo(State.PRESSED).After(10, TimeUnit.MILLISECONDS)
143+
.State(State.PRESSED)
144+
.TransitionTo(State.REFRESHING).After(10, TimeUnit.MILLISECONDS)
145+
.State(State.REFRESHING)
146+
.TransitionTo(State.IDLE).After(10, TimeUnit.MILLISECONDS)
147+
.Build();
148+
149+
Assert.That(m.Current.Identifier, Is.EqualTo(State.IDLE));
150+
m.Update(new GameTime(TimeSpan.FromDays(1), TimeSpan.FromMilliseconds(40)));
151+
Assert.That(m.Current.Identifier, Is.EqualTo(State.IDLE));
152+
}
153+
154+
[Test]
155+
[Category("MonoGameStateMachine.FluentTests")]
156+
public void WhenMultipleAfterConditionsFireOnASingleUpdateOnEnterAndOnExitShoudFire()
157+
{
158+
int enterCount = 0;
159+
int exitCount = 0;
160+
var m = Fsm<State, Trigger>.Builder(State.IDLE)
161+
.State(State.IDLE)
162+
.OnEnter(t => enterCount++)
163+
.OnExit(t => exitCount++)
164+
.TransitionTo(State.OVER).After(10, TimeUnit.MILLISECONDS)
165+
.State(State.OVER)
166+
.OnEnter(t => enterCount++)
167+
.OnExit(t => exitCount++)
168+
.TransitionTo(State.PRESSED).After(10, TimeUnit.MILLISECONDS)
169+
.State(State.PRESSED)
170+
.OnEnter(t => enterCount++)
171+
.OnExit(t => exitCount++)
172+
.TransitionTo(State.REFRESHING).After(10, TimeUnit.MILLISECONDS)
173+
.State(State.REFRESHING)
174+
.OnEnter(t => enterCount++)
175+
.OnExit(t => exitCount++)
176+
.TransitionTo(State.IDLE).After(10, TimeUnit.MILLISECONDS)
177+
.Build();
178+
179+
Assert.That(m.Current.Identifier, Is.EqualTo(State.IDLE));
180+
m.Update(new GameTime(TimeSpan.FromDays(1), TimeSpan.FromMilliseconds(40)));
181+
Assert.That(enterCount, Is.EqualTo(4));
182+
Assert.That(exitCount, Is.EqualTo(4));
183+
}
133184
}
134185
}

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
StateMachine:[![NuGet](https://img.shields.io/nuget/v/StateMachine.svg?maxAge=2592000)](https://www.nuget.org/packages/StateMachine/)
2-
MonoGameStateMachine:[![NuGet](https://img.shields.io/nuget/v/MonoGameStateMachine.svg?maxAge=2592000)](https://www.nuget.org/packages/MonoGameStateMachine/)
3-
[![license](https://img.shields.io/github/license/unterrainerinformatik/FiniteStateMachine.svg?maxAge=2592000)](http://unlicense.org) [![Twitter Follow](https://img.shields.io/twitter/follow/throbax.svg?style=social&label=Follow&maxAge=2592000)](https://twitter.com/throbax)
1+
[![license](https://img.shields.io/github/license/unterrainerinformatik/FiniteStateMachine.svg?maxAge=2592000)](http://unlicense.org) [![Twitter Follow](https://img.shields.io/twitter/follow/throbax.svg?style=social&label=Follow&maxAge=2592000)](https://twitter.com/throbax)
2+
3+
| Project | Package |
4+
| -------------------- | :--------------------------------------: |
5+
| StateMachine | [![NuGet](https://img.shields.io/nuget/v/StateMachine.svg?maxAge=2592000)](https://www.nuget.org/packages/StateMachine/) |
6+
| MonoGameStateMachine | [![NuGet](https://img.shields.io/nuget/v/MonoGameStateMachine.svg?maxAge=2592000)](https://www.nuget.org/packages/MonoGameStateMachine/) |
47

58
# Finite-State-Machine
69

0 commit comments

Comments
 (0)