upgrade tests project to .net6

This commit is contained in:
daver32
2022-07-04 02:37:21 +02:00
parent 501d92d878
commit 6ca8f4fe0e
6 changed files with 604 additions and 607 deletions
@@ -3,163 +3,163 @@ using FluentAssertions;
using FluentAssertions.Common;
using Xunit;
namespace InterfaceGenerator.Tests
namespace InterfaceGenerator.Tests;
public class AccessorsGenerationTests
{
public class AccessorsGenerationTests
private readonly IAccessorsTestsService _sut;
public AccessorsGenerationTests()
{
private readonly IAccessorsTestsService _sut;
_sut = new AccessorsTestsService();
}
public AccessorsGenerationTests()
[Fact]
public void GetSetIndexer_IsImplemented()
{
var indexer = typeof(IAccessorsTestsService).GetIndexerByParameterTypes(new[] { typeof(string) });
indexer.Should().NotBeNull();
indexer.GetMethod.Should().NotBeNull();
indexer.SetMethod.Should().NotBeNull();
var _ = _sut[string.Empty];
_sut[string.Empty] = 0;
}
[Fact]
public void PublicProperty_IsImplemented()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(IAccessorsTestsService.PublicProperty))!;
prop.Should().NotBeNull();
prop.GetMethod.Should().NotBeNull();
prop.SetMethod.Should().NotBeNull();
var _ = _sut.PublicProperty;
_sut.PublicProperty = string.Empty;
}
[Fact]
public void InitProperty_IsImplemented()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(IAccessorsTestsService.InitOnlyProperty))!;
prop.Should().NotBeNull();
prop.GetMethod.Should().NotBeNull();
prop.SetMethod.Should().NotBeNull();
prop.SetMethod!.ReturnParameter!.GetRequiredCustomModifiers().Should().Contain(typeof(IsExternalInit));
var _ = _sut.InitOnlyProperty;
}
[Fact]
public void PrivateSetter_IsOmitted()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(IAccessorsTestsService.PropertyWithPrivateSetter))!;
prop.Should().NotBeNull();
prop.GetMethod.Should().NotBeNull();
prop.SetMethod.Should().BeNull();
var _ = _sut.PropertyWithPrivateSetter;
}
[Fact]
public void PrivateGetter_IsOmitted()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(IAccessorsTestsService.PropertyWithPrivateGetter))!;
prop.Should().NotBeNull();
prop.SetMethod.Should().NotBeNull();
prop.GetMethod.Should().BeNull();
_sut.PropertyWithPrivateGetter = string.Empty;
}
[Fact]
public void ProtectedSetter_IsOmitted()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(IAccessorsTestsService.PropertyWithProtectedSetter))!;
prop.Should().NotBeNull();
prop.GetMethod.Should().NotBeNull();
prop.SetMethod.Should().BeNull();
var _ = _sut.PropertyWithProtectedSetter;
}
[Fact]
public void ProtectedGetter_IsOmitted()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(IAccessorsTestsService.PropertyWithProtectedGetter))!;
prop.Should().NotBeNull();
prop.SetMethod.Should().NotBeNull();
prop.GetMethod.Should().BeNull();
_sut.PropertyWithProtectedGetter = string.Empty;
}
[Fact]
public void IgnoredProperty_IsOmitted()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(AccessorsTestsService.IgnoredProperty));
prop.Should().BeNull();
}
[Fact]
public void StaticProperty_IsOmitted()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(AccessorsTestsService.StaticProperty));
prop.Should().BeNull();
}
}
// ReSharper disable UnusedMember.Local, ValueParameterNotUsed
[GenerateAutoInterface]
internal class AccessorsTestsService : IAccessorsTestsService
{
public int this[string x]
{
get => 0;
set
{
_sut = new AccessorsTestsService();
}
[Fact]
public void GetSetIndexer_IsImplemented()
{
var indexer = typeof(IAccessorsTestsService).GetIndexerByParameterTypes(new[] { typeof(string) });
indexer.Should().NotBeNull();
indexer.GetMethod.Should().NotBeNull();
indexer.SetMethod.Should().NotBeNull();
int _ = _sut[string.Empty];
_sut[string.Empty] = 0;
}
[Fact]
public void PublicProperty_IsImplemented()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(IAccessorsTestsService.PublicProperty))!;
prop.Should().NotBeNull();
prop.GetMethod.Should().NotBeNull();
prop.SetMethod.Should().NotBeNull();
string _ = _sut.PublicProperty;
_sut.PublicProperty = string.Empty;
}
[Fact]
public void InitProperty_IsImplemented()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(IAccessorsTestsService.InitOnlyProperty))!;
prop.Should().NotBeNull();
prop.GetMethod.Should().NotBeNull();
prop.SetMethod.Should().NotBeNull();
prop.SetMethod!.ReturnParameter!.GetRequiredCustomModifiers().Should().Contain(typeof(IsExternalInit));
string _ = _sut.InitOnlyProperty;
}
[Fact]
public void PrivateSetter_IsOmitted()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(IAccessorsTestsService.PropertyWithPrivateSetter))!;
prop.Should().NotBeNull();
prop.GetMethod.Should().NotBeNull();
prop.SetMethod.Should().BeNull();
string _ = _sut.PropertyWithPrivateSetter;
}
[Fact]
public void PrivateGetter_IsOmitted()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(IAccessorsTestsService.PropertyWithPrivateGetter))!;
prop.Should().NotBeNull();
prop.SetMethod.Should().NotBeNull();
prop.GetMethod.Should().BeNull();
_sut.PropertyWithPrivateGetter = string.Empty;
}
[Fact]
public void ProtectedSetter_IsOmitted()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(IAccessorsTestsService.PropertyWithProtectedSetter))!;
prop.Should().NotBeNull();
prop.GetMethod.Should().NotBeNull();
prop.SetMethod.Should().BeNull();
string _ = _sut.PropertyWithProtectedSetter;
}
[Fact]
public void ProtectedGetter_IsOmitted()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(IAccessorsTestsService.PropertyWithProtectedGetter))!;
prop.Should().NotBeNull();
prop.SetMethod.Should().NotBeNull();
prop.GetMethod.Should().BeNull();
_sut.PropertyWithProtectedGetter = string.Empty;
}
[Fact]
public void IgnoredProperty_IsOmitted()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(AccessorsTestsService.IgnoredProperty));
prop.Should().BeNull();
}
[Fact]
public void StaticProperty_IsOmitted()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(AccessorsTestsService.StaticProperty));
prop.Should().BeNull();
}
}
// ReSharper disable UnusedMember.Local, ValueParameterNotUsed
[GenerateAutoInterface]
internal class AccessorsTestsService : IAccessorsTestsService
{
public int this[string x]
{
get => 0;
set { }
}
public string PublicProperty { get; set; }
public string InitOnlyProperty { get; init; }
public string PublicProperty { get; set; }
public string PropertyWithPrivateSetter { get; private set; }
public string PropertyWithPrivateGetter { private get; set; }
public string PropertyWithProtectedSetter { get; protected set; }
public string PropertyWithProtectedGetter { protected get; set; }
public string InitOnlyProperty { get; init; }
[AutoInterfaceIgnore]
public string IgnoredProperty { get; set; }
public static string StaticProperty { get; set; }
}
// ReSharper enable UnusedMember.Local, ValueParameterNotUsed
}
public string PropertyWithPrivateSetter { get; private set; }
public string PropertyWithPrivateGetter { private get; set; }
public string PropertyWithProtectedSetter { get; protected set; }
public string PropertyWithProtectedGetter { protected get; set; }
[AutoInterfaceIgnore] public string IgnoredProperty { get; set; }
public static string StaticProperty { get; set; }
}
// ReSharper enable UnusedMember.Local, ValueParameterNotUsed
@@ -3,35 +3,36 @@ using System.Reflection;
using FluentAssertions;
using Xunit;
namespace InterfaceGenerator.Tests
namespace InterfaceGenerator.Tests;
public class GenericInterfaceTests
{
public class GenericInterfaceTests
[Fact]
public void GenericParametersGeneratedCorrectly()
{
[Fact]
public void GenericParametersGeneratedCorrectly()
{
var genericArgs = typeof(IGenericInterfaceTestsService<,>).GetGenericArguments();
var genericArgs = typeof(IGenericInterfaceTestsService<,>).GetGenericArguments();
genericArgs.Should().HaveCount(2);
genericArgs[0].Name.Should().Be("TX");
genericArgs[1].Name.Should().Be("TY");
genericArgs.Should().HaveCount(2);
genericArgs[0].Name.Should().Be("TX");
genericArgs[1].Name.Should().Be("TY");
genericArgs[0].IsClass.Should().BeTrue();
genericArgs[0].GenericParameterAttributes
.Should().HaveFlag(GenericParameterAttributes.DefaultConstructorConstraint);
genericArgs[0].IsClass.Should().BeTrue();
genericArgs[0]
.GenericParameterAttributes
.Should()
.HaveFlag(GenericParameterAttributes.DefaultConstructorConstraint);
var iEquatableOfTx = typeof(IEquatable<>).MakeGenericType(genericArgs[0]);
genericArgs[0].GetGenericParameterConstraints().Should().HaveCount(1).And.Contain(iEquatableOfTx);
var iEquatableOfTx = typeof(IEquatable<>).MakeGenericType(genericArgs[0]);
genericArgs[0].GetGenericParameterConstraints().Should().HaveCount(1).And.Contain(iEquatableOfTx);
genericArgs[1].IsValueType.Should().BeTrue();
}
genericArgs[1].IsValueType.Should().BeTrue();
}
}
[GenerateAutoInterface]
// ReSharper disable once UnusedType.Global
internal class GenericInterfaceTestsService<TX, TY> : IGenericInterfaceTestsService<TX, TY>
where TX : class, IEquatable<TX>, new()
where TY : struct
{
}
[GenerateAutoInterface]
// ReSharper disable once UnusedType.Global
internal class GenericInterfaceTestsService<TX, TY> : IGenericInterfaceTestsService<TX, TY>
where TX : class, IEquatable<TX>, new()
where TY : struct
{
}
@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="FluentAssertions" Version="5.10.3"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\InterfaceGenerator\InterfaceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\InterfaceGenerator\InterfaceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>
</Project>
+323 -325
View File
@@ -5,341 +5,339 @@ using System.Text;
using FluentAssertions;
using Xunit;
namespace InterfaceGenerator.Tests
namespace InterfaceGenerator.Tests;
public class MethodGenerationTests
{
public class MethodGenerationTests
private readonly IMethodsTestService _sut;
public MethodGenerationTests()
{
private readonly IMethodsTestService _sut;
public MethodGenerationTests()
{
_sut = new MethodsTestService();
}
[Fact]
public void VoidMethod_IsImplemented()
{
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.VoidMethod))!;
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Should().BeEmpty();
_sut.VoidMethod();
}
[Fact]
public void VoidMethodWithKeywordParam_IsImplemented()
{
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.VoidMethodWithKeywordParam))!;
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Select(x => x.ParameterType).Should().AllBeEquivalentTo(typeof(string));
parameters.Should().HaveCount(1);
parameters[0].Name.Should().Be("void");
_sut.VoidMethodWithKeywordParam("");
}
[Fact]
public void VoidMethodWithParams_IsImplemented()
{
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.VoidMethodWithParams),
new[] { typeof(string), typeof(string) })!;
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Select(x => x.ParameterType).Should().AllBeEquivalentTo(typeof(string));
parameters.Should().HaveCount(2);
_sut.VoidMethodWithParams(string.Empty, string.Empty);
}
[Fact]
public void VoidMethodWithOutParam_IsImplemented()
{
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.VoidMethodWithOutParam),
new[] { typeof(string).MakeByRefType() })!;
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Select(x => x.ParameterType).Should().AllBeEquivalentTo(typeof(string).MakeByRefType());
parameters.Should().HaveCount(1);
parameters[0].IsOut.Should().BeTrue();
_sut.VoidMethodWithOutParam(out var _);
}
[Fact]
public void VoidMethodWithInParam_IsImplemented()
{
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.VoidMethodWithInParam),
new[] { typeof(string).MakeByRefType() })!;
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Select(x => x.ParameterType).Should().AllBeEquivalentTo(typeof(string).MakeByRefType());
parameters.Should().HaveCount(1);
parameters[0].IsIn.Should().BeTrue();
var stub = string.Empty;
_sut.VoidMethodWithInParam(in stub);
}
[Fact]
public void VoidMethodWithRefParam_IsImplemented()
{
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.VoidMethodWithRefParam),
new[] { typeof(string).MakeByRefType() })!;
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Select(x => x.ParameterType).Should().AllBeEquivalentTo(typeof(string).MakeByRefType());
parameters.Should().HaveCount(1);
parameters[0].IsIn.Should().BeFalse();
parameters[0].IsOut.Should().BeFalse();
var stub = string.Empty;
_sut.VoidMethodWithRefParam(ref stub);
}
[Fact]
public void StringMethod_IsImplemented()
{
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.StringMethod))!;
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(string));
var parameters = method.GetParameters();
parameters.Should().BeEmpty();
var _ = _sut.StringMethod();
}
[Fact]
public void GenericVoidMethod_IsImplemented()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.First(x => x.Name == nameof(MethodsTestService.GenericVoidMethod));
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
method.GetParameters().Should().BeEmpty();
var genericArgs = method.GetGenericArguments();
genericArgs.Should().HaveCount(2);
_sut.GenericVoidMethod<string, int>();
}
[Fact]
public void GenericVoidMethodWithGenericParam_IsImplemented()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.First(x => x.Name == nameof(MethodsTestService.GenericVoidMethodWithGenericParam));
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var genericArgs = method.GetGenericArguments();
genericArgs.Should().HaveCount(2);
var parameters = method.GetParameters();
parameters.Should().HaveCount(1);
parameters[0].ParameterType.Should().Be(genericArgs[0]);
_sut.GenericVoidMethodWithGenericParam<string, int>(string.Empty);
}
[Fact]
public void GenericVoidMethodWithConstraints_IsImplemented()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.First(x => x.Name == nameof(MethodsTestService.GenericVoidMethodWithConstraints));
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var genericArgs = method.GetGenericArguments();
genericArgs.Should().HaveCount(2);
genericArgs[0].IsClass.Should().BeTrue();
genericArgs[0].GetGenericParameterConstraints().Should().HaveCount(0);
genericArgs[1].IsClass.Should().BeTrue();
genericArgs[1].GetGenericParameterConstraints().Should().HaveCount(1);
genericArgs[1].GetGenericParameterConstraints()[0].Should().Be(genericArgs[0]);
genericArgs[1].GenericParameterAttributes.Should()
.HaveFlag(GenericParameterAttributes.DefaultConstructorConstraint);
_sut.GenericVoidMethodWithConstraints<object, StringBuilder>();
}
[Fact]
public void VoidMethodWithOptionalParams_IsImplemented()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.First(x => x.Name == nameof(MethodsTestService.VoidMethodWithOptionalParams));
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Should().HaveCount(7);
parameters.Select(x => x.IsOptional).Should().AllBeEquivalentTo(true);
parameters[0].DefaultValue.Should().Be("cGFyYW0=");
parameters[1].DefaultValue.Should().Be(MethodsTestService.StringConstant);
parameters[2].DefaultValue.Should().Be(0.1f);
parameters[3].DefaultValue.Should().Be(0.2d);
parameters[4].DefaultValue.Should().Be(0.3d);
parameters[5].DefaultValue.Should().Be(true);
parameters[6].DefaultValue.Should().Be(false);
_sut.VoidMethodWithOptionalParams();
}
[Fact]
public void VoidMethodWithExpandingParam_IsImplemented()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.First(x => x.Name == nameof(MethodsTestService.VoidMethodWithExpandingParam));
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Should().HaveCount(1);
parameters[0].ParameterType.Should().Be(typeof(string[]));
parameters[0].GetCustomAttribute<ParamArrayAttribute>().Should().NotBeNull();
}
[Fact]
public void IgnoreMethod_IsOmitted()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.FirstOrDefault(x => x.Name == nameof(MethodsTestService.IgnoredMethod));
method.Should().BeNull();
}
[Fact]
public void StaticMethod_IsOmitted()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.FirstOrDefault(x => x.Name == nameof(MethodsTestService.StaticMethod));
method.Should().BeNull();
}
_sut = new MethodsTestService();
}
[GenerateAutoInterface]
internal class MethodsTestService : IMethodsTestService
[Fact]
public void VoidMethod_IsImplemented()
{
public const string StringConstant = "Const";
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.VoidMethod))!;
public void VoidMethod()
{
}
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
public void VoidMethodWithParams(string a, string b)
{
}
public void VoidMethodWithKeywordParam(string @void)
{
}
var parameters = method.GetParameters();
parameters.Should().BeEmpty();
public void VoidMethodWithOutParam(out string a)
{
a = default;
}
public void VoidMethodWithRefParam(ref string a)
{
}
public void VoidMethodWithInParam(in string a)
{
}
public string StringMethod()
{
return string.Empty;
}
public void GenericVoidMethod<TX, TY>()
{
}
public void GenericVoidMethodWithGenericParam<TX, TY>(TX a)
{
}
public void GenericVoidMethodWithConstraints<TX, TY>()
where TX : class
where TY : class, TX, new()
{
}
public void VoidMethodWithOptionalParams(
string stringLiteral = "cGFyYW0=",
string stringConstant = StringConstant,
float floatLiteral = 0.1f,
double doubleLiteral = 0.2,
decimal decimalLiteral = 0.3m,
bool trueLiteral = true,
bool falseLiteral = false)
{
}
public void VoidMethodWithExpandingParam(params string[] strings)
{
}
[AutoInterfaceIgnore]
public void IgnoredMethod()
{
}
public static void StaticMethod()
{
}
_sut.VoidMethod();
}
[GenerateAutoInterface]
internal class MethodsTestServiceGeneric<T> : IMethodsTestServiceGeneric<T> where T : class
[Fact]
public void VoidMethodWithKeywordParam_IsImplemented()
{
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.VoidMethodWithKeywordParam))!;
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Select(x => x.ParameterType).Should().AllBeEquivalentTo(typeof(string));
parameters.Should().HaveCount(1);
parameters[0].Name.Should().Be("void");
_sut.VoidMethodWithKeywordParam("");
}
[Fact]
public void VoidMethodWithParams_IsImplemented()
{
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.VoidMethodWithParams),
new[] { typeof(string), typeof(string) })!;
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Select(x => x.ParameterType).Should().AllBeEquivalentTo(typeof(string));
parameters.Should().HaveCount(2);
_sut.VoidMethodWithParams(string.Empty, string.Empty);
}
[Fact]
public void VoidMethodWithOutParam_IsImplemented()
{
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.VoidMethodWithOutParam),
new[] { typeof(string).MakeByRefType() })!;
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Select(x => x.ParameterType).Should().AllBeEquivalentTo(typeof(string).MakeByRefType());
parameters.Should().HaveCount(1);
parameters[0].IsOut.Should().BeTrue();
_sut.VoidMethodWithOutParam(out var _);
}
[Fact]
public void VoidMethodWithInParam_IsImplemented()
{
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.VoidMethodWithInParam),
new[] { typeof(string).MakeByRefType() })!;
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Select(x => x.ParameterType).Should().AllBeEquivalentTo(typeof(string).MakeByRefType());
parameters.Should().HaveCount(1);
parameters[0].IsIn.Should().BeTrue();
var stub = string.Empty;
_sut.VoidMethodWithInParam(in stub);
}
[Fact]
public void VoidMethodWithRefParam_IsImplemented()
{
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.VoidMethodWithRefParam),
new[] { typeof(string).MakeByRefType() })!;
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Select(x => x.ParameterType).Should().AllBeEquivalentTo(typeof(string).MakeByRefType());
parameters.Should().HaveCount(1);
parameters[0].IsIn.Should().BeFalse();
parameters[0].IsOut.Should().BeFalse();
var stub = string.Empty;
_sut.VoidMethodWithRefParam(ref stub);
}
[Fact]
public void StringMethod_IsImplemented()
{
var method = typeof(IMethodsTestService).GetMethod(
nameof(MethodsTestService.StringMethod))!;
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(string));
var parameters = method.GetParameters();
parameters.Should().BeEmpty();
var _ = _sut.StringMethod();
}
[Fact]
public void GenericVoidMethod_IsImplemented()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.First(x => x.Name == nameof(MethodsTestService.GenericVoidMethod));
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
method.GetParameters().Should().BeEmpty();
var genericArgs = method.GetGenericArguments();
genericArgs.Should().HaveCount(2);
_sut.GenericVoidMethod<string, int>();
}
[Fact]
public void GenericVoidMethodWithGenericParam_IsImplemented()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.First(x => x.Name == nameof(MethodsTestService.GenericVoidMethodWithGenericParam));
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var genericArgs = method.GetGenericArguments();
genericArgs.Should().HaveCount(2);
var parameters = method.GetParameters();
parameters.Should().HaveCount(1);
parameters[0].ParameterType.Should().Be(genericArgs[0]);
_sut.GenericVoidMethodWithGenericParam<string, int>(string.Empty);
}
[Fact]
public void GenericVoidMethodWithConstraints_IsImplemented()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.First(x => x.Name == nameof(MethodsTestService.GenericVoidMethodWithConstraints));
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var genericArgs = method.GetGenericArguments();
genericArgs.Should().HaveCount(2);
genericArgs[0].IsClass.Should().BeTrue();
genericArgs[0].GetGenericParameterConstraints().Should().HaveCount(0);
genericArgs[1].IsClass.Should().BeTrue();
genericArgs[1].GetGenericParameterConstraints().Should().HaveCount(1);
genericArgs[1].GetGenericParameterConstraints()[0].Should().Be(genericArgs[0]);
genericArgs[1]
.GenericParameterAttributes.Should()
.HaveFlag(GenericParameterAttributes.DefaultConstructorConstraint);
_sut.GenericVoidMethodWithConstraints<object, StringBuilder>();
}
[Fact]
public void VoidMethodWithOptionalParams_IsImplemented()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.First(x => x.Name == nameof(MethodsTestService.VoidMethodWithOptionalParams));
method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Should().HaveCount(7);
parameters.Select(x => x.IsOptional).Should().AllBeEquivalentTo(true);
parameters[0].DefaultValue.Should().Be("cGFyYW0=");
parameters[1].DefaultValue.Should().Be(MethodsTestService.StringConstant);
parameters[2].DefaultValue.Should().Be(0.1f);
parameters[3].DefaultValue.Should().Be(0.2d);
parameters[4].DefaultValue.Should().Be(0.3d);
parameters[5].DefaultValue.Should().Be(true);
parameters[6].DefaultValue.Should().Be(false);
_sut.VoidMethodWithOptionalParams();
}
[Fact]
public void VoidMethodWithExpandingParam_IsImplemented()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.First(x => x.Name == nameof(MethodsTestService.VoidMethodWithExpandingParam));
method.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Should().HaveCount(1);
parameters[0].ParameterType.Should().Be(typeof(string[]));
parameters[0].GetCustomAttribute<ParamArrayAttribute>().Should().NotBeNull();
}
[Fact]
public void IgnoreMethod_IsOmitted()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.FirstOrDefault(x => x.Name == nameof(MethodsTestService.IgnoredMethod));
method.Should().BeNull();
}
[Fact]
public void StaticMethod_IsOmitted()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.FirstOrDefault(x => x.Name == nameof(MethodsTestService.StaticMethod));
method.Should().BeNull();
}
}
[GenerateAutoInterface]
internal class MethodsTestService : IMethodsTestService
{
public const string StringConstant = "Const";
public void VoidMethod()
{
}
public void VoidMethodWithParams(string a, string b)
{
}
public void VoidMethodWithKeywordParam(string @void)
{
}
public void VoidMethodWithOutParam(out string a)
{
a = default;
}
public void VoidMethodWithRefParam(ref string a)
{
}
public void VoidMethodWithInParam(in string a)
{
}
public string StringMethod()
{
return string.Empty;
}
public void GenericVoidMethod<TX, TY>()
{
}
public void GenericVoidMethodWithGenericParam<TX, TY>(TX a)
{
}
public void GenericVoidMethodWithConstraints<TX, TY>()
where TX : class
where TY : class, TX, new()
{
}
public void VoidMethodWithOptionalParams(
string stringLiteral = "cGFyYW0=",
string stringConstant = StringConstant,
float floatLiteral = 0.1f,
double doubleLiteral = 0.2,
decimal decimalLiteral = 0.3m,
bool trueLiteral = true,
bool falseLiteral = false)
{
}
public void VoidMethodWithExpandingParam(params string[] strings)
{
}
[AutoInterfaceIgnore]
public void IgnoredMethod()
{
}
public static void StaticMethod()
{
}
}
[GenerateAutoInterface]
internal class MethodsTestServiceGeneric<T> : IMethodsTestServiceGeneric<T> where T : class
{
}
@@ -2,70 +2,69 @@ using System.Runtime.CompilerServices;
using FluentAssertions;
using Xunit;
namespace InterfaceGenerator.Tests
namespace InterfaceGenerator.Tests;
public class RecordInterfaceGenerationTests
{
public class RecordInterfaceGenerationTests
private readonly ITestRecord _sut;
public RecordInterfaceGenerationTests()
{
private readonly ITestRecord _sut;
public RecordInterfaceGenerationTests()
{
_sut = new TestRecord(420);
}
[Fact]
public void RecordProperty_IsGenerated()
{
var prop = typeof(ITestRecord)
.GetProperty(nameof(TestRecord.RecordProperty))!;
prop.Should().NotBeNull();
prop.GetMethod.Should().NotBeNull();
prop.SetMethod.Should().NotBeNull();
prop.SetMethod!.ReturnParameter!.GetRequiredCustomModifiers().Should().Contain(typeof(IsExternalInit));
_sut.RecordProperty.Should().Be(420);
}
[Fact]
public void RecordMethod_IsGenerated()
{
var method = typeof(ITestRecord).GetMethod(
nameof(TestRecord.RecordMethod));
method.Should().NotBeNull();
method!.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Should().BeEmpty();
_sut.RecordMethod();
}
[Fact]
public void Deconstruct_IsGenerated()
{
var method = typeof(ITestRecord).GetMethod(
nameof(TestRecord.Deconstruct));
method.Should().NotBeNull();
method!.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Length.Should().Be(1);
var parameter = parameters[0];
parameter.ParameterType.Should().Be(typeof(int).MakeByRefType());
parameter.IsOut.Should().BeTrue();
}
_sut = new TestRecord(420);
}
[GenerateAutoInterface]
internal record TestRecord(int RecordProperty) : ITestRecord
[Fact]
public void RecordProperty_IsGenerated()
{
var prop = typeof(ITestRecord)
.GetProperty(nameof(TestRecord.RecordProperty))!;
prop.Should().NotBeNull();
prop.GetMethod.Should().NotBeNull();
prop.SetMethod.Should().NotBeNull();
prop.SetMethod!.ReturnParameter!.GetRequiredCustomModifiers().Should().Contain(typeof(IsExternalInit));
_sut.RecordProperty.Should().Be(420);
}
[Fact]
public void RecordMethod_IsGenerated()
{
var method = typeof(ITestRecord).GetMethod(
nameof(TestRecord.RecordMethod));
method.Should().NotBeNull();
method!.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Should().BeEmpty();
_sut.RecordMethod();
}
[Fact]
public void Deconstruct_IsGenerated()
{
var method = typeof(ITestRecord).GetMethod(
nameof(TestRecord.Deconstruct));
method.Should().NotBeNull();
method!.ReturnType.Should().Be(typeof(void));
var parameters = method.GetParameters();
parameters.Length.Should().Be(1);
var parameter = parameters[0];
parameter.ParameterType.Should().Be(typeof(int).MakeByRefType());
parameter.IsOut.Should().BeTrue();
}
}
[GenerateAutoInterface]
internal record TestRecord(int RecordProperty) : ITestRecord
{
public void RecordMethod()
{
public void RecordMethod()
{
}
}
}
@@ -2,56 +2,55 @@
using FluentAssertions;
using Xunit;
namespace InterfaceGenerator.Tests
namespace InterfaceGenerator.Tests;
public class VisibilityModifierTests
{
public class VisibilityModifierTests
[Fact]
public void IExplicitlyPublicService_IsPublic()
{
[Fact]
public void IExplicitlyPublicService_IsPublic()
{
var type = typeof(IExplicitlyPublicService);
type.Attributes.Should().HaveFlag(TypeAttributes.Public);
}
[Fact]
public void IExplicitlyInternalService_IsInternal()
{
var type = typeof(IExplicitlyInternalService);
type.Attributes.Should().HaveFlag(TypeAttributes.NotPublic);
}
[Fact]
public void IImplicitlyPublicService_IsPublic()
{
var type = typeof(IImplicitlyPublicService);
type.Attributes.Should().HaveFlag(TypeAttributes.Public);
}
[Fact]
public void IImplicitlyInternalService_IsInternal()
{
var type = typeof(IImplicitlyInternalService);
type.Attributes.Should().HaveFlag(TypeAttributes.NotPublic);
}
var type = typeof(IExplicitlyPublicService);
type.Attributes.Should().HaveFlag(TypeAttributes.Public);
}
[GenerateAutoInterface(VisibilityModifier = "public")]
internal class ExplicitlyPublicService : IExplicitlyPublicService
[Fact]
public void IExplicitlyInternalService_IsInternal()
{
var type = typeof(IExplicitlyInternalService);
type.Attributes.Should().HaveFlag(TypeAttributes.NotPublic);
}
[GenerateAutoInterface(VisibilityModifier = "internal")]
public class ExplicitlyInternalService : IExplicitlyInternalService
[Fact]
public void IImplicitlyPublicService_IsPublic()
{
var type = typeof(IImplicitlyPublicService);
type.Attributes.Should().HaveFlag(TypeAttributes.Public);
}
[GenerateAutoInterface]
public class ImplicitlyPublicService : IImplicitlyPublicService
{
}
[GenerateAutoInterface]
internal class ImplicitlyInternalService : IImplicitlyInternalService
[Fact]
public void IImplicitlyInternalService_IsInternal()
{
var type = typeof(IImplicitlyInternalService);
type.Attributes.Should().HaveFlag(TypeAttributes.NotPublic);
}
}
[GenerateAutoInterface(VisibilityModifier = "public")]
internal class ExplicitlyPublicService : IExplicitlyPublicService
{
}
[GenerateAutoInterface(VisibilityModifier = "internal")]
public class ExplicitlyInternalService : IExplicitlyInternalService
{
}
[GenerateAutoInterface]
public class ImplicitlyPublicService : IImplicitlyPublicService
{
}
[GenerateAutoInterface]
internal class ImplicitlyInternalService : IImplicitlyInternalService
{
}