Skip to content

Commit

Permalink
Updated ctor params to support nullable reference or a nullable item …
Browse files Browse the repository at this point in the history
…in the array. Added nullable preprocessor directive in unit tests
  • Loading branch information
Asafima authored and kzu committed Sep 27, 2024
1 parent a481e8d commit 18dc741
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/Moq.Tests/MockFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -998,13 +998,22 @@ public void ExpectGetAndExpectSetMatchArguments()
target.VerifyAll();
}

#nullable enable
[Fact]
public void ArgumentNullMatchProperCtor()
{
var target = new Mock<Foo>(null);
Assert.Null(target.Object.Bar);
}

[Fact]
public void ParamsArrayContainsNullMatchProperCtor()
{
var target = new Mock<Foo>(new Bar?[] { null });
Assert.Null(target.Object.Bar);
}
#nullable disable

[Fact]
public void DistinguishesSameMethodsWithDifferentGenericArguments()
{
Expand Down
11 changes: 4 additions & 7 deletions src/Moq/Mock`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public Mock()
/// var mock = new Mock&lt;MyProvider&gt;(someArgument, 25);
/// </code>
/// </example>
public Mock(params object[] args)
public Mock(params object?[]? args)
: this(MockBehavior.Default, args)
{
}
Expand All @@ -154,7 +154,7 @@ public Mock(params object[] args)
/// </code>
/// </example>
public Mock(MockBehavior behavior)
: this(behavior, new object[0])
: this(behavior, Array.Empty<object?>())
{
}

Expand All @@ -168,14 +168,11 @@ public Mock(MockBehavior behavior)
/// The mock will try to find the best match constructor given the constructor arguments,
/// and invoke that to initialize the instance. This applies only to classes, not interfaces.
/// </remarks>
public Mock(MockBehavior behavior, params object[] args)
public Mock(MockBehavior behavior, params object?[]? args)
{
Guard.IsMockable(typeof(T));

if (args == null)
{
args = new object[] { null };
}
args ??= new object?[] { null };

this.additionalInterfaces = new List<Type>();
this.behavior = behavior;
Expand Down

0 comments on commit 18dc741

Please sign in to comment.