diff --git a/README.md b/README.md new file mode 100644 index 0000000..d45e646 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# 기능 목록 + +### 1. 플레이어 및 사다리 정보 입력 기능 ++ 플레이어 이름과 결과 종류를 입력한다. ++ 사다리 넓이 : (플레이어 수 - 1) ++ 사다리 높이 : 사용자 입력 +### 2. 사다리 실행 기능 ++ |-----|-----| 가로 라인이 겹치지 않게 사다리를 생성한다. +=> 즉, 앞 라인이 true면 다음 라인은 무조건 false이다. +### 3. 사다리 출력 기능 ++ 플레이어 이름 입력 순으로 출력 ++ 생성된 사다리 출력 ++ 결과 종류 입력 순으로 출력 ++ 보고 싶은 실행 결과 출력 ("all"일 때는 모두 출력) + +### <사다리 구현> ++ Boolean (다리) + + ture : 연결 + + false : 연결 X ++ List<Boolean> (한 층의 다리 모음) + + 사다리의 넒이만큼 + + 다리가 연결되어 있으면(=true) 수평 방향으로 이동이 가능하다. ++ List<Line> (모든 층의 다리 모음) + + 사다리의 높이만큼 diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 0000000..2020bd6 --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,22 @@ +import controller.LadderController; +import domain.Ladder; +import domain.Players; +import domain.ResultTypes; + +public class Application { + public static void main(String[] args) { + runLadderGame(); + } + + private static void runLadderGame() { + LadderController ladderController = new LadderController(); + + Players players = ladderController.initializePlayers(); + ResultTypes resultTypes = ladderController.initializeResults(players.getPlayersSize()); + Ladder ladder = ladderController.initializeLadder(players.getPlayersSize()); + + ladderController.playGame(players, ladder); + + ladderController.displayAllOutput(players, resultTypes, ladder); + } +} diff --git a/src/main/java/controller/LadderController.java b/src/main/java/controller/LadderController.java new file mode 100644 index 0000000..a13eb5e --- /dev/null +++ b/src/main/java/controller/LadderController.java @@ -0,0 +1,49 @@ +package controller; + +import domain.Ladder; +import domain.Players; +import domain.ResultTypes; +import view.InputView; +import view.OutputView; + +import java.util.List; + +public class LadderController { + + public Players initializePlayers() { + List playerNames = InputView.splitString(InputView.inputNames()); + + return new Players(playerNames); + } + + public ResultTypes initializeResults(int resultsSize) { + List kindOfResults = InputView.splitString(InputView.inputLadderResults()); + + return new ResultTypes(kindOfResults, resultsSize); + } + + public Ladder initializeLadder(int width) { + int height = InputView.inputHeight(); + + return new Ladder(width, height); + } + + public void playGame(Players players, Ladder ladder) { + ladder.determineLadderResults(players); + } + + public void displayAllOutput(Players players, ResultTypes resultTypes, Ladder ladder) { + displayLadder(players, resultTypes, ladder); + displayResults(resultTypes, players); + } + + private void displayLadder(Players players, ResultTypes resultTypes, Ladder ladder) { + OutputView.printPlayers(players); + OutputView.drawLadder(ladder); + OutputView.printResultTypes(resultTypes.getResultTypes()); + } + + private void displayResults(ResultTypes resultTypes, Players players) { + OutputView.printResult(players, resultTypes.getResultTypes()); + } +} diff --git a/src/main/java/domain/Ladder.java b/src/main/java/domain/Ladder.java new file mode 100644 index 0000000..ca71811 --- /dev/null +++ b/src/main/java/domain/Ladder.java @@ -0,0 +1,29 @@ +package domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Ladder { + + private final List lines; + + public Ladder(int width, int height) { + this.lines = new ArrayList<>(); + createLadder(width, height); + } + + public void createLadder(int width, int height) { + for (int i = 0; i < height; i++) { + lines.add(new Line(width)); + } + } + + public void determineLadderResults(Players players) { + players.getPlayers().forEach(player -> player.moveAlongLadder(lines)); + } + + public List getLadder() { + return Collections.unmodifiableList(lines); + } +} diff --git a/src/main/java/domain/LadderStep.java b/src/main/java/domain/LadderStep.java new file mode 100644 index 0000000..c4d6a52 --- /dev/null +++ b/src/main/java/domain/LadderStep.java @@ -0,0 +1,43 @@ +package domain; + +import java.util.function.Function; + +public enum LadderStep { + CONNECTED { + @Override + public boolean canMove() { + return true; + } + + @Override + public LadderStep nextStep() { + return NOT_CONNECTED; + } + }, + NOT_CONNECTED { + @Override + public boolean canMove() { + return false; + } + + @Override + public LadderStep nextStep() { + return FROM_BOOLEAN.apply(randomTrueOrFalse()); + } + }; + + public abstract boolean canMove(); + + public abstract LadderStep nextStep(); + + public static final Function FROM_BOOLEAN = value -> { + if (value) { + return CONNECTED; + } + return NOT_CONNECTED; + }; + + public static boolean randomTrueOrFalse() { + return Math.random() < 0.5; + } +} diff --git a/src/main/java/domain/Line.java b/src/main/java/domain/Line.java new file mode 100644 index 0000000..d47132a --- /dev/null +++ b/src/main/java/domain/Line.java @@ -0,0 +1,49 @@ +package domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static domain.LadderStep.randomTrueOrFalse; + +public class Line { + private final List ladderSteps; + + public Line(int width) { + this.ladderSteps = new ArrayList<>(); + createLine(width); + } + + public void createLine(int width) { + ladderSteps.add(LadderStep.FROM_BOOLEAN.apply(randomTrueOrFalse())); + + for (int i = 1; i < width - 1; i++) { + ladderSteps.add(ladderSteps.get(i - 1).nextStep()); + } + } + + public void decideWhereToGo(Position position) { + int ladderOrder = position.getPosition(); + + if (canMoveRight(ladderOrder)) { + position.moveRight(ladderSteps.size()); + return; + } + + if (canMoveLeft(ladderOrder)) { + position.moveLeft(); + } + } + + private boolean canMoveRight(int ladderOrder) { + return ladderOrder < ladderSteps.size() && ladderSteps.get(ladderOrder).canMove(); + } + + private boolean canMoveLeft(int ladderOrder) { + return ladderOrder != 0 && ladderSteps.get(ladderOrder - 1).canMove(); + } + + public List getLine() { + return Collections.unmodifiableList(ladderSteps); + } +} diff --git a/src/main/java/domain/Player.java b/src/main/java/domain/Player.java new file mode 100644 index 0000000..d47a4d4 --- /dev/null +++ b/src/main/java/domain/Player.java @@ -0,0 +1,26 @@ +package domain; + +import java.util.List; + +public class Player { + + private final PlayerName playerName; + private final Position position; + + public Player(PlayerName name, Position position) { + this.playerName = name; + this.position = position; + } + + public void moveAlongLadder(List ladder) { + ladder.forEach(line -> line.decideWhereToGo(this.position)); + } + + public PlayerName getPlayerName() { + return playerName.getName(); + } + + public int getPosition() { + return position.getPosition(); + } +} diff --git a/src/main/java/domain/PlayerName.java b/src/main/java/domain/PlayerName.java new file mode 100644 index 0000000..1f678ba --- /dev/null +++ b/src/main/java/domain/PlayerName.java @@ -0,0 +1,59 @@ +package domain; + +public class PlayerName { + + private final String name; + private final static int MAX_LENGTH = 5; + private final static String INVALID_NAME = "all"; + + public PlayerName(String name) { + validateName(name); + this.name = name; + } + + private void validateName(String name) { + validateNotBlank(name); + validateMaxLength(name); + validateNotEqualAll(name); + } + + private void validateNotBlank(String name) { + if (name.isBlank()) { + throw new IllegalArgumentException("이름은 공백일 수 없습니다."); + } + } + + private void validateMaxLength(String name) { + if (name.length() > MAX_LENGTH) { + throw new IllegalArgumentException("이름은 " + MAX_LENGTH + "자를 초과할 수 없습니다."); + } + } + + private void validateNotEqualAll(String name) { + if (name.equals(INVALID_NAME)) { + throw new IllegalArgumentException("이름은 " + INVALID_NAME + "일 수 없습니다."); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; // 메모리 주소 비교 + if (o == null || getClass() != o.getClass()) return false; + PlayerName that = (PlayerName) o; + return name.equals(that.name); + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + @Override + public String toString() { + return name; + } + + public PlayerName getName() { + return this; + } +} diff --git a/src/main/java/domain/Players.java b/src/main/java/domain/Players.java new file mode 100644 index 0000000..7c72bf7 --- /dev/null +++ b/src/main/java/domain/Players.java @@ -0,0 +1,60 @@ +package domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Set; + +public class Players { + + private final List players; + private final static int MIN_PLAYER_SIZE = 2; + + public Players(List playerNames) { + validateSize(playerNames); + validateDuplicateName(playerNames); + this.players = createPlayer(playerNames); + } + + private void validateSize(List playerNames) { + if (playerNames.size() < MIN_PLAYER_SIZE) { + throw new IllegalArgumentException("플레이어 수는 2명 이상이어야 합니다."); + } + } + + private void validateDuplicateName(List playerNames) { + Set playerNamesSet = new HashSet<>(playerNames); + + if (playerNames.size() != playerNamesSet.size()) { + throw new IllegalArgumentException("동명이인인 플레이어가 존재합니다."); + } + } + + private List createPlayer(List playerNames) { + List players = new ArrayList<>(); + for (int i = 0; i < playerNames.size(); i++) { + players.add(new Player(new PlayerName(playerNames.get(i)), new Position(i))); + } + + return players; + } + + public Player findByName(String viewerName) { + PlayerName playerName = new PlayerName(viewerName); + + return players.stream() + .filter(player -> player.getPlayerName().equals(playerName)) + .findFirst() + .orElseThrow(() -> new NoSuchElementException("플레이어 이름 '" + viewerName + "' 이 존재하지 않습니다.")); + } + + public List getPlayers() { + return Collections.unmodifiableList(players); + } + + public int getPlayersSize() { + return players.size(); + } +} diff --git a/src/main/java/domain/Position.java b/src/main/java/domain/Position.java new file mode 100644 index 0000000..d1c0e40 --- /dev/null +++ b/src/main/java/domain/Position.java @@ -0,0 +1,28 @@ +package domain; + +public class Position { + + private int position; + + public Position(int position) { + this.position = position; + } + + public void moveLeft() { + if (position == 0) { + throw new IllegalStateException("가장 왼쪽 사다리이므로 왼쪽으로 이동이 불가합니다."); + } + position--; + } + + public void moveRight(int maxPosition) { + if (position == maxPosition) { + throw new IllegalStateException("가장 오른쪽 사다리이므로 오른쪽으로 이동이 불가합니다."); + } + position++; + } + + public int getPosition() { + return position; + } +} diff --git a/src/main/java/domain/ResultTypes.java b/src/main/java/domain/ResultTypes.java new file mode 100644 index 0000000..278d177 --- /dev/null +++ b/src/main/java/domain/ResultTypes.java @@ -0,0 +1,34 @@ +package domain; + +import java.util.List; + +public class ResultTypes { + + private final List resultTypes; + + public ResultTypes(List resultTypes, int width) { + validate(resultTypes, width); + this.resultTypes = resultTypes; + } + + private void validate(List resultTypes, int width) { + resultTypes.forEach(this::validateNotBlank); + validateSize(resultTypes, width); + } + + private void validateNotBlank(String resultType) { + if (resultType.isBlank()) { + throw new IllegalArgumentException("실행 결과는 공백일 수 없습니다."); + } + } + + private void validateSize(List resultTypes, int width) { + if (resultTypes.size() != width) { + throw new IllegalArgumentException("실행 결과는 사다리의 개수와 일치해야 합니다."); + } + } + + public List getResultTypes() { + return resultTypes; + } +} diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java new file mode 100644 index 0000000..2113248 --- /dev/null +++ b/src/main/java/view/InputView.java @@ -0,0 +1,58 @@ +package view; + +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +public class InputView { + private static final Scanner input = new Scanner(System.in); + + public static List splitString(String string) { + return Arrays.asList(string.split(",")); + } + + public static String inputNames() { + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + return input.nextLine(); + } + + public static String inputLadderResults() { + System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); + return input.nextLine(); + } + + public static int inputHeight() { + System.out.println("최대 사다리 높이는 몇 개인가요?"); + int height = input.nextInt(); + validateHeightSize(height); + input.nextLine(); + return height; + } + + public static void validateHeightSize(int height) { + if (height < 1) { + throw new IllegalArgumentException("사다리 높이는 1 이상이어야 합니다."); + } + } + + public static String inputViewerName() { + String viewerName; + while (true) { + System.out.println("\n결과를 보고 싶은 사람은?"); + viewerName = input.nextLine(); + try { + validateViewerNameNotBlank(viewerName); + break; + } catch (IllegalArgumentException e) { + System.out.println("입력 값이 공백일 수 없습니다. 다시 입력해 주세요."); + } + } + return viewerName; + } + + public static void validateViewerNameNotBlank(String viewerName) { + if (viewerName.isBlank()) { + throw new IllegalArgumentException("이름은 공백일 수 없습니다."); + } + } +} diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java new file mode 100644 index 0000000..076a5da --- /dev/null +++ b/src/main/java/view/OutputView.java @@ -0,0 +1,63 @@ +package view; + +import domain.Ladder; +import domain.LadderStep; +import domain.Line; +import domain.Player; +import domain.Players; + +import java.util.List; + +public class OutputView { + + // 플레이어 이름 출력 + public static void printPlayers(Players players) { + System.out.println("\n사다리 결과\n"); + players.getPlayers().forEach(Player::getPlayerName); + System.out.println(); + } + + private static String changeValueToView(LadderStep ladderStep) { + if (ladderStep.canMove()) + return "-----"; + + return " "; + } + + private static void drawLine(Line line) { + line.getLine().forEach(ladderStep -> System.out.print("|" + changeValueToView(ladderStep))); + System.out.println("|"); + } + + // 사다리 출력 + public static void drawLadder(Ladder ladder) { + ladder.getLadder().forEach(OutputView::drawLine); + } + + // 결과 종류 출력 + public static void printResultTypes(List resultTypes) { + resultTypes.forEach(System.out::print); + System.out.println(); + } + + public static void printAllResult(Players players, List resultTypes) { + for (Player player : players.getPlayers()) { + System.out.println(player.getPlayerName() + " : " + resultTypes.get(player.getPosition())); + } + } + + // 보고 싶은 사다리 결과 출력 + public static void printResult(Players players, List resultTypes) { + String viewerName = InputView.inputViewerName(); + + System.out.println("\n실행결과"); + + if ("all".equals(viewerName)) { + printAllResult(players, resultTypes); + return; + } + + Player player = players.findByName(viewerName); + System.out.println(resultTypes.get(player.getPosition())); + } +} diff --git a/src/test/java/domain/LadderStepTest.java b/src/test/java/domain/LadderStepTest.java new file mode 100644 index 0000000..8181224 --- /dev/null +++ b/src/test/java/domain/LadderStepTest.java @@ -0,0 +1,66 @@ +package domain; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LadderStepTest { + + @Test + public void CONNECTED는_이동_가능하다() { + LadderStep step = LadderStep.CONNECTED; + + boolean canMove = step.canMove(); + + assertThat(canMove).isTrue(); + } + + @Test + public void NOT_CONNECTED는_이동_불가능하다() { + LadderStep step = LadderStep.NOT_CONNECTED; + + boolean canMove = step.canMove(); + + assertThat(canMove).isFalse(); + } + + @Test + public void CONNECTED에서_nextStep은_NOT_CONNECTED으로_변경된다() { + LadderStep step = LadderStep.CONNECTED; + + LadderStep nextStep = step.nextStep(); + + assertThat(nextStep).isEqualTo(LadderStep.NOT_CONNECTED); + } + + @Test + public void NOT_CONNECTED에서_nextStep은_랜덤값에_따라_결정된다() { + LadderStep step = LadderStep.NOT_CONNECTED; + + LadderStep nextStep = step.nextStep(); + + assertThat(nextStep).isIn(LadderStep.CONNECTED, LadderStep.NOT_CONNECTED); + } + + @Test + public void randomTrueOrFalse는_true를_반환할_수_있다() { + boolean hasReturnedTrue; + + do { + hasReturnedTrue = LadderStep.randomTrueOrFalse(); + } while (!hasReturnedTrue); + + assertThat(hasReturnedTrue).isTrue(); + } + + @Test + public void randomTrueOrFalse는_false를_반환할_수_있다() { + boolean hasReturnedTrue; + + do { + hasReturnedTrue = LadderStep.randomTrueOrFalse(); + } while (hasReturnedTrue); + + assertThat(hasReturnedTrue).isFalse(); + } +} diff --git a/src/test/java/domain/LadderTest.java b/src/test/java/domain/LadderTest.java new file mode 100644 index 0000000..06457f5 --- /dev/null +++ b/src/test/java/domain/LadderTest.java @@ -0,0 +1,39 @@ +package domain; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LadderTest { + + @Test + public void 사다리_생성_테스트() { + int playerSize = 5; + int width = 4; + int height = 3; + + Ladder ladder = new Ladder(playerSize, height); + List lines = ladder.getLadder(); + + assertThat(lines).hasSize(height); + lines.forEach(line -> assertThat(line.getLine()).hasSize(width)); + } + + + @Test + public void 사다리_결과가_사다리_범위_안에_존재해야_한다() { + int width = 5; + int height = 3; + + Ladder ladder = new Ladder(width, height); + Players players = new Players(List.of("A", "B")); + + ladder.determineLadderResults(players); + + players.getPlayers().forEach(player -> + assertThat(player.getPosition()).isGreaterThanOrEqualTo(0).isLessThan(width) + ); + } +} diff --git a/src/test/java/domain/LineTest.java b/src/test/java/domain/LineTest.java new file mode 100644 index 0000000..979886a --- /dev/null +++ b/src/test/java/domain/LineTest.java @@ -0,0 +1,44 @@ +package domain; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class LineTest { + + private Line line; + + @BeforeEach + void 설정() throws Exception { + line = new Line(3); + + Field ladderStepsField = Line.class.getDeclaredField("ladderSteps"); + ladderStepsField.setAccessible(true); + + List fixedSteps = Arrays.asList( + LadderStep.CONNECTED, + LadderStep.NOT_CONNECTED, + LadderStep.CONNECTED + ); + ladderStepsField.set(line, fixedSteps); + } + + @Test + void 오른쪽으로_이동_테스트() { + Position position = new Position(0); + line.decideWhereToGo(position); + assertEquals(1, position.getPosition()); + } + + @Test + void 왼쪽으로_이동_테스트() { + Position position = new Position(3); + line.decideWhereToGo(position); + assertEquals(2, position.getPosition()); + } +} diff --git a/src/test/java/domain/PlayerNameTest.java b/src/test/java/domain/PlayerNameTest.java new file mode 100644 index 0000000..922172d --- /dev/null +++ b/src/test/java/domain/PlayerNameTest.java @@ -0,0 +1,58 @@ +package domain; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +public class PlayerNameTest { + + @Test + public void 유효한_이름으로_생성된다() { + PlayerName name = new PlayerName("Test"); + assertThat(name.toString()).isEqualTo("Test"); + } + + @Test + public void 이름이_공백이면_예외를_던진다() { + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new PlayerName(" ")) + .withMessage("이름은 공백일 수 없습니다."); + } + + @Test + public void 이름이_5자를_초과하면_예외를_던진다() { + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new PlayerName("TestTest")) + .withMessage("이름은 5자를 초과할 수 없습니다."); + } + + @Test + public void 이름이_all이면_예외를_던진다() { + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new PlayerName("all")) + .withMessage("이름은 all일 수 없습니다."); + } + + @Test + public void 이름이_5자_이하의_경우_정상_생성된다() { + PlayerName name = new PlayerName("Test"); + assertThat(name.toString()).isEqualTo("Test"); + } + + @Test + public void 같은_이름은_같은_객체로_취급된다() { + PlayerName name1 = new PlayerName("Test"); + PlayerName name2 = new PlayerName("Test"); + + assertThat(name1).isEqualTo(name2); + } + + @Test + public void 다른_이름은_다른_객체로_취급된다() { + PlayerName name1 = new PlayerName("Test1"); + PlayerName name2 = new PlayerName("Test2"); + + assertThat(name1).isNotEqualTo(name2); + } +} diff --git a/src/test/java/domain/PlayerTest.java b/src/test/java/domain/PlayerTest.java new file mode 100644 index 0000000..2f35734 --- /dev/null +++ b/src/test/java/domain/PlayerTest.java @@ -0,0 +1,26 @@ +package domain; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PlayerTest { + + private Player player; + private PlayerName playerName; + private Position position; + + @BeforeEach + public void setUp() { + playerName = new PlayerName("Test"); + position = new Position(0); + player = new Player(playerName, position); + } + + @Test + public void 플레이어는_정상적으로_생성된다() { + assertThat(player.getPlayerName()).isEqualTo("Test"); + assertThat(player.getPosition()).isEqualTo(0); + } +} diff --git a/src/test/java/domain/PlayersTest.java b/src/test/java/domain/PlayersTest.java new file mode 100644 index 0000000..fc4ddee --- /dev/null +++ b/src/test/java/domain/PlayersTest.java @@ -0,0 +1,53 @@ +package domain; + +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.NoSuchElementException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertAll; + +public class PlayersTest { + + @Test + public void 플레이어가_정상적으로_생성된다() { + List playerNames = List.of("Test1", "Test2"); + Players players = new Players(playerNames); + + assertAll( + () -> assertThat(players.getPlayersSize()).isEqualTo(2), + () -> assertThat(players.getPlayers().get(0).getPlayerName().toString()).isEqualTo("Test1"), + () -> assertThat(players.getPlayers().get(1).getPlayerName().toString()).isEqualTo("Test2") + ); + } + + @Test + public void 플레이어수가_2명_미만일_때_예외를_던진다() { + List playerNames = List.of("Test"); + + assertThatThrownBy(() -> new Players(playerNames)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("플레이어 수는 2명 이상이어야 합니다."); + } + + @Test + public void 존재하지_않는_플레이어를_찾을_때_예외를_던진다() { + List playerNames = List.of("Test1", "Test2"); + Players players = new Players(playerNames); + + assertThatThrownBy(() -> players.findByName("테스트")) + .isInstanceOf(NoSuchElementException.class) + .hasMessage("플레이어 이름 '테스트' 이 존재하지 않습니다."); + } + + @Test + public void 존재하는_플레이어를_정상적으로_찾는다() { + List playerNames = List.of("Test1", "Test2"); + Players players = new Players(playerNames); + + Player foundPlayer = players.findByName("Test1"); + assertThat(foundPlayer.getPlayerName().toString()).isEqualTo("Test1"); + } +} diff --git a/src/test/java/domain/ResultTypesTest.java b/src/test/java/domain/ResultTypesTest.java new file mode 100644 index 0000000..e73dca6 --- /dev/null +++ b/src/test/java/domain/ResultTypesTest.java @@ -0,0 +1,45 @@ +package domain; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class ResultTypesTest { + + @Test + public void 유효한_결과타입으로_생성된다() { + List resultTypes = List.of("Result1", "Result2", "Result3"); + ResultTypes resultTypesObject = new ResultTypes(resultTypes, 3); + + assertThat(resultTypesObject.getResultTypes()).containsExactly("Result1", "Result2", "Result3"); + } + + @Test + public void 결과타입이_공백이면_예외를_던진다() { + List resultTypes = List.of("Result1", " ", "Result3"); + + assertThatThrownBy(() -> new ResultTypes(resultTypes, 3)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("실행 결과는 공백일 수 없습니다."); + } + + @Test + public void 결과타입의_개수가_사다리의_개수와_다르면_예외를_던진다() { + List resultTypes = List.of("Result1", "Result2"); + + assertThatThrownBy(() -> new ResultTypes(resultTypes, 3)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("실행 결과는 사다리의 개수와 일치해야 합니다."); + } + + @Test + public void 결과타입의_개수가_사다리의_개수와_일치해야_정상생성된다() { + List resultTypes = List.of("Result1", "Result2", "Result3"); + ResultTypes resultTypesObject = new ResultTypes(resultTypes, 3); + + assertThat(resultTypesObject.getResultTypes()).hasSize(3); + } +}