Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make stringBuilder.AppendValueOf safer #786

Merged
merged 1 commit into from
Mar 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ This release contains several **minor breaking changes**. Please review your cod
* Use of `SetupSet` 'forgets' method setup (@TimothyHayes, #432)
* Recursive mocks don't work with argument matching (@thalesmello, #142)
* Recursive property setup overrides previous setups (@jamesfoster, #110)
* Formatting of enumerable object for error message broke EF Core test case (@MichaelSagalovich, #741)


## 4.10.1 (2018-12-03)
Expand Down
32 changes: 17 additions & 15 deletions src/Moq/StringBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -82,14 +83,15 @@ public static StringBuilder AppendValueOf(this StringBuilder stringBuilder, obje
{
stringBuilder.Append(d.ToString("G17"));
}
else if (obj is IEnumerable enumerable && !(obj is IMocked))
{ // ^^^^^^^^^^^^^^^^^
// This second check ensures that we have a usable implementation of IEnumerable.
// If value is a mocked object, its IEnumerable implementation might very well
// not work correctly.
else if (obj.GetType().IsEnum)
{
stringBuilder.AppendNameOf(obj.GetType()).Append('.').Append(obj);
}
else if (obj.GetType().IsArray || (obj.GetType().IsConstructedGenericType && obj.GetType().GetGenericTypeDefinition() == typeof(List<>)))
{
stringBuilder.Append('[');
const int maxCount = 10;
var enumerator = enumerable.GetEnumerator();
var enumerator = ((IEnumerable)obj).GetEnumerator();
for (int i = 0; enumerator.MoveNext() && i < maxCount + 1; ++i)
{
if (i > 0)
Expand All @@ -107,17 +109,17 @@ public static StringBuilder AppendValueOf(this StringBuilder stringBuilder, obje
}
stringBuilder.Append(']');
}
else if (obj.ToString() == obj.GetType().ToString())
{
stringBuilder.AppendNameOf(obj.GetType());
}
else if (obj.GetType().IsEnum)
{
stringBuilder.AppendNameOf(obj.GetType()).Append('.').Append(obj);
}
else
{
stringBuilder.Append(obj.ToString());
var formatted = obj.ToString();
if (formatted == null || formatted == obj.GetType().ToString())
{
stringBuilder.AppendNameOf(obj.GetType());
}
else
{
stringBuilder.Append(formatted);
}
}

return stringBuilder;
Expand Down