From b21c36a1880278819998dd050ab2da88a8c2f4fc Mon Sep 17 00:00:00 2001 From: Beenie93 Date: Mon, 13 Sep 2021 20:38:07 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20CommandOptions=EC=97=90=EC=84=9C=20?= =?UTF-8?q?MoveOptions=20=EB=B0=98=ED=99=98=ED=95=98=EA=B2=8C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/controller/ChessController.java | 9 ++--- .../chess/domain/command/CommandOptions.java | 4 +-- .../domain/command/CommandOptionsTest.java | 33 +++++++++++++++++-- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/main/java/chess/controller/ChessController.java b/src/main/java/chess/controller/ChessController.java index c7984ba..aa70151 100644 --- a/src/main/java/chess/controller/ChessController.java +++ b/src/main/java/chess/controller/ChessController.java @@ -4,12 +4,9 @@ import chess.domain.ChessGame; import chess.domain.board.Status; import chess.domain.command.CommandOptions; -import chess.domain.command.MoveOptions; import chess.view.InputView; import chess.view.OutputView; -import java.util.List; - public class ChessController { private final InputView inputView; @@ -61,12 +58,12 @@ private void play(final ChessGame chessGame) { private void executeCommand(final ChessGame chessGame, final CommandOptions commandOptions) { if (commandOptions.isEnd()) { chessGame.end(); + return; } if (commandOptions.isMove()) { - List options = commandOptions.getOptions(); - MoveOptions moveOptions = new MoveOptions(options); - chessGame.move(moveOptions); + chessGame.move(commandOptions.getMoveOptions()); + return; } if (commandOptions.isStatus()) { diff --git a/src/main/java/chess/domain/command/CommandOptions.java b/src/main/java/chess/domain/command/CommandOptions.java index bd27dd3..a46abce 100644 --- a/src/main/java/chess/domain/command/CommandOptions.java +++ b/src/main/java/chess/domain/command/CommandOptions.java @@ -70,7 +70,7 @@ public boolean isStatus() { return command.isStatus(); } - public List getOptions() { - return Collections.unmodifiableList(options); + public MoveOptions getMoveOptions() { + return new MoveOptions(options); } } diff --git a/src/test/java/chess/domain/command/CommandOptionsTest.java b/src/test/java/chess/domain/command/CommandOptionsTest.java index 9774bd1..22532de 100644 --- a/src/test/java/chess/domain/command/CommandOptionsTest.java +++ b/src/test/java/chess/domain/command/CommandOptionsTest.java @@ -1,9 +1,11 @@ package chess.domain.command; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -11,14 +13,14 @@ class CommandOptionsTest { @ParameterizedTest - @CsvSource(value = {"start, 0", "end, 0", "move b2 b3, 2", "status, 0"}) + @CsvSource(value = {"start", "end", "move b2 b3", "status"}) @DisplayName("옵션이 담긴 명령어를 반환한다.") - void of(String text, int expected) { + void of(String text) { //when CommandOptions commandOptions = CommandOptions.of(text); //then - assertThat(commandOptions.getOptions()).hasSize(expected); + assertThat(commandOptions).isNotNull(); } @ParameterizedTest @@ -85,4 +87,29 @@ void isStatus(String text, boolean expected) { //then assertThat(actual).isEqualTo(expected); } + + @Test + @DisplayName("이동 명령 관련 옵션을 반환한다.") + void getMoveOptions() { + //given + CommandOptions commandOptions = CommandOptions.of("move b2 b3"); + + //when + MoveOptions moveOptions = commandOptions.getMoveOptions(); + + //then + assertThat(moveOptions).isNotNull(); + } + + @ParameterizedTest + @ValueSource(strings = {"start", "end", "status"}) + @DisplayName("이동 명령이 아닌 다른 명령이 이동 관련 옵션을 반환하려고 할 경우 예외가 발생한다.") + void getMoveOptions_fail(String text) { + //given + CommandOptions commandOptions = CommandOptions.of(text); + + //when //then + assertThatThrownBy(commandOptions::getMoveOptions) + .isInstanceOf(IndexOutOfBoundsException.class); + } }