Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds generic support to record unions. #24

Merged
merged 3 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Dunet/GenerateUnionRecord/StringBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text;

namespace Dunet.GenerateUnionRecord;

internal static class StringBuilderExtensions
{
public static void AppendTypeParams(
this StringBuilder builder,
IReadOnlyList<TypeParameter> typeParams
)
{
if (typeParams.Count <= 0)
{
return;
}

var typeParamList = string.Join(", ", typeParams.Select(typeParam => typeParam.Name));

builder.Append("<");
builder.Append(typeParamList);
builder.Append(">");
}
}
5 changes: 4 additions & 1 deletion src/Dunet/GenerateUnionRecord/UnionRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ internal record TypeParameter(string Name)
public sealed override string ToString() => Name;
}

internal record RecordProperty(string Type, string Name);
internal record RecordProperty(string Type, string Name)
{
public sealed override string ToString() => $"{Type} {Name}";
}
11 changes: 8 additions & 3 deletions src/Dunet/GenerateUnionRecord/UnionRecordSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ public static string GenerateRecord(UnionRecord record)
builder.AppendLine($"namespace {record.Namespace};");
}

builder.AppendLine($"abstract partial record {record.Name}");
builder.Append($"abstract partial record {record.Name}");
builder.AppendTypeParams(record.TypeParameters);
builder.AppendLine();
builder.AppendLine("{");
builder.AppendLine($" private {record.Name}() {{}}");

foreach (var member in record.Members)
{
builder.AppendLine($" public sealed partial record {member.Name} : {record.Name};");
builder.AppendLine();
builder.Append($" public sealed partial record {member.Name}");
builder.AppendTypeParams(member.TypeParameters);
builder.Append($" : {record.Name}");
builder.AppendTypeParams(record.TypeParameters);
builder.AppendLine(" {}");
}

builder.AppendLine("}");
Expand Down
104 changes: 104 additions & 0 deletions test/Dunet.Test/GenerateUnionRecord/GenericGenerationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using Dunet.Test.Runtime;

namespace Dunet.Test.GenerateUnionRecord;

public class GenericGenerationTests : UnionRecordTests
{
[Fact]
public void UnionTypeMayHaveGenericParameter()
{
var programCs =
@"
using Dunet;

Option<int> some = new Option<int>.Some(1);
Option<int> none = new Option<int>.None();

[Union]
partial record Option<T>
{
partial record Some(T Value);
partial record None();
}";
// Act.
var result = Compile.ToAssembly(programCs);

// Assert.
result.CompilationErrors.Should().BeEmpty();
result.GenerationDiagnostics.Should().BeEmpty();
}

[Fact]
public void UnionMemberMayHaveGenericParameter()
{
var programCs =
@"
using Dunet;

Option some = new Option.Some<int>(1);
Option none = new Option.None();

[Union]
partial record Option
{
partial record Some<T>(T Value);
partial record None();
}";
// Act.
var result = Compile.ToAssembly(programCs);

// Assert.
result.CompilationErrors.Should().BeEmpty();
result.GenerationDiagnostics.Should().BeEmpty();
}

[Theory]
[InlineData(1, 2, "0.5")]
[InlineData(1, 0, "Error: division by zero.")]
public void CanReturnImplementationsOfGenericUnion(
int dividend,
int divisor,
string expectedOutput
)
{
var programCs =
@$"
using Dunet;

static string GetActualArea() => Divide() switch
{{
Option.Some<double> some => some.Value.ToString(),
Option.None none => ""Error: division by zero."",
_ => throw new System.InvalidOperationException(),
}};

static Option Divide()
{{
var dividend = {dividend};
var divisor = {divisor};

if (divisor is 0)
{{
return new Option.None();
}}

return new Option.Some<double>((double)dividend / divisor);
}}

[Union]
partial record Option
{{
partial record Some<T>(T Value);
partial record None();
}}";

// Act.
var result = Compile.ToAssembly(programCs);
var actualArea = result.Assembly.ExecuteStaticMethod<string>("GetActualArea");

// Assert.
result.CompilationErrors.Should().BeEmpty();
result.GenerationErrors.Should().BeEmpty();
actualArea.Should().Be(expectedOutput);
}
}
4 changes: 2 additions & 2 deletions test/Dunet.Test/GenerateUnionRecord/SwitchExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void SwitchExpressionMatchesCorrectCase(string shapeDeclaration, double e
@$"
using Dunet;

static double GetArea()
static double GetActualArea()
{{
{shapeDeclaration}
return shape switch
Expand All @@ -69,7 +69,7 @@ partial record Triangle(double Base, double Height);
}}";
// Act.
var result = Compile.ToAssembly(source);
var actualArea = result.Assembly.ExecuteStaticMethod<double>("GetArea");
var actualArea = result.Assembly.ExecuteStaticMethod<double>("GetActualArea");

// Assert.
result.CompilationErrors.Should().BeEmpty();
Expand Down