public void IsAssignable_ReturnsTrueIfAncestorTypeImplementsInterface() { // Arrange var source = @" namespace TestApp { public interface ITestInterface { } public class ITestInterfaceA : ITestInterface { } public class ITestInterfaceB : ITestInterfaceA { } public class TestClass : ITestInterfaceB { }"; var compilation = TestCompilation.Create(source); var sourceType = compilation.GetTypeByMetadataName("TestApp.ITestInterface"); var target = compilation.GetTypeByMetadataName("TestApp.TestClass"); // Act var isAssignableFrom = CodeAnalysisExtensions.IsAssignableFrom(sourceType, target); var isAssignableFromDerived = CodeAnalysisExtensions.IsAssignableFrom(target, sourceType); // Assert Assert.True(isAssignableFrom); Assert.False(isAssignableFromDerived); // Inverse shouldn't be true }
public void IsAssignable_ReturnsTrue_IfSourceAndDestinationAreTheSameInterface() { // Arrange var source = @" namespace TestApp { public interface ITestInterface { } }"; var compilation = TestCompilation.Create(source); var sourceType = compilation.GetTypeByMetadataName("TestApp.ITestInterface"); var target = compilation.GetTypeByMetadataName("TestApp.ITestInterface"); // Act var isAssignableFrom = CodeAnalysisExtensions.IsAssignableFrom(sourceType, target); // Assert Assert.True(isAssignableFrom); }
public void IsAssignable_ReturnsFalseIfTypeDoesNotImplementInterface() { // Arrange var source = @" namespace TestApp { public class TypeA { } }"; var compilation = TestCompilation.Create(source); var sourceType = compilation.GetTypeByMetadataName("TestApp.TypeA"); var target = compilation.GetTypeByMetadataName("System.IDisposable"); // Act var isAssignableFrom = CodeAnalysisExtensions.IsAssignableFrom(sourceType, target); // Assert Assert.False(isAssignableFrom); }
public void IsAssignable_ReturnsTrueIfTypesAreExact() { // Arrange var source = @" namespace TestApp { public class TestType { } }"; var compilation = TestCompilation.Create(source); var sourceType = compilation.GetTypeByMetadataName("TestApp.TestType"); var target = compilation.GetTypeByMetadataName("TestApp.TestType"); // Act var isAssignableFrom = CodeAnalysisExtensions.IsAssignableFrom(sourceType, target); // Assert Assert.True(isAssignableFrom); }
public void IsAssignable_ReturnsFalseForDifferentTypes() { // Arrange var source = @" namespace TestApp { public class TypeA { } public class TypeB { } }"; var compilation = TestCompilation.Create(source); var sourceType = compilation.GetTypeByMetadataName("TestApp.TypeA"); var target = compilation.GetTypeByMetadataName("TestApp.TypeB"); // Act var isAssignableFrom = CodeAnalysisExtensions.IsAssignableFrom(sourceType, target); // Assert Assert.False(isAssignableFrom); }