Skip to content

Commit

Permalink
Fix tests so they pass (post-SDK migration)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed May 31, 2024
1 parent 031f8e9 commit cca60c2
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 84 deletions.
8 changes: 4 additions & 4 deletions EOLib.Graphics.Test/NativeGraphicsManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void WhenLoadTexture_MaleHat_Transparent_SetsSpecialColorToTransparent()

Texture2D resultTexture;
var bmp = LoadGFXReturnsBitmap(GFXTypes.MaleHat, requestedResource);
FillBitmapWithColor(bmp, new Color(0x08, 0x00, 0x00, 0xff));
FillBitmapWithColor(bmp, new Color(0xff000008));
resultTexture = _nativeGraphicsManager.TextureFromResource(GFXTypes.MaleHat, requestedResource, true);

var data = new Microsoft.Xna.Framework.Color[resultTexture.Width * resultTexture.Height];
Expand All @@ -171,7 +171,7 @@ public void WhenLoadTexture_FemaleHat_Transparent_SetsSpecialColorToTransparent(

Texture2D resultTexture;
var bmp = LoadGFXReturnsBitmap(GFXTypes.FemaleHat, requestedResource);
FillBitmapWithColor(bmp, new Color(0x08, 0x00, 0x00, 0xff));
FillBitmapWithColor(bmp, new Color(0xff000008));
resultTexture = _nativeGraphicsManager.TextureFromResource(GFXTypes.FemaleHat, requestedResource, true);

var data = new Color[resultTexture.Width * resultTexture.Height];
Expand Down Expand Up @@ -221,9 +221,9 @@ private static void FillBitmapWithColor(Memory<byte> image, Color color)
{
for (int i = 54; i < image.Length; i+=4)
{
image.Span[i] = color.R;
image.Span[i] = color.B;
image.Span[i + 1] = color.G;
image.Span[i + 2] = color.B;
image.Span[i + 2] = color.R;
image.Span[i + 3] = color.A;
}
}
Expand Down
4 changes: 2 additions & 2 deletions EOLib.IO.Test/Map/MapFilePropertiesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void MapFileProperties_DeserializeFromByteArray_ThrowsExceptionWhenNotEMF

private static IMapFileProperties CreateMapPropertiesWithSomeTestData(IMapFileProperties props)
{
return props.WithChecksum(new List<int> {1, 2, 3, 4})
return props.WithChecksum(new List<int> {1, 2})
.WithName("Some test name")
.WithWidth(200)
.WithHeight(100)
Expand All @@ -117,7 +117,7 @@ private static byte[] CreateExpectedBytes(IMapFileProperties props)
var ret = new List<byte>();

ret.AddRange(Encoding.ASCII.GetBytes(props.FileType));
ret.AddRange(props.Checksum.Cast<byte>());
ret.AddRange(props.Checksum.SelectMany(x => numberEncoderService.EncodeNumber(x, 2)));

var fullName = Enumerable.Repeat((byte)0xFF, 24).ToArray();
var encodedName = mapStringEncoderService.EncodeMapString(props.Name, props.Name.Length);
Expand Down
24 changes: 15 additions & 9 deletions EOLib.IO.Test/Services/Serializers/PubFileSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ public abstract class PubFileSerializerTest<T, U>
[Test]
public void DeserializeFromByteArray_WrongLength_Throws()
{
const int ExpectedChecksum = 1234567890;
const int ExpectedChecksum1 = 12345;
const int ExpectedChecksum2 = 6789;
const int ExpectedLength = 4;

var expectedChecksum = new List<int> { ExpectedChecksum1, ExpectedChecksum2 };

var records = new[]
{
new U().WithID(1).WithName("Rec_1"),
Expand All @@ -41,16 +44,19 @@ public void DeserializeFromByteArray_WrongLength_Throws()
new U().WithID(4).WithName("Rec_4"),
};

var pubBytesShort = MakePubFileBytes(ExpectedChecksum, ExpectedLength - 1, records);
var pubBytesShort = MakePubFileBytes(expectedChecksum, ExpectedLength - 1, records);
Assert.That(() => CreateSerializer().DeserializeFromByteArray(1, pubBytesShort, () => new T()), Throws.InstanceOf<IOException>());
}

[Test]
public void DeserializeFromByteArray_HasExpectedHeader()
{
const int ExpectedChecksum = 1234567890;
const int ExpectedChecksum1 = 12345;
const int ExpectedChecksum2 = 6789;
const int ExpectedLength = 4;

var expectedChecksum = new List<int> { ExpectedChecksum1, ExpectedChecksum2 };

var records = new[]
{
new U().WithID(1).WithName("Rec_1"),
Expand All @@ -59,17 +65,17 @@ public void DeserializeFromByteArray_HasExpectedHeader()
new U().WithID(4).WithName("Rec_4"),
};

var pubBytes = MakePubFileBytes(ExpectedChecksum, ExpectedLength, records);
var pubBytes = MakePubFileBytes(expectedChecksum, ExpectedLength, records);
var file = CreateSerializer().DeserializeFromByteArray(1, pubBytes, () => new T());

Assert.That(file.CheckSum, Is.EqualTo(ExpectedChecksum));
Assert.That(file.CheckSum, Is.EqualTo(expectedChecksum));
Assert.That(file.Length, Is.EqualTo(ExpectedLength));
}

[Test]
public void SerializeToByteArray_ReturnsExpectedBytes()
{
var expectedBytes = MakePubFileBytes(55565554,
var expectedBytes = MakePubFileBytes(new List<int> { 5556, 5554 },
9,
new U().WithID(1).WithName("TestFixture"),
new U().WithID(2).WithName("Test2"),
Expand Down Expand Up @@ -104,7 +110,7 @@ public void DeserializeFromByteArray_HasExpectedIDAndNames()
new U().WithID(8).WithName("Test8"),
new U().WithID(9).WithName("eof")
};
var bytes = MakePubFileBytes(55565554, 9, records);
var bytes = MakePubFileBytes(new List<int> { 5556, 5554 }, 9, records);

var serializer = CreateSerializer();
var file = serializer.DeserializeFromByteArray(1, bytes, () => new T());
Expand All @@ -113,14 +119,14 @@ public void DeserializeFromByteArray_HasExpectedIDAndNames()
file.Select(x => new { x.ID, x.Name }).ToList());
}

private byte[] MakePubFileBytes(int checksum, int length, params IPubRecord[] records)
private byte[] MakePubFileBytes(List<int> checksum, int length, params IPubRecord[] records)
{
var numberEncoderService = new NumberEncoderService();
var recordSerializer = new PubRecordSerializer(numberEncoderService);

var bytes = new List<byte>();
bytes.AddRange(Encoding.ASCII.GetBytes(new T().FileType));
bytes.AddRange(numberEncoderService.EncodeNumber(checksum, 4));
bytes.AddRange(checksum.SelectMany(x => numberEncoderService.EncodeNumber(x, 2)));
bytes.AddRange(numberEncoderService.EncodeNumber(length, 2));
bytes.Add(numberEncoderService.EncodeNumber(1, 1)[0]);
foreach (var record in records)
Expand Down
Loading

0 comments on commit cca60c2

Please sign in to comment.