Skip to content

Instantly share code, notes, and snippets.

@kvu787
Last active December 26, 2015 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kvu787/7118220 to your computer and use it in GitHub Desktop.
Save kvu787/7118220 to your computer and use it in GitHub Desktop.
See class comments
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
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();
// 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(expected = IllegalArgumentException.class)
public void testAssassinManagerConstructor1() {
new AssassinManager(null);
}
@Test(expected = IllegalArgumentException.class)
public void testAssassinManagerConstructor2() {
new AssassinManager(new ArrayList<String>());
}
@Test
public void testIsGameOverAndWinner() {
assertNull(manager.winner());
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\n lisa is stalking tom\n tom 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\n tom is stalking lisa\n bob 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\n tom is stalking bob\n lisa 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\n lisa is stalking bob\n tom was killed by lisa\n";
assertEquals(expected, outContent.toString());
}
@Test
public void testKillGraveyardPrintOrder() {
manager.kill("TOM");
manager.kill("lisa");
manager.printKillRing();
manager.printGraveyard();
String expected = " bob is stalking bob\n lisa was killed by bob\n tom was killed by lisa\n";
assertEquals(expected, outContent.toString());
}
@Test(expected = IllegalStateException.class)
public void testKillException1() {
manager.kill("tom");
manager.kill("bob");
manager.kill("lisa");
}
@Test(expected = IllegalArgumentException.class)
public void testKillException2() {
manager.kill("howard");
}
@Test(expected = IllegalStateException.class)
public void testKillException3() {
manager.kill("tom");
manager.kill("bob");
manager.kill("howard");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment