Skip to content

Commit

Permalink
refactor: CommandOptions에서 MoveOptions 반환하게 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
seongbeenkim committed Sep 13, 2021
1 parent 3ef6adb commit b21c36a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
9 changes: 3 additions & 6 deletions src/main/java/chess/controller/ChessController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> options = commandOptions.getOptions();
MoveOptions moveOptions = new MoveOptions(options);
chessGame.move(moveOptions);
chessGame.move(commandOptions.getMoveOptions());
return;
}

if (commandOptions.isStatus()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/chess/domain/command/CommandOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public boolean isStatus() {
return command.isStatus();
}

public List<String> getOptions() {
return Collections.unmodifiableList(options);
public MoveOptions getMoveOptions() {
return new MoveOptions(options);
}
}
33 changes: 30 additions & 3 deletions src/test/java/chess/domain/command/CommandOptionsTest.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
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;

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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit b21c36a

Please sign in to comment.