Skip to content

Commit

Permalink
add ProfanityFilter::Base.profane?
Browse files Browse the repository at this point in the history
  • Loading branch information
thoughtless committed Jan 20, 2010
1 parent 973e3a3 commit be405a2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/profanity_filter.rb
Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion test/profanity_filter_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

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

0 comments on commit be405a2

Please sign in to comment.