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 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_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_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_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); }