Skip to content

Commit

Permalink
Updated params formatting to individually format each arg and highlight
Browse files Browse the repository at this point in the history
  each one that does not match the specified call.

* Added ArgumentSpecification.FormatArgument(object) which delegates
  to its matcher's formatting if the matcher implements IArgumentFormatter,
  otherwise falls back to the default format provided by ArgumentFormatter.
* Updated ArrayContentsArgumentMatcher to implement IArgumentsFormatter
  and format each params argument against its respective argspec.
* Updated CallFormatter to accept pre-formatted args so it no longer has to
  dig into the spec's non-matching args. Replaced its ICallFormatter interface
  with more general IMethodFormatter (it is basically a composite method
  formatter).
* Removed ArgumentsFormatter and replaced with Join extension method.
  • Loading branch information
dtchepak committed Mar 20, 2012
1 parent c2712c3 commit 4bd7d19
Show file tree
Hide file tree
Showing 43 changed files with 602 additions and 6,552 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
unreleased changes
* [NEW] Show details of params arguments when displaying received calls. Thanks to Sauli T�hk�p�� for implementing this feature. (#65)
* [NEW] Detailed descriptions for custom argument matchers using IDescribeNonMatches interface.
* [NEW] Custom argument matchers (Arg.Matches syntax).
* [FIX] Race condition between checking received calls and building the exception could cause nonsensical exception messages like "Expected 5, actually received 5" when called concurrently. (#64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ public void Should_list_non_matching_calls()
}
}

[Pending]
public class When_checking_call_to_method_with_params : Context
{
protected override void ConfigureContext()
{
Sample.ParamsMethod(2, "hello", "everybody");
Sample.ParamsMethod(1, new[] {"hello", "everybody"});
Sample.ParamsMethod(1, new[] {"hello", "everybody"});
Sample.ParamsMethod(1, "hello");
Sample.ParamsMethod(3, "1", "2", "3");
}

Expand All @@ -284,12 +284,38 @@ public void Should_show_expected_call()
[Test]
public void Should_show_non_matching_calls_with_params_expanded()
{
ExceptionMessageContains("ParamsMethod(*2*, *\"hello\", \"everybody\"*)");
ExceptionMessageContains("ParamsMethod(1, *\"hello\", \"everybody\"*)");
ExceptionMessageContains("ParamsMethod(*3*, *\"1\", \"2\", \"3\"*)");
ExceptionMessageContains("ParamsMethod(*2*, \"hello\", *\"everybody\"*)");
ExceptionMessageContains("ParamsMethod(1, \"hello\", *\"everybody\"*)");
ExceptionMessageContains("ParamsMethod(1, \"hello\")");
ExceptionMessageContains("ParamsMethod(*3*, *\"1\"*, *\"2\"*, *\"3\"*)");
}
}

public class When_checking_call_to_method_with_params_specified_as_an_array : Context
{
protected override void ConfigureContext()
{
Sample.ParamsMethod(2, "hello", "everybody");
}
}

protected override void ExpectedCall()
{
Sample.Received().ParamsMethod(1, Arg.Is(new[] {"hello", "world"}));
}

[Test]
public void Should_show_expected_call()
{
ExceptionMessageContains("ParamsMethod(1, String[])");
}

[Test]
public void Should_show_non_matching_calls_as_per_specification_rather_than_as_individual_params()
{
ExceptionMessageContains("ParamsMethod(*2*, *String[]*)");
}
}

public class When_using_custom_describable_matcher : Context
{
private Car _expectedCar;
Expand Down
115 changes: 0 additions & 115 deletions Source/NSubstitute.Specs/Arguments/ArgumentFormatInfoFactorySpecs.cs

This file was deleted.

This file was deleted.

51 changes: 0 additions & 51 deletions Source/NSubstitute.Specs/Arguments/ArgumentsFormatterSpecs.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System.Linq;
using NSubstitute.Core.Arguments;
using NSubstitute.Specs.Infrastructure;
using NUnit.Framework;
Expand All @@ -9,7 +9,6 @@ public class ArrayContentsArgumentMatcherSpecs : ConcernFor<ArrayContentsArgumen
{
private IArgumentSpecification[] _argumentSpecifications;
private string[] _argument;
private Type _forType;

[Test]
public void Should_match_when_all_argument_specs_match()
Expand Down Expand Up @@ -45,18 +44,50 @@ public void Should_incorporate_toString_of_all_specifications_in_toString()
}

[Test]
public void Should_return_argument_specifications_inside()
{
Assert.That(sut.ArgumentSpecifications, Is.EqualTo(_argumentSpecifications));
public void Should_format_each_spec_and_argument_when_they_are_the_same_length()
{
_argumentSpecifications[0].stub(x => x.FormatArgument(_argument[0])).Return("first");
_argumentSpecifications[1].stub(x => x.FormatArgument(_argument[1])).Return("second");
var expected = "first, second";
var result = sut.Format(_argument, true);
Assert.That(result, Is.EqualTo(expected));
}

[Test]
public void Should_handle_formatting_when_there_are_more_arguments_than_specs()
{
_argumentSpecifications[0].stub(x => x.FormatArgument(_argument[0])).Return("first");
_argumentSpecifications[1].stub(x => x.FormatArgument(_argument[1])).Return("second");
var argsWithExtra = _argument.Concat(new[] { "doh" }).ToArray();
var expected = "first, second, " + DefaultFormat("doh", true);

var result = sut.Format(argsWithExtra, true);
Assert.That(result, Is.EqualTo(expected));
}

[Test]
public void Should_handle_formatting_when_there_are_less_arguments_than_specs()
{
_argumentSpecifications[0].stub(x => x.FormatArgument(_argument[0])).Return("first");
_argumentSpecifications[1].stub(x => x.FormatArgument(_argument[1])).Return("second");
var lessArgsThanSpecs = new[] { _argument[0] };
var expected = "first";

var result = sut.Format(lessArgsThanSpecs, true);
Assert.That(result, Is.EqualTo(expected));
}

public override void Context()
{
_argument = new[] { "blah", "meh" };
_forType = _argument.GetType();
_argumentSpecifications = new[] { mock<IArgumentSpecification>(), mock<IArgumentSpecification>() };
_argumentSpecifications[0].stub(x => x.IsSatisfiedBy(_argument[0])).Return(true);
_argumentSpecifications[1].stub(x => x.IsSatisfiedBy(_argument[1])).Return(true);
_argumentSpecifications[1].stub(x => x.IsSatisfiedBy(_argument[1])).Return(true);
}

private string DefaultFormat(string text, bool highlight)
{
return new ArgumentFormatter().Format(text, highlight);
}

public override ArrayContentsArgumentMatcher CreateSubjectUnderTest()
Expand Down
Loading

0 comments on commit 4bd7d19

Please sign in to comment.