Ejemplo n.º 1
0
        private void PacketReceive_EventCanceled <TPacket, TEvent>(TPacket packet, IPlayer sender)
            where TPacket : IPacket
            where TEvent : Event
        {
            Action <PacketReceiveEvent <TPacket> >?registeredHandler = null;

            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            Mock.Get(events)
            .Setup(em => em.RegisterHandler(It.IsAny <Action <PacketReceiveEvent <TPacket> > >(), log))
            .Callback <Action <PacketReceiveEvent <TPacket> >, ILogger>(
                (handler, log) => registeredHandler = handler);

            using var npcService = new OrionNpcService(events, log);

            var evt = new PacketReceiveEvent <TPacket>(packet, sender);

            Mock.Get(events)
            .Setup(em => em.Raise(It.IsAny <TEvent>(), log))
            .Callback <TEvent, ILogger>((evt, log) => evt.Cancel());

            Assert.NotNull(registeredHandler);
            registeredHandler !(evt);

            Assert.True(evt.IsCanceled);

            Mock.Get(events).VerifyAll();
        }
Ejemplo n.º 2
0
        public void NpcLoot_EventTriggered()
        {
            // Clear the item so that we know it's empty.
            Terraria.Main.item[0] = new Terraria.Item {
                whoAmI = 0
            };

            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            Mock.Get(events)
            .Setup(em => em.Raise(
                       It.Is <NpcLootEvent>(
                           evt => ((OrionNpc)evt.Npc).Wrapped == Terraria.Main.npc[0] && evt.Item.Id == ItemId.Gel &&
                           (evt.Item.StackSize >= 1 || evt.Item.StackSize <= 2) &&
                           evt.Item.Prefix == ItemPrefix.Random),
                       log));

            Terraria.Main.npc[0].SetDefaults((int)NpcId.BlueSlime);
            Terraria.Main.npc[0].life = 0;

            Terraria.Main.npc[0].checkDead();

            Assert.Equal(ItemId.Gel, (ItemId)Terraria.Main.item[0].type);

            Mock.Get(events).VerifyAll();
        }
Ejemplo n.º 3
0
        public void NpcLoot_EventCanceled()
        {
            // Clear the item so that we know it's empty.
            Terraria.Main.item[0] = new Terraria.Item {
                whoAmI = 0
            };

            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            Mock.Get(events)
            .Setup(em => em.Raise(It.IsAny <NpcLootEvent>(), log))
            .Callback <NpcLootEvent, ILogger>((evt, log) => evt.Cancel());

            Terraria.Main.npc[0].SetDefaults((int)NpcId.BlueSlime);
            Terraria.Main.npc[0].life = 0;

            Terraria.Main.npc[0].checkDead();

            Assert.NotEqual(ItemId.Gel, (ItemId)Terraria.Main.item[0].type);

            Mock.Get(events).VerifyAll();
        }
Ejemplo n.º 4
0
        public void Item_GetIndexOutOfRange_ThrowsIndexOutOfRangeException(int index)
        {
            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            Assert.Throws <IndexOutOfRangeException>(() => npcService[index]);
        }
Ejemplo n.º 5
0
        public void Count_Get()
        {
            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            Assert.Equal(Terraria.Main.maxNPCs, npcService.Count);
        }
Ejemplo n.º 6
0
        public void Item_Get()
        {
            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            var npc = npcService[1];

            Assert.Equal(1, npc.Index);
            Assert.Same(Terraria.Main.npc[1], ((OrionNpc)npc).Wrapped);
        }
Ejemplo n.º 7
0
        public void Item_GetMultipleTimes_ReturnsSameInstance()
        {
            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            var npc  = npcService[0];
            var npc2 = npcService[0];

            Assert.Same(npc, npc2);
        }
Ejemplo n.º 8
0
        public void GetEnumerator()
        {
            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            var npcs = npcService.ToList();

            for (var i = 0; i < npcs.Count; ++i)
            {
                Assert.Same(Terraria.Main.npc[i], ((OrionNpc)npcs[i]).Wrapped);
            }
        }
Ejemplo n.º 9
0
        public void NpcTick_EventCanceled()
        {
            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            Mock.Get(events)
            .Setup(em => em.Raise(It.IsAny <NpcTickEvent>(), log))
            .Callback <NpcTickEvent, ILogger>((evt, log) => evt.Cancel());

            Terraria.Main.npc[0].UpdateNPC(0);

            Mock.Get(events).VerifyAll();
        }
Ejemplo n.º 10
0
        public void Spawn()
        {
            // Clear the NPC so that we know it's empty.
            Terraria.Main.npc[0] = new Terraria.NPC {
                whoAmI = 0
            };

            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            var npc = npcService.Spawn(NpcId.BlueSlime, Vector2f.Zero);

            Assert.NotNull(npc);
            Assert.Equal(NpcId.BlueSlime, npc !.Id);
        }
