ModernUO/Projects/Server.Tests/Tests/Network/Packets/Outgoing/VendorBuyPacketTests.cs
Kamron Batman 3551d962f7
C# 9 Cleanup (#325)
- [X] Removes EventArgs - not needed
- [X] Merges sequential checks
- [X] Removes redundant type declarations
2020-11-27 00:29:21 -08:00

198 lines
6.7 KiB
C#

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using Server.Items;
using Server.Network;
using Xunit;
namespace Server.Tests.Network
{
public class VendorBuyPacketTests : IClassFixture<ServerFixture>
{
[Fact]
public void TestVendorBuyContent()
{
var cont = new Container(World.NewItem);
var buyStates = new List<BuyItemState>
{
new("First Item", cont.Serial, World.NewItem, 10, 1, 0x01, 0),
new("Second Item", cont.Serial, World.NewItem, 20, 2, 0x0A, 0),
new("Third Item", cont.Serial, World.NewItem, 30, 10, 0x0F, 0)
};
var data = new VendorBuyContent(buyStates).Compile();
Span<byte> expectedData = stackalloc byte[5 + buyStates.Count * 19];
var pos = 0;
expectedData.Write(ref pos, (byte)0x3C); // Packet ID
expectedData.Write(ref pos, (ushort)expectedData.Length); // Length
expectedData.Write(ref pos, (ushort)buyStates.Count); // Count
for (var i = buyStates.Count - 1; i >= 0; i--)
{
var buyState = buyStates[i];
expectedData.Write(ref pos, buyState.MySerial);
expectedData.Write(ref pos, (ushort)buyState.ItemID);
pos++; // ItemID Offset
expectedData.Write(ref pos, (ushort)buyState.Amount);
expectedData.Write(ref pos, (ushort)(i + 1)); // X
expectedData.Write(ref pos, (ushort)1); // Y
expectedData.Write(ref pos, buyState.ContainerSerial);
expectedData.Write(ref pos, (ushort)buyState.Hue);
}
AssertThat.Equal(data, expectedData);
}
[Fact]
public void TestVendorBuyContent6017()
{
var cont = new Container(World.NewItem);
var buyStates = new List<BuyItemState>
{
new("First Item", cont.Serial, World.NewItem, 10, 1, 0x01, 0),
new("Second Item", cont.Serial, World.NewItem, 20, 2, 0x0A, 0),
new("Third Item", cont.Serial, World.NewItem, 30, 10, 0x0F, 0)
};
var data = new VendorBuyContent6017(buyStates).Compile();
Span<byte> expectedData = stackalloc byte[5 + buyStates.Count * 20];
var pos = 0;
expectedData.Write(ref pos, (byte)0x3C); // Packet ID
expectedData.Write(ref pos, (ushort)expectedData.Length); // Length
expectedData.Write(ref pos, (ushort)buyStates.Count); // Count
for (var i = buyStates.Count - 1; i >= 0; i--)
{
var buyState = buyStates[i];
expectedData.Write(ref pos, buyState.MySerial);
expectedData.Write(ref pos, (ushort)buyState.ItemID);
pos++; // ItemID Offset
expectedData.Write(ref pos, (ushort)buyState.Amount);
expectedData.Write(ref pos, (ushort)(i + 1)); // X
expectedData.Write(ref pos, (ushort)1); // Y
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (byte)0); // Grid Location?
#else
pos++;
#endif
expectedData.Write(ref pos, buyState.ContainerSerial);
expectedData.Write(ref pos, (ushort)buyState.Hue);
}
AssertThat.Equal(data, expectedData);
}
[Fact]
public void TestDisplayBuyList()
{
var vendor = new Mobile(0x1);
vendor.DefaultMobileInit();
var data = new DisplayBuyList(vendor).Compile();
Span<byte> expectedData = stackalloc byte[7];
var pos = 0;
expectedData.Write(ref pos, (byte)0x24); // Packet ID
expectedData.Write(ref pos, vendor.Serial);
expectedData.Write(ref pos, (ushort)0x30); // Buy gump
AssertThat.Equal(data, expectedData);
}
[Fact]
public void TestDisplayBuyListHS()
{
var vendor = new Mobile(0x1);
vendor.DefaultMobileInit();
var data = new DisplayBuyListHS(vendor).Compile();
Span<byte> expectedData = stackalloc byte[9];
var pos = 0;
expectedData.Write(ref pos, (byte)0x24); // Packet ID
expectedData.Write(ref pos, vendor.Serial);
expectedData.Write(ref pos, (ushort)0x30); // Buy gump
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (ushort)0);
#endif
AssertThat.Equal(data, expectedData);
}
[Fact]
public void TestVendorBuyList()
{
var vendor = new Mobile(0x1);
vendor.DefaultMobileInit();
var cont = new Container(World.NewItem);
var buyStates = new List<BuyItemState>
{
new("First Item", cont.Serial, World.NewItem, 10, 1, 0x01, 0),
new("Second Item", cont.Serial, World.NewItem, 20, 2, 0x0A, 0),
new("Third Item", cont.Serial, World.NewItem, 30, 10, 0x0F, 0)
};
var data = new VendorBuyList(vendor, buyStates).Compile();
var length = 8 + buyStates.Sum(state => 6 + state.Description.Length);
Span<byte> expectedData = stackalloc byte[length];
var pos = 0;
expectedData.Write(ref pos, (byte)0x74); // Packet ID
expectedData.Write(ref pos, (ushort)expectedData.Length); // Length
expectedData.Write(ref pos, Serial.MinusOne); // Vendor Buy Pack Serial or -1
expectedData.Write(ref pos, (byte)buyStates.Count);
for (var i = 0; i < buyStates.Count; i++)
{
var state = buyStates[i];
expectedData.Write(ref pos, state.Price);
var description = state.Description ?? "";
expectedData.Write(ref pos, (byte)Math.Min(255, description.Length + 1));
expectedData.WriteAsciiNull(ref pos, description, 255);
}
AssertThat.Equal(data, expectedData);
}
[Fact]
public void TestEndVendorBuy()
{
var vendor = new Mobile(0x1);
vendor.DefaultMobileInit();
var data = new EndVendorBuy(vendor).Compile();
Span<byte> expectedData = stackalloc byte[8];
var pos = 0;
expectedData.Write(ref pos, (byte)0x3B); // Packet ID
expectedData.Write(ref pos, (ushort)0x8); // Length
expectedData.Write(ref pos, vendor.Serial);
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (byte)-);
#endif
AssertThat.Equal(data, expectedData);
}
}
}