public void GetAttributes_BaseTypeWithAttributes() { // 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(testClass, attribute, inherit: true); // Assert Assert.Collection( attributes, attributeData => { Assert.Same(attribute, attributeData.AttributeClass); Assert.Equal(attributeData.ConstructorArguments[0].Value, compilation.GetSpecialType(SpecialType.System_Int32)); }, attributeData => { Assert.Same(attribute, attributeData.AttributeClass); Assert.Equal(attributeData.ConstructorArguments[0].Value, compilation.GetSpecialType(SpecialType.System_Object)); }, attributeData => { Assert.Same(attribute, attributeData.AttributeClass); Assert.Equal(attributeData.ConstructorArguments[0].Value, compilation.GetSpecialType(SpecialType.System_String)); }); }
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); }