-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#27: Added first version of Dependencies finder
- Loading branch information
1 parent
6d56374
commit cb7b745
Showing
5 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
Documentation/Synergy.Documentation.Tests/Architecture/Dependencies/Relations.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Synergy.Documentation.Api; | ||
using Synergy.Documentation.Markup; | ||
|
||
namespace Synergy.Documentation.Tests.Architecture.Dependencies; | ||
|
||
[UsesVerify] | ||
public class Relations | ||
{ | ||
[Theory] | ||
[InlineData(typeof(Markdown))] | ||
[InlineData(typeof(Markdown.Document))] | ||
public async Task Generate(Type type) | ||
{ | ||
// ARRANGE | ||
var dependencies = Synergy.Documentation.Api.Dependencies.Of(type); | ||
|
||
|
||
// TODO: Marcin Celej [from: Marcin Celej on: 26-12-2023]: Doe not work for Markdown.Document | ||
// ACT | ||
var publicApi = ApiDescription.GenerateFor(dependencies); | ||
|
||
// ASSERT | ||
await Verifier.Verify(publicApi, "md") | ||
.UseMethodName("of." + type.Name); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...Documentation.Tests/Architecture/Dependencies/Relations.of.Document.verified.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
3 changes: 3 additions & 0 deletions
3
...Documentation.Tests/Architecture/Dependencies/Relations.of.Markdown.verified.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Synergy.Documentation.Markup.Markdown (class) | ||
- ctor() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.Collections; | ||
using System.Reflection; | ||
using Synergy.Catalogue; | ||
|
||
namespace Synergy.Documentation.Api; | ||
|
||
public class Dependencies | ||
{ | ||
public static List<Type> Of(Type root) | ||
{ | ||
return new Dependencies().DependsOn(root).ToList(); | ||
} | ||
|
||
private readonly List<Type> visited = new(100); | ||
public Dependencies() | ||
{ | ||
} | ||
|
||
public IEnumerable<Type> DependsOn(Type type) | ||
{ | ||
var underlyingType = Nullable.GetUnderlyingType(type); | ||
if (underlyingType != null) | ||
type = underlyingType; | ||
|
||
if (type.IsPrimitive) | ||
yield break; | ||
|
||
if (type.In(typeof(string), typeof(Guid), typeof(DateTime), typeof(decimal))) | ||
yield break; | ||
|
||
if (type.IsArray) | ||
type = type.GetElementType(); | ||
else if (type.IsAssignableTo(typeof(IEnumerable))) | ||
{ | ||
if ( type.GetGenericArguments().Length == 0) | ||
yield break; | ||
|
||
type = type.GetGenericArguments().First(); | ||
} | ||
|
||
if (type.Namespace.StartsWith("System.")) | ||
yield break; | ||
|
||
if (this.visited.Contains(type)) | ||
yield break; | ||
|
||
this.visited.Add(type); | ||
|
||
yield return type; | ||
|
||
foreach (var property in this.GetPropertiesOf(type)) | ||
{ | ||
foreach (var dependency in this.DependsOn(property.PropertyType)) | ||
{ | ||
yield return dependency; | ||
} | ||
} | ||
} | ||
|
||
private PropertyInfo[] GetPropertiesOf(Type type) | ||
{ | ||
return type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy); | ||
} | ||
} |