diff --git a/appveyor.yml b/appveyor.yml index c14a1aa..297179a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,7 @@ # http://www.appveyor.com/docs/appveyor-yml environment: - base_version: 2.2.0 + base_version: 2.3.0 # version format version: $(base_version).{build} @@ -27,7 +27,7 @@ for: skip_tags: true install: - - dotnet tool install Nuke.GlobalTool --global --version 6.3.0 --no-cache + - dotnet tool install Nuke.GlobalTool --global --no-cache before_build: - dotnet restore ./build/_build.csproj @@ -46,7 +46,7 @@ for: skip_tags: true install: - - dotnet tool install Nuke.GlobalTool --global --version 6.3.0 --no-cache + - dotnet tool install Nuke.GlobalTool --global --no-cache before_build: - dotnet restore ./build/_build.csproj diff --git a/build/Build.cs b/build/Build.cs index 5a97cab..5583060 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -32,7 +32,7 @@ class Build : NukeBuild [GitRepository] readonly GitRepository GitRepository; [Parameter("Version to be injected in the Build")] - public string Version { get; set; } = $"2.2.0"; + public string Version { get; set; } = $"2.3.0"; [Parameter("The Buildnumber provided by the CI")] public int BuildNo = 1; diff --git a/src/Polaroider/Mapping/DefaultMapper.cs b/src/Polaroider/Mapping/DefaultMapper.cs index 81cc9cd..af442d2 100644 --- a/src/Polaroider/Mapping/DefaultMapper.cs +++ b/src/Polaroider/Mapping/DefaultMapper.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Linq; using System.Reflection; +using System.Text; namespace Polaroider.Mapping { @@ -69,22 +70,33 @@ public void Map(MapperContext ctx, object item) .Where(p => p.GetGetMethod() != null) .OrderBy(p => p.Name)) { - var header = $"{property.Name}:".Indent(ctx.Indentation); - var value = property.GetValue(item); + try + { + var header = $"{property.Name}:".Indent(ctx.Indentation); + var value = property.GetValue(item); - if (MapValueType(ctx, property.PropertyType, value, $"{header} ")) - { - continue; - } + if (MapValueType(ctx, property.PropertyType, value, $"{header} ")) + { + continue; + } - ctx.AddLine(new Line(header)); + ctx.AddLine(new Line(header)); - if (MapRegisteredType(property.PropertyType, ctx.Clone(ctx.Indentation + 2), value)) - { - continue; - } + if (MapRegisteredType(property.PropertyType, ctx.Clone(ctx.Indentation + 2), value)) + { + continue; + } - Map(ctx.Clone(ctx.Indentation + 2), value); + Map(ctx.Clone(ctx.Indentation + 2), value); + } + catch(NotSupportedException e) + { + var msg = new StringBuilder() + .AppendLine($"Could not map property {property.Name} to the Snapshot") + .AppendLine(e.Message) + .AppendLine(e.StackTrace); + System.Diagnostics.Trace.WriteLine(msg.ToString()); + } } } diff --git a/src/Polaroider/ObjectExtensions.cs b/src/Polaroider/ObjectExtensions.cs new file mode 100644 index 0000000..bf80948 --- /dev/null +++ b/src/Polaroider/ObjectExtensions.cs @@ -0,0 +1,23 @@ +using System; + +namespace Polaroider +{ + /// + /// + /// + public static class ObjectExtensions + { + /// + /// Map a object to another object. Can be used to make simpler snapshots + /// + /// + /// + /// + /// + /// + public static Tout MapTo(this Tin obj, Func factory) + { + return factory(obj); + } + } +} diff --git a/src/Tests/Polaroider.Tests/ObjectExtensionsTests.cs b/src/Tests/Polaroider.Tests/ObjectExtensionsTests.cs new file mode 100644 index 0000000..35cae67 --- /dev/null +++ b/src/Tests/Polaroider.Tests/ObjectExtensionsTests.cs @@ -0,0 +1,32 @@ +using FluentAssertions; +using NUnit.Framework; + +namespace Polaroider.Tests +{ + public class ObjectExtensionsTests + { + [Test] + public void ObjectExtensions_MapTo() + { + var obj = new + { + Age = 25, + Name = "John", + LastName = "Doe" + }; + + obj.MapTo(o => + new + { + Name = o.Name, + LastName = o.LastName + }) + .Should() + .BeEquivalentTo(new + { + Name = "John", + LastName = "Doe" + }); + } + } +} diff --git a/src/Tests/Polaroider.Tests/ObjectMapTests.cs b/src/Tests/Polaroider.Tests/ObjectMapTests.cs new file mode 100644 index 0000000..4022856 --- /dev/null +++ b/src/Tests/Polaroider.Tests/ObjectMapTests.cs @@ -0,0 +1,15 @@ +using NUnit.Framework; +using System.Text; + +namespace Polaroider.Tests +{ + public class ObjectMapTests + { + [Test] + public void ObjectMap_Encoding() + { + new UTF8Encoding(true, true) + .MatchSnapshot(); + } + } +} diff --git a/src/Tests/Polaroider.Tests/_Snapshots/ObjectMapTests_ObjectMap_Encoding.snapshot b/src/Tests/Polaroider.Tests/_Snapshots/ObjectMapTests_ObjectMap_Encoding.snapshot new file mode 100644 index 0000000..436aea2 --- /dev/null +++ b/src/Tests/Polaroider.Tests/_Snapshots/ObjectMapTests_ObjectMap_Encoding.snapshot @@ -0,0 +1,17 @@ +---data +BodyName: utf-8 +CodePage: 65001 +DecoderFallback: + MaxCharCount: 0 +EncoderFallback: + MaxCharCount: 0 +EncodingName: Unicode (UTF-8) +HeaderName: utf-8 +IsBrowserDisplay: True +IsBrowserSave: True +IsMailNewsDisplay: True +IsMailNewsSave: True +IsReadOnly: True +IsSingleByte: False +WebName: utf-8 +WindowsCodePage: 1200