Support raw arguments for attributes

This commit is contained in:
2024-10-18 18:49:31 +02:00
parent ed154276e2
commit 6c62a7a584
6 changed files with 36 additions and 11 deletions
+3
View File
@@ -35,6 +35,7 @@ public sealed class TestClass<T> : BaseTestClass, IInterface where T : new(), cl
return this.fieldString;
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization | MethodImplOptions.PreserveSig)]
public string GetSomething();
}";
@@ -101,6 +102,8 @@ public sealed class TestClass<T> : BaseTestClass, IInterface where T : new(), cl
.WithMethod(
SyntaxBuilder.CreateMethod("string", "GetSomething")
.WithModifier("public")
.WithAttribute(SyntaxBuilder.CreateAttribute("MethodImpl")
.WithRawArgument("MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization | MethodImplOptions.PreserveSig"))
.WithNoBody())))
.Build();
@@ -8,11 +8,14 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.5.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
+10
View File
@@ -10,6 +10,7 @@ public class AttributeBuilderTests
private const string AttributeName = "SomeAttribute";
private const string PropertyName = "SomeProperty";
private const string StringValue = "SomeString";
private const string RawArgument = "MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization | MethodImplOptions.PreserveSig";
private const int IntValue = 1;
private const uint UintValue = 1U;
private const long LongValue = 1L;
@@ -51,6 +52,7 @@ public class AttributeBuilderTests
private const string AttributeWithBoolTrueValue = $"{AttributeName}(true)";
private const string AttributeWithBoolFalseValue = $"{AttributeName}(false)";
private const string AttributeWithNullValue = $"{AttributeName}(null)";
private const string AttributeWithRawArgument = $"{AttributeName}(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization | MethodImplOptions.PreserveSig)";
private readonly AttributeBuilder attributeBuilder = SyntaxBuilder.CreateAttribute(AttributeName);
@@ -433,4 +435,12 @@ public class AttributeBuilderTests
result.Should().Be(AttributeWithNullValue);
}
[TestMethod]
public void WithRawArgument_WithRawValue_ReturnsExpected()
{
var result = this.attributeBuilder.WithRawArgument(RawArgument).Build().ToFullString();
result.Should().Be(AttributeWithRawArgument);
}
}
+5 -5
View File
@@ -8,11 +8,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.5.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
+9
View File
@@ -408,6 +408,15 @@ namespace Sybil
return this;
}
public AttributeBuilder WithRawArgument(string rawArgument)
{
this.AttributeSyntax = this.AttributeSyntax.AddArgumentListArguments(
SyntaxFactory.AttributeArgument(
SyntaxFactory.ParseExpression(rawArgument)));
return this;
}
public AttributeSyntax Build()
{
return this.AttributeSyntax
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.8.3</Version>
<Version>0.8.4</Version>
</PropertyGroup>
<PropertyGroup>