Skip to content

Commit

Permalink
Merge pull request #5 from RubenMateus/feat_SF_add-casing-strategy
Browse files Browse the repository at this point in the history
Add new strategy type and tests
  • Loading branch information
RubenMateus authored Dec 13, 2018
2 parents afd4658 + e48b603 commit 390e3b6
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 10 deletions.
73 changes: 73 additions & 0 deletions Sluggy.Tests/CompositionStrategyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Moq;
using System.Linq;
using Xunit;

namespace Sluggy.Tests
{
public class CompositionStrategyTests
{
[Trait("Project", "Sluggy")]
[Fact(DisplayName = "Should Maintain the Strategies Order")]
public void MaintainsOrder()
{
const int numberOfStrategies = 30;
const string initialValue = "0";

var strategies = Enumerable
.Repeat(0, numberOfStrategies)
.Select((t, index) =>
{
var mock = new Mock<ITranslationStrategy>();
var indexString = index.ToString();

mock.Setup(strat => strat.Translate(indexString))
.Returns<string>(text => (int.Parse(text) + 1).ToString());

return mock;
})
.ToList();

var composite = new CompositeStrategy(strategies.Select(t => t.Object));

composite.Translate(initialValue);

var assertionIndex = 0;
foreach (var curr in strategies)
{
curr.Verify(t => t.Translate(assertionIndex.ToString()), Times.Once);
assertionIndex++;
}
}

[Trait("Project", "Sluggy")]
[Fact(DisplayName = "Should Execute All Strategies")]
public void AllStrategiesAreExecuted()
{
const int numberOfStrategies = 30;
const string textValue = "Dummy";

var strategies = Enumerable
.Repeat(0, numberOfStrategies)
.Select(t =>
{
var mock = new Mock<ITranslationStrategy>();

mock
.Setup(strat => strat.Translate(textValue))
.Returns(textValue);

return mock;
})
.ToList();

var composite = new CompositeStrategy(strategies.Select(t => t.Object));

composite.Translate(textValue);

foreach (var curr in strategies)
{
curr.Verify(t => t.Translate(textValue), Times.Once);
}
}
}
}
33 changes: 33 additions & 0 deletions Sluggy.Tests/NormalizationStrategyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using Xunit;

