Skip to content

Commit

Permalink
#248 Improved MessageListener.EnsureReceived() to return friendly mes…
Browse files Browse the repository at this point in the history
…sage dumps for common types (#254)
  • Loading branch information
Suremaker committed Mar 28, 2022
1 parent f17cdd2 commit aab7b71
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ LightBDD

Version [next]
----------------------------------------
+ #248 (Framework)(Change) Improved MessageListener.EnsureReceived() to return friendly message dumps for common types
+ #252 (LightBdd.XUnit2)(Change) Added LightBddScopeAttribute.DiagnosticMessageSink property

Version 3.4.0 (VSIX)
Expand Down
14 changes: 14 additions & 0 deletions src/LightBDD.Core/Formatting/Diagnostics/ObjectFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ private static StringBuilder Dump(this StringBuilder builder, object obj, int le
return builder.AppendFormat(CultureInfo.InvariantCulture, "{0}", obj);
if (type.IsEnum)
return builder.Append(Enum.GetName(type, obj));
var basic = TryFormatBasic(obj);
if (basic != null)
return builder.Append('\"').Append(basic).Append('\"');

if (level == 0)
return builder.Append("{...}");
Expand All @@ -90,6 +93,17 @@ private static StringBuilder Dump(this StringBuilder builder, object obj, int le
return builder.Append("}");
}

private static string TryFormatBasic(object value)
{
return value switch
{
DateTimeOffset dateTimeOffset => dateTimeOffset.ToString("O", CultureInfo.InvariantCulture),
DateTime dateTime => dateTime.ToString("O", CultureInfo.InvariantCulture),
IFormattable formattable => formattable.ToString(null, CultureInfo.InvariantCulture),
_ => null
};
}

private static StringBuilder DumpProperty(this StringBuilder builder, PropertyInfo member, object obj, int level)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class Problematic
public string Prop => throw new InvalidOperationException("foo");
}

class BasicTypes
{
public TimeSpan? Span { get; set; }
public DateTime? DateTime { get; set; }
public DateTimeOffset? DateTimeOffset { get; set; }
public Guid? Guid { get; set; }
public decimal Decimal { get; set; }
public long? NullableLong { get; set; }
}

[Test]
public void It_should_format_object()
{
Expand All @@ -41,7 +51,7 @@ public void It_should_format_object()
};

var actual = ObjectFormatter.Dump(obj);
Assert.That(actual, Is.EqualTo("ComplexType: { StringField=\"test!\" Int=5 Flag=True ReadonlyFloat=3.14 Array=[ Local Unspecified Utc ] Dictionary=[ { Key=\"abc\" Value={ StringField=null Int=0 Flag=False ReadonlyFloat=3.14 Array={...} Dictionary={...} Span=null } } ] Span={ Ticks=864000000000 Days=1 Hours=0 Milliseconds=0 Minutes=0 Seconds=0 TotalDays=1 TotalHours=24 TotalMilliseconds=86400000 TotalMinutes=1440 TotalSeconds=86400 } }"));
Assert.That(actual, Is.EqualTo("ComplexType: { StringField=\"test!\" Int=5 Flag=True ReadonlyFloat=3.14 Array=[ Local Unspecified Utc ] Dictionary=[ { Key=\"abc\" Value={ StringField=null Int=0 Flag=False ReadonlyFloat=3.14 Array={...} Dictionary={...} Span=null } } ] Span=\"1.00:00:00\" }"));
}

[Test]
Expand All @@ -63,7 +73,7 @@ public void It_should_format_object_collection()
};

var actual = ObjectFormatter.DumpMany(items);
Assert.That(actual.Replace("\r", ""), Is.EqualTo(@"ComplexType: { StringField=""test!"" Int=5 Flag=True ReadonlyFloat=3.14 Array=[ Local Unspecified Utc ] Dictionary=[ {...} ] Span={ Ticks=432000000000 Days=0 Hours=12 Milliseconds=0 Minutes=0 Seconds=0 TotalDays=0.5 TotalHours=12 TotalMilliseconds=43200000 TotalMinutes=720 TotalSeconds=43200 } }
Assert.That(actual.Replace("\r", ""), Is.EqualTo(@"ComplexType: { StringField=""test!"" Int=5 Flag=True ReadonlyFloat=3.14 Array=[ Local Unspecified Utc ] Dictionary=[ {...} ] Span=""12:00:00"" }
BaseType: { StringField=""123"" }
Double: 3.14
Boolean: False".Replace("\r", "")));
Expand All @@ -75,5 +85,21 @@ public void It_should_format_problematic_property()
var actual = ObjectFormatter.Dump(new Problematic());
Assert.That(actual, Is.EqualTo("Problematic: { Prop=!InvalidOperationException:\"foo\" }"));
}

[Test]
public void It_should_format_basic_types_properly()
{
var obj = new BasicTypes
{
DateTimeOffset = new DateTimeOffset(2022,01,02,03,04,05,TimeSpan.FromHours(3)),
DateTime = new DateTime(2022,01,02,03,04,05,DateTimeKind.Utc),
Decimal = (decimal)3.14,
Guid = Guid.Parse("8bdcb730-6d08-409d-8170-12da52cb71e1"),
NullableLong = 34,
Span = TimeSpan.FromTicks(101427461000),
};
var actual = ObjectFormatter.Dump(obj);
Assert.That(actual.Replace("\r", ""), Is.EqualTo("BasicTypes: { Span=\"02:49:02.7461000\" DateTime=\"2022-01-02T03:04:05.0000000Z\" DateTimeOffset=\"2022-01-02T03:04:05.0000000+03:00\" Guid=\"8bdcb730-6d08-409d-8170-12da52cb71e1\" Decimal=\"3.14\" NullableLong=34 }".Replace("\r", "")));
}
}
}

0 comments on commit aab7b71

Please sign in to comment.