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

Default collection formatter not throwing exception #394

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
###In Development

- [#392](https://github.com/MehdiK/Humanizer/issues/392): Default implementation for collection formatter using regular style instead of an exception. Default separator is ampercent.

[Commits](https://github.com/MehdiK/Humanizer/compare/v1.34.0...master)

###v1.34.0 - 2015-03-04
Expand Down
21 changes: 21 additions & 0 deletions src/Humanizer.Tests/Localisation/DefaultFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Globalization;
using System.Threading;
using Humanizer.Localisation;
using Humanizer.Localisation.Formatters;
using Xunit;
Expand All @@ -20,5 +21,25 @@ public void TimeSpanHumanizeThrowsExceptionForTimeUnitsLargerThanWeek(TimeUnit t
{
Assert.Throws<ArgumentOutOfRangeException>(() => new DefaultFormatter(CultureInfo.InvariantCulture.Name).TimeSpanHumanize(timeUnit, unit));
}

[Fact]
public void Issue_392_A_collection_formatter_for_the_current_culture_has_not_been_implemented_yet()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see where you're going with the method name; but it's inconsistent with the rest of the codebase. Please rename it to something like HandlesNotImplementedCollectionFormattersGracefully. You can provide an actual url to the issue on GitHub in a comment.

{
var originalCulture = Thread.CurrentThread.CurrentCulture;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use something like:

using(new AmbientCulture("es")) {
    // test
}

var originalUiCulture = Thread.CurrentThread.CurrentUICulture;

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("es");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to use different culture, because "es" culture is going to be implemented some day, and the test will break.

Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("es");

// start: code from issue report
var a = new[] { DateTime.UtcNow, DateTime.UtcNow.AddDays(10) };
var b = a.Humanize(); // THROWS!
// end: code from issue report

Assert.Equal(a[0] + " & " + a[1], b);

Thread.CurrentThread.CurrentCulture = originalCulture;
Thread.CurrentThread.CurrentUICulture = originalUiCulture;
}
}
}
16 changes: 8 additions & 8 deletions src/Humanizer/Configuration/CollectionFormatterRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ namespace Humanizer.Configuration
internal class CollectionFormatterRegistry : LocaliserRegistry<ICollectionFormatter>
{
public CollectionFormatterRegistry()
: base(new DefaultCollectionFormatter())
: base(new DefaultCollectionFormatter("&"))
{
Register("en", new OxfordStyleCollectionFormatter("and"));
Register("it", new RegularStyleCollectionFormatter("e"));
Register("de", new RegularStyleCollectionFormatter("und"));
Register("dk", new RegularStyleCollectionFormatter("og"));
Register("nl", new RegularStyleCollectionFormatter("en"));
Register("pt", new RegularStyleCollectionFormatter("e"));
Register("nn", new RegularStyleCollectionFormatter("og"));
Register("nb", new RegularStyleCollectionFormatter("og"));
Register("it", new DefaultCollectionFormatter("e"));
Register("de", new DefaultCollectionFormatter("und"));
Register("dk", new DefaultCollectionFormatter("og"));
Register("nl", new DefaultCollectionFormatter("en"));
Register("pt", new DefaultCollectionFormatter("e"));
Register("nn", new DefaultCollectionFormatter("og"));
Register("nb", new DefaultCollectionFormatter("og"));
}
}
}
1 change: 0 additions & 1 deletion src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
<Compile Include="CollectionHumanizeExtensions.cs" />
<Compile Include="Configuration\CollectionFormatterRegistry.cs" />
<Compile Include="Localisation\CollectionFormatters\DefaultCollectionFormatter.cs" />
<Compile Include="Localisation\CollectionFormatters\RegularStyleCollectionFormatter.cs" />
<Compile Include="Localisation\CollectionFormatters\ICollectionFormatter.cs" />
<Compile Include="Localisation\CollectionFormatters\OxfordStyleCollectionFormatter.cs" />
<Compile Include="Localisation\Formatters\SerbianFormatter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Humanizer.Localisation.CollectionFormatters
{
class DefaultCollectionFormatter : ICollectionFormatter
{
protected String DefaultSeparator = "";

public DefaultCollectionFormatter(string defaultSeparator)
{
DefaultSeparator = defaultSeparator;
}

public virtual string Humanize<T>(IEnumerable<T> collection)
{
return Humanize(collection, o => o.ToString(), DefaultSeparator);
Expand All @@ -24,7 +30,26 @@ public virtual string Humanize<T>(IEnumerable<T> collection, String separator)

public virtual string Humanize<T>(IEnumerable<T> collection, Func<T, String> objectFormatter, String separator)
{
throw new NotImplementedException("A collection formatter for the current culture has not been implemented yet.");
if (collection == null)
throw new ArgumentException("collection");

T[] itemsArray = collection as T[] ?? collection.ToArray();

int count = itemsArray.Length;

if (count == 0)
return "";

if (count == 1)
return objectFormatter(itemsArray[0]);

IEnumerable<T> itemsBeforeLast = itemsArray.Take(count - 1);
T lastItem = itemsArray.Skip(count - 1).First();

return String.Format("{0} {1} {2}",
String.Join(", ", itemsBeforeLast.Select(objectFormatter)),
separator,
objectFormatter(lastItem));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Humanizer.Localisation.CollectionFormatters
internal class OxfordStyleCollectionFormatter : DefaultCollectionFormatter
{
public OxfordStyleCollectionFormatter(string defaultSeparator)
: base(defaultSeparator ?? "and")
{
DefaultSeparator = defaultSeparator ?? "and";
}

public override string Humanize<T>(IEnumerable<T> collection, Func<T, string> objectFormatter, String separator)
Expand Down

This file was deleted.