public async Task GetAttributes_OnNewMethodOfVirtualBaseMethod() { // Arrange var compilation = await GetCompilation("GetAttributes_WithNewMethod"); var attribute = compilation.GetTypeByMetadataName(typeof(ProducesResponseTypeAttribute).FullName); var testClass = compilation.GetTypeByMetadataName($"{Namespace}.{nameof(GetAttributes_WithNewMethodDerived)}"); var method = (IMethodSymbol)testClass.GetMembers(nameof(GetAttributes_WithNewMethodDerived.VirtualMethod)).First(); // Act var attributes = CodeAnalysisExtensions.GetAttributes(method, attribute, inherit: true); // Assert Assert.Collection( attributes, attributeData => Assert.Equal(400, attributeData.ConstructorArguments[0].Value)); }
public async Task GetAttributesSymbolOverload_OnMethodSymbol() { // Arrange var compilation = await GetCompilation("GetAttributes_WithMethodOverridding"); var attribute = compilation.GetTypeByMetadataName(typeof(ProducesResponseTypeAttribute).FullName); var testClass = compilation.GetTypeByMetadataName($"{Namespace}.{nameof(GetAttributes_WithInheritFalse_ReturnsAllAttributesOnCurrentActionClass)}"); var method = (IMethodSymbol)testClass.GetMembers(nameof(GetAttributes_WithInheritFalse_ReturnsAllAttributesOnCurrentActionClass.Method)).First(); // Act var attributes = CodeAnalysisExtensions.GetAttributes(symbol: method, attribute: attribute); // Assert Assert.Collection( attributes, attributeData => Assert.Equal(400, attributeData.ConstructorArguments[0].Value)); }
public async Task GetAttributesSymbolOverload_OnTypeSymbol() { // Arrange var compilation = await GetCompilation(nameof(GetAttributes_BaseTypeWithAttributes)); var attribute = compilation.GetTypeByMetadataName(typeof(ApiConventionTypeAttribute).FullName); var testClass = compilation.GetTypeByMetadataName(typeof(GetAttributes_BaseTypeWithAttributesDerived).FullName); // Act var attributes = CodeAnalysisExtensions.GetAttributes(symbol: testClass, attribute: attribute); // Assert Assert.Collection( attributes, attributeData => { Assert.Same(attribute, attributeData.AttributeClass); Assert.Equal(attributeData.ConstructorArguments[0].Value, compilation.GetSpecialType(SpecialType.System_Int32)); }); }
public void GetAttributes_OnNewMethodOfNonVirtualBaseMethod() { // Arrange var source = @" using Microsoft.AspNetCore.Mvc; namespace TestApp { public class BaseClass { [ProducesResponseType(200)] [ProducesResponseType(404)] public virtual void VirtualMethod() { } [ProducesResponseType(200)] [ProducesResponseType(404)] public virtual void NotVirtualMethod() { } } public class TestClass : BaseClass { [ProducesResponseType(400)] public new void VirtualMethod() { } [ProducesResponseType(401)] public new void NotVirtualMethod() { } } }"; var compilation = TestCompilation.Create(source); var attribute = compilation.GetTypeByMetadataName(typeof(ProducesResponseTypeAttribute).FullName); var testClass = compilation.GetTypeByMetadataName("TestApp.TestClass"); var method = (IMethodSymbol)testClass.GetMembers("NotVirtualMethod").First(); // Act var attributes = CodeAnalysisExtensions.GetAttributes(method, attribute, inherit: true); // Assert Assert.Collection( attributes, attributeData => Assert.Equal(401, attributeData.ConstructorArguments[0].Value)); }
public void GetAttributes_OnTypeWithoutAttributes() { // Arrange var source = @" using Microsoft.AspNetCore.Mvc; namespace TestApp { public class TestClass { } }"; var compilation = TestCompilation.Create(source); var attribute = compilation.GetTypeByMetadataName(typeof(ApiConventionTypeAttribute).FullName); var testClass = compilation.GetTypeByMetadataName("TestApp.TestClass"); // Act var attributes = CodeAnalysisExtensions.GetAttributes(testClass, attribute, inherit: true); // Assert Assert.Empty(attributes); }
public void GetAttributes_WithInheritTrue_ReturnsAllAttributesOnCurrentActionAndOverridingMethod() { /// Arrange var source = @" using Microsoft.AspNetCore.Mvc; namespace TestApp { public class BaseClass { [ProducesResponseType(200)] [ProducesResponseType(404)] public virtual void Method() { } } public class TestClass : BaseClass { [ProducesResponseType(400)] public override void Method() { } } } "; var compilation = TestCompilation.Create(source); var attribute = compilation.GetTypeByMetadataName(typeof(ProducesResponseTypeAttribute).FullName); var testClass = compilation.GetTypeByMetadataName("TestApp.TestClass"); var method = (IMethodSymbol)testClass.GetMembers("Method").First(); // Act var attributes = CodeAnalysisExtensions.GetAttributes(method, attribute, inherit: true); // Assert Assert.Collection( attributes, attributeData => Assert.Equal(400, attributeData.ConstructorArguments[0].Value), attributeData => Assert.Equal(200, attributeData.ConstructorArguments[0].Value), attributeData => Assert.Equal(404, attributeData.ConstructorArguments[0].Value)); }
public void GetAttributes_OnMethodWithoutAttributes() { // Arrange var source = @" namespace TestApp { public class TestController { public void Method() { } } }"; var compilation = TestCompilation.Create(source); var attribute = compilation.GetTypeByMetadataName(typeof(ProducesResponseTypeAttribute).FullName); var testClass = compilation.GetTypeByMetadataName("TestApp.TestController"); var method = (IMethodSymbol)testClass.GetMembers("Method").First(); // Act var attributes = CodeAnalysisExtensions.GetAttributes(method, attribute, inherit: true); // Assert Assert.Empty(attributes); }
public void GetAttributesSymbolOverload_OnTypeSymbol() { // Arrange var source = @" using Microsoft.AspNetCore.Mvc; namespace TestApp { [ApiConventionType(typeof(object))] [ApiController] [ApiConventionType(typeof(string))] public class BaseType { } [ApiConventionType(typeof(int))] public class TestClass : BaseType { } }"; var compilation = TestCompilation.Create(source); var attribute = compilation.GetTypeByMetadataName(typeof(ApiConventionTypeAttribute).FullName); var testClass = compilation.GetTypeByMetadataName("TestApp.TestClass"); // Act var attributes = CodeAnalysisExtensions.GetAttributes(symbol: testClass, attribute: attribute); // Assert Assert.Collection( attributes, attributeData => { Assert.Same(attribute, attributeData.AttributeClass); Assert.Equal(attributeData.ConstructorArguments[0].Value, compilation.GetSpecialType(SpecialType.System_Int32)); }); }