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

Euromoney Recruitment Test Submission. #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .nuget/NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
<activePackageSource>
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
</activePackageSource>
</configuration>
112 changes: 112 additions & 0 deletions ContentConsole.Test.Unit/BannedWordCounterServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ContentConsole.Helpers;
using ContentConsole.Services;
using Moq;
using NUnit.Framework;

namespace ContentConsole.Test.Unit
{
[TestFixture]
public class BannedWordCounterServiceTests
{
[Test]
public void CountBannedWords_OnLowercaseContentwithUppercaseBannedWord_CountsBannedWords()
{
const string upperBannedWord = "BANNED";
const string lowercaseContent = "banned content";
var lowercaseRetrievedBannedWordsService = new Mock<IBannedWordRetrievalService>();
lowercaseRetrievedBannedWordsService
.Setup(service => service.GetBannedWords())
.Returns(new List<string> { upperBannedWord });

IBannedWordCounterService bannedWordCounterService
= new BannedWordCounterService(lowercaseRetrievedBannedWordsService.Object, new PhraseMatcher());

Assert.AreEqual(bannedWordCounterService.CountBannedWords(lowercaseContent), 1);
}

[Test]
public void CountBannedWords_OnUppercaseContentwithLowercaseBannedWord_CountsBannedWords()
{
const string lowercaseBannedWord = "banned";
const string uppercaseContent = "BANNED CONTENT";
var lowercaseRetrievedBannedWordsService = new Mock<IBannedWordRetrievalService>();
lowercaseRetrievedBannedWordsService
.Setup(service => service.GetBannedWords())
.Returns(new List<string> { lowercaseBannedWord });

IBannedWordCounterService bannedWordCounterService
= new BannedWordCounterService(lowercaseRetrievedBannedWordsService.Object, new PhraseMatcher());

Assert.AreEqual(bannedWordCounterService.CountBannedWords(uppercaseContent), 1);
}

[Test]
public void CountBannedWords_BannedWordAtSentenceEndNoPunctuation_CountsBannedWords()
{
const string lowercaseBannedWord = "banned";
const string uppercaseContent = "This word is banned";
var lowercaseRetrievedBannedWordsService = new Mock<IBannedWordRetrievalService>();
lowercaseRetrievedBannedWordsService
.Setup(service => service.GetBannedWords())
.Returns(new List<string> { lowercaseBannedWord });

IBannedWordCounterService bannedWordCounterService
= new BannedWordCounterService(lowercaseRetrievedBannedWordsService.Object, new PhraseMatcher());

Assert.AreEqual(bannedWordCounterService.CountBannedWords(uppercaseContent), 1);
}

[Test]
public void CountBannedWords_BannedWordAtSentenceEndWithPunctuation_CountsBannedWords()
{
const string lowercaseBannedWord = "banned";
const string uppercaseContent = "This word is banned.";
var lowercaseRetrievedBannedWordsService = new Mock<IBannedWordRetrievalService>();
lowercaseRetrievedBannedWordsService
.Setup(service => service.GetBannedWords())
.Returns(new List<string> { lowercaseBannedWord });

IBannedWordCounterService bannedWordCounterService
= new BannedWordCounterService(lowercaseRetrievedBannedWordsService.Object, new PhraseMatcher());

Assert.AreEqual(bannedWordCounterService.CountBannedWords(uppercaseContent), 1);
}

[Test]
public void CountBannedWords_BannedWordAtSentenceStartAndEnd_CountsBannedWords()
{
const string lowercaseBannedWord = "banned";
const string uppercaseContent = "banned words are banned";
var lowercaseRetrievedBannedWordsService = new Mock<IBannedWordRetrievalService>();
lowercaseRetrievedBannedWordsService
.Setup(service => service.GetBannedWords())
.Returns(new List<string> { lowercaseBannedWord });

IBannedWordCounterService bannedWordCounterService
= new BannedWordCounterService(lowercaseRetrievedBannedWordsService.Object, new PhraseMatcher());

Assert.AreEqual(bannedWordCounterService.CountBannedWords(uppercaseContent), 2);
}

[Test]
public void CountBannedWords_BannedWordAtSentenceStart_CountsBannedWords()
{
const string lowercaseBannedWord = "banned";
const string uppercaseContent = "banned words";
var lowercaseRetrievedBannedWordsService = new Mock<IBannedWordRetrievalService>();
lowercaseRetrievedBannedWordsService
.Setup(service => service.GetBannedWords())
.Returns(new List<string> { lowercaseBannedWord });

IBannedWordCounterService bannedWordCounterService
= new BannedWordCounterService(lowercaseRetrievedBannedWordsService.Object, new PhraseMatcher());

Assert.AreEqual(bannedWordCounterService.CountBannedWords(uppercaseContent), 1);
}
}
}
8 changes: 8 additions & 0 deletions ContentConsole.Test.Unit/BannedWordFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

b1
b2

b3
B3


42 changes: 42 additions & 0 deletions ContentConsole.Test.Unit/BannedWordFileRepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ContentConsole.DataRepository;
using NUnit.Framework;

