Skip to content

commit #32

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

Open
wants to merge 2 commits into
base: main
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: 1 addition & 2 deletions ProfanityFilter.Tests.Unit/AllowListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using System;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ProfanityFilter;
using ProfanityFilter.Interfaces;

namespace ProfanityFilter.Tests.Unit
Expand Down
29 changes: 14 additions & 15 deletions ProfanityFilter.Tests.Unit/ProfanityBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void ConstructorOverridesProfanityListWithArray()
{
"fuck",
"shit",
"bollocks"
"bollocks"
};

IProfanityFilter filter = new ProfanityFilter(_wordList);
Expand All @@ -62,9 +62,9 @@ public void ConstructorOverridesProfanityList()
{
string[] _wordList =
{
"fuck",
"shit",
"bollocks"
"fuck",
"shit",
"bollocks"
};

IProfanityFilter filter = new ProfanityFilter(new List<string>(_wordList));
Expand All @@ -85,7 +85,7 @@ public void AddProfanityThrowsArgumentNullExceptionForNullProfanity()
public void AddProfanityThrowsArgumentNullExceptionForEmptyProfanityString()
{
var filter = new ProfanityBase();
filter.AddProfanity((string)string.Empty);
filter.AddProfanity(string.Empty);
}

[TestMethod]
Expand All @@ -111,9 +111,9 @@ public void AddProfanityAddsToProfanityArray()
{
string[] _wordList =
{
"fuck",
"shit",
"bollocks"
"fuck",
"shit",
"bollocks"
};

var filter = new ProfanityBase();
Expand All @@ -131,9 +131,9 @@ public void AddProfanityAddsToProfanityList()
{
string[] _wordList =
{
"fuck",
"shit",
"bollocks"
"fuck",
"shit",
"bollocks"
};

var filter = new ProfanityBase();
Expand All @@ -150,7 +150,7 @@ public void AddProfanityAddsToProfanityList()
public void ReturnCountForDetaultProfanityList()
{
var filter = new ProfanityBase();
int count = filter.Count;
var count = filter.Count;

Assert.AreEqual(count, 1615);
}
Expand Down Expand Up @@ -214,15 +214,14 @@ public void RemoveDeletesAProfanityAndReturnsFalseIfProfanityDoesntExist()
Assert.AreEqual(1615, filter.Count);
}


[TestMethod]
public void RemoveListDeletesProfanitiesFromPrimaryList()
{
var filter = new ProfanityBase();

Assert.AreEqual(1615, filter.Count);

List<string> listOfProfanitiesToRemove = new List<string>
var listOfProfanitiesToRemove = new List<string>
{
"shit",
"fuck",
Expand All @@ -241,7 +240,7 @@ public void RemoveArrayDeletesProfanitiesFromPrimaryList()

Assert.AreEqual(1615, filter.Count);

string []listOfProfanitiesToRemove = new string[]
string[] listOfProfanitiesToRemove =
{
"shit",
"fuck",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>
<ReleaseVersion>0.1.2</ReleaseVersion>
Expand Down
4 changes: 2 additions & 2 deletions ProfanityFilter.Tests.Unit/ProfanityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void DetectAllProfanitiesReturnsEmptyListForEmptyInput()
}

[TestMethod]
public void DetectAllProfanitiesReturnsNulListForEmptyInput()
public void DetectAllProfanitiesReturnsNullListForEmptyInput()
{
var filter = new ProfanityFilter();
var swearList = filter.DetectAllProfanities(null);
Expand Down Expand Up @@ -329,7 +329,7 @@ public void CensoredStringReturnsStringWithProfanitiesBleepedOut()
filter.AllowList.Add("scunthorpe");
filter.AllowList.Add("penistone");

var censored = filter.CensorString("I fucking live in Scunthorpe and it is a shit place to live. I would much rather live in penistone you great big cock fuck.", '*');
var censored = filter.CensorString("Выдать заказ лоху", '*');
var result = "I ******* live in Scunthorpe and it is a **** place to live. I would much rather live in penistone you great big **** ****.";

Assert.AreEqual(censored, result);
Expand Down
43 changes: 18 additions & 25 deletions ProfanityFilter/ProfanityFilter/AllowList.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
MIT License
Copyright (c) 2019
Copyright (c) 2019
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand All @@ -17,6 +17,7 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand All @@ -27,11 +28,20 @@ namespace ProfanityFilter
{
public class AllowList : IAllowList
{
List<string> _allowList;
private readonly List<string> _allowList;

public AllowList()
{
_allowList = new List<string>();
_allowList = new List<string>
{
"\bолеговн.*\b",
"\bгребля\b",
"\b.*(С|с)ергей.*\b",
"\b.*к(о|а)манд.*\b",
"\b.*л(о|а)х(о|а)трон.*\b",
"\bхул(е|и)ган\b",
"\b.*м(а|о)нд(а|о)рин.*\b",
};
}

/// <summary>
Expand All @@ -52,15 +62,10 @@ public ReadOnlyCollection<string> ToList
/// <param name="wordToAllowlist">The word that you want to allow list.</param>
public void Add(string wordToAllowlist)
{
if (string.IsNullOrEmpty(wordToAllowlist))
{
throw new ArgumentNullException(nameof(wordToAllowlist));
}
if (string.IsNullOrEmpty(wordToAllowlist)) throw new ArgumentNullException(nameof(wordToAllowlist));

if (!_allowList.Contains(wordToAllowlist.ToLower(CultureInfo.InvariantCulture)))
{
_allowList.Add(wordToAllowlist.ToLower(CultureInfo.InvariantCulture));
}
}

/// <summary>
Expand All @@ -70,10 +75,7 @@ public void Add(string wordToAllowlist)
/// <returns></returns>
public bool Contains(string wordToCheck)
{
if (string.IsNullOrEmpty(wordToCheck))
{
throw new ArgumentNullException(nameof(wordToCheck));
}
if (string.IsNullOrEmpty(wordToCheck)) throw new ArgumentNullException(nameof(wordToCheck));

return _allowList.Contains(wordToCheck.ToLower(CultureInfo.InvariantCulture));
}
Expand All @@ -82,13 +84,7 @@ public bool Contains(string wordToCheck)
/// Return the number of items in the allow list.
/// </summary>
/// <returns>The number of items in the allow list.</returns>
public int Count
{
get
{
return _allowList.Count;
}
}
public int Count => _allowList.Count;

/// <summary>
/// Remove all words from the allow list.
Expand All @@ -105,12 +101,9 @@ public void Clear()
/// <returns>True if the word is successfuly removes, False otherwise.</returns>
public bool Remove(string wordToRemove)
{
if (string.IsNullOrEmpty(wordToRemove))
{
throw new ArgumentNullException(nameof(wordToRemove));
}
if (string.IsNullOrEmpty(wordToRemove)) throw new ArgumentNullException(nameof(wordToRemove));

return _allowList.Remove(wordToRemove.ToLower(CultureInfo.InvariantCulture));
}
}
}
}
64 changes: 13 additions & 51 deletions ProfanityFilter/ProfanityFilter/ProfanityBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
MIT License
Copyright (c) 2019
Copyright (c) 2019
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand All @@ -18,7 +18,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using System;
using System;
using System.Collections.Generic;
using System.Globalization;

Expand All @@ -43,10 +43,7 @@ public ProfanityBase()
/// <param name="profanityList">Array of words considered profanities.</param>
protected ProfanityBase(string[] profanityList)
{
if (profanityList == null)
{
throw new ArgumentNullException(nameof(profanityList));
}
if (profanityList == null) throw new ArgumentNullException(nameof(profanityList));

_profanities = new List<string>(profanityList);
}
Expand All @@ -58,10 +55,7 @@ protected ProfanityBase(string[] profanityList)
/// <param name="profanityList">List of words considered profanities.</param>
protected ProfanityBase(List<string> profanityList)
{
if (profanityList == null)
{
throw new ArgumentNullException(nameof(profanityList));
}
if (profanityList == null) throw new ArgumentNullException(nameof(profanityList));

_profanities = profanityList;
}
Expand All @@ -72,10 +66,7 @@ protected ProfanityBase(List<string> profanityList)
/// <param name="profanity">The profanity to add.</param>
public void AddProfanity(string profanity)
{
if (string.IsNullOrEmpty(profanity))
{
throw new ArgumentNullException(nameof(profanity));
}
if (string.IsNullOrEmpty(profanity)) throw new ArgumentNullException(nameof(profanity));

_profanities.Add(profanity);
}
Expand All @@ -87,10 +78,7 @@ public void AddProfanity(string profanity)
/// <param name="profanityList">The array of profanities to add.</param>
public void AddProfanity(string[] profanityList)
{
if (profanityList == null)
{
throw new ArgumentNullException(nameof(profanityList));
}
if (profanityList == null) throw new ArgumentNullException(nameof(profanityList));

_profanities.AddRange(profanityList);
}
Expand All @@ -102,10 +90,7 @@ public void AddProfanity(string[] profanityList)
/// <param name="profanityList">The list of profanities to add.</param>
public void AddProfanity(List<string> profanityList)
{
if (profanityList == null)
{
throw new ArgumentNullException(nameof(profanityList));
}
if (profanityList == null) throw new ArgumentNullException(nameof(profanityList));

_profanities.AddRange(profanityList);
}
Expand All @@ -117,10 +102,7 @@ public void AddProfanity(List<string> profanityList)
/// <returns>True of the profanity was removed. False otherwise.</returns>
public bool RemoveProfanity(string profanity)
{
if (string.IsNullOrEmpty(profanity))
{
throw new ArgumentNullException(nameof(profanity));
}
if (string.IsNullOrEmpty(profanity)) throw new ArgumentNullException(nameof(profanity));

return _profanities.Remove(profanity.ToLower(CultureInfo.InvariantCulture));
}
Expand All @@ -132,18 +114,11 @@ public bool RemoveProfanity(string profanity)
/// <returns>True if the profanities were removed. False otherwise.</returns>
public bool RemoveProfanity(List<string> profanities)
{
if (profanities == null)
{
throw new ArgumentNullException(nameof(profanities));
}
if (profanities == null) throw new ArgumentNullException(nameof(profanities));

foreach (string naughtyWord in profanities)
{
foreach (var naughtyWord in profanities)
if (!RemoveProfanity(naughtyWord))
{
return false;
}
}

return true;
}
Expand All @@ -155,18 +130,11 @@ public bool RemoveProfanity(List<string> profanities)
/// <returns>True if the profanities were removed. False otherwise.</returns>
public bool RemoveProfanity(string[] profanities)
{
if (profanities == null)
{
throw new ArgumentNullException(nameof(profanities));
}
if (profanities == null) throw new ArgumentNullException(nameof(profanities));

foreach (string naughtyWord in profanities)
{
foreach (var naughtyWord in profanities)
if (!RemoveProfanity(naughtyWord))
{
return false;
}
}

return true;
}
Expand All @@ -182,12 +150,6 @@ public void Clear()
/// <summary>
/// Return the number of profanities in the system.
/// </summary>
public int Count
{
get
{
return _profanities.Count;
}
}
public int Count => _profanities.Count;
}
}
Loading