Skip to content

Commit bf8bb2d

Browse files
authored
Add new tests (#24)
1 parent 8073f02 commit bf8bb2d

21 files changed

+549
-336
lines changed

src/BitzArt.XDoc/DocumentationElements/AssemblyDocumentation.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ public sealed class AssemblyDocumentation : DocumentationElement, IDocumentation
1919
/// </summary>
2020
private readonly Dictionary<Type, TypeDocumentation> _typeData;
2121

22-
internal AssemblyDocumentation(XDoc source, Assembly assembly) : base(source, null)
22+
internal AssemblyDocumentation(XDoc source, Assembly assembly)
23+
: this(source, assembly, XmlUtility.Fetch(source, assembly)) { }
24+
25+
internal AssemblyDocumentation(XDoc source, Assembly assembly, Dictionary<Type, TypeDocumentation> typeData) : base(source, null)
2326
{
2427
Assembly = assembly;
25-
_typeData = XmlUtility.Fetch(source, assembly);
28+
_typeData = typeData;
2629
}
2730

2831
/// <summary>

src/BitzArt.XDoc/DocumentationElements/TypeDocumentation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private MemberInfo Validate(MemberInfo member)
7272
{
7373
if (member.DeclaringType != Type)
7474
{
75-
throw new InvalidOperationException("The provided property is not defined in this type.");
75+
throw new InvalidOperationException("The provided property is not declared in this type.");
7676
}
7777

7878
if (member.DeclaringType != member.ReflectedType)

src/BitzArt.XDoc/Utility/XmlParser.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@ internal XmlParser(XDoc source, Assembly assembly, XmlDocument xml)
2929

3030
internal Dictionary<Type, TypeDocumentation> Parse()
3131
{
32-
var nodeList = _xml.SelectNodes("/doc/members/member");
33-
34-
if (nodeList == null || nodeList.Count == 0)
35-
{
36-
throw new InvalidOperationException("No documentation found in the XML file.");
37-
}
32+
var nodeList = _xml.SelectNodes("/doc/members/member")
33+
?? throw new InvalidOperationException("Invalid XML.");
3834

3935
foreach (XmlNode node in nodeList) Parse(node);
4036

@@ -46,12 +42,8 @@ private void Parse(XmlNode node)
4642
if (node.Attributes is null || node.Attributes.Count == 0)
4743
throw new InvalidOperationException("Invalid XML node.");
4844

49-
var name = node.Attributes["name"]?.Value;
50-
51-
if (name == null)
52-
{
53-
throw new InvalidOperationException($"No 'name' attribute found in XML node '{node.Value}'.");
54-
}
45+
var name = (node.Attributes["name"]?.Value)
46+
?? throw new InvalidOperationException($"No 'name' attribute found in XML node '{node.Value}'.");
5547

5648
switch (name[0])
5749
{
@@ -64,11 +56,8 @@ private void Parse(XmlNode node)
6456

6557
private TypeDocumentation ParseTypeNode(XmlNode node, string name)
6658
{
67-
var type = _assembly.GetType(name);
68-
if (type == null)
69-
{
70-
throw new InvalidOperationException($"Type '{name}' not found.");
71-
}
59+
var type = _assembly.GetType(name)
60+
?? throw new InvalidOperationException($"Type '{name}' not found.");
7261

7362
// We could handle this case by finding and updating the existing object,
7463
// but I don't see a reason why this would be necessary.

src/BitzArt.XDoc/XDoc.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,17 @@ namespace BitzArt.XDoc;
2525
/// </summary>
2626
public class XDoc
2727
{
28-
private readonly ConcurrentDictionary<Assembly, AssemblyDocumentation> _fetchedAssemblies = [];
28+
private readonly ConcurrentDictionary<Assembly, AssemblyDocumentation> _fetchedAssemblies;
29+
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="XDoc"/> class.
32+
/// </summary>
33+
public XDoc() : this(new()) { }
34+
35+
internal XDoc(ConcurrentDictionary<Assembly, AssemblyDocumentation> fetchedAssemblies)
36+
{
37+
_fetchedAssemblies = fetchedAssemblies;
38+
}
2939

3040
/// <summary>
3141
/// Fetches documentation for the specified <see cref="Assembly"/>.

src/EntityFrameworkCore/BitzArt.XDoc.EntityFrameworkCore/BitzArt.XDoc.EntityFrameworkCore.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
<ItemGroup>
2727
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" PrivateAssets="All"/>
28-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.2"/>
29-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.2"/>
28+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
29+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.3" />
3030
</ItemGroup>
3131

3232
<ItemGroup>

tests/BitzArt.XDoc.Tests/BitzArt.XDoc.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
</PackageReference>
1919
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.3" />
2020
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.3" />
21-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0"/>
22-
<PackageReference Include="xunit" Version="2.9.3"/>
21+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
22+
<PackageReference Include="xunit" Version="2.9.3" />
2323
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
2424
<PrivateAssets>all</PrivateAssets>
2525
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -28,11 +28,11 @@
2828
</ItemGroup>
2929

3030
<ItemGroup>
31-
<ProjectReference Include="..\..\src\BitzArt.XDoc\BitzArt.XDoc.csproj"/>
31+
<ProjectReference Include="..\..\src\BitzArt.XDoc\BitzArt.XDoc.csproj" />
3232
</ItemGroup>
3333

3434
<ItemGroup>
35-
<Using Include="Xunit"/>
35+
<Using Include="Xunit" />
3636
</ItemGroup>
3737

3838
<PropertyGroup>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace BitzArt.XDoc.Tests;
2+
3+
public static class StringExtensions
4+
{
5+
// Offsets the content by the specified number of spaces.
6+
// In case if the content has multiple lines, each line will be offset.
7+
public static string Offset(this string content, int offset, bool exceptFirstLine = false)
8+
{
9+
var lines = content.Split('\n');
10+
11+
var offsetString = new string(' ', offset);
12+
13+
for (var i = exceptFirstLine ? 1 : 0; i < lines.Length; i++)
14+
lines[i] = $"{offsetString}{lines[i]}";
15+
16+
return string.Join('\n', lines);
17+
}
18+
}

tests/BitzArt.XDoc.Tests/TestMemberNode.cs renamed to tests/BitzArt.XDoc.Tests/TestNodes/TestMemberNode.cs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -48,44 +48,3 @@ private static string GetMemberName(MemberInfo member)
4848
_ => throw new UnreachableException()
4949
};
5050
}
51-
52-
public enum TestNodeType
53-
{
54-
Type,
55-
Field,
56-
Property,
57-
Method
58-
}
59-
60-
public static class TestXmlNodesExtensions
61-
{
62-
public static string GetXml(this IEnumerable<TestMemberNode> nodes, Assembly assembly) =>
63-
$"""
64-
<?xml version="1.0"?>
65-
<doc>
66-
<assembly>
67-
<name>{assembly.GetName().Name}</name>
68-
</assembly>
69-
<members>
70-
{string.Join('\n', nodes.Select(node => node.GetXml().Offset(8, exceptFirstLine: true)))}
71-
</members>
72-
</doc>
73-
""";
74-
}
75-
76-
public static class StringExtensions
77-
{
78-
// Offsets the content by the specified number of spaces.
79-
// In case if the content has multiple lines, each line will be offset.
80-
public static string Offset(this string content, int offset, bool exceptFirstLine = false)
81-
{
82-
var lines = content.Split('\n');
83-
84-
var offsetString = new string(' ', offset);
85-
86-
for (var i = exceptFirstLine ? 1 : 0; i < lines.Length; i++)
87-
lines[i] = $"{offsetString}{lines[i]}";
88-
89-
return string.Join('\n', lines);
90-
}
91-
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BitzArt.XDoc.Tests;
2+
3+
public enum TestNodeType
4+
{
5+
Type,
6+
Field,
7+
Property,
8+
Method
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Reflection;
2+
3+
namespace BitzArt.XDoc.Tests;
4+
5+
public static class TestXmlNodesExtensions
6+
{
7+
public static string GetXml(this IEnumerable<TestMemberNode> nodes, Assembly assembly) =>
8+
$"""
9+
<?xml version="1.0"?>
10+
<doc>
11+
<assembly>
12+
<name>{assembly.GetName().Name}</name>
13+
</assembly>
14+
<members>
15+
{string.Join('\n', nodes.Select(node => node.GetXml().Offset(8, exceptFirstLine: true)))}
16+
</members>
17+
</doc>
18+
""";
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace BitzArt.XDoc.Tests;
2+
3+
public interface ITestInterface
4+
{
5+
public string TestProperty { get; set; }
6+
7+
public void TestMethod();
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace BitzArt.XDoc.Tests;
2+
3+
public class InheritedTestClass : TestClass
4+
{
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace BitzArt.XDoc.Tests;
2+
3+
public class TestClass
4+
{
5+
/// blah
6+
public string TestProperty { get; set; } = null!;
7+
8+
public void TestMethod() { }
9+
10+
public string TestField = null!;
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace BitzArt.XDoc.Tests;
2+
3+
public struct TestStruct
4+
{
5+
public string TestProperty { get; set; }
6+
7+
public void TestMethod() { }
8+
9+
public string TestField;
10+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
namespace BitzArt.XDoc.Tests;
2+
3+
public class AssemblyDocumentationTests
4+
{
5+
[Fact]
6+
public void GetDocumentation_TypeFromAnotherAssembly_ShouldThrow()
7+
{
8+
// Arrange
9+
var assembly = GetType().Assembly;
10+
var assemblyDocumentation = new AssemblyDocumentation(new XDoc(), assembly, []);
11+
12+
var testType = typeof(object);
13+
14+
// Act + Assert
15+
Assert.ThrowsAny<Exception>(() => assemblyDocumentation.GetDocumentation(testType));
16+
}
17+
18+
[Fact]
19+
public void GetDocumentation_TypeWithoutDocumentation_ShouldReturnNull()
20+
{
21+
// Arrange
22+
var assembly = GetType().Assembly;
23+
var assemblyDocumentation = new AssemblyDocumentation(new XDoc(), assembly, []);
24+
var testType = typeof(TestClass);
25+
26+
// Act
27+
var result = assemblyDocumentation.GetDocumentation(testType);
28+
29+
// Assert
30+
Assert.Null(result);
31+
}
32+
33+
[Fact]
34+
public void GetDocumentation_TypeWithDocumentation_ShouldReturnDocumentation()
35+
{
36+
// Arrange
37+
var assembly = GetType().Assembly;
38+
39+
var testType = typeof(TestClass);
40+
41+
var typeDocumentation = new TypeDocumentation(null!, testType, null);
42+
var typeDict = new Dictionary<Type, TypeDocumentation>
43+
{
44+
{ testType, typeDocumentation }
45+
};
46+
var assemblyDocumentation = new AssemblyDocumentation(null!, assembly, typeDict);
47+
48+
// Act
49+
var result = assemblyDocumentation.GetDocumentation(testType);
50+
51+
// Assert
52+
Assert.NotNull(result);
53+
Assert.Same(typeDocumentation, result);
54+
}
55+
}

0 commit comments

Comments
 (0)