Skip to content

Commit

Permalink
Handle nil values appropriately. [#9 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
adambair committed Jun 29, 2008
1 parent a6407ba commit f1cdf29
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/profanity_filter.rb
Expand Up @@ -34,7 +34,7 @@ class Base
DICTIONARY = YAML.load_file(File.join(File.dirname(__FILE__), '../config/dictionary.yml'))

def self.clean(text, replace_method = '')
text.split(/(\W)/).collect{|word| replace_method == 'dictionary' ? clean_word_dictionary(word) : clean_word_basic(word)}.join
text.split(/(\W)/).collect{|word| replace_method == 'dictionary' ? clean_word_dictionary(word) : clean_word_basic(word)}.join rescue nil
end

def self.clean_word_dictionary(word)
Expand Down
28 changes: 28 additions & 0 deletions test/benign_filter_test.rb
Expand Up @@ -29,6 +29,20 @@ def test_it_should_filter_specified_fields
assert_equal 'This is some @#$% post by a @#$% user', p.body_clean
assert_equal 'This is some shitty post by a fucking user', p.body_original
end

def test_it_should_handle_nil_fields_bug_9
p = Post.new({:title => nil, :body => nil})
p.save
assert_equal nil, p.title
assert_equal nil, p.body
end

def test_it_should_handle_blank_fields_bug_9
p = Post.new({:title => "", :body => ""})
p.save
assert_equal "", p.title
assert_equal "", p.body
end
end

class DictionaryProfanityFilterTest < Test::Unit::TestCase
Expand All @@ -48,4 +62,18 @@ def test_it_should_filter_specified_fields
assert_equal 'This is some sh*tty post by a f*ck*ng user', p.body_clean
assert_equal 'This is some shitty post by a fucking user', p.body_original
end

def test_it_should_handle_nil_fields_bug_9
p = Post.new({:title => nil, :body => nil})
p.save
assert_equal nil, p.title
assert_equal nil, p.body
end

def test_it_should_handle_blank_fields_bug_9
p = Post.new({:title => "", :body => ""})
p.save
assert_equal "", p.title
assert_equal "", p.body
end
end
28 changes: 28 additions & 0 deletions test/destructive_filter_test.rb
Expand Up @@ -25,6 +25,20 @@ def test_it_should_filter_specified_fields
assert_equal 'A @#$% Title', p.title
assert_equal 'This is some @#$% post by a @#$% user', p.body
end

def test_it_should_handle_nil_fields_bug_9
p = Post.new({:title => nil, :body => nil})
p.save
assert_equal nil, p.title
assert_equal nil, p.body
end

def test_it_should_handle_blank_fields_bug_9
p = Post.new({:title => "", :body => ""})
p.save
assert_equal "", p.title
assert_equal "", p.body
end
end

class DictionaryProfanityFilterTest < Test::Unit::TestCase
Expand All @@ -40,4 +54,18 @@ def test_it_should_filter_specified_fields
assert_equal 'A f*ck*ng Title', p.title
assert_equal 'This is some sh*tty post by a f*ck*ng user', p.body
end

def test_it_should_handle_nil_fields_bug_9
p = Post.new({:title => nil, :body => nil})
p.save
assert_equal nil, p.title
assert_equal nil, p.body
end

def test_it_should_handle_blank_fields_bug_9
p = Post.new({:title => "", :body => ""})
p.save
assert_equal "", p.title
assert_equal "", p.body
end
end

0 comments on commit f1cdf29

Please sign in to comment.