public void ProcessSimulationForStrategies(SingleProcessSimulation simulation, List <IStrategyFactory> strategyFactories)
 {
     foreach (var strategyFactory in strategyFactories)
     {
         simulation.PageList  = _pageListPrototype.Clone();
         simulation.FrameList = _frameListPrototype.Clone();
         simulation.Process   = _processFactory.Create(strategyFactory, _pageReferencesListPrototype.Clone());
         simulation.ProcessAll();
     }
 }
        public void TestSingleProcessSimulation()
        {
            var pages = new List <IPage>
            {
                Mock.Of <IPage>(p => p.Id == Guid.NewGuid()),
                Mock.Of <IPage>(p => p.Id == Guid.NewGuid()),
                Mock.Of <IPage>(p => p.Id == Guid.NewGuid()),
                Mock.Of <IPage>(p => p.Id == Guid.NewGuid()),
            };

            var frames = new List <IFrame>()
            {
                Mock.Of <IFrame>(f => f.Id == Guid.NewGuid()),
                Mock.Of <IFrame>(f => f.Id == Guid.NewGuid()),
            };

            var pageReferences = new List <IPageReference>()
            {
                Mock.Of <IPageReference>(r => r.PageId == pages[0].Id),
                Mock.Of <IPageReference>(r => r.PageId == pages[1].Id),
                Mock.Of <IPageReference>(r => r.PageId == pages[1].Id),
                Mock.Of <IPageReference>(r => r.PageId == pages[2].Id),
                Mock.Of <IPageReference>(r => r.PageId == pages[1].Id),
                Mock.Of <IPageReference>(r => r.PageId == pages[3].Id),
                Mock.Of <IPageReference>(r => r.PageId == pages[1].Id),
            };

            var nextReplacedFrameIndex = 0;

            var strategy = Mock.Of <IOrganizingStrategy>();

            Mock.Get(strategy).Setup(m => m.CreateReplacingVisitor(It.IsAny <IPageReference>()))
            .Returns((IPageReference pageReference) =>
            {
                var visitor = Mock.Of <IReplacingVisitor>(m =>
                                                          m.PageId == pageReference.PageId &&
                                                          m.FrameId == frames[nextReplacedFrameIndex].Id);
                nextReplacedFrameIndex = 1 - nextReplacedFrameIndex;
                return(visitor);
            });

            var pagesReferencesIterator = pageReferences.GetEnumerator();

            pagesReferencesIterator.MoveNext();

            var process = Mock.Of <IProcess>();

            Mock.Get(process).SetupAllProperties();
            Mock.Get(process).Setup(m => m.HasNextPageToLoad).Returns(
                () => pagesReferencesIterator.Current != null);
            Mock.Get(process).Setup(m => m.LoadNextPage()).Callback(() =>
            {
                var pageReference = pagesReferencesIterator.Current;
                var replacing     = strategy.CreateReplacingVisitor(pageReference);
                if (replacing == null)
                {
                    throw new NullReferenceException("no more pages to load");
                }
                process.VirtualMemory.Accept(replacing);
                process.PhysicalMemory.Accept(replacing);
                pagesReferencesIterator.MoveNext();
            });

            void ReplacePages(IReplacingVisitor visitor) => frames.First(f => f.Id.Equals(visitor.FrameId)).Page = visitor.Page;

            var virtualMemory = Mock.Of <IVirtualMemory>();

            Mock.Get(virtualMemory).Setup(m => m.Accept(It.IsAny <IReplacingVisitor>())).Callback((IReplacingVisitor visitor) => visitor.Page = pages.First(p => p.Id.Equals(visitor.PageId)));

            var virtualMemoryFactory = Mock.Of <IVirtualMemoryFactory>();

            Mock.Get(virtualMemoryFactory).Setup(m => m.Create(It.IsAny <List <IPage> >())).Returns(virtualMemory);

            var physicalMemory = Mock.Of <IPhysicalMemory>();

            Mock.Get(physicalMemory).Setup(m => m.Accept(It.IsAny <IReplacingVisitor>())).Callback((IReplacingVisitor visitor) => ReplacePages(visitor));
            Mock.Get(physicalMemory).Setup(m => m.Contains(It.IsAny <Guid>())).Returns <Guid>((p) => frames.Where(f => f.Page != null).Count(f => f.Page.Id.Equals(p)) == 1);

            var physicalMemoryFactory = Mock.Of <IPhysicalMemoryFactory>();

            Mock.Get(physicalMemoryFactory).Setup(m => m.Create(It.IsAny <List <IFrame> >())).Returns(physicalMemory);

            var simulation = new SingleProcessSimulation(physicalMemoryFactory, virtualMemoryFactory)
            {
                PageReferenceList = pageReferences,
                Process           = process
            };

            simulation.ProcessAll();

            Mock.Get(physicalMemory).Verify(m => m.Accept(It.IsAny <IReplacingVisitor>()), Times.Exactly(7));
        }