generated from frankhaugen/DotnetRepoTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
3c72455
commit 11b1b90
Showing
12 changed files
with
180 additions
and
10 deletions.
There are no files selected for viewing
5 changes: 3 additions & 2 deletions
5
Frank.Testing.TestOutputExtensions/Frank.Testing.TestOutputExtensions.csproj
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
53 changes: 53 additions & 0 deletions
53
Frank.Testing.TestOutputExtensions/TestOutputCSharpExtensions.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,53 @@ | ||
using System.ComponentModel; | ||
|
||
using FluentAssertions; | ||
|
||
using VarDump; | ||
using VarDump.Visitor; | ||
|
||
namespace Xunit.Abstractions; | ||
|
||
public static class TestOutputCSharpExtensions | ||
{ | ||
/// <summary> | ||
/// Writes the provided source object as C# code to the test output. | ||
/// </summary> | ||
/// <remarks> | ||
/// The C# code is written using the <see cref="CSharpDumper"/> | ||
/// </remarks> | ||
/// <example> | ||
/// <code> | ||
/// var testPerson = new Frank.Testing.Tests.TestPerson | ||
/// { | ||
/// Name = "John Doe", | ||
/// Age = 30, | ||
/// Address = new Frank.Testing.Tests.TestAddress | ||
/// { | ||
/// City = "Main City", | ||
/// ZipCode = 18846 | ||
/// } | ||
/// }; | ||
/// </code> | ||
/// </example> | ||
/// <typeparam name="T">The type of the source object.</typeparam> | ||
/// <param name="outputHelper">The test output helper.</param> | ||
/// <param name="source">The source object to be written as C# code.</param> | ||
/// <param name="dumpOptions">The optional dump options to control the formatting of the C# code.</param> | ||
public static void WriteCSharp<T>(this ITestOutputHelper outputHelper, T source, DumpOptions? dumpOptions = null) | ||
{ | ||
var options = dumpOptions ?? DumpOptions; | ||
var dumper = new CSharpDumper(options); | ||
outputHelper.WriteLine(dumper.Dump(source)); | ||
} | ||
|
||
private static DumpOptions DumpOptions => new DumpOptions() | ||
{ | ||
IgnoreNullValues = true, | ||
DateKind = DateKind.ConvertToUtc, | ||
DateTimeInstantiation = DateTimeInstantiation.Parse, | ||
UseTypeFullName = true, | ||
GenerateVariableInitializer = true, | ||
SortDirection = ListSortDirection.Ascending, | ||
MaxDepth = 64, | ||
}; | ||
} |
6 changes: 2 additions & 4 deletions
6
...tOutputExtensions/TestOutputExtensions.cs → ...putExtensions/TestOutputJsonExtensions.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
29 changes: 29 additions & 0 deletions
29
Frank.Testing.TestOutputExtensions/TestOutputXmlExtensions.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,29 @@ | ||
using System.Text; | ||
using System.Xml; | ||
using System.Xml.Serialization; | ||
|
||
namespace Xunit.Abstractions; | ||
|
||
public static class TestOutputXmlExtensions | ||
{ | ||
public static void WriteXml<T>(this ITestOutputHelper outputHelper, T source, XmlWriterSettings? xmlWriterSettings = null) | ||
{ | ||
var settings = xmlWriterSettings ?? XmlWriterSettings; | ||
|
||
using var textWriter = new StringWriter(); | ||
using var xmlWriter = XmlWriter.Create(textWriter, settings); | ||
var xmlSerializer = new XmlSerializerFactory().CreateSerializer(typeof(T)); | ||
xmlSerializer.Serialize(xmlWriter, source); | ||
outputHelper.WriteLine(textWriter.ToString()); | ||
} | ||
|
||
private static XmlWriterSettings XmlWriterSettings => new() | ||
{ | ||
Indent = true, | ||
IndentChars = new string(' ', 4), | ||
NewLineChars = "\n", | ||
NewLineHandling = NewLineHandling.Replace, | ||
OmitXmlDeclaration = false, | ||
Encoding = new UTF8Encoding(false), | ||
}; | ||
} |
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
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,17 @@ | ||
using JetBrains.Annotations; | ||
|
||
using Xunit.Abstractions; | ||
|
||
namespace Frank.Testing.Tests; | ||
|
||
[TestSubject(typeof(TestOutputCSharpExtensions))] | ||
public class TestOutputCSharpExtensionsTests(ITestOutputHelper outputHelper) | ||
{ | ||
[Fact] | ||
public void WriteCSharp_ShouldWriteCSharpOutput_WhenSourceIsSimpleObject() | ||
{ | ||
var source = new TestPerson { Name = "John Doe", Age = 30 }; | ||
|
||
outputHelper.WriteCSharp(source); | ||
} | ||
} |
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,40 @@ | ||
using JetBrains.Annotations; | ||
|
||
using Xunit.Abstractions; | ||
|
||
namespace Frank.Testing.Tests; | ||
|
||
[TestSubject(typeof(TestOutputXmlExtensions))] | ||
public class TestOutputXmlExtensionsTests(ITestOutputHelper outputHelper) | ||
{ | ||
[Fact] | ||
public void WriteXml_ShouldWriteXmlOutput_WhenSourceIsSimpleObject() | ||
{ | ||
var source = new TestPerson { Name = "John Doe", Age = 30 }; | ||
|
||
outputHelper.WriteXml(source); | ||
} | ||
|
||
[Fact] | ||
public void WriteXml_ShouldWriteXmlOutput_WhenSourceIsNestedObject() | ||
{ | ||
var source = new TestPerson() { Name = "John Doe", Age = 30, Address = new TestAddress() { City = "Main City", ZipCode = 18846 } }; | ||
outputHelper.WriteXml(source); | ||
} | ||
|
||
[Fact] | ||
public void WriteXml_ShouldHandleEmptyObjects() | ||
{ | ||
var source = new TestPerson(); | ||
|
||
outputHelper.WriteXml(source); | ||
} | ||
|
||
[Fact] | ||
public void WriteXml_ShouldHandleNullObjects() | ||
{ | ||
object? source = null; | ||
|
||
outputHelper.WriteXml(source); | ||
} | ||
} |
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,8 @@ | ||
namespace Frank.Testing.Tests; | ||
|
||
public class TestAddress | ||
{ | ||
public string City { get; set; } | ||
Check warning on line 5 in Frank.Testing.Tests/TestingInfrastructure/TestAddress.cs GitHub Actions / Merge Job / Publish Preview Job
|
||
|
||
public int ZipCode { get; set; } | ||
} |
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,9 @@ | ||
namespace Frank.Testing.Tests; | ||
|
||
public class TestPerson | ||
{ | ||
public string Name { get; set; } | ||
Check warning on line 5 in Frank.Testing.Tests/TestingInfrastructure/TestPerson.cs GitHub Actions / Merge Job / Publish Preview Job
|
||
public int Age { get; set; } | ||
|
||
public TestAddress? Address { get; set; } | ||
} |
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