using System; using Amphora; using UnitTestSharp; namespace Ariadne.UnitTests { public class ForeignSpillTests : TestFixture { public static TickContext At(ExceptionSandbox sandbox, double gameSeconds) { return TestTicks.At(sandbox, TimeSpan.FromMilliseconds(16), TimeSpan.FromSeconds(gameSeconds)); } public class ExpressionTests : TestFixture { private ArcTests.Gates gates; private ExceptionSandbox sandbox; public override void TestSetup() { gates = new ArcTests.Gates(); sandbox = TestTicks.NewSandbox(); } public void CallInABiggerStatement_PausesInsideTheOtherArc() { var script = new ForeignExpressionScript(); script.Tick(gates, At(sandbox, 0)); CheckEqual(new[] { "first", "wait" }, script.Cursor); CheckEqual(0, script.Log.Count); } public void ValueHandedBack_JoinsTheRestOfTheStatementWithALocalReadAfterTheCall() { var script = new ForeignExpressionScript(); script.Tick(gates, At(sandbox, 0)); gates.Open.Add("go"); script.Tick(gates, At(sandbox, 1)); CheckEqual(new[] { "total 9" }, script.Log); CheckEqual(5, script.Worker.Total); CheckTrue(script.Finished); } public void TwoCallsOnTwoArcs_AreAddedInOrder() { var script = new TwoForeignCallsScript(); script.Tick(gates, At(sandbox, 0)); gates.Open.Add("go"); script.Tick(gates, At(sandbox, 1)); CheckEqual(new[] { "sum 12" }, script.Log); CheckEqual(5, script.First.Total); CheckEqual(7, script.Second.Total); } public void SecondCallOfTwo_IsPausedInWhileTheFirstIsDone() { var script = new GatedSumScript(); script.Tick(gates, At(sandbox, 0)); CheckEqual(new[] { "sum", "wait" }, script.Cursor); gates.Open.Add("a"); script.Tick(gates, At(sandbox, 1)); CheckEqual(new[] { "sum#1", "wait" }, script.Cursor); CheckEqual(0, script.Log.Count); gates.Open.Add("b"); script.Tick(gates, At(sandbox, 2)); CheckEqual(new[] { "sum 12" }, script.Log); } } public class WaitTests : TestFixture { private ArcTests.Gates gates; private ExceptionSandbox sandbox; public override void TestSetup() { gates = new ArcTests.Gates(); sandbox = TestTicks.NewSandbox(); } public void CallOnAnotherArcInTheSpan_PausesUntilItIsDone() { var script = new ForeignAskWaitScript(); script.Tick(gates, At(sandbox, 10)); script.Tick(gates, At(sandbox, 11)); CheckEqual(new[] { "first", "ask" }, script.Cursor); CheckEqual(0, script.Log.Count); } public void DeadlineCountsFromWhenTheCallFinishes() { var script = new ForeignAskWaitScript(); script.Tick(gates, At(sandbox, 10)); gates.Open.Add("answer"); script.Tick(gates, At(sandbox, 12)); CheckEqual(new[] { "first#1" }, script.Cursor); script.Tick(gates, At(sandbox, 13.9)); CheckEqual(0, script.Log.Count); script.Tick(gates, At(sandbox, 14)); CheckEqual(new[] { "done" }, script.Log); CheckTrue(script.Finished); } } } }