fix(core): Converts more packets and switches to Moq (#415)

- [X] Switches testing to moq for mocking
- [X] Converts race changer packets
- [X] Renames some more folders and misc cleanup
This commit is contained in:
Kamron Batman 2021-01-16 22:09:05 -08:00 committed by GitHub
parent fbafd7210d
commit b4b1b0b122
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
144 changed files with 197 additions and 181 deletions

View file

@ -1,95 +0,0 @@
using System;
using System.Linq;
using Server.Accounting;
namespace Server.Tests.Network
{
public class MockAccount : IAccount, IComparable<MockAccount>
{
private readonly Mobile[] m_Mobiles;
private string m_Password;
public MockAccount(Mobile[] mobiles)
{
m_Mobiles = mobiles;
foreach (var mobile in mobiles)
{
if (mobile != null)
{
mobile.Account = this;
}
}
Length = mobiles.Length;
Count = mobiles.Count(t => t != null);
Limit = mobiles.Length;
}
public int TotalGold { get; private set; }
public int TotalPlat { get; private set; }
public bool DepositGold(int amount)
{
TotalGold += amount;
return true;
}
public bool DepositPlat(int amount)
{
TotalPlat += amount;
return true;
}
public bool WithdrawGold(int amount)
{
if (TotalGold - amount < 0)
{
return false;
}
TotalGold -= amount;
return true;
}
public bool WithdrawPlat(int amount)
{
if (TotalPlat - amount < 0)
{
return false;
}
TotalPlat -= amount;
return true;
}
public long GetTotalGold() => TotalGold + TotalPlat * 100;
public int CompareTo(IAccount other) => string.CompareOrdinal(Username, other?.Username);
public string Username { get; set; }
public string Email { get; set; }
public AccessLevel AccessLevel { get; set; }
public int Length { get; }
public int Limit { get; }
public int Count { get; }
public Mobile this[int index]
{
get => m_Mobiles[index];
set => m_Mobiles[index] = value;
}
public void Delete()
{
}
public void SetPassword(string password)
{
m_Password = password;
}
public bool CheckPassword(string password) => m_Password == password;
public int CompareTo(MockAccount other) => string.CompareOrdinal(Username, other?.Username);
}
}

View file

@ -1,22 +0,0 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Server.Network;
namespace Server.Tests.Network
{
public class MockSocket : ISocket
{
public EndPoint LocalEndPoint { get; set; }
public EndPoint RemoteEndPoint { get; set; }
public Task<int> SendAsync(IList<ArraySegment<byte>> buffers, SocketFlags flags) => Task.Run(() => 0);
public Task<int> ReceiveAsync(IList<ArraySegment<byte>> buffers, SocketFlags flags) => Task.Run(() => 0);
public void Shutdown(SocketShutdown how)
{
}
}
}

View file

@ -4,6 +4,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.0-preview-20210106-01" />
<PackageReference Include="Moq" Version="4.16.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Zlib.Bindings" Version="1.4.0" />

View file

@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Net;
using Moq;
using Server.Accounting;
using Server.Network;
using Xunit;
@ -7,6 +10,28 @@ namespace Server.Tests.Network
{
public class AccountPacketTests : IClassFixture<ServerFixture>
{
public readonly Dictionary<int, Mobile> dictionary = new();
public Mock<IAccount> accountMock;
public AccountPacketTests()
{
accountMock = new Mock<IAccount>();
accountMock
.Setup(sb => sb[It.IsAny<int>()])
.Returns((int key) => dictionary[key]);
accountMock
.SetupSet(sb => sb[It.IsAny<int>()] = It.IsAny<Mobile>())
.Callback(
(int key, Mobile m) =>
{
dictionary[key] = m;
if (m != null)
{
m.Account = accountMock.Object;
}
});
}
[Fact]
public void TestChangeCharacter()
{
@ -18,7 +43,12 @@ namespace Server.Tests.Network
secondMobile.DefaultMobileInit();
secondMobile.RawName = null;
var account = new MockAccount(new[] { firstMobile, null, secondMobile });
var account = accountMock.Object;
account[0] = firstMobile;
account[1] = null;
account[2] = secondMobile;
// var account = new MockAccount(new[] { firstMobile, null, secondMobile });
var expected = new ChangeCharacter(account).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
@ -72,7 +102,12 @@ namespace Server.Tests.Network
firstMobile.DefaultMobileInit();
firstMobile.Name = "Test Mobile";
var account = new MockAccount(new[] { firstMobile, null, null, null, null });
var account = accountMock.Object;
account[0] = firstMobile;
account[1] = null;
account[2] = null;
account[3] = null;
account[4] = null;
using var ns = PacketTestUtilities.CreateTestNetState();
ns.Account = account;
@ -125,12 +160,17 @@ namespace Server.Tests.Network
firstMobile.DefaultMobileInit();
firstMobile.RawName = "Test Mobile";
var acct = new MockAccount(new[] { null, firstMobile, null, null, null });
var account = accountMock.Object;
account[0] = null;
account[1] = firstMobile;
account[2] = null;
account[3] = null;
account[4] = null;
var expected = new CharacterListUpdate(acct).Compile();
var expected = new CharacterListUpdate(account).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCharacterListUpdate(acct);
ns.SendCharacterListUpdate(account);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
@ -143,17 +183,23 @@ namespace Server.Tests.Network
firstMobile.DefaultMobileInit();
firstMobile.Name = "Test Mobile";
var acct = new MockAccount(new[] { null, firstMobile, null, null, null });
var account = accountMock.Object;
account[0] = null;
account[1] = firstMobile;
account[2] = null;
account[3] = null;
account[4] = null;
var info = new[]
{
new CityInfo("Test City", "Test Building", 50, 100, 10, -10)
};
var expected = new CharacterList(acct, info).Compile();
var expected = new CharacterList(account, info).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.CityInfo = info;
ns.Account = acct;
ns.Account = account;
ns.ProtocolChanges = ProtocolChanges.Version70130;
ns.SendCharacterList();
@ -169,17 +215,23 @@ namespace Server.Tests.Network
firstMobile.DefaultMobileInit();
firstMobile.Name = "Test Mobile";
var acct = new MockAccount(new[] { null, firstMobile, null, null, null });
var account = accountMock.Object;
account[0] = null;
account[1] = firstMobile;
account[2] = null;
account[3] = null;
account[4] = null;
var info = new[]
{
new CityInfo("Test City", "Test Building", 50, 100, 10, -10)
};
var expected = new CharacterListOld(acct, info).Compile();
var expected = new CharacterListOld(account, info).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.CityInfo = info;
ns.Account = acct;
ns.Account = account;
ns.SendCharacterList();

View file

@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using Moq;
using Server.Network;
namespace Server.Tests.Network
@ -8,7 +11,18 @@ namespace Server.Tests.Network
public static Span<byte> Compile(this Packet p) =>
p.Compile(false, out var length).AsSpan(0, length);
public static NetState CreateTestNetState() =>
new(new MockSocket());
public static NetState CreateTestNetState()
{
var socket = new Mock<ISocket>();
socket
.Setup(s => s.SendAsync(It.IsAny<IList<ArraySegment<byte>>>(), SocketFlags.None))
.ReturnsAsync(() => 0);
socket
.Setup(s => s.ReceiveAsync(It.IsAny<IList<ArraySegment<byte>>>(), SocketFlags.None))
.ReturnsAsync(() => 0);
return new NetState(socket.Object);
}
}
}

View file

@ -0,0 +1,43 @@
using System;
using Moq;
using Server.Tests;
using Server.Tests.Network;
using Xunit;
namespace Server.Engines.MLQuests
{
public class MLQuestPacketTests
{
[Theory]
[InlineData(true, 1)]
[InlineData(false, 2)]
public void TestRaceChanger(bool female, int raceId)
{
var raceMock = new Mock<Race>(
raceId, 0, "Test Race", "Test Races", 0x1, 0x2, 0x3, 0x4, Expansion.None
);
var race = raceMock.Object;
var expected = new RaceChanger(female, race).Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendRaceChanger(female, race);
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
[Fact]
public void TestCloseRaceChanger()
{
var expected = new CloseRaceChanger().Compile();
using var ns = PacketTestUtilities.CreateTestNetState();
ns.SendCloseRaceChanger();
var result = ns.SendPipe.Reader.TryRead();
AssertThat.Equal(result.Buffer[0].AsSpan(0), expected);
}
}
}

View file

@ -0,0 +1,28 @@
using Server.Network;
namespace Server.Engines.MLQuests
{
public sealed class RaceChanger : Packet
{
public RaceChanger(bool female, Race targetRace) : base(0xBF)
{
EnsureCapacity(7);
Stream.Write((short)0x2A);
Stream.Write((byte)(female ? 1 : 0));
Stream.Write((byte)(targetRace.RaceID + 1));
}
}
public sealed class CloseRaceChanger : Packet
{
public CloseRaceChanger() : base(0xBF)
{
EnsureCapacity(7);
Stream.Write((short)0x2A);
Stream.Write((byte)0);
Stream.Write((byte)0xFF);
}
}
}

View file

@ -4,6 +4,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.0-preview-20210106-01" />
<PackageReference Include="Moq" Version="4.16.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<ProjectReference Include="..\Server\Server.csproj" />

View file

@ -97,7 +97,7 @@ namespace Server.Engines.MLQuests.Gumps
CloseCurrent(ns);
m_Pending[ns] = new RaceChangeState(owner, ns, targetRace);
ns.Send(new RaceChanger(from.Female, targetRace));
ns.SendRaceChanger(from.Female, targetRace);
}
private static void CloseCurrent(NetState ns)
@ -108,7 +108,7 @@ namespace Server.Engines.MLQuests.Gumps
m_Pending.Remove(ns);
}
ns.Send(CloseRaceChanger.Instance);
ns.SendCloseRaceChanger();
}
private static void Timeout(NetState ns)
@ -116,7 +116,7 @@ namespace Server.Engines.MLQuests.Gumps
if (IsPending(ns))
{
m_Pending.Remove(ns);
ns.Send(CloseRaceChanger.Instance);
ns.SendCloseRaceChanger();
}
}
@ -284,34 +284,6 @@ namespace Server.Engines.MLQuests.Gumps
}
}
public sealed class RaceChanger : Packet
{
public RaceChanger(bool female, Race targetRace)
: base(0xBF)
{
EnsureCapacity(7);
Stream.Write((short)0x2A);
Stream.Write((byte)(female ? 1 : 0));
Stream.Write((byte)(targetRace.RaceID + 1));
}
}
public sealed class CloseRaceChanger : Packet
{
public static readonly Packet Instance = SetStatic(new CloseRaceChanger());
private CloseRaceChanger()
: base(0xBF)
{
EnsureCapacity(7);
Stream.Write((short)0x2A);
Stream.Write((byte)0);
Stream.Write((byte)0xFF);
}
}
public class RaceChangeDeed : Item, IRaceChanger
{
[Constructible]

Some files were not shown because too many files have changed in this diff Show more