namespace ContentConsole.Test.Unit
{
[TestFixture]
public class BannedWordFileRepositoryTests
{
[Test]
public void Initialize_GivenFileThatDoesNotExist_ThrowsFileNotFoundException()
{
Assert.Throws(typeof(ArgumentException),
delegate { throw new ArgumentException(); });
Assert.Throws<FileNotFoundException>(() => new BannedWordFileRepository("NotABannedWordFile.txt"));
}

[Test]
public void Initialize_GivenFileThatExists_DoesNotThrowException()
{
IBannedWordRepository repository = new BannedWordFileRepository("../../BannedWordFile.txt");
}

[Test]
public void Initialize_GivenFileThatExists_ReadsFileWords()
{
IBannedWordRepository repository = new BannedWordFileRepository("../../BannedWordFile.txt");

Assert.IsTrue(repository.Contains("b1"));
Assert.IsTrue(repository.Contains("b2"));
Assert.IsTrue(repository.Contains("b3"));
Assert.IsTrue(repository.Contains("B3"));

Assert.AreEqual(repository.GetAll().Count(), 3);
}
}
}
111 changes: 111 additions & 0 deletions ContentConsole.Test.Unit/BannedWordReplacementServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ContentConsole.Helpers;
using ContentConsole.Services;
using Moq;
using NUnit.Framework;

namespace ContentConsole.Test.Unit
{
[TestFixture]
public class BannedWordReplacementServiceTests
{

private IBannedWordRetrievalService CreatedMockBannedWordRetrievalService()
{
var mockRetrievalService = new Mock<IBannedWordRetrievalService>();
mockRetrievalService.Setup(service => service.GetBannedWords())
.Returns(new List<string> { "a", "an", "banned", "WORD", "bad" });
return mockRetrievalService.Object;
}

[Test]
public void ReplaceBannedWords_OnGivenNullContent_ReturnsNull()
{
IBannedWordReplacementService replacementService =
new BannedWordReplacementService(CreatedMockBannedWordRetrievalService(), new PhraseMatcher());
Assert.IsNull(replacementService.ReplaceBannedWords(null));
}

[Test]
public void ReplaceBannedWords_OnGivenEmptyContent_ReturnsEmptyString()
{
IBannedWordReplacementService replacementService =
new BannedWordReplacementService(CreatedMockBannedWordRetrievalService(), new PhraseMatcher());
Assert.AreEqual("", replacementService.ReplaceBannedWords(""));
}

[Test]
public void ReplaceBannedWords_OnGivenEmptyPaddedContent_ReturnsEmptyOriginalPaddedInput()
{
IBannedWordReplacementService replacementService =
new BannedWordReplacementService(CreatedMockBannedWordRetrievalService(), new PhraseMatcher());
const string testContent = "\t\n ";
Assert.AreEqual(testContent, replacementService.ReplaceBannedWords(testContent), testContent);
}

[Test]
public void ReplaceBannedWords_OnGivenBannedWordContentLower_ReplacesBannedWordsWithOriginalCapitalization()
{
IBannedWordReplacementService replacementService =
new BannedWordReplacementService(CreatedMockBannedWordRetrievalService(), new PhraseMatcher());
const string testContent = "lower case banned word ";
const string expectedReplacement = "lower case b####d w##d ";
Assert.AreEqual(expectedReplacement, replacementService.ReplaceBannedWords(testContent));
}

[Test]
public void ReplaceBannedWords_OnGivenBannedWordContentUpper_ReplacesBannedWordsWithOriginalCapitalization()
{
IBannedWordReplacementService replacementService =
new BannedWordReplacementService(CreatedMockBannedWordRetrievalService(), new PhraseMatcher());
const string testContent = "UPPER CASE BANNED WORD ";
const string expectedReplacement = "UPPER CASE B####D W##D ";
Assert.AreEqual(expectedReplacement, replacementService.ReplaceBannedWords(testContent));
}

[Test]
public void ReplaceBannedWords_OnGivenSingleLetterBannedWord_HidesEntireBannedWord()
{
IBannedWordReplacementService replacementService =
new BannedWordReplacementService(CreatedMockBannedWordRetrievalService(), new PhraseMatcher());
const string testContent = "a lower case banned word ";
const string expectedReplacement = "# lower case b####d w##d ";
Assert.AreEqual(expectedReplacement, replacementService.ReplaceBannedWords(testContent));
}

[Test]
public void ReplaceBannedWords_OnGivenTwoLetterBannedWord_HidesEntireBannedWord()
{
IBannedWordReplacementService replacementService =
new BannedWordReplacementService(CreatedMockBannedWordRetrievalService(), new PhraseMatcher());
const string testContent = "AN UPPER CASE BANNED WORD ";
const string expectedReplacement = "## UPPER CASE B####D W##D ";
Assert.AreEqual(expectedReplacement, replacementService.ReplaceBannedWords(testContent));
}

[Test]
public void ReplaceBannedWords_OnGivenThreeLetterBannedWord_HidesMiddleLetterOfBannedWord()
{
IBannedWordReplacementService replacementService =
new BannedWordReplacementService(CreatedMockBannedWordRetrievalService(), new PhraseMatcher());
const string testContent = "The weather in Manchester in winter is bad.It rains all the time -it must be horrible for people visiting.";
const string expectedReplacement = "The weather in Manchester in winter is b#d.It rains all the time -it must be horrible for people visiting.";
Assert.AreEqual(expectedReplacement, replacementService.ReplaceBannedWords(testContent));
}

// We might want to check for this, but for now don't check for pluralization and skip the word.
[Test]
public void ReplaceBannedWords_OnGivenBadWordPluralized_DoesNotReplacePluralizedBannedWord()
{
IBannedWordReplacementService replacementService =
new BannedWordReplacementService(CreatedMockBannedWordRetrievalService(), new PhraseMatcher());
const string testContent = "UPPER CASE BANNED WORDS ";
const string expectedReplacement = "UPPER CASE B####D WORDS ";
Assert.AreEqual(expectedReplacement, replacementService.ReplaceBannedWords(testContent));
}
}
}
Loading