Skip to content

Commit

Permalink
Merge pull request #1549 from blairconrad/create-good-typed-arrays
Browse files Browse the repository at this point in the history
Create properly-typed arrays
  • Loading branch information
thomaslevesque committed Dec 18, 2018
2 parents f2bef25 + 01a405b commit c0a4099
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/FakeItEasy/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace FakeItEasy
{
using System;
using System.Linq.Expressions;
using System.Reflection;

Expand Down Expand Up @@ -123,14 +124,16 @@ private static bool TryFastEvaluate(Expression expression, out object result)

case ExpressionType.NewArrayInit:
var newArrayExpression = (NewArrayExpression)expression;
object[] arrayItems = new object[newArrayExpression.Expressions.Count];
var arrayItems = Array.CreateInstance(newArrayExpression.Type.GetElementType(), newArrayExpression.Expressions.Count);

for (int i = 0; i < newArrayExpression.Expressions.Count; i++)
{
if (!TryFastEvaluate(newArrayExpression.Expressions[i], out arrayItems[i]))
if (!TryFastEvaluate(newArrayExpression.Expressions[i], out object item))
{
return false;
}

arrayItems.SetValue(item, i);
}

result = arrayItems;
Expand Down
28 changes: 28 additions & 0 deletions tests/FakeItEasy.Specs/ConfigurationSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface IFoo
IFoo Bafoo();

IFoo Bafoo(out int i);

IFoo Wrap(IFoo wrappee);
}

[SuppressMessage("Microsoft.Design", "CA1040:AvoidEmptyInterfaces", Justification = "It's just used for testing.")]
Expand Down Expand Up @@ -709,6 +711,21 @@ public static void CallsBaseMethodAndDoesNothing(BaseClass fake)
.x(() => exception.Should().BeAnExceptionOfType<ExpectationException>());
}

[Scenario]
public static void NestedCallThatIncludesArrayConstruction(
IFoo fake,
Exception exception)
{
"Given a fake"
.x(() => fake = A.Fake<IFoo>());

"When I specify a call and create a non-object array in the call configuration"
.x(() => exception = Record.Exception(() => A.CallTo(() => fake.Wrap(Foo.BuildFromArray(new[] { 1, 2, 3 })))));

"Then it doesn't throw"
.x(() => exception.Should().BeNull());
}

public class BaseClass
{
public bool WasCalled { get; private set; }
Expand Down Expand Up @@ -754,6 +771,12 @@ public sealed override int ReturnSomething()

public class Foo : IFoo
{
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "integers", Justification = "Required for testing.")]
public static IFoo BuildFromArray(int[] integers)
{
return new Foo();
}

public void Bar()
{
throw new NotSupportedException();
Expand All @@ -778,6 +801,11 @@ public IFoo Bafoo(out int i)
{
throw new NotSupportedException();
}

public IFoo Wrap(IFoo wrappee)
{
throw new NotImplementedException();
}
}

public class FooFactory : DummyFactory<IFoo>
Expand Down

0 comments on commit c0a4099

Please sign in to comment.