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 1 commit
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
171 changes: 165 additions & 6 deletions src/test/java/domain/GameTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
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 org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -159,4 +154,168 @@ void test1() {
assertThat(game.getBank().getTotalCoin()).isEqualTo(originalBankCoins - effectCoins);

}

@Test
@DisplayName("""
當玩家B有2張麵包店,且輪到B擲骰子
當骰子擲出點數為2時
B可以從銀行得到2元
""")
void GREEN2_當B有2張麵包店_可以得到2元() {
ChanWu77 marked this conversation as resolved.
Show resolved Hide resolved
// 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(), 1);

// then
int bakerySize = handCard.getEstablishments(Bakery.class).size();
Copy link
Collaborator

Choose a reason for hiding this comment

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

這裡不應該計算 bakerySize,而是手牌中 bakery 數量可獲得的金額,建議可改成 totalBakeryBonuses

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 - bakerySize);
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. isEqualTo(2) 改為 isEqualTo(totalBakeryBonuses)
  2. bakerySize 改為 totalBakeryBonuses

}
@Test
@DisplayName("""
當玩家A有2張麵包店,且輪到A擲骰子
當玩家B有2張森林
當玩家C有1張森林
當A骰子擲出點數為5時,
B可以從銀行得到2元,C可以從銀行得到1元
""")
void BLUE5_當B有2張森林B得到2元_當C有1張森林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.

function name 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(), 1);

// 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 PURPLE6_當A有1張體育館_B跟C玩家各自要給A_2元() {
// 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(5)
.dices(List.of(dice))
.bank(new Bank())
.build();

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

// when
Mockito.when(dice.throwDice()).thenReturn(6);
game.rollDice(playerA.getId(), 1);
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擲骰子
當玩家B有1張家庭餐廳
當A骰子擲出點數為10時,
A要給B 2塊
""")
void RED3_當B有1張家庭餐廳_A要給B_2塊() {
// given
Dice dice1 = Mockito.mock(Dice.class);
Dice dice2 = 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(10).handCard(handCardA).build();
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(new Bank())
.build();

var originalBankCoins = game.getBank().getTotalCoin();
int point1 = dice1.throwDice();
int point2 = dice2.throwDice();
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(dice1.throwDice()).thenReturn(5);
// Mockito.when(dice2.throwDice()).thenReturn(5);
// game.rollDice(playerA.getId(), 2);

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