Skip to content

Commit 228cd73

Browse files
simondrumSimon Bernard
andauthored
Fix: Interfaces should have their interfaces as basetype (#75)
* Fix: Interfaces should have their interfaces as basetype * fix english --------- Co-authored-by: Simon Bernard <sbernard@proginov.com>
1 parent e86876c commit 228cd73

3 files changed

Lines changed: 77 additions & 4 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace PCTTools.Sample.SAssemblyCatalog.Misc
2+
{
3+
public interface IParentInterface
4+
{
5+
string ParentProp { get; set; }
6+
}
7+
8+
public interface IParentBInterface
9+
{
10+
string ParentProp { get; set; }
11+
}
12+
13+
public interface IChildInterface : IParentInterface
14+
{
15+
string ChildProp { get; set; }
16+
}
17+
18+
public interface IChildMultiParentInterface : IParentInterface, IParentBInterface
19+
{
20+
string ChildProp { get; set; }
21+
}
22+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using NUnit.Framework;
2+
using PCTTools.Sample.SAssemblyCatalog.Misc;
3+
4+
namespace PCTTools.Tests.TAssemblyCatalog
5+
{
6+
7+
[TestFixture()]
8+
public class BaseTypeTests
9+
{
10+
[Test()]
11+
public void InterfaceBaseTypesTest()
12+
{
13+
var pct = new AssemblyCatalog();
14+
15+
pct.GenerateDocumentationFromAssembly(typeof(IParentInterface).Assembly);
16+
Assert.That(pct.HasError, Is.False);
17+
18+
var childMultiParentInterface = pct.TypeDocumentations.First(x => x.IsInterface && x.ShortName == nameof(IChildMultiParentInterface));
19+
Assert.That(childMultiParentInterface.BaseTypes.Count, Is.EqualTo(2), $"{nameof(IChildMultiParentInterface)} should have 2 parents");
20+
Assert.That(childMultiParentInterface.BaseTypes, Does.Contain(typeof(IParentInterface).FullName));
21+
Assert.That(childMultiParentInterface.BaseTypes, Does.Contain(typeof(IParentBInterface).FullName));
22+
23+
var childParentInterface = pct.TypeDocumentations.First(x => x.IsInterface && x.ShortName == nameof(IChildInterface));
24+
Assert.That(childParentInterface.BaseTypes.Count, Is.EqualTo(1), $"{nameof(IChildInterface)} should have 1 parent");
25+
Assert.That(childParentInterface.BaseTypes, Does.Contain(typeof(IParentInterface).FullName));
26+
27+
var doctype = pct.TypeDocumentations.First(t => t.Name.Equals(
28+
typeof(IParentInterface).FullName));
29+
30+
Assert.That(doctype.IsInterface, Is.EqualTo(true), "IParentInterface should be an interface");
31+
Assert.That(doctype.BaseTypes, Is.Not.Null, "BaseTypes should not be null");
32+
Assert.That(doctype.BaseTypes, Is.Empty, "An interface with no parent should have no BaseTypes");
33+
34+
doctype = pct.TypeDocumentations.First(t => t.Name.Equals(
35+
typeof(IChildInterface).FullName));
36+
37+
Assert.That(doctype.IsInterface, Is.EqualTo(true), "IChildInterface should be an interface");
38+
Assert.That(doctype.BaseTypes, Is.Not.Null, "BaseTypes should not be null");
39+
}
40+
}
41+
}

PCTTools/AssemblyCatalog.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,22 @@ internal List<string> GetBaseTypes(Type type)
411411
{
412412
var baseTypes = new List<string>();
413413

414-
var baseType = type.BaseType;
415-
while (baseType != null)
414+
if (type.IsInterface)
416415
{
417-
baseTypes.Add(baseType.FullName);
416+
foreach (var iface in type.GetInterfaces())
417+
{
418+
baseTypes.Add(iface.FullName);
419+
}
420+
}
421+
else
422+
{
423+
var baseType = type.BaseType;
424+
while (baseType != null)
425+
{
426+
baseTypes.Add(baseType.FullName);
418427

419-
baseType = baseType.BaseType;
428+
baseType = baseType.BaseType;
429+
}
420430
}
421431
return baseTypes;
422432
}

0 commit comments

Comments
 (0)