コード例 #1
0
        public void should_dispose_entity_subscriptions_when_removed_from_group()
        {
            var fakeEntity1  = Substitute.For <IEntity>();
            var fakeEntity2  = Substitute.For <IEntity>();
            var fakeEntities = new List <IEntity> {
                fakeEntity1, fakeEntity2
            };

            var id1 = 1;
            var id2 = 2;

            fakeEntity1.Id.Returns(id1);
            fakeEntity2.Id.Returns(id2);

            var mockObservableGroup = Substitute.For <IObservableGroup>();

            mockObservableGroup.GetEnumerator()
            .Returns(fakeEntities.GetEnumerator());
            mockObservableGroup.OnEntityAdded.Returns(new Subject <IEntity>());

            var removedSubject = new Subject <IEntity>();

            mockObservableGroup.OnEntityRemoved.Returns(removedSubject);

            var mockCollectionManager = Substitute.For <IEntityCollectionManager>();

            var fakeGroup = new Group();

            mockCollectionManager.GetObservableGroup(Arg.Is(fakeGroup)).Returns(mockObservableGroup);

            var firstEntitySubject  = new Subject <int>();
            var secondEntitySubject = new Subject <int>();
            var mockSystem          = Substitute.For <IReactToDataSystem <int> >();

            mockSystem.Group.Returns(fakeGroup);
            mockSystem.ReactToData(Arg.Is(fakeEntity1)).Returns(firstEntitySubject);
            mockSystem.ReactToData(Arg.Is(fakeEntity2)).Returns(secondEntitySubject);

            var systemHandler = new ReactToDataSystemHandler(mockCollectionManager);

            systemHandler.SetupSystem(mockSystem);

            Assert.Equal(1, systemHandler._entitySubscriptions.Count);
            Assert.Equal(2, systemHandler._entitySubscriptions[mockSystem].Count);
            Assert.True(systemHandler._entitySubscriptions[mockSystem].Keys.Contains(id1));
            Assert.True(systemHandler._entitySubscriptions[mockSystem].Keys.Contains(id2));
            Assert.All(systemHandler._entitySubscriptions[mockSystem].Values, Assert.NotNull);

            removedSubject.OnNext(fakeEntity1);

            Assert.Equal(1, systemHandler._entitySubscriptions.Count);
            Assert.Equal(1, systemHandler._entitySubscriptions[mockSystem].Count);
            Assert.True(systemHandler._entitySubscriptions[mockSystem].Keys.Contains(id2));
            Assert.All(systemHandler._entitySubscriptions[mockSystem].Values, Assert.NotNull);
        }
コード例 #2
0
        public void should_only_execute_system_when_predicate_met()
        {
            var fakeEntity1  = Substitute.For <IEntity>();
            var fakeEntity2  = Substitute.For <IEntity>();
            var fakeEntities = new List <IEntity> {
                fakeEntity1, fakeEntity2
            };

            var id1 = 1;
            var id2 = 2;

            fakeEntity1.Id.Returns(id1);
            fakeEntity2.Id.Returns(id2);

            var mockObservableGroup = Substitute.For <IObservableGroup>();

            mockObservableGroup.GetEnumerator().Returns(fakeEntities.GetEnumerator());
            mockObservableGroup.OnEntityAdded.Returns(new Subject <IEntity>());
            mockObservableGroup.OnEntityRemoved.Returns(new Subject <IEntity>());

            var mockCollectionManager = Substitute.For <IEntityCollectionManager>();

            var fakeGroup = new Group(x => x.Id == id1);

            mockCollectionManager.GetObservableGroup(Arg.Is(fakeGroup)).Returns(mockObservableGroup);

            var firstEntitySubject  = new Subject <int>();
            var secondEntitySubject = new Subject <int>();
            var mockSystem          = Substitute.For <IReactToDataSystem <int> >();

            mockSystem.Group.Returns(fakeGroup);
            mockSystem.ReactToData(Arg.Is(fakeEntity1)).Returns(firstEntitySubject);
            mockSystem.ReactToData(Arg.Is(fakeEntity2)).Returns(secondEntitySubject);

            var systemHandler = new ReactToDataSystemHandler(mockCollectionManager);

            systemHandler.SetupSystem(mockSystem);

            firstEntitySubject.OnNext(1);
            secondEntitySubject.OnNext(2);

            mockSystem.Received(1).Process(Arg.Is(fakeEntity1), Arg.Is(1));
            mockSystem.Received(0).Process(Arg.Is(fakeEntity2), Arg.Is(2));

            Assert.Equal(1, systemHandler._systemSubscriptions.Count);
            Assert.NotNull(systemHandler._systemSubscriptions[mockSystem]);

            Assert.Equal(1, systemHandler._entitySubscriptions.Count);
            Assert.Equal(2, systemHandler._entitySubscriptions[mockSystem].Count);
            Assert.True(systemHandler._entitySubscriptions[mockSystem].Keys.Contains(id1));
            Assert.True(systemHandler._entitySubscriptions[mockSystem].Keys.Contains(id2));
            Assert.All(systemHandler._entitySubscriptions[mockSystem].Values, Assert.NotNull);
        }
