Skip to content
This repository was archived by the owner on May 17, 2026. It is now read-only.

Commit 11b1b90

Browse files
committed
Add XML and C# serialization methods to ITestOutputHelper
This update introduces additional methods to ITestOutputHelper: WriteCSharp for outputting objects as C# code, and WriteXml for XML serialization. It incorporates various classes and test cases to support the new functionality. Projects and namespace allocations were also updated where necessary.
1 parent 3c72455 commit 11b1b90

12 files changed

Lines changed: 180 additions & 10 deletions

Frank.Testing.TestOutputExtensions/Frank.Testing.TestOutputExtensions.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<RootNamespace>Xunit</RootNamespace>
4+
<RootNamespace>Xunit.Abstractions</RootNamespace>
55
<Description>Extends ITestOutputHelper to allow output of a generic type using a serializer.</Description>
6-
<PackageTags>test, xunit, output, helper, serializer, json, console</PackageTags>
6+
<PackageTags>test, testing, extensions, xunit, output, helper, serializer, json, xml, csharp, dump, var, dumpvar, vardump, xmlserializer, jsonserializer, testoutputhelper, testoutput, outputhelper, ITestOutputHelper, abstractions, xunit.abstractions, xunit.abstractions.extensions, xunit.extensions.ordering</PackageTags>
77
</PropertyGroup>
88

99
<ItemGroup>
1010
<PackageReference Include="FluentAssertions" Version="6.12.0" />
11+
<PackageReference Include="VarDump" Version="0.2.11" />
1112
<PackageReference Include="xunit.abstractions" Version="2.0.3" />
1213
<PackageReference Include="Xunit.Extensions.Ordering" Version="1.4.5" />
1314
</ItemGroup>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.ComponentModel;
2+
3+
using FluentAssertions;
4+
5+
using VarDump;
6+
using VarDump.Visitor;
7+
8+
namespace Xunit.Abstractions;
9+
10+
public static class TestOutputCSharpExtensions
11+
{
12+
/// <summary>
13+
/// Writes the provided source object as C# code to the test output.
14+
/// </summary>
15+
/// <remarks>
16+
/// The C# code is written using the <see cref="CSharpDumper"/>
17+
/// </remarks>
18+
/// <example>
19+
/// <code>
20+
/// var testPerson = new Frank.Testing.Tests.TestPerson
21+
/// {
22+
/// Name = "John Doe",
23+
/// Age = 30,
24+
/// Address = new Frank.Testing.Tests.TestAddress
25+
/// {
26+
/// City = "Main City",
27+
/// ZipCode = 18846
28+
/// }
29+
/// };
30+
/// </code>
31+
/// </example>
32+
/// <typeparam name="T">The type of the source object.</typeparam>
33+
/// <param name="outputHelper">The test output helper.</param>
34+
/// <param name="source">The source object to be written as C# code.</param>
35+
/// <param name="dumpOptions">The optional dump options to control the formatting of the C# code.</param>
36+
public static void WriteCSharp<T>(this ITestOutputHelper outputHelper, T source, DumpOptions? dumpOptions = null)
37+
{
38+
var options = dumpOptions ?? DumpOptions;
39+
var dumper = new CSharpDumper(options);
40+
outputHelper.WriteLine(dumper.Dump(source));
41+
}
42+
43+
private static DumpOptions DumpOptions => new DumpOptions()
44+
{
45+
IgnoreNullValues = true,
46+
DateKind = DateKind.ConvertToUtc,
47+
DateTimeInstantiation = DateTimeInstantiation.Parse,
48+
UseTypeFullName = true,
49+
GenerateVariableInitializer = true,
50+
SortDirection = ListSortDirection.Ascending,
51+
MaxDepth = 64,
52+
};
53+
}

Frank.Testing.TestOutputExtensions/TestOutputExtensions.cs renamed to Frank.Testing.TestOutputExtensions/TestOutputJsonExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using System.Text.Json;
22
using System.Text.Json.Serialization;
33

4-
using Xunit.Abstractions;
4+
namespace Xunit.Abstractions;
55