Ejemplo n.º 11
0
        public void NpcSetDefaults_EventModified(NpcId oldId, NpcId newId)
        {
            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            Mock.Get(events)
            .Setup(em => em.Raise(It.IsAny <NpcDefaultsEvent>(), log))
            .Callback <NpcDefaultsEvent, ILogger>((evt, log) => evt.Id = newId);

            Terraria.Main.npc[0].SetDefaults((int)oldId);

            Assert.Equal(newId, (NpcId)Terraria.Main.npc[0].netID);

            Mock.Get(events).VerifyAll();
        }
Ejemplo n.º 12
0
        public void NpcSetDefaults_EventTriggered(NpcId id)
        {
            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            Mock.Get(events)
            .Setup(em => em.Raise(
                       It.Is <NpcDefaultsEvent>(evt => ((OrionNpc)evt.Npc).Wrapped == Terraria.Main.npc[0] && evt.Id == id),
                       log));

            Terraria.Main.npc[0].SetDefaults((int)id);

            Assert.Equal(id, (NpcId)Terraria.Main.npc[0].netID);

            Mock.Get(events).VerifyAll();
        }
Ejemplo n.º 13
0
        public void Spawn_ReturnsNull()
        {
            // Fill up all of the NPC slots so that the spawn fails.
            for (var i = 0; i < Terraria.Main.maxNPCs; ++i)
            {
                Terraria.Main.npc[i] = new Terraria.NPC {
                    whoAmI = i, active = true
                };
            }

            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            var npc = npcService.Spawn(NpcId.BlueSlime, Vector2f.Zero);

            Assert.Null(npc);
        }
Ejemplo n.º 14
0
        public void NpcKilled_EventTriggered()
        {
            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            Mock.Get(events)
            .Setup(em => em.Raise(
                       It.Is <NpcKilledEvent>(evt => ((OrionNpc)evt.Npc).Wrapped == Terraria.Main.npc[0]),
                       log));

            Terraria.Main.npc[0].SetDefaults((int)NpcId.BlueSlime);
            Terraria.Main.npc[0].life = 0;

            Terraria.Main.npc[0].checkDead();

            Mock.Get(events).VerifyAll();
        }
Ejemplo n.º 15
0
        public void NpcSpawn_EventTriggered()
        {
            INpc?evtNpc = null;

            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            Mock.Get(events)
            .Setup(em => em.Raise(It.IsAny <NpcSpawnEvent>(), log))
            .Callback <NpcSpawnEvent, ILogger>((evt, log) => evtNpc = evt.Npc);

            var npcIndex = Terraria.NPC.NewNPC(0, 0, (int)NpcId.BlueSlime);

            Assert.NotNull(evtNpc);
            Assert.Same(Terraria.Main.npc[npcIndex], ((OrionNpc)evtNpc !).Wrapped);

            Mock.Get(events).VerifyAll();
        }
Ejemplo n.º 16
0
        public void NpcTick_EventTriggered()
        {
            // Clear the NPC so that we know it's empty.
            Terraria.Main.npc[0] = new Terraria.NPC {
                whoAmI = 0
            };

            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            Mock.Get(events)
            .Setup(em => em.Raise(
                       It.Is <NpcTickEvent>(evt => ((OrionNpc)evt.Npc).Wrapped == Terraria.Main.npc[0]),
                       log));

            Terraria.Main.npc[0].UpdateNPC(0);

            Mock.Get(events).VerifyAll();
        }
Ejemplo n.º 17
0
        public void NpcSetDefaults_EventCanceled()
        {
            // Clear the NPC so that we know it's empty.
            Terraria.Main.npc[0] = new Terraria.NPC {
                whoAmI = 0
            };

            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            Mock.Get(events)
            .Setup(em => em.Raise(It.IsAny <NpcDefaultsEvent>(), log))
            .Callback <NpcDefaultsEvent, ILogger>((evt, log) => evt.Cancel());

            Terraria.Main.npc[0].SetDefaults((int)NpcId.BlueSlime);

            Assert.Equal(NpcId.None, (NpcId)Terraria.Main.npc[0].netID);

            Mock.Get(events).VerifyAll();
        }
Ejemplo n.º 18
0
        public void NpcSpawn_EventCanceled()
        {
            // Clear the NPC so that we know it's empty.
            Terraria.Main.npc[0] = new Terraria.NPC {
                whoAmI = 0
            };

            var events = Mock.Of <IEventManager>();
            var log    = Mock.Of <ILogger>();

            using var npcService = new OrionNpcService(events, log);

            Mock.Get(events)
            .Setup(em => em.Raise(It.IsAny <NpcSpawnEvent>(), log))
            .Callback <NpcSpawnEvent, ILogger>((evt, log) => evt.Cancel());

            var npcIndex = Terraria.NPC.NewNPC(0, 0, (int)NpcId.BlueSlime);

            Assert.Equal(npcService.Count, npcIndex);
            Assert.False(Terraria.Main.npc[0].active);

            Mock.Get(events).VerifyAll();
        }