コード例 #3
0
        public void should_execute_system_without_predicate()
        {
            var fakeEntity1  = Substitute.For <IEntity>();
            var fakeEntity2  = Substitute.For <IEntity>();
            var fakeEntities = new[] { fakeEntity1, fakeEntity2 };

            var guid1 = Guid.NewGuid();
            var guid2 = Guid.NewGuid();

            fakeEntity1.Id.Returns(guid1);
            fakeEntity2.Id.Returns(guid2);

            var mockObservableGroup = Substitute.For <IObservableGroup>();

            mockObservableGroup.Entities.Returns(fakeEntities);
            mockObservableGroup.OnEntityAdded.Returns(new Subject <IEntity>());
            mockObservableGroup.OnEntityRemoved.Returns(new Subject <IEntity>());

            var mockCollectionManager = Substitute.For <IEntityCollectionManager>();

            var fakeGroup = new Group();

            mockCollectionManager.CreateObservableGroup(Arg.Is(fakeGroup)).Returns(mockObservableGroup);

            var firstEntitySubject  = new Subject <int>();
            var secondEntitySubject = new Subject <int>();
            var mockSystem          = Substitute.For <IReactToDataSystem <int> >();

            mockSystem.TargetGroup.Returns(fakeGroup);
            mockSystem.ReactToData(Arg.Is(fakeEntity1)).Returns(firstEntitySubject);
            mockSystem.ReactToData(Arg.Is(fakeEntity2)).Returns(secondEntitySubject);

            var systemHandler = new ReactToDataSystemHandler(mockCollectionManager);

            systemHandler.SetupSystem(mockSystem);

            firstEntitySubject.OnNext(1);
            secondEntitySubject.OnNext(2);

            mockSystem.Received(1).Execute(Arg.Is(fakeEntity1), Arg.Is(1));
            mockSystem.Received(1).Execute(Arg.Is(fakeEntity2), Arg.Is(2));

            Assert.Equal(1, systemHandler._systemSubscriptions.Count);
            Assert.NotNull(systemHandler._systemSubscriptions[mockSystem]);

            Assert.Equal(1, systemHandler._entitySubscriptions.Count);
            Assert.Equal(2, systemHandler._entitySubscriptions[mockSystem].Count);
            Assert.True(systemHandler._entitySubscriptions[mockSystem].Keys.Contains(guid1));
            Assert.True(systemHandler._entitySubscriptions[mockSystem].Keys.Contains(guid2));
            Assert.All(systemHandler._entitySubscriptions[mockSystem].Values, Assert.NotNull);
        }
コード例 #4
0
        public void should_execute_system_when_entity_added_to_group()
        {
            var fakeEntity1 = Substitute.For <IEntity>();
            var fakeEntity2 = Substitute.For <IEntity>();

            var id1 = 1;
            var id2 = 2;

            fakeEntity1.Id.Returns(id1);
            fakeEntity2.Id.Returns(id2);

            var mockObservableGroup = Substitute.For <IObservableGroup>();

            mockObservableGroup.GetEnumerator().Returns(new List <IEntity>().GetEnumerator());
            mockObservableGroup.OnEntityRemoved.Returns(new Subject <IEntity>());

            var addedSubject = new Subject <IEntity>();

            mockObservableGroup.OnEntityAdded.Returns(addedSubject);

            var observableGroupManager = Substitute.For <IObservableGroupManager>();

            var fakeGroup = new Group();

            observableGroupManager.GetObservableGroup(Arg.Is(fakeGroup), Arg.Any <int[]>()).Returns(mockObservableGroup);

            var firstEntitySubject  = new Subject <int>();
            var secondEntitySubject = new Subject <int>();
            var mockSystem          = Substitute.For <IReactToDataSystem <int> >();

            mockSystem.Group.Returns(fakeGroup);
            mockSystem.ReactToData(Arg.Is(fakeEntity1)).Returns(firstEntitySubject);
            mockSystem.ReactToData(Arg.Is(fakeEntity2)).Returns(secondEntitySubject);

            var systemHandler = new ReactToDataSystemHandler(observableGroupManager);

            systemHandler.SetupSystem(mockSystem);

            Assert.Equal(1, systemHandler.EntitySubscriptions.Count);
            Assert.Equal(0, systemHandler.EntitySubscriptions[mockSystem].Count);

            mockSystem.Received(0).ReactToData(Arg.Any <IEntity>());
            addedSubject.OnNext(fakeEntity1);
            addedSubject.OnNext(fakeEntity2);

            mockSystem.Received(1).ReactToData(Arg.Is(fakeEntity1));
            mockSystem.Received(1).ReactToData(Arg.Is(fakeEntity2));

            firstEntitySubject.OnNext(1);
            secondEntitySubject.OnNext(2);

            mockSystem.Received(1).Process(Arg.Is(fakeEntity1), Arg.Is(1));
            mockSystem.Received(1).Process(Arg.Is(fakeEntity2), Arg.Is(2));

            Assert.Equal(1, systemHandler.SystemSubscriptions.Count);
            Assert.NotNull(systemHandler.SystemSubscriptions[mockSystem]);

            Assert.Equal(1, systemHandler.EntitySubscriptions.Count);
            Assert.Equal(2, systemHandler.EntitySubscriptions[mockSystem].Count);
            Assert.True(systemHandler.EntitySubscriptions[mockSystem].Keys.Contains(id1));
            Assert.True(systemHandler.EntitySubscriptions[mockSystem].Keys.Contains(id2));
            Assert.All(systemHandler.EntitySubscriptions[mockSystem].Values, Assert.NotNull);
        }