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

Euro Money Recruitment Test #21

Open
wants to merge 1 commit 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
8 changes: 8 additions & 0 deletions ContentConsole.Test.Unit/ContentConsole.Test.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,19 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="NegativeWordsUnitTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestNegativeWordCheck.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ContentConsole\ContentConsole.csproj">
<Project>{70DA2B36-EBF3-4438-9F95-ECC828A64527}</Project>
<Name>ContentConsole</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
86 changes: 86 additions & 0 deletions ContentConsole.Test.Unit/NegativeWordsUnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using NegativeWordUnitTest;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ContentConsole.Test.Unit
{
[TestFixture]
public class NegativeWordsUnitTests
{
private const string Phrase = "The weather in Manchester in winter is bad. It rains all the time - it must be horrible for people visiting.";

[Test]
public void NullResultCheck()
{
var negativeWordCheck = new TestNegativeWordCheck();
var result = negativeWordCheck.RunNegativeWordCheck(Phrase, false);

Assert.IsNotNull(result);
}

[Test]
public void NegativeFilteringNotEnabled()
{
var negativeWordCheck = new TestNegativeWordCheck();
var result = negativeWordCheck.RunNegativeWordCheck(Phrase, false);

Assert.IsTrue(!result.Phrase.Contains("#")
&& result.Phrase == Phrase
&& result.NegativeWordCount == 2);
}

[Test]
public void NegativeFilteringEnabled()
{
var negativeWordCheck = new TestNegativeWordCheck();
var result = negativeWordCheck.RunNegativeWordCheck(Phrase, true);
var hashNum = result.Phrase.Count(x => x == '#');

Assert.IsTrue(hashNum == 7);
}

[Test]
public void WholeWordFilteredOnly()
{
var negativeWordCheck = new TestNegativeWordCheck();
var phrase = "The weather in Manchester in winter is badminton. It rains all the time - it must be horrible for people visiting.";
var result = negativeWordCheck.RunNegativeWordCheck(phrase, true);
var hashNum = result.Phrase.Count(x => x == '#');

Assert.IsTrue(hashNum == 6);
}

[Test]
public void WholeNegativeWordsCountedOnly()
{
var negativeWordCheck = new TestNegativeWordCheck();
var phrase = "The weather in Manchester in winter is badminton. It rains all the time - it must be horrible for people visiting.";
var result = negativeWordCheck.RunNegativeWordCheck(phrase, true);
var hashNum = result.Phrase.Count(x => x == '#');

Assert.IsTrue(result.NegativeWordCount == 1);
}

[Test]
public void NegativeWordCountCheck()
{
var negativeWordCheck = new TestNegativeWordCheck();
var result = negativeWordCheck.RunNegativeWordCheck(Phrase, true);

Assert.IsTrue(result.NegativeWordCount == 2);
}

[Test]
public void NegativeWordEmptyList()
{
var negativeWordCheck = new TestNegativeWordCheck {NegativeWords = new List<string>()};
var result = negativeWordCheck.RunNegativeWordCheck(Phrase, true);

Assert.IsTrue(result.NegativeWordCount == 0);
}
}
}
28 changes: 28 additions & 0 deletions ContentConsole.Test.Unit/TestNegativeWordCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using ContentConsole;
using System;
using System.Collections.Generic;
using System.Text;

namespace NegativeWordUnitTest
{
public class TestNegativeWordCheck: NegativeWordCheck
{
public List<string> NegativeWords { get; set; }

public override List<string> GetNegativeWords(List<string> negativeWords)
{
if (this.NegativeWords == null)
{
negativeWords = new List<string>()
{
"swine",
"bad",
"nasty",
"horrible",
};
}

return negativeWords;
}
}
}
2 changes: 2 additions & 0 deletions ContentConsole/ContentConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="NegativePhrase.cs" />
<Compile Include="NegativeWordCheck.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions ContentConsole/NegativePhrase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ContentConsole
{
public class NegativePhrase
{
public int NegativeWordCount { get; set; }

public string Phrase { get; set; }
}
}
51 changes: 51 additions & 0 deletions ContentConsole/NegativeWordCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;

namespace ContentConsole
{
public class NegativeWordCheck
{
public virtual NegativePhrase RunNegativeWordCheck(string text = null, bool enableFiltering = true)
{
var negativeWords = this.GetNegativeWords(new List<string>());

var result = this.PhraseCheck(negativeWords, text, enableFiltering);

return result;
}

public virtual NegativePhrase PhraseCheck(List<string> negativeWords, string text, bool enableFiltering)
{
var negativeWordCount = 0;

foreach (var word in negativeWords)
{
negativeWordCount += Regex.Matches(text, @"\b" + word + @"\b").Count;

if (!enableFiltering) continue;
var safeWord = word.Replace(word.Substring(1, word.Length - 2), new string('#', word.Length - 2));
text = Regex.Replace(text, @"\b" + word + @"\b", safeWord);
}

return new NegativePhrase() { NegativeWordCount = negativeWordCount, Phrase = text };
}

public virtual List<string> GetNegativeWords(List<string> negativeWords)
{
Console.WriteLine("\nPlease add a bad word and click enter");
negativeWords.Add(Console.ReadLine());
Console.WriteLine("Bad Word added, please press Y to add another word or Enter to continue");

var key = Console.ReadKey().Key;

if (key.Equals(System.ConsoleKey.Y))
{
GetNegativeWords(negativeWords);
}

return negativeWords;
}
}
}
34 changes: 7 additions & 27 deletions ContentConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,16 @@ public static class Program
{
public static void Main(string[] args)
{
string bannedWord1 = "swine";
string bannedWord2 = "bad";
string bannedWord3 = "nasty";
string bannedWord4 = "horrible";
Console.WriteLine("\nPlease enter phrase to check over");
var text = Console.ReadLine();
Console.WriteLine("Please press Y to disable filtering of negative words or press Enter to continue");

string content =
"The weather in Manchester in winter is bad. It rains all the time - it must be horrible for people visiting.";
var enableFiltering = !Console.ReadKey().Key.Equals(ConsoleKey.Y);
var negativeWordCheck = new NegativeWordCheck();

int badWords = 0;
if (content.Contains(bannedWord1))
{
badWords = badWords + 1;
}
if (content.Contains(bannedWord2))
{
badWords = badWords + 1;
}
if (content.Contains(bannedWord3))
{
badWords = badWords + 1;
}
if (content.Contains(bannedWord4))
{
badWords = badWords + 1;
}

Console.WriteLine("Scanned the text:");
Console.WriteLine(content);
Console.WriteLine("Total Number of negative words: " + badWords);
var result = negativeWordCheck.RunNegativeWordCheck(text, enableFiltering);

Console.WriteLine("Scanned the Text: \n" + result.Phrase + "\nTotal Number of negative words: " + result.NegativeWordCount);
Console.WriteLine("Press ANY key to exit.");
Console.ReadKey();
}
Expand Down