Created
March 13, 2010 06:31
-
-
Save ukstudio/331153 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class WordFilter | |
| def initialize(*ng_words) | |
| @ng_words = ng_words | |
| end | |
| def detect(message) | |
| message =~ /#{@ng_words.join('|')}/ | |
| end | |
| def censor(message) | |
| message.gsub(/#{@ng_words.join('|')}/, "<censored>") | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'spec' | |
| require 'word_filter' | |
| describe WordFilter do | |
| it "NGWordをひとつ指定できること" do | |
| lambda do | |
| filter = WordFilter.new("NGWord") | |
| end.should_not raise_error | |
| end | |
| it "NGWordを複数指定できること" do | |
| lambda do | |
| filter = WordFilter.new("NGWord1", "NGWord2") | |
| end.should_not raise_error | |
| end | |
| context "NGWordが指定されている場合" do | |
| before do | |
| @filter = WordFilter.new("NGWord") | |
| end | |
| it "NGWordをdetectできること" do | |
| @filter.detect("NGWord").should be_true | |
| end | |
| it "NGWordじゃない場合detectしない" do | |
| @filter.detect("hoge").should be_false | |
| end | |
| it "NGWordをcensorできること" do | |
| @filter.censor("t_wada: あいうNGWord").should == "t_wada: あいう<censored>" | |
| end | |
| it "NGWordを含まなければそのまま返すこと" do | |
| @filter.censor("t_wada: あいうえお").should == "t_wada: あいうえお" | |
| end | |
| end | |
| context "NGWordが複数指定している場合" do | |
| before do | |
| @filter = WordFilter.new("NGWord1", "NGWord2") | |
| end | |
| it "NGWordをdetectできること" do | |
| @filter.detect("NGWord1").should be_true | |
| @filter.detect("NGWord2").should be_true | |
| end | |
| it "NGWordをcensorできること" do | |
| @filter.censor("t_wada: あいうNGWord1").should == "t_wada: あいう<censored>" | |
| @filter.censor("t_wada: あいうNGWord2").should == "t_wada: あいう<censored>" | |
| @filter.censor("t_wada: あいうえお").should == "t_wada: あいうえお" | |
| end | |
| end | |
| context "IDがNGWordだったら" do | |
| before do | |
| @filter = WordFilter.new('t_wada') | |
| end | |
| it "IDはcensorしない" do | |
| pending | |
| @filter.censor("t_wada: t_wada").should == "t_wada: <censored>" | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment