Skip to content

Commit

Permalink
fix: custom namespaces and types work
Browse files Browse the repository at this point in the history
  • Loading branch information
bmazzarol committed Sep 7, 2024
1 parent 454df72 commit a448e71
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Tuxedo.SourceGenerators/RefinementSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ ImmutableArray<MethodDeclarationSyntax> methodDeclarations
.Expression.ToString();

// get the first parameter type
var parameterType = methodDeclaration.ParameterList.Parameters.First().Type!.ToString();
var parameterTypeSemanticModel = compilation.GetSemanticModel(
methodDeclaration.ParameterList.Parameters.First().Type!.SyntaxTree
);
var parameterType = parameterTypeSemanticModel
.GetTypeInfo(methodDeclaration.ParameterList.Parameters.First().Type!)
.Type!.ToDisplayString();

// get the generic type arguments
var genericTypeArguments = methodSymbol.TypeArguments;
Expand Down
42 changes: 42 additions & 0 deletions Tuxedo.Tests/CustomTypeRefinement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using FluentAssertions;
using Tuxedo.Tests.Custom;
using Xunit;

namespace Tuxedo.Tests
{
namespace Custom
{
public sealed record Widget(int Id, string Name);
}

public static class CustomTypeRefinement
{
[Refinement("The widget must have a valid Id and Name")]
public static bool ValidWidget(Widget widget) =>
widget.Id > 0 && !string.IsNullOrWhiteSpace(widget.Name);
}

public sealed class CustomTypeRefinementTests
{
[Fact(DisplayName = "A widget can be refined to a valid widget")]
public void Case1()
{
var widget = new Widget(1, "Widget");
Refined<Widget, ValidWidget> refined = widget;
refined.Value.Should().Be(widget);
refined.Value.Id.Should().Be(1);
refined.Value.Name.Should().Be("Widget");
}

[Fact(DisplayName = "An invalid widget should fail the refinement")]
public void Case2()
{
var widget = new Widget(0, "Widget");
Assert
.Throws<RefinementFailureException>(() => (Refined<Widget, ValidWidget>)widget)
.Message.Should()
.Be("The widget must have a valid Id and Name");
Refined.TryRefine(widget, out Refined<Widget, ValidWidget> _).Should().BeFalse();
}
}
}

0 comments on commit a448e71

Please sign in to comment.