Skip to content

Commit

Permalink
Add test data for required members
Browse files Browse the repository at this point in the history
  • Loading branch information
afxres committed Oct 14, 2023
1 parent b830677 commit 4e822a2
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[SourceGeneratorContext]
[SourceGeneratorInclude<FieldOnlyItem>]
[SourceGeneratorInclude<PropertyOnlyItem>]
[SourceGeneratorInclude<MixedItem>]
public partial class RequiredMembersSourceGeneratorContext { }

[NamedObject]
Expand Down Expand Up @@ -43,6 +44,28 @@ public class PropertyOnlyItem : IEquatable<PropertyOnlyItem?>
public override int GetHashCode() => HashCode.Combine(RequiredMember, OptionalMember);
}

[NamedObject]
public class MixedItem : IEquatable<MixedItem?>
{
[NamedKey("a")]
public required int A { get; init; }

[NamedKey("b")]
public required string? B;

[NamedKey("opt-a")]
public string? OptionalA { get; init; }

[NamedKey("opt-b")]
public int OptionalB;

public bool Equals(MixedItem? other) => other is not null && A == other.A && this.B == other.B && OptionalA == other.OptionalA && this.OptionalB == other.OptionalB;

public override bool Equals(object? obj) => Equals(obj as MixedItem);

public override int GetHashCode() => HashCode.Combine(A, this.B, OptionalA, this.OptionalB);
}

public class RequiredMembersTests
{
public static IEnumerable<object[]> FieldOnlyAllMemberSetData()
Expand Down Expand Up @@ -73,11 +96,20 @@ public static IEnumerable<object[]> PropertyOnlyRequiredMemberSetData()
yield return new object[] { keys, new PropertyOnlyItem { RequiredMember = 256 } };
}

public static IEnumerable<object[]> MixedData()
{
yield return new object[] { new string[] { "a", "b" }, new MixedItem { A = 1, B = "2" } };
yield return new object[] { new string[] { "a", "b", "opt-a" }, new MixedItem { A = 3, B = "4", OptionalA = "five" } };
yield return new object[] { new string[] { "a", "b", "opt-b" }, new MixedItem { A = 6, B = "7", OptionalB = 8 } };
yield return new object[] { new string[] { "a", "b", "opt-a", "opt-b" }, new MixedItem { A = 9, B = "10", OptionalA = "11", OptionalB = 12 } };
}

[Theory(DisplayName = "Encode Decode Test")]
[MemberData(nameof(FieldOnlyAllMemberSetData))]
[MemberData(nameof(PropertyOnlyAllMemberSetData))]
[MemberData(nameof(FieldOnlyRequiredMemberSetData))]
[MemberData(nameof(PropertyOnlyRequiredMemberSetData))]
[MemberData(nameof(MixedData))]
public void EncodeDecodeTest<T>(IEnumerable<string> expectedKeys, T source)
{
var generator = Generator.CreateAotBuilder()
Expand Down

0 comments on commit 4e822a2

Please sign in to comment.