Created
August 8, 2011 00:23
-
-
Save mike-neck/1130980 to your computer and use it in GitHub Desktop.
The demonstrated code at my session in the Android Test Festival (https://sites.google.com/site/atecfes/home) that is almost completed.
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 orz.mikeneck.sample.bowling; | |
public class Frame { | |
public static final int MAX = 10; | |
public static final int MIN = 0; | |
private int first = 0; | |
private int second = 0; | |
private Frame nextFrame; | |
public Frame first(int pin) | |
throws IllegalPinException { | |
this.first = validatePin(pin, MIN, MAX); | |
if(isStrike()){ | |
nextFrame = new Frame(); | |
return nextFrame; | |
} else { | |
return this; | |
} | |
} | |
public int first() { | |
return first; | |
} | |
public Frame second(int pin) | |
throws IllegalPinException { | |
this.second = validatePin(pin, MIN, MAX - first); | |
nextFrame = new Frame(); | |
return nextFrame; | |
} | |
public int frameTotal() { | |
return first + second; | |
} | |
private int validatePin(int pin, int min, int max) | |
throws IllegalPinException { | |
if(pin < min || pin > max){ | |
throw new IllegalPinException(); | |
} else { | |
return pin; | |
} | |
} | |
public boolean isStrike() { | |
return first == MAX; | |
} | |
public boolean isSpare() { | |
return !isOpenFrame() && !isStrike(); | |
} | |
private boolean isOpenFrame() { | |
return first + second < MAX; | |
} | |
public int spareBonus() { | |
return first; | |
} | |
public int strikeBonus() { | |
return frameTotal() | |
+ ((isStrike())? nextFrame.first() : 0); | |
} | |
public int score() { | |
return frameTotal() + bonus(); | |
} | |
public int bonus() { | |
if(isSpare()) { | |
return nextFrame.spareBonus(); | |
} else if(isStrike()) { | |
return nextFrame.strikeBonus(); | |
} | |
return 0; | |
} | |
} |
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 orz.mikeneck.sample.bowling.test; | |
import org.junit.Test; | |
import org.junit.experimental.runners.Enclosed; | |
import org.junit.runner.RunWith; | |
import orz.mikeneck.sample.bowling.Frame; | |
import orz.mikeneck.sample.bowling.IllegalPinException; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertThat; | |
import static org.junit.Assert.assertTrue; | |
import static org.hamcrest.CoreMatchers.is; | |
import static org.hamcrest.CoreMatchers.not; | |
@RunWith(Enclosed.class) | |
public class FrameTest { | |
static private Frame frame; | |
public static class FrameTotalTest { | |
@Test | |
public void testGetFrameTotalOnOpenFrameCase() { | |
frame = new Frame(); | |
frame.first(1); | |
frame.second(2); | |
assertThat(frame.frameTotal(), is(3)); | |
frame = new Frame(); | |
frame.first(0); | |
frame.second(0); | |
assertThat(frame.frameTotal(), is(0)); | |
} | |
@Test | |
public void testGetFrameTotalNotOpenFrameCase() { | |
frame = new Frame(); | |
frame.first(10); | |
assertThat(frame.frameTotal(), is(10)); | |
frame = new Frame(); | |
frame.first(1).second(9); | |
assertThat(frame.frameTotal(), is(10)); | |
} | |
} | |
// FrameScore <- frameTotal + (spareBonus or strikeBonus) | |
public static class FrameScoreTest { | |
// Open Frame <- frameTotal | |
@Test | |
public void testOpenFrameScore() { | |
frame = new Frame(); | |
frame.first(1); | |
Frame secondFrame = frame.second(8); | |
assertThat(frame.score(), is(9)); | |
secondFrame.first(1); | |
assertThat(frame.score(), is(9)); | |
} | |
// Spare Frame <- frameTotal + spareBonus | |
@Test | |
public void testSpareFrameScore() { | |
frame = new Frame(); | |
frame.first(1); | |
Frame secondFrame = frame.second(9); | |
secondFrame.first(1); | |
assertThat(frame.score(), is(11)); | |
secondFrame.second(1); | |
assertThat(frame.score(), is(11)); | |
} | |
// Strike Frame <- strikeBonus | |
@Test | |
public void testStrikeScore() { | |
frame = new Frame(); | |
Frame secondFrame = frame.first(10); | |
Frame thirdFrame = secondFrame.first(10); | |
thirdFrame.first(1); | |
assertThat(frame.score(), is(21)); | |
} | |
} | |
public static class BonusTest { | |
@Test | |
public void testGetSpareBonus() { | |
frame = new Frame().first(1).second(9); | |
assertThat(frame.spareBonus(), is(0)); | |
frame.first(1); | |
assertThat(frame.spareBonus(), is(1)); | |
frame.second(1); | |
assertThat(frame.spareBonus(), is(1)); | |
} | |
@Test | |
public void testSpareBonus() { | |
frame = new Frame(); | |
Frame secondFrame = frame.first(1).second(9); | |
secondFrame.first(1).second(1); | |
assertThat(frame.bonus(), is(1)); | |
} | |
@Test | |
public void testGetStrikeBonusOnNextFrameBeOpen() { | |
frame = new Frame().first(10); | |
assertThat(frame.strikeBonus(), is(0)); | |
frame.first(1); | |
assertThat(frame.strikeBonus(), is(1)); | |
frame.second(1); | |
assertThat(frame.strikeBonus(), is(2)); | |
} | |
@Test | |
public void testStrikeBonuxOnNextFrameBeOpen() { | |
frame = new Frame(); | |
Frame secondFrame = frame.first(10); | |
secondFrame.first(1).second(2); | |
assertThat(frame.bonus(), is(3)); | |
} | |
@Test | |
public void testGetStrikeBonusOnNextFrameStrike() { | |
frame = new Frame().first(10); | |
Frame thirdFrame = frame.first(10); | |
thirdFrame.first(1); | |
assertThat(frame.strikeBonus(), is(11)); | |
} | |
@Test | |
public void testStrikeBonusOnNextFrameStrike() { | |
frame = new Frame(); | |
Frame secondFrame = frame.first(10); | |
Frame thirdFrame = secondFrame.first(10); | |
thirdFrame.first(1); | |
assertThat(frame.bonus(), is(11)); | |
} | |
} | |
//TODO Frame Number | |
//TODO Easy Interface | |
//TODO first, second -> put | |
//TODO first(10), second(10 - first()) -> strike(), spare() | |
public static class ExceptionTest { | |
@Test(expected = IllegalPinException.class) | |
public void testIsIllegalPinExceptionThrown() { | |
frame = new Frame(); | |
frame.first(-1); | |
} | |
@Test(expected = IllegalPinException.class) | |
public void testIsIllegalPinExceptionThrownAfterPutsEleven() { | |
frame = new Frame(); | |
frame.first(11); | |
} | |
@Test(expected = IllegalPinException.class) | |
public void testIsIllegalPinExceptionThrownAfterSecond() { | |
frame = new Frame(); | |
frame.first(1); | |
frame.second(10); | |
} | |
@Test | |
public void testIllegalPinExceptionShouldNotBeThrown() { | |
frame = new Frame(); | |
frame.first(1); | |
frame.second(9); | |
assertTrue(true); | |
} | |
} | |
public static class ReturnTypeTest { | |
@Test | |
public void testFirstReturnsSameFrameIfNotStrike() { | |
frame = new Frame(); | |
assertThat(frame.first(9), is(frame)); | |
} | |
@Test | |
public void testFirstReturnsAnotherFrameIfStrike() { | |
frame = new Frame(); | |
assertThat(frame.first(10), is(not(frame))); | |
} | |
@Test | |
public void testSecondReturnsAnotherFrame() { | |
frame = new Frame(); | |
frame.first(1); | |
assertThat(frame.second(1), is(not(frame))); | |
} | |
} | |
public static class StrikeTest { | |
@Test | |
public void testIsStrikeAfterStrike() { | |
frame = new Frame(); | |
frame.first(10); | |
assertThat(frame.isStrike(), is(true)); | |
} | |
@Test | |
public void testIsNotStrikeAfterOpenFrame() { | |
frame = new Frame(); | |
frame.first(9); | |
frame.second(1); | |
assertFalse(frame.isStrike()); | |
} | |
} | |
public static class SpareTest { | |
@Test | |
public void testIsSpareTrue() { | |
frame = new Frame(); | |
frame.first(1); | |
frame.second(9); | |
assertTrue(frame.isSpare()); | |
} | |
@Test | |
public void testIsSpareFalseOnOpenFrame() { | |
frame = new Frame(); | |
frame.first(1); | |
frame.second(8); | |
assertFalse(frame.isSpare()); | |
} | |
@Test | |
public void testIsSpareFalseOnStrike() { | |
frame = new Frame(); | |
frame.first(10); | |
assertFalse(frame.isSpare()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment