Setup Type Parameters support

This commit is contained in:
2024-09-22 20:52:33 +02:00
parent bfdd2ce911
commit 7bac357a14
10 changed files with 285 additions and 4 deletions
@@ -0,0 +1,38 @@
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests;
[TestClass]
public class TypeParameterBuilderTests
{
private const string TypeName = "T";
private readonly TypeParameterBuilder builder;
public TypeParameterBuilderTests()
{
this.builder = new TypeParameterBuilder(TypeName);
}
[TestMethod]
public void Constructor_NullType_ThrowsArgumentNullException()
{
var action = () =>
{
_ = new TypeParameterBuilder(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Build_ReturnsPropertyDeclarationSyntax()
{
var syntax = this.builder.Build();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<TypeParameterSyntax>();
}
}
@@ -0,0 +1,91 @@
using FluentAssertions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace Sybil.Tests;
[TestClass]
public class TypeParameterConstraintBuilderTests
{
private const string TypeName = "T";
private const string ConstraintType = "TType";
private const string Expected = "where T : class, struct, new(), TType";
private readonly TypeParameterConstraintBuilder builder;
public TypeParameterConstraintBuilderTests()
{
this.builder = new TypeParameterConstraintBuilder(TypeName);
}
[TestMethod]
public void Constructor_NullType_ThrowsArgumentNullException()
{
var action = () =>
{
_ = new TypeParameterConstraintBuilder(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void WithClass_ReturnsBuilder()
{
this.builder.WithClass().Should().BeOfType<TypeParameterConstraintBuilder>();
}
[TestMethod]
public void WithStruct_ReturnsBuilder()
{
this.builder.WithStruct().Should().BeOfType<TypeParameterConstraintBuilder>();
}
[TestMethod]
public void WithParameterlessConstructor_ReturnsBuilder()
{
this.builder.WithParameterlessConstructor().Should().BeOfType<TypeParameterConstraintBuilder>();
}
[TestMethod]
public void WithType_ReturnsBuilder()
{
this.builder.WithType(ConstraintType).Should().BeOfType<TypeParameterConstraintBuilder>();
}
[TestMethod]
public void WithType_NullType_ThrowsArgumentNullException()
{
var action = () =>
{
_ = this.builder.WithType(null);
};
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void Build_ReturnsPropertyDeclarationSyntax()
{
var syntax = this.builder.Build();
syntax.Should().NotBeNull().And.Subject.Should().BeOfType<TypeParameterConstraintClauseSyntax>();
}
[TestMethod]
public void Build_ReturnsExpectedString()
{
var syntax = this.builder
.WithClass()
.WithStruct()
.WithParameterlessConstructor()
.WithType(ConstraintType)
.Build();
var result = syntax.NormalizeWhitespace().ToFullString();
result.Should().Be(Expected);
}
}