Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jcouls29/dynamic #70

Closed
wants to merge 8 commits into from
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ tools/**
!tools/packages.config

# Artifacts
artifacts/**
artifacts/**
/.vs
219 changes: 219 additions & 0 deletions src/Automatonymous.Tests/Dynamic Modify/Activity_Specs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
namespace Automatonymous.Tests.DynamicModify
{
using NUnit.Framework;


[TestFixture(Category = "Dynamic Modify")]
public class When_specifying_an_event_activity
{
[Test]
public void Should_transition_to_the_proper_state()
{
Assert.AreEqual(Running, _instance.CurrentState);
}

State Running;
Event Initialized;

Instance _instance;
StateMachine<Instance> _machine;

[OneTimeSetUp]
public void Specifying_an_event_activity()
{
_instance = new Instance();
_machine = AutomatonymousStateMachine<Instance>
.Create(builder => builder
.State("Running", out Running)
.Event("Initialized", out Initialized)
.InstanceState(b => b.CurrentState)
.During(builder.Initial)
.When(Initialized, b => b.TransitionTo(Running))
);

_machine.RaiseEvent(_instance, Initialized)
.Wait();
}


class Instance
{
public State CurrentState { get; set; }
}
}


[TestFixture(Category = "Dynamic Modify")]
public class When_specifying_an_event_activity_using_initially
{
[Test]
public void Should_transition_to_the_proper_state()
{
Assert.AreEqual(Running, _instance.CurrentState);
}

State Running;
Event Initialized;

Instance _instance;
StateMachine<Instance> _machine;

[OneTimeSetUp]
public void Specifying_an_event_activity()
{
_instance = new Instance();
_machine = AutomatonymousStateMachine<Instance>
.Create(builder => builder
.State("Running", out Running)
.Event("Initialized", out Initialized)
.InstanceState(b => b.CurrentState)
.During(builder.Initial)
.When(Initialized, b => b.TransitionTo(Running))
);

_machine.RaiseEvent(_instance, Initialized);
}


class Instance
{
public State CurrentState { get; set; }
}


class InstanceStateMachine :
AutomatonymousStateMachine<Instance>
{
public InstanceStateMachine()
{
Initially(
When(Initialized)
.TransitionTo(Running));
}

public State Running { get; private set; }

public Event Initialized { get; private set; }
}
}


[TestFixture(Category = "Dynamic Modify")]
public class When_specifying_an_event_activity_using_finally
{
[Test]
public void Should_have_called_the_finally_activity()
{
Assert.AreEqual(Finalized, _instance.Value);
}

[Test]
public void Should_transition_to_the_proper_state()
{
Assert.AreEqual(_machine.Final, _instance.CurrentState);
}

const string Finalized = "Finalized";

State Running;
Event Initialized;

Instance _instance;
StateMachine<Instance> _machine;

[OneTimeSetUp]
public void Specifying_an_event_activity()
{
const string Finalized = "Finalized";

_instance = new Instance();
_machine = AutomatonymousStateMachine<Instance>
.Create(builder => builder
.State("Running", out Running)
.Event("Initialized", out Initialized)
.InstanceState(b => b.CurrentState)
.During(builder.Initial)
.When(Initialized, b => b.Finalize())
.Finally(b => b.Then(context => context.Instance.Value = Finalized))
);

_machine.RaiseEvent(_instance, Initialized)
.Wait();
}


class Instance
{
public string Value { get; set; }
public State CurrentState { get; set; }
}
}


[TestFixture(Category = "Dynamic Modify")]
public class When_hooking_the_initial_enter_state_event
{
[Test]
public void Should_call_the_activity()
{
Assert.AreEqual(_machine.Final, _instance.CurrentState);
}

[Test]
public void Should_have_trigger_the_final_before_enter_event()
{
Assert.AreEqual(Running, _instance.FinalState);
}

[Test]
public void Should_have_triggered_the_after_leave_event()
{
Assert.AreEqual(_machine.Initial, _instance.LeftState);
}

[Test]
public void Should_have_triggered_the_before_enter_event()
{
Assert.AreEqual(Initializing, _instance.EnteredState);
}

State Running;
State Initializing;
Event Initialized;

Instance _instance;
StateMachine<Instance> _machine;

[OneTimeSetUp]
public void Specifying_an_event_activity()
{
_instance = new Instance();
_machine = AutomatonymousStateMachine<Instance>
.Create(builder => builder
.State("Running", out Running)
.State("Initializing", out Initializing)
.Event("Initialized", out Initialized)
.InstanceState(b => b.CurrentState)
.During(Initializing)
.When(Initialized, b => b.TransitionTo(Running))
.DuringAny()
.When(builder.Initial.Enter, b => b.TransitionTo(Initializing))
.When(builder.Initial.AfterLeave, b => b.Then(context => context.Instance.LeftState = context.Data))
.When(Initializing.BeforeEnter, b => b.Then(context => context.Instance.EnteredState = context.Data))
.When(Running.Enter, b => b.Finalize())
.When(builder.Final.BeforeEnter, b => b.Then(context => context.Instance.FinalState = context.Instance.CurrentState))
);

_machine.RaiseEvent(_instance, Initialized)
.Wait();
}

class Instance
{
public State CurrentState { get; set; }
public State EnteredState { get; set; }
public State LeftState { get; set; }
public State FinalState { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
namespace Automatonymous.Tests.DynamicModify
{
using NUnit.Framework;


[TestFixture(Category = "Dynamic Modify")]
public class When_any_state_transition_occurs
{
[Test]
public void Should_be_running()
{
Assert.AreEqual(Running, _instance.CurrentState);
}

[Test]
public void Should_have_entered_running()
{
Assert.AreEqual(Running, _instance.LastEntered);
}

[Test]
public void Should_have_left_initial()
{
Assert.AreEqual(_machine.Initial, _instance.LastLeft);
}

State Running;
Event Initialized;
Event Finish;

Instance _instance;
StateMachine<Instance> _machine;

[OneTimeSetUp]
public void Setup()
{
_instance = new Instance();
_machine = AutomatonymousStateMachine<Instance>
.Create(builder => builder
.State("Running", out Running)
.Event("Initialized", out Initialized)
.Event("Finish", out Finish)
.InstanceState(b => b.CurrentState)
.During(builder.Initial)
.When(Initialized, b => b.TransitionTo(Running))
.During(Running)
.When(Finish, b => b.Finalize())
.BeforeEnterAny(b => b.Then(context => context.Instance.LastEntered = context.Data))
.AfterLeaveAny(b => b.Then(context => context.Instance.LastLeft = context.Data))
);

_machine.RaiseEvent(_instance, Initialized)
.Wait();
}


class Instance
{
public State CurrentState { get; set; }

public State LastEntered { get; set; }
public State LastLeft { get; set; }
}
}
}
90 changes: 90 additions & 0 deletions src/Automatonymous.Tests/Dynamic Modify/Anytime_Specs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
namespace Automatonymous.Tests.DynamicModify
{
using System.Threading.Tasks;
using NUnit.Framework;


[TestFixture(Category = "Dynamic Modify")]
public class Anytime_events
{
[Test]
public async Task Should_be_called_regardless_of_state()
{
var instance = new Instance();

await _machine.RaiseEvent(instance, Init);
await _machine.RaiseEvent(instance, Hello);

Assert.IsTrue(instance.HelloCalled);
Assert.AreEqual(_machine.Final, instance.CurrentState);
}

[Test]
public async Task Should_have_value_of_event_data()
{
var instance = new Instance();

await _machine.RaiseEvent(instance, Init);
await _machine.RaiseEvent(instance, EventA, new A
{
Value = "Test"
});

Assert.AreEqual("Test", instance.AValue);
Assert.AreEqual(_machine.Final, instance.CurrentState);
}

[Test]
public void Should_not_be_handled_on_initial()
{
var instance = new Instance();

Assert.That(async () => await _machine.RaiseEvent(instance, Hello), Throws.TypeOf<UnhandledEventException>());

Assert.IsFalse(instance.HelloCalled);
Assert.AreEqual(_machine.Initial, instance.CurrentState);
}

State Ready;
Event Init;
Event Hello;
Event<A> EventA;

StateMachine<Instance> _machine;

[OneTimeSetUp]
public void A_state_is_declared()
{
_machine = AutomatonymousStateMachine<Instance>
.Create(builder => builder
.State("Ready", out Ready)
.Event("Init", out Init)
.Event("Hello", out Hello)
.Event("EventA", out EventA)
.Initially()
.When(Init, b => b.TransitionTo(Ready))
.DuringAny()
.When(Hello, b => b
.Then(context => context.Instance.HelloCalled = true)
.Finalize()
)
.When(EventA, b => b
.Then(context => context.Instance.AValue = context.Data.Value)
.Finalize()
)
);
}

class A
{
public string Value { get; set; }
}

class Instance
{
public bool HelloCalled { get; set; }
public string AValue { get; set; }
public State CurrentState { get; set; }
}
}
}
Loading