6-
namespace Xunit;
7-
8-
public static class TestOutputExtensions
6+
public static class TestOutputJsonExtensions
97
{
108
/// <summary>
119
/// Writes the specified object's string representation followed by the current line terminator to the output.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Text;
2+
using System.Xml;
3+
using System.Xml.Serialization;
4+
5+
namespace Xunit.Abstractions;
6+
7+
public static class TestOutputXmlExtensions
8+
{
9+
public static void WriteXml<T>(this ITestOutputHelper outputHelper, T source, XmlWriterSettings? xmlWriterSettings = null)
10+
{
11+
var settings = xmlWriterSettings ?? XmlWriterSettings;
12+
13+
using var textWriter = new StringWriter();
14+
using var xmlWriter = XmlWriter.Create(textWriter, settings);
15+
var xmlSerializer = new XmlSerializerFactory().CreateSerializer(typeof(T));
16+
xmlSerializer.Serialize(xmlWriter, source);
17+
outputHelper.WriteLine(textWriter.ToString());
18+
}
19+
20+
private static XmlWriterSettings XmlWriterSettings => new()
21+
{
22+
Indent = true,
23+
IndentChars = new string(' ', 4),
24+
NewLineChars = "\n",
25+
NewLineHandling = NewLineHandling.Replace,
26+
OmitXmlDeclaration = false,
27+
Encoding = new UTF8Encoding(false),
28+
};
29+
}

Frank.Testing.Tests/Frank.Testing.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13+
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
1314
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
1415
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
1516
<PackageReference Include="xunit" Version="2.6.4" />
@@ -25,6 +26,7 @@
2526

2627
<ItemGroup>
2728
<ProjectReference Include="..\Frank.Testing.Logging\Frank.Testing.Logging.csproj" />
29+
<ProjectReference Include="..\Frank.Testing.TestOutputExtensions\Frank.Testing.TestOutputExtensions.csproj" />
2830
</ItemGroup>
2931

3032
</Project>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
namespace Frank.Testing.Tests;
1212

13-
public class UnitTest1
13+
public class TestLoggingTests
1414
{
1515
private readonly ITestOutputHelper _outputHelper;
1616

17-
public UnitTest1(ITestOutputHelper outputHelper)
17+
public TestLoggingTests(ITestOutputHelper outputHelper)
1818
{
1919
_outputHelper = outputHelper;
2020
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using JetBrains.Annotations;
2+
3+
using Xunit.Abstractions;
4+
5+
namespace Frank.Testing.Tests;
6+
7+
[TestSubject(typeof(TestOutputCSharpExtensions))]
8+
public class TestOutputCSharpExtensionsTests(ITestOutputHelper outputHelper)
9+
{
10+
[Fact]
11+
public void WriteCSharp_ShouldWriteCSharpOutput_WhenSourceIsSimpleObject()
12+
{
13+
var source = new TestPerson { Name = "John Doe", Age = 30 };
14+
15+
outputHelper.WriteCSharp(source);
16+
}
17+
}

Frank.Testing.Tests/TestOutputExtensionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Frank.Testing.Tests;
66

7-
[TestSubject(typeof(TestOutputExtensions))]
7+
[TestSubject(typeof(TestOutputJsonExtensions))]
88
public class TestOutputExtensionsTests
99
{
1010
private readonly ITestOutputHelper _outputHelper;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using JetBrains.Annotations;
2+
3+
using Xunit.Abstractions;
4+
5+
namespace Frank.Testing.Tests;
6+
7+
[TestSubject(typeof(TestOutputXmlExtensions))]
8+
public class TestOutputXmlExtensionsTests(ITestOutputHelper outputHelper)
9+
{
10+
[Fact]
11+
public void WriteXml_ShouldWriteXmlOutput_WhenSourceIsSimpleObject()
12+
{
13+
var source = new TestPerson { Name = "John Doe", Age = 30 };
14+
15+
outputHelper.WriteXml(source);
16+
}
17+
18+
[Fact]
19+
public void WriteXml_ShouldWriteXmlOutput_WhenSourceIsNestedObject()
20+
{
21+
var source = new TestPerson() { Name = "John Doe", Age = 30, Address = new TestAddress() { City = "Main City", ZipCode = 18846 } };
22+
outputHelper.WriteXml(source);
23+
}
24+
25+
[Fact]
26+
public void WriteXml_ShouldHandleEmptyObjects()
27+
{
28+
var source = new TestPerson();
29+
30+
outputHelper.WriteXml(source);
31+
}
32+
33+
[Fact]
34+
public void WriteXml_ShouldHandleNullObjects()
35+
{
36+
object? source = null;
37+
38+
outputHelper.WriteXml(source);
39+
}
40+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Frank.Testing.Tests;
2+
3+
public class TestAddress
4+
{
5+
public string City { get; set; }
6+
7+
public int ZipCode { get; set; }
8+
}

0 commit comments

Comments
 (0)