namespace Sluggy.Tests
{
public class NormalizationStrategyTests
{
[Trait("Project", "Sluggy")]
[Theory(DisplayName = "Should Normalize string")]
[InlineData("áãâàóôòõêè", "aaaaooooee")]
[InlineData("ä ö ű ő", "a o u o")]
[InlineData("", "")]
public void ShouldNormalize(string value, string expectation)
{
var strategy = new NormalizationStrategy();

var translated = strategy.Translate(value);

Assert.Equal(expectation, translated);
}

[Trait("Project", "Sluggy")]
[Fact(DisplayName = "NormalizationStrategy Should Throw NullArgumentException")]
public void ShouldThrowNullArgumentException()
{
const string text = null;

var strategy = new NormalizationStrategy();

Assert.Throws<ArgumentNullException>(() => strategy.Translate(text));
}
}
}
1 change: 1 addition & 0 deletions Sluggy.Tests/SluggyIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class SluggyIntegrationTests
[Theory(DisplayName = "Should Use Separator")]
[InlineData("EU GOSTO DE TÁRTE", "euAgostoAdeAtarte", "A")]
[InlineData("EU GOSTO", "euBananagosto", "Banana")]
[InlineData("", "", "")]
public void ShouldUseSeparator(string value, string expectation, string separator)
{
var result = value.ToSlug(separator);
Expand Down
7 changes: 4 additions & 3 deletions Sluggy.Tests/SluggyUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ namespace Sluggy.Tests
public class SluggyUnitTests
{
[Trait("Project", "Sluggy")]
[Fact(DisplayName = "Should Throw NullArgumentException")]
[Fact(DisplayName = "ToSlug Should Throw NullArgumentException")]
public void ShouldThrowNullArgumentException()
{
const string cena = null;
const string text = null;

Assert.Throws<ArgumentNullException>(() => cena.ToSlug());
Assert.Throws<ArgumentNullException>(() => text.ToSlug());
}

[Trait("Project", "Sluggy")]
[Theory(DisplayName = "Should Return ToSlug With Mocked Translate")]
[InlineData("EU GOSTO DE TÁRTE", "tarte-tarte-tarte-tarte")]
[InlineData("EU GOSTO", "tarte-tarte")]
[InlineData("EU não GOSTO", "tarte-tarte-tarte")]
[InlineData("", "")]
public void ShouldReturnSlugifiedWithMocked(string value, string expectation)
{
const string translated = "tarte";
Expand Down
33 changes: 33 additions & 0 deletions Sluggy.Tests/ToLowerInvarianStrategyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using Xunit;

namespace Sluggy.Tests
{
public class ToLowerInvarianStrategyTests
{
[Trait("Project", "Sluggy")]
[Theory(DisplayName = "Should LowerCase With InvariantCulture")]
[InlineData("AbcDEf", "abcdef")]
[InlineData("GHIJKL", "ghijkl")]
[InlineData("", "")]
public void ShouldLowerCaseWithInvariantCulture(string value, string expectation)
{
var strategy = new ToLowerInvariantStrategy();

var translated = strategy.Translate(value);

Assert.Equal(expectation, translated);
}

[Trait("Project", "Sluggy")]
[Fact(DisplayName = "ToLowerInvariantStrategy Should Throw NullArgumentException")]
public void ShouldThrowNullArgumentException()
{
const string text = null;

var strategy = new ToLowerInvariantStrategy();

Assert.Throws<ArgumentNullException>(() => strategy.Translate(text));
}
}
}
19 changes: 14 additions & 5 deletions Sluggy/NormalizationStrategy.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
using System.Globalization;
using System;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Sluggy
{
public class NormalizationStrategy : ITranslationStrategy
{
public string Translate(string text) => string
.Concat(text
.Normalize(NormalizationForm.FormD)
.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark));
public string Translate(string text)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}

return string
.Concat(text
.Normalize(NormalizationForm.FormD)
.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark));
}
}
}
2 changes: 1 addition & 1 deletion Sluggy/Sluggy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class Sluggy
public static readonly string DefaultSeparator = "-";

public static readonly ITranslationStrategy DefaultTranslationStrategy = new CompositeStrategy(
new ToLowerInvariantStrategy(),
new NormalizationStrategy());

public static string ToSlug(this string text)
Expand Down Expand Up @@ -36,7 +37,6 @@ public static string ToSlug(this string text, string separator, ITranslationStra
return text
.Split()
.Where(t => t.Length != 0)
.Select(t => t.ToLowerInvariant())
.Select(t => strategy.Translate(t))
.Join(separator);
}
Expand Down
17 changes: 17 additions & 0 deletions Sluggy/ToLowerInvariantStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Sluggy
{
public class ToLowerInvariantStrategy : ITranslationStrategy
{
public string Translate(string text)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}

return text.ToLowerInvariant();
}
}
}
1 change: 1 addition & 0 deletions SluggyUnidecode.Tests/SluggyUnidecodeIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class SluggyUnidecodeIntegrationTests
[InlineData("ä ö ű ő Ä Ö Ũ Ő", "ae-oe-u-o-ae-oe-u-o")]
[InlineData("eu não gosto de pão da avó", "eu-nao-gosto-de-pao-da-avo")]
[InlineData("Работа с кириллицей", "rabota-s-kirillitsey")]
[InlineData("ch\u00e2teau vi\u00f1edos", "chateau-vinedos")]
public void ShouldConvertToSlugWithUnidecode(string value, string expectation)
{
var slugified = value.ToSlug();
Expand Down
2 changes: 1 addition & 1 deletion SluggyUnidecode/SluggyUnidecode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Extends the Sluggy package by using the Unidecode.Net library for normalization.
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Sluggy" Version="1.0.0" />
<PackageReference Include="Sluggy" Version="1.0.1" />
<PackageReference Include="Unidecode.NET" Version="1.3.0" />
</ItemGroup>

Expand Down

0 comments on commit 390e3b6

Please sign in to comment.