-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for nested flattened union type for partition generation
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...neratesExpectedSourceCode_sourceFileName=FlattenedNestedUnionWithPartition.00.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//HintName: DiscriminatedUnionAttribute.g.cs | ||
// <auto-generated/> | ||
#nullable enable | ||
|
||
namespace Funcky | ||
{ | ||
[global::System.Diagnostics.Conditional("Funcky_DiscriminatedUnion")] | ||
[global::System.AttributeUsage(global::System.AttributeTargets.Class)] | ||
internal sealed class DiscriminatedUnionAttribute : global::System.Attribute | ||
{ | ||
/// <summary>Allow only consumers in the same assembly to use the exhaustive <c>Match</c> and <c>Switch</c> methods.</summary> | ||
public bool NonExhaustive { get; set; } | ||
|
||
/// <summary>Generates exhaustive <c>Match</c> and <c>Switch</c> methods for the entire type hierarchy.</summary> | ||
public bool Flatten { get; set; } | ||
|
||
/// <summary>If a specialized partition extension method for <c>IEnumerable<YourType></c> should be generated. Defaults to <see langword="false"/>.</summary> | ||
public bool GeneratePartitionExtension { get; set; } | ||
|
||
/// <summary>Customized the generic type name used for the result in the generated <c>Match</c> methods. Defaults to <c>TResult</c>.</summary> | ||
public string? MatchResultTypeName { get; set; } | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...neratesExpectedSourceCode_sourceFileName=FlattenedNestedUnionWithPartition.01.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//HintName: DiscriminatedUnionGenerator.g.cs | ||
// <auto-generated/> | ||
#nullable enable | ||
|
||
namespace Funcky.DiscriminatedUnion.Test | ||
{ | ||
partial record FlattenedNestedUnionWithPartition | ||
{ | ||
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")] | ||
public abstract TResult Match<TResult>(global::System.Func<Keyword, TResult> keyword, global::System.Func<Literal.Number.Integer, TResult> integer); | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")] | ||
public abstract void Switch(global::System.Action<Keyword> keyword, global::System.Action<Literal.Number.Integer> integer); | ||
|
||
partial record Keyword | ||
{ | ||
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")] | ||
public override TResult Match<TResult>(global::System.Func<Keyword, TResult> keyword, global::System.Func<Literal.Number.Integer, TResult> integer) => keyword(this); | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")] | ||
public override void Switch(global::System.Action<Keyword> keyword, global::System.Action<Literal.Number.Integer> integer) => keyword(this); | ||
} | ||
|
||
partial record Literal | ||
{ | ||
partial record Number | ||
{ | ||
partial record Integer | ||
{ | ||
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")] | ||
public override TResult Match<TResult>(global::System.Func<Keyword, TResult> keyword, global::System.Func<Literal.Number.Integer, TResult> integer) => integer(this); | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")] | ||
public override void Switch(global::System.Action<Keyword> keyword, global::System.Action<Literal.Number.Integer> integer) => integer(this); | ||
} | ||
} | ||
} | ||
} | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")] | ||
public static partial class FlattenedNestedUnionWithPartitionEnumerableExtensions | ||
{ | ||
public static (global::System.Collections.Generic.IReadOnlyList<FlattenedNestedUnionWithPartition.Keyword> keyword, global::System.Collections.Generic.IReadOnlyList<FlattenedNestedUnionWithPartition.Literal.Number.Integer> integer) Partition(this global::System.Collections.Generic.IEnumerable<FlattenedNestedUnionWithPartition> source) | ||
{ | ||
var keywordItems = new global::System.Collections.Generic.List<FlattenedNestedUnionWithPartition.Keyword>(); | ||
var integerItems = new global::System.Collections.Generic.List<FlattenedNestedUnionWithPartition.Literal.Number.Integer>(); | ||
foreach (var item in source) | ||
{ | ||
item.Switch(keyword: keywordItems.Add, integer: integerItems.Add); | ||
} | ||
return (keywordItems.AsReadOnly(), integerItems.AsReadOnly()); | ||
} | ||
|
||
public static TResult Partition<TResult>(this global::System.Collections.Generic.IEnumerable<FlattenedNestedUnionWithPartition> source, global::System.Func<global::System.Collections.Generic.IReadOnlyList<FlattenedNestedUnionWithPartition.Keyword>, global::System.Collections.Generic.IReadOnlyList<FlattenedNestedUnionWithPartition.Literal.Number.Integer>, TResult> resultSelector) | ||
{ | ||
var keywordItems = new global::System.Collections.Generic.List<FlattenedNestedUnionWithPartition.Keyword>(); | ||
var integerItems = new global::System.Collections.Generic.List<FlattenedNestedUnionWithPartition.Literal.Number.Integer>(); | ||
foreach (var item in source) | ||
{ | ||
item.Switch(keyword: keywordItems.Add, integer: integerItems.Add); | ||
} | ||
return resultSelector(keywordItems.AsReadOnly(), integerItems.AsReadOnly()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
Funcky.DiscriminatedUnion.Test/Sources/FlattenedNestedUnionWithPartition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace Funcky.DiscriminatedUnion.Test; | ||
|
||
[DiscriminatedUnion(Flatten = true, GeneratePartitionExtension = true)] | ||
public abstract partial record FlattenedNestedUnionWithPartition | ||
{ | ||
public sealed partial record Keyword(string Value) : FlattenedNestedUnionWithPartition; | ||
|
||
public abstract partial record Literal : FlattenedNestedUnionWithPartition | ||
{ | ||
public abstract partial record Number : Literal | ||
{ | ||
public sealed partial record Integer(int Value) : Number; | ||
} | ||
} | ||
} | ||
|
||
public static class FlattenedNestedUnionWithPartitionTest | ||
{ | ||
public static void Test(FlattenedNestedUnionWithPartition[] items) | ||
{ | ||
var (keywords, integers) = items.Partition(); | ||
} | ||
} |