Skip to content

Commit

Permalink
fixes ToString() implementation calling PrintMembers() non virtualy (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianoc committed May 25, 2024
1 parent 08ae882 commit b9d9e8c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
16 changes: 16 additions & 0 deletions Cecilifier.Core.Tests/Tests/OutputBased/RecordTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ public void ToString_WhenInheritFromObjectWithMultipleProperties_ReturnsProperti
AssertOutput("System.Console.WriteLine(new R(42, true)); record R(int Value, bool IsCool);", "R { Value = 42, IsCool = True }");
}

[Test]
public void ToString_WhenInheritFromRecord_IncludesBaseRecordProperties()
{
AssertOutput(
"System.Console.WriteLine(new Derived(42, true)); record Derived(int Value, bool IsCool):Base(IsCool); record Base(bool IsCool);",
"Derived { IsCool = True, Value = 42 }");
}

[Test]
public void ToString_WhenInheritFromRecord_IncludesBaseRecordProperties2()
{
AssertOutput(
"""System.Console.WriteLine(new D(42, true, "42")); record D(int Value, bool IsCool, string Str):B1(IsCool, Str); record B1(bool IsCool, string Str) : B2(Str); record B2(string Str);""",
"D { Str = 42, IsCool = True, Value = 42 }");
}

[Test]
public void Constructor_WhenInheritFromRecordWithProperties_CorrectArgumentsArePassed()
{
Expand Down
4 changes: 2 additions & 2 deletions Cecilifier.Core/CodeGeneration/Record.Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void AddPrintMembersMethod()
printMembersVar,
PrintMembersMethodName,
PrintMembersMethodName,
$"MethodAttributes.Family | {Constants.Cecil.HideBySigNewSlotVirtual}",
$"MethodAttributes.Family | { (HasBaseRecord(record) ? Constants.Cecil.HideBySigVirtual : Constants.Cecil.HideBySigNewSlotVirtual) }",
[builderParameter],
[],
ctx => context.TypeResolver.Bcl.System.Boolean,
Expand Down Expand Up @@ -171,7 +171,7 @@ void AddToStringMethod()
OpCodes.Pop,
OpCodes.Ldarg_0,
OpCodes.Ldloc_0,
OpCodes.Call.WithOperand(PrintMembersMethodToCall()),
OpCodes.Callvirt.WithOperand(PrintMembersMethodToCall()),
OpCodes.Brfalse_S.WithBranchOperand("NoMemberToPrint"),
OpCodes.Ldloc_0,
OpCodes.Ldstr.WithOperand("\" \""),
Expand Down
3 changes: 2 additions & 1 deletion Cecilifier.Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ public struct Cecil
public const string InterfaceMethodDefinitionAttributes = "MethodAttributes.NewSlot | MethodAttributes.Virtual"; // Some common method attributes (like HideBySig) will be explicitly added.
public const string MethodAttributesSpecialName = "MethodAttributes.SpecialName";
public const string MethodAttributesStatic = "MethodAttributes.Static";
public const string HideBySigVirtual = "MethodAttributes.HideBySig | MethodAttributes.Virtual";
public const string HideBySigNewSlotVirtual = "MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual";
public const string DelegateMethodAttributes = $"MethodAttributes.Public | {HideBySigNewSlotVirtual}";
public const string PublicOverrideMethodAttributes = $"MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual";
public const string PublicOverrideMethodAttributes = $"MethodAttributes.Public | {HideBySigVirtual}";

public const string CtorAttributes = "MethodAttributes.RTSpecialName | MethodAttributes.SpecialName";
public const string InstanceConstructorName = "ctor";
Expand Down

0 comments on commit b9d9e8c

Please sign in to comment.