-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from RubenMateus/feat_SF_add-casing-strategy
Add new strategy type and tests
- Loading branch information
Showing
10 changed files
with
178 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters