using System; using System.Linq; using Amphora; using UnitTestSharp; namespace Ariadne.UnitTests { public class VirtualPassageTests : TestFixture { public static ScriptType Reloaded(ScriptType script) where ScriptType : Arc { return LoadedArc.From(ArcRecord.Of(script)); } public class CallTests : TestFixture { private ArcTests.Gates gates; private TickContext tick; public override void TestSetup() { gates = new ArcTests.Gates(); tick = TestTicks.Fresh(); } public void PassageNobodyOverrides_RunsTheBaseVersion() { var script = new PlainGreeter(); script.Tick(gates, tick); gates.Open.Add("hello"); script.Tick(gates, tick); CheckEqual("hello;done;", script.Log); CheckTrue(script.Finished); } public void OverrideCalledFromTheBase_RunsInPlaceOfTheBaseVersion() { var script = new LoudGreeter(); script.Tick(gates, tick); gates.Open.Add("hello"); script.Tick(gates, tick); CheckEqual("", script.Log); gates.Open.Add("shout"); script.Tick(gates, tick); CheckEqual("LOUD3;done;", script.Log); CheckTrue(script.Finished); } public void OverrideOfAnOverride_RunsTheMostDerived() { var script = new LoudestGreeter(); gates.Open.Add("hello"); gates.Open.Add("shout"); script.Tick(gates, tick); CheckEqual("", script.Log); gates.Open.Add("bellow"); script.Tick(gates, tick); CheckEqual("LOUDEST max;done;", script.Log); } public void OverrideThatNeverPauses_FinishesInTheFirstTick() { var script = new SilentGreeter(); script.Tick(gates, tick); CheckEqual("silence;done;", script.Log); CheckTrue(script.Finished); } public void ValueReturnedByAnOverride_ReachesTheCaller() { var script = new Haggler(); script.Tick(gates, tick); gates.Open.Add("haggle2"); script.Tick(gates, tick); gates.Open.Add("haggle3"); script.Tick(gates, tick); CheckEqual(2030, script.Total); } public void ValueReturnedByTheBaseVersion_ReachesTheCaller() { var plain = new PlainAppraiser(); plain.Tick(gates, tick); CheckEqual(203, plain.Total); } public void AbstractPassageOverridden_RunsTheOverrideFromTheBasesRun() { var script = new Stubborn(); script.Tick(gates, tick); gates.Open.Add("deal"); script.Tick(gates, tick); CheckEqual("asked 14;closed;", script.Log); CheckTrue(script.Finished); } public void BaseCallFromAnOverride_RunsTheBaseVersionBetweenTheOverridesPauses() { var script = new Escalator(); script.Tick(gates, tick); gates.Open.Add("before"); script.Tick(gates, tick); CheckEqual("", script.Log); gates.Open.Add("hello"); script.Tick(gates, tick); CheckEqual("hello;after 5;done;", script.Log); CheckTrue(script.Finished); } public void RestartedWhilePausedInsideTheOverride_StartsItFromTheTopWithoutFaulting() { var script = new LoudGreeter(); ExceptionSandbox sandbox = TestTicks.NewSandbox(); script.Tick(gates, TestTicks.With(sandbox)); script.Restart(); script.Tick(gates, TestTicks.With(sandbox)); gates.Open.Add("shout"); script.Tick(gates, TestTicks.With(sandbox)); CheckEqual(0, sandbox.Entries.Count); CheckEqual("LOUD3;done;", script.Log); } public void RestartedWhilePausedInsideABaseCall_StartsItFromTheTopWithoutFaulting() { var script = new Escalator(); ExceptionSandbox sandbox = TestTicks.NewSandbox(); script.Tick(gates, TestTicks.With(sandbox)); gates.Open.Add("before"); script.Tick(gates, TestTicks.With(sandbox)); script.Restart(); script.Tick(gates, TestTicks.With(sandbox)); gates.Open.Add("hello"); script.Tick(gates, TestTicks.With(sandbox)); CheckEqual(0, sandbox.Entries.Count); CheckEqual("hello;after 5;done;", script.Log); } public void RunningTwice_StartsTheOverrideFromTheTopBothTimes() { var script = new LoudGreeter(); script.Tick(gates, tick); gates.Open.Add("shout"); script.Tick(gates, tick); CheckEqual("LOUD3;done;", script.Log); script.Log = ""; script.Restart(); gates.Open.Remove("shout"); script.Tick(gates, tick); gates.Open.Add("shout"); script.Tick(gates, tick); CheckEqual("LOUD3;done;", script.Log); } } public class SaveTests : TestFixture { private ArcTests.Gates gates; private TickContext tick; public override void TestSetup() { gates = new ArcTests.Gates(); tick = TestTicks.Fresh(); } public void LocalOfAnOverride_IsSavedUnderTheOverridesPause() { var script = new LoudGreeter(); script.Tick(gates, tick); ArcRecord record = ArcRecord.Of(script); CheckEqual(new[] { "LoudGreeter.Greet.volume" }, record.AllLocals().Keys.OrderBy(key => key, StringComparer.Ordinal).ToArray()); CheckEqual(3, record.AllLocals()["LoudGreeter.Greet.volume"]); } public void OverrideReloadedWhilePaused_ResumesWithItsLocal() { var script = new LoudGreeter(); script.Tick(gates, tick); script = Reloaded(script); gates.Open.Add("shout"); script.Tick(gates, tick); CheckEqual("LOUD3;done;", script.Log); CheckTrue(script.Finished); } public void OverrideOfAnOverrideReloadedWhilePaused_ResumesWithItsLocal() { var script = new LoudestGreeter(); script.Tick(gates, tick); script = Reloaded(script); gates.Open.Add("bellow"); script.Tick(gates, tick); CheckEqual("LOUDEST max;done;", script.Log); } public void BaseVersionReloadedWhilePaused_ResumesWithItsLocal() { var script = new PlainGreeter(); script.Tick(gates, tick); script = Reloaded(script); gates.Open.Add("hello"); script.Tick(gates, tick); CheckEqual("hello;done;", script.Log); } public void OverrideReloadedWhilePausedInsideItsBaseCall_ResumesBothLocals() { var script = new Escalator(); script.Tick(gates, tick); gates.Open.Add("before"); script.Tick(gates, tick); script = Reloaded(script); gates.Open.Add("hello"); script.Tick(gates, tick); CheckEqual("hello;after 5;done;", script.Log); } public void OverrideReloadedWhilePausedBeforeItsBaseCall_ResumesItsLocal() { var script = new Escalator(); script.Tick(gates, tick); script = Reloaded(script); gates.Open.Add("before"); gates.Open.Add("hello"); script.Tick(gates, tick); CheckEqual("hello;after 5;done;", script.Log); } public void OverrideOfAnAbstractPassageReloadedWhilePaused_ResumesWithItsLocal() { var script = new Stubborn(); script.Tick(gates, tick); script = Reloaded(script); gates.Open.Add("deal"); script.Tick(gates, tick); CheckEqual("asked 14;closed;", script.Log); } public void ValueReturningOverrideReloadedWhilePaused_ResumesAndHandsBackItsValue() { var script = new Haggler(); script.Tick(gates, tick); script = Reloaded(script); gates.Open.Add("haggle2"); script.Tick(gates, tick); script = Reloaded(script); gates.Open.Add("haggle3"); script.Tick(gates, tick); CheckEqual(2030, script.Total); } } public class RecursionTests : TestFixture { public void RecursivePassageCallingAVirtualHook_RunsEveryLevelThroughTheOverride() { var gates = new ArcTests.Gates(); var script = new PausingSurveyor(); script.Tick(gates, TestTicks.Fresh()); foreach (string gate in new[] { "look0", "look1", "look2" }) { gates.Open.Add(gate); script.Tick(gates, TestTicks.Fresh()); } CheckEqual("0;10;20;", script.Log); CheckTrue(script.Finished); } public void RecursivePassageCallingAVirtualHook_ReloadedAtEachPause_RunsEveryLevel() { var gates = new ArcTests.Gates(); var script = new PausingSurveyor(); script.Tick(gates, TestTicks.Fresh()); foreach (string gate in new[] { "look0", "look1", "look2" }) { script = Reloaded(script); gates.Open.Add(gate); script.Tick(gates, TestTicks.Fresh()); } CheckEqual("0;10;20;", script.Log); CheckTrue(script.Finished); } public void VirtualPassageStartedAgainThroughAnOverride_FaultsTheArc() { var script = new LoopingWalker(); ExceptionSandbox sandbox = TestTicks.NewSandbox(); script.Tick(new ArcTests.Gates(), TestTicks.With(sandbox)); CheckEqual(1, sandbox.Entries.Count); CheckTrue(sandbox.Entries[0].Exception is InvalidOperationException); CheckTrue(sandbox.Entries[0].Exception.Message.Contains("Visit")); CheckEqual("visit;", script.Log); } public void VirtualPassageThatRanToTheEnd_MayRunAgain() { var script = new PlainWalker(); var gates = new ArcTests.Gates(); TickContext tick = TestTicks.Fresh(); script.Tick(gates, tick); script.Restart(); script.Tick(gates, tick); CheckEqual("visit;visit;", script.Log); } } } }