Skip to content

Commit

Permalink
Add test for generated enum types
Browse files Browse the repository at this point in the history
  • Loading branch information
TSRBerry committed Feb 16, 2025
1 parent 08eb5a7 commit 7f7c0e0
Show file tree
Hide file tree
Showing 13 changed files with 345 additions and 0 deletions.
66 changes: 66 additions & 0 deletions RyuSocks.Generator.Test/PacketGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,71 @@ await Verify.VerifyGeneratedSources(
expectedDiagnostics
);
}

[Fact]
public async Task GeneratedSources_WorksWithGeneratedTypes()
{
const int ExpectedGeneratedSourcesLength = 11;
const string Directory = "GeneratedTypes/";
(string, string)[] generatedSources = GetDefaultGeneratedSources(ExpectedGeneratedSourcesLength);
DiagnosticResult[] expectedDiagnostics = [
// Type not found
DiagnosticResult.CompilerError("CS0246").WithSpan(9, 20, 9, 37).WithArguments("GeneratedEnumByte"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Byte1.g.cs", 8, 20, 8, 37).WithArguments("GeneratedEnumByte"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Byte1.g.cs", 12, 21, 12, 38).WithArguments("GeneratedEnumByte"),
DiagnosticResult.CompilerError("CS0246").WithSpan(12, 22, 12, 40).WithArguments("GeneratedEnumSByte"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.SByte1.g.cs", 8, 22, 8, 40).WithArguments("GeneratedEnumSByte"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.SByte1.g.cs", 12, 21, 12, 39).WithArguments("GeneratedEnumSByte"),
DiagnosticResult.CompilerError("CS0246").WithSpan(15, 23, 15, 42).WithArguments("GeneratedEnumUShort"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.UShort1.g.cs", 8, 23, 8, 42).WithArguments("GeneratedEnumUShort"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.UShort1.g.cs", 12, 21, 12, 40).WithArguments("GeneratedEnumUShort"),
DiagnosticResult.CompilerError("CS0246").WithSpan(18, 21, 18, 39).WithArguments("GeneratedEnumShort"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Short1.g.cs", 8, 21, 8, 39).WithArguments("GeneratedEnumShort"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Short1.g.cs", 12, 21, 12, 39).WithArguments("GeneratedEnumShort"),
DiagnosticResult.CompilerError("CS0246").WithSpan(21, 31, 21, 48).WithArguments("GeneratedEnumUInt"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.UInt1.g.cs", 8, 31, 8, 48).WithArguments("GeneratedEnumUInt"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.UInt1.g.cs", 12, 21, 12, 38).WithArguments("GeneratedEnumUInt"),
DiagnosticResult.CompilerError("CS0246").WithSpan(24, 13, 24, 29).WithArguments("GeneratedEnumInt"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Int1.g.cs", 8, 13, 8, 29).WithArguments("GeneratedEnumInt"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Int1.g.cs", 12, 21, 12, 37).WithArguments("GeneratedEnumInt"),
DiagnosticResult.CompilerError("CS0246").WithSpan(27, 13, 27, 31).WithArguments("GeneratedEnumULong"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.ULong1.g.cs", 8, 13, 8, 31).WithArguments("GeneratedEnumULong"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.ULong1.g.cs", 12, 21, 12, 39).WithArguments("GeneratedEnumULong"),
DiagnosticResult.CompilerError("CS0246").WithSpan(30, 20, 30, 37).WithArguments("GeneratedEnumLong"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Long1.g.cs", 8, 20, 8, 37).WithArguments("GeneratedEnumLong"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Long1.g.cs", 12, 21, 12, 38).WithArguments("GeneratedEnumLong"),
DiagnosticResult.CompilerError("CS0246").WithSpan(33, 20, 33, 37).WithArguments("GeneratedEnumByte"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Byte2.g.cs", 8, 20, 8, 37).WithArguments("GeneratedEnumByte"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Byte2.g.cs", 12, 21, 12, 38).WithArguments("GeneratedEnumByte"),
DiagnosticResult.CompilerError("CS0246").WithSpan(36, 13, 36, 29).WithArguments("GeneratedEnumInt"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Int2.g.cs", 8, 13, 8, 29).WithArguments("GeneratedEnumInt"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Int2.g.cs", 12, 13, 12, 29).WithArguments("GeneratedEnumInt"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Int2.g.cs", 12, 45, 12, 61).WithArguments("GeneratedEnumInt"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Int2.g.cs", 16, 30, 16, 46).WithArguments("GeneratedEnumInt"),
DiagnosticResult.CompilerError("CS0246").WithSpan(39, 23, 39, 40).WithArguments("GeneratedEnumByte"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Byte3.g.cs", 8, 23, 8, 40).WithArguments("GeneratedEnumByte"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Byte3.g.cs", 12, 13, 12, 30).WithArguments("GeneratedEnumByte"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Byte3.g.cs", 12, 46, 12, 63).WithArguments("GeneratedEnumByte"),
DiagnosticResult.CompilerError("CS0246").WithSpan("RyuSocks.Generator/RyuSocks.Generator.PacketGenerator/TestPacket.Byte3.g.cs", 16, 30, 16, 47).WithArguments("GeneratedEnumByte"),
];

generatedSources[DefaultSourcesAmount] = GetGeneratedSourceFromFile(Directory + "TestPacket.Byte1.g.cs");
generatedSources[DefaultSourcesAmount + 1] = GetGeneratedSourceFromFile(Directory + "TestPacket.SByte1.g.cs");
generatedSources[DefaultSourcesAmount + 2] = GetGeneratedSourceFromFile(Directory + "TestPacket.UShort1.g.cs");
generatedSources[DefaultSourcesAmount + 3] = GetGeneratedSourceFromFile(Directory + "TestPacket.Short1.g.cs");
generatedSources[DefaultSourcesAmount + 4] = GetGeneratedSourceFromFile(Directory + "TestPacket.UInt1.g.cs");
generatedSources[DefaultSourcesAmount + 5] = GetGeneratedSourceFromFile(Directory + "TestPacket.Int1.g.cs");
generatedSources[DefaultSourcesAmount + 6] = GetGeneratedSourceFromFile(Directory + "TestPacket.ULong1.g.cs");
generatedSources[DefaultSourcesAmount + 7] = GetGeneratedSourceFromFile(Directory + "TestPacket.Long1.g.cs");
generatedSources[DefaultSourcesAmount + 8] = GetGeneratedSourceFromFile(Directory + "TestPacket.Byte2.g.cs");
generatedSources[DefaultSourcesAmount + 9] = GetGeneratedSourceFromFile(Directory + "TestPacket.Int2.g.cs");
generatedSources[DefaultSourcesAmount + 10] = GetGeneratedSourceFromFile(Directory + "TestPacket.Byte3.g.cs");

await Verify.VerifyGeneratedSources(
GetSourceFromFile(Directory + "TestPacket.cs"),
generatedSources,
expectedDiagnostics
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using RyuSocks.Packets;
using System;
using System.Buffers.Binary;
using System.Text;

partial class TestPacket
{
public partial GeneratedEnumByte Byte1
{
get
{
return (GeneratedEnumByte)this[0];
}
set
{
this[0] = (byte)value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using RyuSocks.Packets;
using System;
using System.Buffers.Binary;
using System.Text;

partial class TestPacket
{
public partial GeneratedEnumByte Byte2
{
get
{
return (GeneratedEnumByte)this[this.AnOffset];
}
set
{
this[this.AnOffset] = (byte)value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using RyuSocks.Packets;
using System;
using System.Buffers.Binary;
using System.Text;

partial class TestPacket
{
protected partial GeneratedEnumByte[] Byte3
{
get
{
GeneratedEnumByte[] result = new GeneratedEnumByte[this.ALength];

for (int i = 0; i < this.ALength; i++)
{
result[i] = (GeneratedEnumByte)this[this.AnOffset + i];
}

return result;
}
set
{
if (value.Length != this.ALength)
{
throw new ArgumentOutOfRangeException(nameof(value.Length), value.Length, $"{nameof(value.Length)} must be equal to: {this.ALength}");
}

for (int i = 0; i < this.ALength; i++)
{
this[this.AnOffset + i] = (byte)value[i];
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using RyuSocks.Packets;
using System;
using System.Buffers.Binary;
using System.Text;

partial class TestPacket
{
partial GeneratedEnumInt Int1
{
get
{
return (GeneratedEnumInt)BinaryPrimitives.ReadInt32LittleEndian(this.AsSpan(5, 4));
}
set
{
BinaryPrimitives.WriteInt32LittleEndian(this.AsSpan(5, 4), (int)value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using RyuSocks.Packets;
using System;
using System.Buffers.Binary;
using System.Text;

partial class TestPacket
{
partial GeneratedEnumInt[] Int2
{
get
{
GeneratedEnumInt[] result = new GeneratedEnumInt[this.ALength];

for (int i = 0; i < this.ALength; i++)
{
result[i] = (GeneratedEnumInt)BinaryPrimitives.ReadInt32LittleEndian(this.AsSpan(9 + (i * 4), 4));
}

return result;
}
set
{
if (value.Length != this.ALength)
{
throw new ArgumentOutOfRangeException(nameof(value.Length), value.Length, $"{nameof(value.Length)} must be equal to: {this.ALength}");
}

for (int i = 0; i < this.ALength; i++)
{
BinaryPrimitives.WriteInt32LittleEndian(this.AsSpan(9 + (i * 4), 4), (int)value[i]);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using RyuSocks.Packets;
using System;
using System.Buffers.Binary;
using System.Text;

partial class TestPacket
{
public partial GeneratedEnumLong Long1
{
get
{
return (GeneratedEnumLong)BinaryPrimitives.ReadInt64LittleEndian(this.AsSpan(7, 8));
}
set
{
BinaryPrimitives.WriteInt64LittleEndian(this.AsSpan(7, 8), (long)value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using RyuSocks.Packets;
using System;
using System.Buffers.Binary;
using System.Text;

partial class TestPacket
{
internal partial GeneratedEnumSByte SByte1
{
get
{
return (GeneratedEnumSByte)(sbyte)this[1];
}
set
{
this[1] = (byte)value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using RyuSocks.Packets;
using System;
using System.Buffers.Binary;
using System.Text;

partial class TestPacket
{
private partial GeneratedEnumShort Short1
{
get
{
return (GeneratedEnumShort)BinaryPrimitives.ReadInt16LittleEndian(this.AsSpan(3, 2));
}
set
{
BinaryPrimitives.WriteInt16LittleEndian(this.AsSpan(3, 2), (short)value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using RyuSocks.Packets;
using System;
using System.Buffers.Binary;
using System.Text;

partial class TestPacket
{
protected private partial GeneratedEnumUInt UInt1
{
get
{
return (GeneratedEnumUInt)BinaryPrimitives.ReadUInt32LittleEndian(this.AsSpan(4, 4));
}
set
{
BinaryPrimitives.WriteUInt32LittleEndian(this.AsSpan(4, 4), (uint)value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using RyuSocks.Packets;
using System;
using System.Buffers.Binary;
using System.Text;

partial class TestPacket
{
partial GeneratedEnumULong ULong1
{
get
{
return (GeneratedEnumULong)BinaryPrimitives.ReadUInt64LittleEndian(this.AsSpan(6, 8));
}
set
{
BinaryPrimitives.WriteUInt64LittleEndian(this.AsSpan(6, 8), (ulong)value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using RyuSocks.Packets;
using System;
using System.Buffers.Binary;
using System.Text;

partial class TestPacket
{
protected partial GeneratedEnumUShort UShort1
{
get
{
return (GeneratedEnumUShort)BinaryPrimitives.ReadUInt16LittleEndian(this.AsSpan(2, 2));
}
set
{
BinaryPrimitives.WriteUInt16LittleEndian(this.AsSpan(2, 2), (ushort)value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using RyuSocks.Packets;

public partial class TestPacket : Packet
{
private int AnOffset => 2;
protected int ALength => 3;

[PacketField(0, AssumeGeneratedEnumType="byte")]
public partial GeneratedEnumByte Byte1 { get; set; }

[PacketField(1, AssumeGeneratedEnumType="SBYTE")]
internal partial GeneratedEnumSByte SByte1 { get; set; }

[PacketField(2, AssumeGeneratedEnumType="UsHoRt")]
protected partial GeneratedEnumUShort UShort1 { get; set; }

[PacketField(3, AssumeGeneratedEnumType="sHoRt")]
private partial GeneratedEnumShort Short1 { get; set; }

[PacketField(4, AssumeGeneratedEnumType="uint")]
protected private partial GeneratedEnumUInt UInt1 { get; set; }

[PacketField(5, AssumeGeneratedEnumType="int")]
partial GeneratedEnumInt Int1 { get; set; }

[PacketField(6, AssumeGeneratedEnumType="ulong")]
partial GeneratedEnumULong ULong1 { get; set; }

[PacketField(7, AssumeGeneratedEnumType="long")]
public partial GeneratedEnumLong Long1 { get; set; }

[PacketField(nameof(AnOffset), AssumeGeneratedEnumType="byte")]
public partial GeneratedEnumByte Byte2 { get; set; }

[PacketField(9, LengthMember = nameof(ALength), AssumeGeneratedEnumType="int")]
partial GeneratedEnumInt[] Int2 { get; set; }

[PacketField(nameof(AnOffset), LengthMember = nameof(ALength), AssumeGeneratedEnumType="byte")]
protected partial GeneratedEnumByte[] Byte3 { get; set; }
}

0 comments on commit 7f7c0e0

Please sign in to comment.