diff --git a/PCTTools.Sample/SAssemblyCatalog/Misc/InterfaceDemo.cs b/PCTTools.Sample/SAssemblyCatalog/Misc/InterfaceDemo.cs new file mode 100644 index 0000000..e72a3a7 --- /dev/null +++ b/PCTTools.Sample/SAssemblyCatalog/Misc/InterfaceDemo.cs @@ -0,0 +1,22 @@ +namespace PCTTools.Sample.SAssemblyCatalog.Misc +{ + public interface IParentInterface + { + string ParentProp { get; set; } + } + + public interface IParentBInterface + { + string ParentProp { get; set; } + } + + public interface IChildInterface : IParentInterface + { + string ChildProp { get; set; } + } + + public interface IChildMultiParentInterface : IParentInterface, IParentBInterface + { + string ChildProp { get; set; } + } +} \ No newline at end of file diff --git a/PCTTools.Tests/TAssemblyCatalog/BaseTypeTests.cs b/PCTTools.Tests/TAssemblyCatalog/BaseTypeTests.cs new file mode 100644 index 0000000..64dac43 --- /dev/null +++ b/PCTTools.Tests/TAssemblyCatalog/BaseTypeTests.cs @@ -0,0 +1,41 @@ +using NUnit.Framework; +using PCTTools.Sample.SAssemblyCatalog.Misc; + +namespace PCTTools.Tests.TAssemblyCatalog +{ + + [TestFixture()] + public class BaseTypeTests + { + [Test()] + public void InterfaceBaseTypesTest() + { + var pct = new AssemblyCatalog(); + + pct.GenerateDocumentationFromAssembly(typeof(IParentInterface).Assembly); + Assert.That(pct.HasError, Is.False); + + var childMultiParentInterface = pct.TypeDocumentations.First(x => x.IsInterface && x.ShortName == nameof(IChildMultiParentInterface)); + Assert.That(childMultiParentInterface.BaseTypes.Count, Is.EqualTo(2), $"{nameof(IChildMultiParentInterface)} should have 2 parents"); + Assert.That(childMultiParentInterface.BaseTypes, Does.Contain(typeof(IParentInterface).FullName)); + Assert.That(childMultiParentInterface.BaseTypes, Does.Contain(typeof(IParentBInterface).FullName)); + + var childParentInterface = pct.TypeDocumentations.First(x => x.IsInterface && x.ShortName == nameof(IChildInterface)); + Assert.That(childParentInterface.BaseTypes.Count, Is.EqualTo(1), $"{nameof(IChildInterface)} should have 1 parent"); + Assert.That(childParentInterface.BaseTypes, Does.Contain(typeof(IParentInterface).FullName)); + + var doctype = pct.TypeDocumentations.First(t => t.Name.Equals( + typeof(IParentInterface).FullName)); + + Assert.That(doctype.IsInterface, Is.EqualTo(true), "IParentInterface should be an interface"); + Assert.That(doctype.BaseTypes, Is.Not.Null, "BaseTypes should not be null"); + Assert.That(doctype.BaseTypes, Is.Empty, "An interface with no parent should have no BaseTypes"); + + doctype = pct.TypeDocumentations.First(t => t.Name.Equals( + typeof(IChildInterface).FullName)); + + Assert.That(doctype.IsInterface, Is.EqualTo(true), "IChildInterface should be an interface"); + Assert.That(doctype.BaseTypes, Is.Not.Null, "BaseTypes should not be null"); + } + } +} \ No newline at end of file diff --git a/PCTTools/AssemblyCatalog.cs b/PCTTools/AssemblyCatalog.cs index df971d7..6f3e565 100644 --- a/PCTTools/AssemblyCatalog.cs +++ b/PCTTools/AssemblyCatalog.cs @@ -411,12 +411,22 @@ internal List GetBaseTypes(Type type) { var baseTypes = new List(); - var baseType = type.BaseType; - while (baseType != null) + if (type.IsInterface) { - baseTypes.Add(baseType.FullName); + foreach (var iface in type.GetInterfaces()) + { + baseTypes.Add(iface.FullName); + } + } + else + { + var baseType = type.BaseType; + while (baseType != null) + { + baseTypes.Add(baseType.FullName); - baseType = baseType.BaseType; + baseType = baseType.BaseType; + } } return baseTypes; }