Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 4ColorsCard usecase #99

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 167 additions & 6 deletions src/test/java/domain/GameTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package domain;

import domain.card.establishment.AppleOrchard;
import domain.card.establishment.Bakery;
import domain.card.establishment.Cafe;
import domain.card.establishment.ConvenienceStore;
import domain.card.establishment.Establishment;
import domain.card.establishment.WheatField;
import domain.card.establishment.*;
import domain.card.landmark.Landmark;
import domain.card.landmark.ShoppingMall;
import domain.card.landmark.TrainStation;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -159,4 +155,169 @@ void test1() {
assertThat(game.getBank().getTotalCoin()).isEqualTo(originalBankCoins - effectCoins);

}

@Test
@DisplayName("""
當玩家B有2張麵包店,且輪到B擲骰子
當骰子擲出點數為2時
B可以從銀行得到2元
""")
void greenCardbackeryCardTest() {
// given
Dice dice = Mockito.mock(Dice.class);

HandCard handCard = new HandCard();
handCard.addHandCard(new Bakery());
handCard.addHandCard(new Bakery());
Player playerB = Player.builder().id("B01").name("B").coins(0).handCard(handCard).build();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建立 PlayerA、B、C 物件可以抽成屬性,在 @beforeach 內賦值。
如果有測試案例用到 Player 就直接取用這些屬性。
整個檔案出現類似的程式碼都套用這個規則。

private Player playerA;

@BeforeEach
void setup() {
    playerA = Player.builder()
            .id("A01")
            .name("A")
            .build();
}

在各自的測試方法內呼叫 addCardToHandCard() 加入 handCard

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

整個檔案中有用到的 Player 物件可以抽成屬性,並在 @Beforecach 內賦值
測試方法中有用到 Player 時可以直接調用屬性,不必重新 new 物件

private Player playerA;
@BeforeEach
void setup() {
    playerA = Player.builder()
            .id("A01")
            .name("A")
            .build();
}


Game game = Game.builder()
.players(List.of(playerB))
.turnPlayer(playerB)
.currentDicePoint(2)
.dices(List.of(dice))
.bank(new Bank())
.build();

var originalBankCoins = game.getBank().getTotalCoin();

// when
Mockito.when(dice.throwDice()).thenReturn(2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

放在 mock 骰子物件下方。
這不是 when 的程式碼,而是 given 模擬的資料。
整個檔案出現類似的程式碼都套用這個規則

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when 要寫的是測試的動作,
Mockito.when(dice.throwDice()).thenReturn(2);
是在 given 的時候建立模擬丟骰子的資料

game.rollDice(playerB.getId(), false);

// then
int totalBakeryBonuses = handCard.getEstablishments(Bakery.class).size();
assertThat(playerB.getTotalCoins()).isEqualTo(2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isEqualTo(2)的 2 命名為具有領域意義的名稱,會更好理解

assertThat(playerB.getTotalCoins()).isEqualTo(totalBakeryBonuses);

整個檔案內的 assertion 都套用此規則

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

調整DisplayName說明清楚

assertThat(game.getBank().getTotalCoin()).isEqualTo(originalBankCoins - totalBakeryBonuses);
}

@Test
@DisplayName("""
當玩家A有2張麵包店,且輪到A擲骰子
當玩家B有2張森林
當玩家C有1張森林
當A骰子擲出點數為5時,
B可以從銀行得到2元,C可以從銀行得到1元
""")
void buleCardForestTest() {
// given
Dice dice = Mockito.mock(Dice.class);

HandCard handCardA = new HandCard();
handCardA.addHandCard(new Bakery());
handCardA.addHandCard(new Bakery());
Player playerA = Player.builder().id("A01").name("A").coins(0).handCard(handCardA).build();
HandCard handCardB = new HandCard();
handCardB.addHandCard(new Forest());
handCardB.addHandCard(new Forest());
Player playerB = Player.builder().id("B01").name("B").coins(0).handCard(handCardB).build();
HandCard handCardC = new HandCard();
handCardC.addHandCard(new Forest());
Player playerC = Player.builder().id("C01").name("C").coins(0).handCard(handCardC).build();

Game game = Game.builder()
.players(List.of(playerA, playerB, playerC))
.turnPlayer(playerA)
.currentDicePoint(5)
.dices(List.of(dice))
.bank(new Bank())
.build();

var originalBankCoins = game.getBank().getTotalCoin();

// when
Mockito.when(dice.throwDice()).thenReturn(5);
game.rollDice(playerA.getId(), false);

// then
assertThat(playerA.getTotalCoins()).isEqualTo(0);
assertThat(playerB.getTotalCoins()).isEqualTo(2);
assertThat(playerC.getTotalCoins()).isEqualTo(1);
}

@Test
@DisplayName("""
當玩家A有1張體育館,且輪到A擲骰子
當玩家B有2張森林
當玩家C有1張森林
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit
測試 PlayerA 體育館的效果,PlayerB、C 可以不用有手牌也可以測試體育館效果。

當A骰子擲出點數為6時,
B跟C玩家各自要給A 2元
""")
void purpleStatdiumTest() {
// given
Dice dice = Mockito.mock(Dice.class);

HandCard handCardA = new HandCard();
handCardA.addHandCard(new Stadium());
Player playerA = Player.builder().id("A01").name("A").coins(0).handCard(handCardA).build();
HandCard handCardB = new HandCard();
handCardB.addHandCard(new Forest());
handCardB.addHandCard(new Forest());
Player playerB = Player.builder().id("B01").name("B").coins(10).handCard(handCardB).build();
HandCard handCardC = new HandCard();
handCardC.addHandCard(new Forest());
Player playerC = Player.builder().id("C01").name("C").coins(6).handCard(handCardC).build();

Game game = Game.builder()
.players(List.of(playerA, playerB, playerC))
.turnPlayer(playerA)
.currentDicePoint(6)
.dices(List.of(dice))
.bank(new Bank())
.build();

var originalBankCoins = game.getBank().getTotalCoin();

// when
Mockito.when(dice.throwDice()).thenReturn(6);
game.rollDice(playerA.getId(), false);
playerC.payCoin(2);
playerB.payCoin(2);
playerA.gainCoin(4);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit,
體育館的發動效果沒有在 takeAllPlayersEffect() 裡面是正常的嗎?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已新增takeEffectPurple方法

// then
assertThat(playerA.getTotalCoins()).isEqualTo(4);
assertThat(playerB.getTotalCoins()).isEqualTo(8);
assertThat(playerC.getTotalCoins()).isEqualTo(4);
}

@Test
@DisplayName("""
當玩家A有2張麵包店跟火車站,且輪到A擲骰子
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit,
PlayerA 可以不用有手牌,也可以測試家庭餐廳的效果

當玩家B有1張家庭餐廳
當A骰子擲出點數為10時,
A要給B 2塊
""")
void redCardFamilyRestaurantTest() {
// given
Dice dice1 = Mockito.mock(Dice.class);
Dice dice2 = Mockito.mock(Dice.class);
Bank bank = new Bank();
HandCard handCardA = new HandCard();
handCardA.addHandCard(new Bakery());
handCardA.addHandCard(new Bakery());

Player playerA = Player.builder().id("A01").name("A").coins(10).handCard(handCardA).build();
Landmark trainStation = playerA.getLandMark("火車站");
playerA.flipLandMark(trainStation, bank);
Copy link
Collaborator

@shmily7829 shmily7829 Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 應該要在 Game new 出來之後火車站才能翻牌,並且需要使用 Game 物件裡面的 bank

HandCard handCardB = new HandCard();
handCardB.addHandCard(new FamilyRestaurant());
Player playerB = Player.builder().id("B01").name("B").coins(0).handCard(handCardB).build();

Game game = Game.builder()
.players(List.of(playerA, playerB))
.turnPlayer(playerA)
.currentDicePoint(10)
.dices(List.of(dice1, dice2))
.bank(bank)
.build();

// when
Mockito.when(dice1.throwDice()).thenReturn(5);
Mockito.when(dice2.throwDice()).thenReturn(5);
game.rollDice(playerA.getId(), true);

//then
assertThat(playerA.getTotalCoins()).isEqualTo(4);
assertThat(playerB.getTotalCoins()).isEqualTo(2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PlayerA 的 4 元和 PlayerB 的 2 元如何得出的?

}
}