-
-
Save meizzhou/7128662 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
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertTrue; | |
import java.io.ByteArrayOutputStream; | |
import java.io.PrintStream; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
/** | |
* JUnit tests for the AssassinManager class. | |
* | |
* NOTE: This test suite will *not* work properly if you use 'println' in your | |
* AssassinManager methods. Use 'printf' with '\n' for newlines instead. ( | |
* credit: http://www.cis.upenn.edu | |
* /~matuszek/cit594-2008/Lectures/28-junit-for-output.ppt) | |
* | |
* I'm pretty certain if you fail any of these tests, something is wrong with | |
* your code. However, if you pass all of them, no guarantees that your code is | |
* 100% correct. | |
* | |
* @author Kevin Vu | |
*/ | |
public class AssassinManagerTest { | |
private static AssassinManager manager; | |
// Workaround for testing print output: | |
// http://stackoverflow.com/questions/1119385/junit-test-for-system-out-println | |
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); | |
private final String separator = System.getProperty("line.separator"); | |
// Redirect stdout to outContent. Setup an AssassinManager. | |
@Before | |
public void setUpBefore() throws Exception { | |
System.setOut(new PrintStream(outContent)); | |
manager = new AssassinManager(new ArrayList<String>( | |
Arrays.asList(new String[] { "bob", "lisa", "tom" }))); | |
} | |
// Reset stdout. | |
@After | |
public void cleanUpStreams() { | |
System.setOut(System.out); | |
} | |
@Test | |
public void testIsGameOverAndWinner() { | |
assertTrue(!manager.isGameOver()); | |
manager.kill("Bob"); | |
manager.kill("lisa"); | |
assertTrue(manager.isGameOver()); | |
assertEquals("tom", manager.winner()); | |
} | |
@Test | |
public void testContains() { | |
assertTrue(manager.killRingContains("bob")); | |
assertTrue(!manager.killRingContains("jessica")); | |
manager.kill("bob"); | |
assertTrue(!manager.killRingContains("bOb")); | |
assertTrue(manager.graveyardContains("bob")); | |
assertTrue(!manager.graveyardContains("jessica")); | |
} | |
@Test | |
public void testPrintKillRing() { | |
manager.printKillRing(); | |
String expected = "bob is stalking lisa\nlisa is stalking tom\ntom is stalking bob\n"; | |
assertEquals(expected, outContent.toString()); | |
} | |
@Test | |
public void testKillFront() { | |
manager.kill("Bob"); | |
manager.printKillRing(); | |
manager.printGraveyard(); | |
String expected = "lisa is stalking tom\ntom is stalking lisa\nbob was killed by tom\n"; | |
assertEquals(expected, outContent.toString()); | |
} | |
@Test | |
public void testKillMiddle() { | |
manager.kill("lISa"); | |
manager.printKillRing(); | |
manager.printGraveyard(); | |
String expected = "bob is stalking tom\ntom is stalking bob\nlisa was killed by bob\n"; | |
assertEquals(expected, outContent.toString()); | |
} | |
@Test | |
public void testKillBack() { | |
manager.kill("TOM"); | |
manager.printKillRing(); | |
manager.printGraveyard(); | |
String expected = "bob is stalking lisa\nlisa is stalking bob\ntom was killed by lisa\n"; | |
assertEquals(expected, outContent.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment