Skip to content

Commit be1e04e

Browse files
committed
refactor: CommandOptions에서 MoveOptions 반환하게 수정
1 parent 3ef6adb commit be1e04e

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

src/main/java/chess/controller/ChessController.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
import chess.domain.ChessGame;
55
import chess.domain.board.Status;
66
import chess.domain.command.CommandOptions;
7-
import chess.domain.command.MoveOptions;
87
import chess.view.InputView;
98
import chess.view.OutputView;
109

11-
import java.util.List;
12-
1310
public class ChessController {
1411

1512
private final InputView inputView;
@@ -61,12 +58,12 @@ private void play(final ChessGame chessGame) {
6158
private void executeCommand(final ChessGame chessGame, final CommandOptions commandOptions) {
6259
if (commandOptions.isEnd()) {
6360
chessGame.end();
61+
return;
6462
}
6563

6664
if (commandOptions.isMove()) {
67-
List<String> options = commandOptions.getOptions();
68-
MoveOptions moveOptions = new MoveOptions(options);
69-
chessGame.move(moveOptions);
65+
chessGame.move(commandOptions.getMoveOptions());
66+
return;
7067
}
7168

7269
if (commandOptions.isStatus()) {

src/main/java/chess/domain/command/CommandOptions.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ private CommandOptions(final Command command, final List<String> options) {
2323
public static CommandOptions of(final String text) {
2424
validateEmpty(text);
2525
List<String> commandAndOptions = split(text);
26+
System.out.println(commandAndOptions.size());
2627
Command command = Command.of(commandAndOptions.get(COMMAND_INDEX));
2728
List<String> options = extractOptions(commandAndOptions);
2829

@@ -70,7 +71,7 @@ public boolean isStatus() {
7071
return command.isStatus();
7172
}
7273

73-
public List<String> getOptions() {
74-
return Collections.unmodifiableList(options);
74+
public MoveOptions getMoveOptions() {
75+
return new MoveOptions(options);
7576
}
7677
}

src/test/java/chess/domain/command/CommandOptionsTest.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package chess.domain.command;
22

33
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
45
import org.junit.jupiter.params.ParameterizedTest;
56
import org.junit.jupiter.params.provider.CsvSource;
67
import org.junit.jupiter.params.provider.NullSource;
8+
import org.junit.jupiter.params.provider.ValueSource;
79

810
import static org.assertj.core.api.Assertions.assertThat;
911
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1012

1113
class CommandOptionsTest {
1214

1315
@ParameterizedTest
14-
@CsvSource(value = {"start, 0", "end, 0", "move b2 b3, 2", "status, 0"})
16+
@CsvSource(value = {"start", "end", "move b2 b3", "status"})
1517
@DisplayName("옵션이 담긴 명령어를 반환한다.")
16-
void of(String text, int expected) {
18+
void of(String text) {
1719
//when
1820
CommandOptions commandOptions = CommandOptions.of(text);
1921

2022
//then
21-
assertThat(commandOptions.getOptions()).hasSize(expected);
23+
assertThat(commandOptions).isNotNull();
2224
}
2325

2426
@ParameterizedTest
@@ -85,4 +87,29 @@ void isStatus(String text, boolean expected) {
8587
//then
8688
assertThat(actual).isEqualTo(expected);
8789
}
90+
91+
@Test
92+
@DisplayName("이동 명령 관련 옵션을 반환한다.")
93+
void getMoveOptions() {
94+
//given
95+
CommandOptions commandOptions = CommandOptions.of("move b2 b3");
96+
97+
//when
98+
MoveOptions moveOptions = commandOptions.getMoveOptions();
99+
100+
//then
101+
assertThat(moveOptions).isNotNull();
102+
}
103+
104+
@ParameterizedTest
105+
@ValueSource(strings = {"start", "end", "status"})
106+
@DisplayName("이동 명령이 아닌 다른 명령이 이동 관련 옵션을 반환하려고 할 경우 예외가 발생한다.")
107+
void getMoveOptions_fail(String text) {
108+
//given
109+
CommandOptions commandOptions = CommandOptions.of(text);
110+
111+
//when //then
112+
assertThatThrownBy(commandOptions::getMoveOptions)
113+
.isInstanceOf(IndexOutOfBoundsException.class);
114+
}
88115
}

0 commit comments

Comments
 (0)