Skip to content

Commit

Permalink
Added else conditions for If/IfAsync (IfElse/IfElseAsync)
Browse files Browse the repository at this point in the history
  • Loading branch information
Greybird authored and phatboyg committed Mar 3, 2019
1 parent 35a2c95 commit c647062
Show file tree
Hide file tree
Showing 11 changed files with 477 additions and 149 deletions.
42 changes: 34 additions & 8 deletions src/Automatonymous.Tests/Condition_Specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public async Task Should_allow_if_condition_to_be_evaluated()
Assert.That(_instance.CurrentState, Is.Not.EqualTo(_machine.ShouldNotBeHere));
}

[Test]
public async Task Should_evaluate_else_statement_when_if_condition__is_false()
{
await _machine.RaiseEvent(_instance, _machine.ExplicitFilterStarted, new StartedExplicitFilter());

Assert.That(_instance.ShouldBeCalled, Is.True);
}


[SetUp]
public void Specifying_an_event_activity()
{
Expand All @@ -59,6 +68,8 @@ class Instance
{
public bool InitializeOnly { get; set; }
public State CurrentState { get; set; }

public bool ShouldBeCalled { get; set; }
}


Expand All @@ -75,10 +86,13 @@ public InstanceStateMachine()

During(Initial,
When(ExplicitFilterStarted, context => true)
.If(context => false, binder => binder
.Then(context => Console.WriteLine("Should not be here!"))
.TransitionTo(ShouldNotBeHere))
.If(context => true, binder => binder.Then(context => Console.WriteLine("Initializing Only!"))));
.IfElse(context => false,
binder => binder
.Then(context => Console.WriteLine("Should not be here!"))
.TransitionTo(ShouldNotBeHere),
binder => binder
.Then(context => context.Instance.ShouldBeCalled = true)
.Then(context => Console.WriteLine("Initializing Only!"))));

During(Running,
When(Finish)
Expand Down Expand Up @@ -136,6 +150,14 @@ public async Task Should_allow_if_condition_to_be_evaluated()
Assert.That(_instance.CurrentState, Is.Not.EqualTo(_machine.ShouldNotBeHere));
}

[Test]
public async Task Should_evaluate_else_statement_when_if_condition__is_false()
{
await _machine.RaiseEvent(_instance, _machine.ExplicitFilterStarted, new StartedExplicitFilter());

Assert.That(_instance.ShouldBeCalled, Is.True);
}

[SetUp]
public void Specifying_an_event_activity()
{
Expand All @@ -151,6 +173,7 @@ class Instance
{
public bool InitializeOnly { get; set; }
public State CurrentState { get; set; }
public bool ShouldBeCalled { get; set; }
}


Expand All @@ -167,10 +190,13 @@ public InstanceStateMachine()

During(Initial,
When(ExplicitFilterStarted, context => true)
.IfAsync(context => Task.FromResult(false), binder => binder
.Then(context => Console.WriteLine("Should not be here!"))
.TransitionTo(ShouldNotBeHere))
.IfAsync(context => Task.FromResult(true), binder => binder.Then(context => Console.WriteLine("Initializing Only!"))));
.IfElseAsync(context => Task.FromResult(false),
binder => binder
.Then(context => Console.WriteLine("Should not be here!"))
.TransitionTo(ShouldNotBeHere),
binder => binder
.Then(context => context.Instance.ShouldBeCalled = true)
.Then(context => Console.WriteLine("Initializing Only!"))));

During(Running,
When(Finish)
Expand Down
81 changes: 78 additions & 3 deletions src/Automatonymous.Tests/Exception_Specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ public Instance()
public bool ShouldNotBeCalled { get; set; }

public bool CalledThenClause { get; set; }
public bool CalledSecondThenClause { get; set; }
public bool CalledThenClauseAsync { get; set; }

public bool ThenShouldNotBeCalled { get; set; }
public bool ElseShouldBeCalled { get; set; }
public bool ThenAsyncShouldNotBeCalled { get; set; }
public bool ElseAsyncShouldBeCalled { get; set; }
}


Expand All @@ -70,7 +75,15 @@ public InstanceStateMachine()
.Then(context => context.Instance.CalledThenClause = true)
)
.IfAsync(context => Task.FromResult(true), b => b
.Then(context => context.Instance.CalledSecondThenClause = true)
.Then(context => context.Instance.CalledThenClauseAsync = true)
)
.IfElse(context => false,
b => b.Then(context => context.Instance.ThenShouldNotBeCalled = true),
b => b.Then(context => context.Instance.ElseShouldBeCalled = true)
)
.IfElseAsync(context => Task.FromResult(false),
b => b.Then(context => context.Instance.ThenAsyncShouldNotBeCalled = true),
b => b.Then(context => context.Instance.ElseAsyncShouldBeCalled = true)
)
.Then(context =>
{
Expand Down Expand Up @@ -142,7 +155,31 @@ public void Should_have_called_the_first_if_block()
[Test]
public void Should_have_called_the_async_if_block()
{
Assert.IsTrue(_instance.CalledSecondThenClause);
Assert.IsTrue(_instance.CalledThenClauseAsync);
}

[Test]
public void Should_not_have_called_the_false_condition_then_block()
{
Assert.IsFalse(_instance.ThenShouldNotBeCalled);
}

[Test]
public void Should_not_have_called_the_false_async_condition_then_block()
{
Assert.IsFalse(_instance.ThenAsyncShouldNotBeCalled);
}

[Test]
public void Should_have_called_the_false_condition_else_block()
{
Assert.IsTrue(_instance.ElseShouldBeCalled);
}

[Test]
public void Should_have_called_the_false_async_condition_else_block()
{
Assert.IsTrue(_instance.ElseAsyncShouldBeCalled);
}
}

Expand Down Expand Up @@ -316,6 +353,11 @@ public Instance()

public bool CalledThenClause { get; set; }
public bool CalledSecondThenClause { get; set; }

public bool ThenShouldNotBeCalled { get; set; }
public bool ElseShouldBeCalled { get; set; }
public bool ThenAsyncShouldNotBeCalled { get; set; }
public bool ElseAsyncShouldBeCalled { get; set; }
}


Expand Down Expand Up @@ -343,6 +385,14 @@ public InstanceStateMachine()
.IfAsync(context => Task.FromResult(true), b => b
.Then(context => context.Instance.CalledSecondThenClause = true)
)
.IfElse(context => false,
b => b.Then(context => context.Instance.ThenShouldNotBeCalled = true),
b => b.Then(context => context.Instance.ElseShouldBeCalled = true)
)
.IfElseAsync(context => Task.FromResult(false),
b => b.Then(context => context.Instance.ThenAsyncShouldNotBeCalled = true),
b => b.Then(context => context.Instance.ElseAsyncShouldBeCalled = true)
)
.Then(context =>
{
context.Instance.ExceptionMessage = context.Exception.Message;
Expand Down Expand Up @@ -398,5 +448,30 @@ public void Should_have_called_the_async_if_block()
{
Assert.IsTrue(_instance.CalledSecondThenClause);
}

[Test]
public void Should_not_have_called_the_false_condition_then_block()
{
Assert.IsFalse(_instance.ThenShouldNotBeCalled);
}

[Test]
public void Should_not_have_called_the_false_async_condition_then_block()
{
Assert.IsFalse(_instance.ThenAsyncShouldNotBeCalled);
}

[Test]
public void Should_have_called_the_false_condition_else_block()
{
Assert.IsTrue(_instance.ElseShouldBeCalled);
}

[Test]
public void Should_have_called_the_false_async_condition_else_block()
{
Assert.IsTrue(_instance.ElseAsyncShouldBeCalled);
}
}

}
40 changes: 27 additions & 13 deletions src/Automatonymous/Activities/ConditionActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,47 @@ public class ConditionActivity<TInstance> :
Activity<TInstance>
where TInstance : class
{
readonly Behavior<TInstance> _behavior;
readonly Behavior<TInstance> _thenBehavior;
readonly Behavior<TInstance> _elseBehavior;
readonly StateMachineAsyncCondition<TInstance> _condition;

public ConditionActivity(StateMachineAsyncCondition<TInstance> condition, Behavior<TInstance> behavior)
public ConditionActivity(StateMachineAsyncCondition<TInstance> condition, Behavior<TInstance> thenBehavior, Behavior<TInstance> elseBehavior)
{
_condition = condition;
_behavior = behavior;
_thenBehavior = thenBehavior;
_elseBehavior = elseBehavior;
}

void IProbeSite.Probe(ProbeContext context)
{
var scope = context.CreateScope("condition");

_behavior.Probe(scope);
_thenBehavior.Probe(scope);
_elseBehavior.Probe(scope);
}

void Visitable.Accept(StateMachineVisitor visitor)
{
visitor.Visit(this, x => _behavior.Accept(visitor));
visitor.Visit(this, x => _thenBehavior.Accept(visitor));
visitor.Visit(this, x => _elseBehavior.Accept(visitor));
}

async Task Activity<TInstance>.Execute(BehaviorContext<TInstance> context, Behavior<TInstance> next)
{
if (await _condition(context).ConfigureAwait(false))
await _behavior.Execute(context).ConfigureAwait(false);
await _thenBehavior.Execute(context).ConfigureAwait(false);
else
await _elseBehavior.Execute(context).ConfigureAwait(false);

await next.Execute(context).ConfigureAwait(false);
}

async Task Activity<TInstance>.Execute<T>(BehaviorContext<TInstance, T> context, Behavior<TInstance, T> next)
{
if (await _condition(context).ConfigureAwait(false))
await _behavior.Execute(context).ConfigureAwait(false);
await _thenBehavior.Execute(context).ConfigureAwait(false);
else
await _elseBehavior.Execute(context).ConfigureAwait(false);

await next.Execute(context).ConfigureAwait(false);
}
Expand All @@ -74,25 +82,29 @@ public class ConditionActivity<TInstance, TData> :
Activity<TInstance>
where TInstance : class
{
readonly Behavior<TInstance> _behavior;
readonly Behavior<TInstance> _thenBehavior;
readonly Behavior<TInstance> _elseBehavior;
readonly StateMachineAsyncCondition<TInstance, TData> _condition;

public ConditionActivity(StateMachineAsyncCondition<TInstance, TData> condition, Behavior<TInstance> behavior)
public ConditionActivity(StateMachineAsyncCondition<TInstance, TData> condition, Behavior<TInstance> thenBehavior, Behavior<TInstance> elseBehavior)
{
_condition = condition;
_behavior = behavior;
_thenBehavior = thenBehavior;
_elseBehavior = elseBehavior;
}

void IProbeSite.Probe(ProbeContext context)
{
var scope = context.CreateScope("condition");

_behavior.Probe(scope);
_thenBehavior.Probe(scope);
_elseBehavior.Probe(scope);
}

void Visitable.Accept(StateMachineVisitor visitor)
{
visitor.Visit(this, x => _behavior.Accept(visitor));
visitor.Visit(this, x => _thenBehavior.Accept(visitor));
visitor.Visit(this, x => _elseBehavior.Accept(visitor));
}

Task Activity<TInstance>.Execute(BehaviorContext<TInstance> context, Behavior<TInstance> next)
Expand All @@ -106,7 +118,9 @@ async Task Activity<TInstance>.Execute<T>(BehaviorContext<TInstance, T> context,
if (behaviorContext != null)
{
if (await _condition(behaviorContext).ConfigureAwait(false))
await _behavior.Execute(behaviorContext).ConfigureAwait(false);
await _thenBehavior.Execute(behaviorContext).ConfigureAwait(false);
else
await _elseBehavior.Execute(behaviorContext).ConfigureAwait(false);
}

await next.Execute(context).ConfigureAwait(false);
Expand Down
Loading

0 comments on commit c647062

Please sign in to comment.