Skip to content

Commit

Permalink
Removing tamagotchi's constructor injection. This is causing a depend…
Browse files Browse the repository at this point in the history
…ency loop between the Scoreboard and this class. We now have to start the tamagotchi from the scoreboard instead. As a result, the test are a little bit cleaner but I smell some feature envy maybe?
  • Loading branch information
Sebastian Hermida committed Apr 5, 2011
1 parent 4d58a6e commit a31a8c8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
16 changes: 9 additions & 7 deletions src/com/happyprog/tdgotchi/scoreboard/Scoreboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import org.eclipse.swt.graphics.Image;

import com.happyprog.tdgotchi.level.Level;
import com.happyprog.tdgotchi.level.FirstLevel;
import com.happyprog.tdgotchi.level.Level;
import com.happyprog.tdgotchi.level.ZombieLevel;
import com.happyprog.tdgotchi.subscriber.JUnitTestSubscriber;
import com.happyprog.tdgotchi.subscriber.TestSubscriber;
import com.happyprog.tdgotchi.views.TinyTamagotchi;
import com.happyprog.tdgotchi.views.Tamagotchi;
import com.happyprog.tdgotchi.views.TinyTamagotchi;
import com.happyprog.tdgotchi.views.View;

public class Scoreboard implements TestObserver, TamagotchiObserver {
Expand All @@ -27,7 +27,7 @@ private enum TestRun {
}

public Scoreboard(View view) {
this(view, new TinyTamagotchi(new FirstLevel()), new JUnitTestSubscriber(), new ZombieLevel(), new FirstLevel());
this(view, new TinyTamagotchi(), new JUnitTestSubscriber(), new ZombieLevel(), new FirstLevel());
}

public Scoreboard(View view, Tamagotchi tamagotchi, TestSubscriber subscriber, Level zombieLevel, Level firstLevel) {
Expand All @@ -38,7 +38,7 @@ public Scoreboard(View view, Tamagotchi tamagotchi, TestSubscriber subscriber, L
this.firstLevel = firstLevel;

subscribeToJUnitEvents();
subscribeToTamagotchiEvents(tamagotchi);
startTamagotchi(tamagotchi);
}

@Override
Expand Down Expand Up @@ -73,20 +73,22 @@ private void updateScoreWith(int points) {

private void updateTamagoshiAndHealthLevel() {
if (score < 0) {
tamagotchi.changeLevel(zombieLevel);
tamagotchi.setLevel(zombieLevel);
view.updateHealth(zombieLevel.getHealth());
return;
}

if (score >= 0 && score <= 5) {
tamagotchi.changeLevel(firstLevel);
tamagotchi.setLevel(firstLevel);
view.updateHealth(firstLevel.getHealth());
return;
}
}

private void subscribeToTamagotchiEvents(Tamagotchi tamagotchi) {
private void startTamagotchi(Tamagotchi tamagotchi) {
tamagotchi.setLevel(firstLevel);
tamagotchi.addObserver(this);
tamagotchi.start();
}

private void subscribeToJUnitEvents() {
Expand Down
4 changes: 3 additions & 1 deletion src/com/happyprog/tdgotchi/views/Tamagotchi.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ public interface Tamagotchi {

void onImageSetCallback();

void changeLevel(Level level);
void setLevel(Level level);

void addObserver(TamagotchiObserver observer);

void start();

}
13 changes: 5 additions & 8 deletions src/com/happyprog/tdgotchi/views/TinyTamagotchi.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ public class TinyTamagotchi implements Tamagotchi {

private TamagotchiObserver observer;
private Level level;
private Stack<Image> moodStack;
private Stack<Image> moodStack = new Stack<Image>();

public TinyTamagotchi(Level level) {
this.level = level;

moodStack = new Stack<Image>();
@Override
public void start() {
pushToStack(level.getNormalMood());
updateMood();
}

@Override
Expand All @@ -36,15 +35,13 @@ public void onImageSetCallback() {
}

@Override
public void changeLevel(Level level) {
public void setLevel(Level level) {
this.level = level;
}

@Override
public void addObserver(TamagotchiObserver observer) {
this.observer = observer;

updateMood();
}

private void updateMood() {
Expand Down
15 changes: 6 additions & 9 deletions test/com/happyprog/tdgotchi/scoreboard/ScoreboardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public void subscribesToTestEvents() throws Exception {
verify(subscriber).subscribe(scoreboard);
}

@Test
public void whenStartingGame_tamagotchiLevelIsOne() throws Exception {
verify(tamagotchi).setLevel(firstLevel);
}

@Test
public void onRedToGreen_tamagotchiIsHappy() throws Exception {
scoreboard.onFailingTest();
Expand Down Expand Up @@ -75,15 +80,7 @@ public void ifScoreLessThanZero_tamagotchiLevelIsZombie() throws Exception {
scoreboard.onFailingTest();
scoreboard.onFailingTest();

verify(tamagotchi).changeLevel(zombieLevel);
}

@Test
public void ifScoreEqualsZero_tamagotchiLevelIsOne() throws Exception {
scoreboard.onFailingTest();
scoreboard.onPassingTest();

verify(tamagotchi).changeLevel(firstLevel);
verify(tamagotchi).setLevel(zombieLevel);
}

@Test
Expand Down
32 changes: 15 additions & 17 deletions test/com/happyprog/tdgotchi/views/TinyTamagotchiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,32 @@ public class TinyTamagotchiTest {

private TamagotchiObserver observer;
private Level level;
private Tamagotchi tamagotchi;

@Before
public void before() {
observer = mock(TamagotchiObserver.class);
level = mock(Level.class);
tamagotchi = new TinyTamagotchi();
tamagotchi.setLevel(level);
tamagotchi.addObserver(observer);
}

@Test
public void whenConstructed_updatesTheView() throws Exception {
public void whenStarted_ObserverIsNotifiedOfMoodChange() throws Exception {
when(level.getNormalMood()).thenReturn(DEFAULT_MOOD);

Tamagotchi tamagotchi = new TinyTamagotchi(level);
tamagotchi.addObserver(observer);
tamagotchi.start();

verify(observer).updateMood(DEFAULT2);
}

@Test
public void beHappy_updatesViewsImage() throws Exception {
public void beHappy_updatesObserver() throws Exception {
when(level.getNormalMood()).thenReturn(new Image[] { DEFAULT1 });
when(level.getHappyMood()).thenReturn(HAPPY_MOOD);

Tamagotchi tamagotchi = new TinyTamagotchi(level);
tamagotchi.addObserver(observer);
tamagotchi.start();

tamagotchi.beHappy();
tamagotchi.onImageSetCallback();
Expand All @@ -58,12 +60,11 @@ public void beHappy_updatesViewsImage() throws Exception {
}

@Test
public void beUpset_updatesViewsImage() throws Exception {
public void beUpset_updatesObserver() throws Exception {
when(level.getNormalMood()).thenReturn(new Image[] { DEFAULT1 });
when(level.getUpsetMood()).thenReturn(UPSET_MOOD);

Tamagotchi tamagotchi = new TinyTamagotchi(level);
tamagotchi.addObserver(observer);
tamagotchi.start();

tamagotchi.beUpset();
tamagotchi.onImageSetCallback();
Expand All @@ -72,29 +73,26 @@ public void beUpset_updatesViewsImage() throws Exception {
}

@Test
public void onImageSetCallback_updatesViewsImage() throws Exception {
public void onImageSetCallback_updatesObserver() throws Exception {
when(level.getNormalMood()).thenReturn(DEFAULT_MOOD);

Tamagotchi tamagotchi = new TinyTamagotchi(level);
tamagotchi.addObserver(observer);

tamagotchi.start();
tamagotchi.onImageSetCallback();

verify(observer).updateMood(DEFAULT2);
}

@Test
public void onChangeLevel_updatesTamagotchiLevel() throws Exception {
public void onChangeLevel_updatesObserverWithNewMoodImage() throws Exception {
when(level.getNormalMood()).thenReturn(DEFAULT_MOOD);

Tamagotchi tamagotchi = new TinyTamagotchi(level);
tamagotchi.addObserver(observer);
tamagotchi.start();

Level newLevel = mock(Level.class);
when(newLevel.getNormalMood()).thenReturn(new Image[] { HAPPY1 });

tamagotchi.onImageSetCallback();
tamagotchi.changeLevel(newLevel);
tamagotchi.setLevel(newLevel);
tamagotchi.onImageSetCallback();

verify(observer).updateMood(HAPPY1);
Expand Down

0 comments on commit a31a8c8

Please sign in to comment.