diff --git a/lib/profanity_filter.rb b/lib/profanity_filter.rb index 9cb137e..1cebdd9 100644 --- a/lib/profanity_filter.rb +++ b/lib/profanity_filter.rb @@ -37,6 +37,12 @@ class Base @@dictionary = YAML.load_file(@@dictionary_file) class << self + # Returns true if the text includes any profanity. Returns false if + # the text would not be changed by #clean + def profane?(text, replace_method='') + text != clean(text, replace_method) + end + def clean(text, replace_method = '') return text if text.blank? @replace_method = replace_method diff --git a/test/profanity_filter_test.rb b/test/profanity_filter_test.rb index 973b948..cd2b5a4 100644 --- a/test/profanity_filter_test.rb +++ b/test/profanity_filter_test.rb @@ -32,6 +32,13 @@ def test_basic_profanity_filter_replaces_punctuation_spaced_profane_words assert_equal '@#$%', ProfanityFilter::Base.clean('f.u.c.k') assert_equal 'happy-@#$%', ProfanityFilter::Base.clean('happy-fuck') end + + def test_knows_when_text_is_not_profane + assert !ProfanityFilter::Base.profane?('happy') + end + def test_knows_when_text_is_profane + assert ProfanityFilter::Base.profane?('fuck') + end end class DictionaryProfanityFilterTest < Test::Unit::TestCase @@ -61,6 +68,13 @@ def test_dictionary_profanity_filter_replaces_punctuation_spaced_profane_words assert_equal 'f*ck', ProfanityFilter::Base.clean('f.u.c.k', 'dictionary') assert_equal 'happy-f*ck', ProfanityFilter::Base.clean('happy-fuck', 'dictionary') end + + def test_dictionary_knows_when_text_is_not_profane + assert !ProfanityFilter::Base.profane?('happy', 'dictionary') + end + def test_dictionary_knows_when_text_is_profane + assert ProfanityFilter::Base.profane?('fuck', 'dictionary') + end end @@ -92,6 +106,13 @@ def test_vowels_profanity_filter_replaces_punctuation_spaced_profane_words assert_equal 'f*ck', ProfanityFilter::Base.clean('f.u.c.k', 'vowels') assert_equal 'happy-f*ck', ProfanityFilter::Base.clean('happy-fuck', 'vowels') end + + def test_vowels_knows_when_text_is_not_profane + assert !ProfanityFilter::Base.profane?('happy', 'vowels') + end + def test_vowels_knows_when_text_is_profane + assert ProfanityFilter::Base.profane?('fuck', 'vowels') + end end class HollowProfanityFilterTest < Test::Unit::TestCase @@ -122,4 +143,11 @@ def test_hollow_profanity_filter_replaces_punctuation_spaced_profane_words assert_equal 'f**k', ProfanityFilter::Base.clean('f.u.c.k', 'hollow') assert_equal 'happy-f**k', ProfanityFilter::Base.clean('happy-fuck', 'hollow') end -end \ No newline at end of file + + def test_hollow_knows_when_text_is_not_profane + assert !ProfanityFilter::Base.profane?('happy', 'hollow') + end + def test_hollow_knows_when_text_is_profane + assert ProfanityFilter::Base.profane?('fuck', 'hollow') + end +end