Created
March 13, 2010 05:40
-
-
Save kiy0taka/331136 to your computer and use it in GitHub Desktop.
This file contains 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
package exam1; | |
public class WordFilter { | |
private String word; | |
public WordFilter(String word) { | |
this.word = word; | |
} | |
public boolean detect(String message) { | |
if (message == null) { | |
throw new IllegalArgumentException(); | |
} | |
if (word == null || word.isEmpty()) { | |
return false; | |
} | |
if (message.indexOf(this.word) > 0) { | |
return true; | |
} | |
return false; | |
} | |
public String censor(String message) { | |
if (detect(message)) { | |
return message.replace(word, "<censored>"); | |
} | |
return message; | |
} | |
} |
This file contains 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
package exam1; | |
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class WorldFilterTest { | |
@Test | |
public void ワードが含まれていたらtrueをかえす() throws Exception { | |
WordFilter filter = new WordFilter("TDD"); | |
assertEquals(true, filter.detect("user:TDD楽しい")); | |
} | |
@Test | |
public void ワードが含まれていなかったらfalseをかえす() throws Exception { | |
WordFilter filter = new WordFilter("TDD"); | |
assertEquals(false, filter.detect("user:DDT楽しい")); | |
} | |
@Test | |
public void 異なるワードを指定してもfalseをかえす() throws Exception { | |
WordFilter filter = new WordFilter("BDD"); | |
assertEquals(false, filter.detect("user:DDT楽しい")); | |
} | |
@Test | |
public void 異なるワードを指定してtrueをかえす() throws Exception { | |
WordFilter filter = new WordFilter("BDD"); | |
assertEquals(true, filter.detect("user:BDD楽しい")); | |
} | |
@Test | |
public void ワードがnullならfalseを返す() { | |
WordFilter filter = new WordFilter(null); | |
assertEquals(false, filter.detect("user:TDD楽しい")); | |
} | |
@Test | |
public void ワードが空文字ならfalseを返す() { | |
WordFilter filter = new WordFilter(""); | |
assertEquals(false, filter.detect("user:TDD楽しい")); | |
} | |
@Test | |
public void ワードが複数含まれていたらtrueをかえす() throws Exception { | |
WordFilter filter = new WordFilter("BDD"); | |
assertEquals(true, filter.detect("user:BDD楽しい、たのしいよBDD")); | |
} | |
@Test | |
public void ユーザ名とワードが同じでもメッセージがマッチしないときはfalseを返す() { | |
WordFilter filter = new WordFilter("TDD"); | |
assertEquals(false, filter.detect("TDD:BDD楽しい、たのしいよBDD")); | |
} | |
@Test(expected=IllegalArgumentException.class) | |
public void メッセージがnullなら例外を投げる() throws Exception { | |
WordFilter filter = new WordFilter("TDD"); | |
filter.detect(null); | |
} | |
@Test | |
public void wordが含まれていたらcensoredに置き換える() throws Exception { | |
WordFilter filter = new WordFilter("TDD"); | |
assertEquals("user:<censored>楽しい", filter.censor("user:TDD楽しい")); | |
} | |
@Test | |
public void wordが含まれていなかったらそのまま返す() throws Exception { | |
WordFilter filter = new WordFilter("TDD"); | |
assertEquals("user:BDD楽しい", filter.censor("user:BDD楽しい")); | |
} | |
@Test | |
public void 異なるwordが含まれていてもcensoredに置き換える() throws Exception { | |
WordFilter filter = new WordFilter("DDT"); | |
assertEquals("user:楽しいね<censored>", filter.censor("user:楽しいねDDT")); | |
} | |
@Test(expected=IllegalArgumentException.class) | |
public void messageがnullだったら例外を投げる() throws Exception { | |
WordFilter filter = new WordFilter("DDT"); | |
filter.censor(null); | |
} | |
@Test | |
public void wordが複数含まれていてもcensoredに置き換える() throws Exception { | |
WordFilter filter = new WordFilter("DDT"); | |
assertEquals("user:楽しいね<censored>、<censored>楽しいね", filter.censor("user:楽しいねDDT、DDT楽しいね")); | |
} | |
@Test | |
public void wordは正規表現に対応していないのでcensoredに置き換えられない() throws Exception { | |
WordFilter filter = new WordFilter("D.T"); | |
assertEquals("user:楽しいねDDT、DDT楽しいね", filter.censor("user:楽しいねDDT、DDT楽しいね")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment