public async Task When_composed_via_state() { Task <object> Base(object message) { events.Add("base"); return(TaskResult.Unhandled); } Task <object> XTrait(object message) { events.Add("x"); return(TaskResult.Unhandled); } Task <object> YTrait(object message) { events.Add("y"); return(TaskResult.From("y")); } Receive[] Cast(params Receive[] args) => args; Behavior behavior = new BehaviorTester(events) .State(Base, trait: Cast(XTrait, YTrait)) .Initial(Base); var result = await behavior.Receive("foo"); AssertEqual(new[] { "base", "x", "y" }, events); Assert.AreEqual("y", result); }
public async Task When_become_stacked() { Behavior behavior = new BehaviorTester(events) .Initial("Initial") .State("Initial") .State("A") .State("B") .State("C"); await behavior.BecomeStacked("A"); await behavior.BecomeStacked("B"); await behavior.BecomeStacked("C"); Assert.That(behavior.Current.Name, Is.EqualTo("C")); await behavior.Unbecome(); Assert.That(behavior.Current.Name, Is.EqualTo("B")); await behavior.Unbecome(); Assert.That(behavior.Current.Name, Is.EqualTo("A")); await behavior.Unbecome(); Assert.That(behavior.Current.Name, Is.EqualTo("Initial")); Assert.ThrowsAsync <InvalidOperationException>(() => behavior.Unbecome()); }
public async Task When_transitioning_different_super() { Behavior behavior = new BehaviorTester(events) .State("Initial") .State("A", super: "S") .State("B", super: "SS") .State("S", super: "SS") .State("SS", super: "SSS") .State("C", super: "SSSS") .State("SSS") .State("SSSS") .Initial("A"); await behavior.Become("C"); Assert.That(behavior.Current.Name, Is.EqualTo("C")); AssertEvents( "OnDeactivate_A", "OnDeactivate_S", "OnDeactivate_SS", "OnDeactivate_SSS", "OnUnbecome_A", "OnUnbecome_S", "OnUnbecome_SS", "OnUnbecome_SSS", "OnBecome_SSSS", "OnBecome_C", "OnActivate_SSSS", "OnActivate_C" ); }
public async Task When_transitioning() { Behavior behavior = new BehaviorTester(events) .State("Initial") .State("A"); behavior.Initial("Initial"); var etag = behavior.Etag; await behavior.Become("A"); Assert.That(behavior.Current.Name, Is.EqualTo("A")); Assert.That(behavior.Etag, Is.Not.Null); Assert.That(behavior.Etag, Is.Not.EqualTo(etag)); var expected = new[] { "OnDeactivate_Initial", "OnUnbecome_Initial", "OnBecome_A", "OnActivate_A" }; AssertEvents(expected); }
public async Task When_transitioning_within_same_super() { Behavior behavior = new BehaviorTester(events) .State("Initial") .State("A", super: "S") .State("B", super: "SS") .State("S", super: "SS") .State("SS") .Initial("A"); await behavior.Become("B"); Assert.That(behavior.Current, Is.EqualTo("B")); AssertEvents( "OnTransitioning_A_B", "OnDeactivate_A", "OnDeactivate_S", "OnUnbecome_A", "OnUnbecome_S", "OnBecome_B", "OnActivate_B", "OnTransitioned_B_A" ); }
public async Task When_become_with_arguments() { string passedArg = null; Task <object> Receive(object message) { if (message is Become <string> m) { passedArg = m.Argument; } return(TaskResult.Done); }; Behavior behavior = new BehaviorTester(events) .State("A", Receive) .State("B", Receive) .Initial("A"); await behavior.Become("B", "arg1"); Assert.That(passedArg, Is.EqualTo("arg1")); await behavior.BecomeStacked("A", "arg2"); Assert.That(passedArg, Is.EqualTo("arg2")); }
public void When_returns_null_task() { Behavior behavior = new BehaviorTester(events) .State("A", x => null) .Initial("A"); var exception = Assert.ThrowsAsync <InvalidOperationException>(async() => await behavior.Receive("foo")); Assert.That(exception.Message, Is.EqualTo("Behavior returns null task on handling 'foo' message")); }
public void When_setting_initial() { Behavior behavior = new BehaviorTester(events) .State("Initial"); behavior.Initial("Initial"); Assert.That(behavior.Current.Name, Is.EqualTo("Initial")); Assert.That(events, Has.Count.EqualTo(0), "OnBecome should not be called when setting initial"); }
public void When_receiving_behavior_messages_directly() { Behavior behavior = new BehaviorTester(events) .State("Initial"); behavior.Initial("Initial"); Assert.ThrowsAsync <InvalidOperationException>(() => behavior.Receive(Become.Message)); Assert.ThrowsAsync <InvalidOperationException>(() => behavior.Receive(Unbecome.Message)); Assert.That(behavior.Current.Name, Is.EqualTo("Initial")); Assert.That(events, Has.Count.EqualTo(0)); }
public async Task When_receiving_deactivate_message_initial() { Behavior behavior = new BehaviorTester(events) .State("Initial"); behavior.Initial("Initial"); var result = await behavior.Receive(Deactivate.Message); Assert.That(result, Is.SameAs(Done.Result)); Assert.That(behavior.Current.Name, Is.EqualTo("Initial")); AssertEvents("OnDeactivate_Initial"); }
public async Task When_receiving_message() { Task <object> Receive(object x) => x is string ?Task.FromResult <object>("foo") : Task.FromResult <object>("bar"); Behavior behavior = new BehaviorTester(events) .State("A", Receive); behavior.Initial("A"); Assert.That(await behavior.Receive("1"), Is.EqualTo("foo")); Assert.That(await behavior.Receive(1), Is.EqualTo("bar")); }
public async Task When_using_fluent_dsl() { Behavior behavior = new BehaviorTester(events) .State("Initial") .State("S1") .Substate("S1A") .Substate("S1B") .State("S2") .Substate("S2A") .Initial("Initial"); await behavior.Become("S1A"); Assert.That(behavior.Current.Name, Is.EqualTo("S1A")); await behavior.Become("S1B"); Assert.That(behavior.Current.Name, Is.EqualTo("S1B")); await behavior.Become("S2A"); Assert.That(behavior.Current.Name, Is.EqualTo("S2A")); Debug.WriteLine(string.Join(",\r\n", events.Select(x => $"\"{x}\""))); AssertEvents( "OnDeactivate_Initial", "OnUnbecome_Initial", "OnBecome_S1", "OnBecome_S1A", "OnActivate_S1", "OnActivate_S1A", "OnDeactivate_S1A", "OnUnbecome_S1A", "OnBecome_S1B", "OnActivate_S1B", "OnDeactivate_S1B", "OnDeactivate_S1", "OnUnbecome_S1B", "OnUnbecome_S1", "OnBecome_S2", "OnBecome_S2A", "OnActivate_S2", "OnActivate_S2A" ); }
public void When_becoming_other_during_transition() { async Task <object> AttemptBecomeDuring <T>(Behavior b, string other, object message) { if (message is T) { await b.Become(other); } return(null); } Behavior behavior = null; behavior = new BehaviorTester(events) .State("A", x => AttemptBecomeDuring <Deactivate>(behavior, "C", x)) .State("B") .State("C") .Initial("A"); Assert.ThrowsAsync <InvalidOperationException>(async() => await behavior.Become("B")); behavior = new BehaviorTester(events) .State("A", x => AttemptBecomeDuring <Unbecome>(behavior, "C", x)) .State("B") .State("C") .Initial("A"); Assert.ThrowsAsync <InvalidOperationException>(async() => await behavior.Become("B")); behavior = new BehaviorTester(events) .State("A") .State("B", x => AttemptBecomeDuring <Activate>(behavior, "C", x)) .State("C") .Initial("A"); Assert.ThrowsAsync <InvalidOperationException>(async() => await behavior.Become("B")); behavior = new BehaviorTester(events) .State("A") .State("B", x => AttemptBecomeDuring <Become>(behavior, "C", x)) .State("C") .Initial("A"); Assert.ThrowsAsync <InvalidOperationException>(async() => await behavior.Become("B")); }
public async Task When_transitioning() { Behavior behavior = new BehaviorTester(events) .State("Initial") .State("A"); behavior.Initial("Initial"); await behavior.Become("A"); Assert.That(behavior.Current, Is.EqualTo("A")); var expected = new[] { "OnTransitioning_Initial_A", "OnDeactivate_Initial", "OnUnbecome_Initial", "OnBecome_A", "OnActivate_A", "OnTransitioned_A_Initial" }; AssertEvents(expected); }