diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000000..7dc0def2dd --- /dev/null +++ b/docs/README.md @@ -0,0 +1,50 @@ +# 구현할 기능 목록 + +## 입력 + - [x] 사용자 이름 입력 + - [x] 쉼표로 구분 + - [x] 최대 5글자까지 가능 + - [x] 사다리 높이 입력 + - [x] 자연수 + + +## 출력 + - [x] 실행 결과 + - [x] 첫번째 줄 : 사람 이름 + - [x] 그 아래 줄 : 사다리 그리기 + +## 기능 +- [x] 사람의 이름을 받고 , 기준으로 나누기. +- [x] 한 줄에 | 개수를 사람 이름 개수만큼 반환. +- [x] 랜덤 값 기능 구현 +- [x] 랜덤에 충족하면 ----- 그리기. + - [x] 한줄의 |에 ----- 연결선이 동시에 나오지 않게 +- [x] 테스트 코드 작성 + + +## TDD 객체 명세서 +- [x] Height + - [x] 객체 생성 가능 +- [x] Ladder + - [x] 객체 생성 가능 + - [x] 전체적인 줄이 잘 생성되는지 테스트 +- [x] Line + - [x] 객체 생성 가능 + - [x] 예상되는 사다리 라인 문자열 +- [x] Player + - [x] 객체 생성 가능 +- [x] Players + - [x] 객체 생성 가능 +- [x] util + - [x] LadderHeightValidator + - [x] 정상_입력 + - [x] 정수가_아닌_입력 + - [x] 음수_입력 + - [x] 공백_입력 + - [x] PlayerNamesValidator + - [x] 정상_입력 + - [x] 공백_포함 + - [x] 공백 + - [x] 이름_길이_초과 + - [x] 중복_입력 +# 1차 리뷰 후 개선 방향 \ No newline at end of file diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 0000000000..c5be1d8f5f --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,10 @@ +import controller.LadderGameController; + +import java.io.IOException; + +public class Application { + public static void main(String[] args) throws IOException { + LadderGameController ladderGameController = new LadderGameController(); + ladderGameController.run(); + } +} diff --git a/src/main/java/controller/LadderGameController.java b/src/main/java/controller/LadderGameController.java new file mode 100644 index 0000000000..c0357ea9b7 --- /dev/null +++ b/src/main/java/controller/LadderGameController.java @@ -0,0 +1,57 @@ +package controller; + +import model.Height; +import model.Ladder; +import model.Lines; +import model.Player; +import model.Players; +import util.RandomFootholdGenerator; +import view.InputView; +import view.OutputView; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class LadderGameController { + private final InputView inputView; + private final OutputView outputView; + + public LadderGameController() { + this.inputView = new InputView(); + this.outputView = new OutputView(); + } + + public void run() throws IOException { + Players players = createPlayersFromInput(); + List playerNames = extractPlayerNames(players); + Height height = createHeightFromInput(); + Lines ladderLines = createLadder(height, players); + + List> linesStructure = ladderLines.toBooleanList(); + outputView.printResultSentence(linesStructure, playerNames); + } + + private Players createPlayersFromInput() throws IOException { + String[] playerNamesArray = inputView.readPlayerNames(); + + return new Players(Arrays.asList(playerNamesArray)); + } + + private List extractPlayerNames(Players players) { + return players.getPlayers().stream() + .map(Player::getName) + .collect(Collectors.toList()); + } + + private Height createHeightFromInput() throws IOException { + return new Height(inputView.readLadderHeightNumber()); + } + + private Lines createLadder(Height height, Players players) { + Ladder ladder = new Ladder(); + + return ladder.generateLadder(height, players.findNumberOfPlayers() - 1); + } +} diff --git a/src/main/java/exception/ErrorMessage.java b/src/main/java/exception/ErrorMessage.java new file mode 100644 index 0000000000..ac9102a013 --- /dev/null +++ b/src/main/java/exception/ErrorMessage.java @@ -0,0 +1,22 @@ +package exception; + +public enum ErrorMessage { + + DUPLICATED_CAR_NAME("참여자 이름이 중복될 수 없습니다."), + NOT_ALLOW_BLANK("참여자 이름에 공백이 들어갈 수 없습니다."), + NOT_ALLOW_EMPTY("참여자 입력 값이 비어있을수 없습니다."), + INVALID_NAME_LENGTH("참여자의 이름은 5자 이하여야합니다."), + NOT_NUMERIC("시도 횟수 입력은 숫자여야 합니다."), + ONLY_NATURAL_NUMBER("시도 횟수는 자연수여야합니다"), + NOT_ALLOW_ZERO("사다리 높이는 0이 될 수 없습니다"); + + private final String message; + + ErrorMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +} diff --git a/src/main/java/exception/LadderGameValidationException.java b/src/main/java/exception/LadderGameValidationException.java new file mode 100644 index 0000000000..79065d6e40 --- /dev/null +++ b/src/main/java/exception/LadderGameValidationException.java @@ -0,0 +1,47 @@ +package exception; + +public class LadderGameValidationException { + + public static class DuplicatedPlayerNameException extends IllegalArgumentException { + public DuplicatedPlayerNameException() { + super(ErrorMessage.DUPLICATED_CAR_NAME.getMessage()); + } + } + + public static class BlankPlayerNameException extends IllegalArgumentException { + public BlankPlayerNameException() { + super(ErrorMessage.NOT_ALLOW_BLANK.getMessage()); + } + } + + public static class NotAllowEmptyInputException extends IllegalArgumentException { + public NotAllowEmptyInputException() { + super(ErrorMessage.NOT_ALLOW_EMPTY.getMessage()); + } + } + + public static class InvalidPlayerNameLengthException extends IllegalArgumentException { + public InvalidPlayerNameLengthException() { + super(ErrorMessage.INVALID_NAME_LENGTH.getMessage()); + } + } + + public static class NotNumericException extends IllegalArgumentException { + public NotNumericException() { + super(ErrorMessage.NOT_NUMERIC.getMessage()); + } + } + + public static class NotNaturalNumberException extends IllegalArgumentException { + public NotNaturalNumberException() { + super(ErrorMessage.ONLY_NATURAL_NUMBER.getMessage()); + } + } + + public static class NotAllowZeroNumberException extends IllegalArgumentException { + public NotAllowZeroNumberException() { + super(ErrorMessage.NOT_ALLOW_ZERO.getMessage()); + } + } + +} diff --git a/src/main/java/model/Height.java b/src/main/java/model/Height.java new file mode 100644 index 0000000000..3dbfd7a8b9 --- /dev/null +++ b/src/main/java/model/Height.java @@ -0,0 +1,14 @@ +package model; + +public class Height { + + private final String height; + + public Height(final String height) { + this.height = height; + } + + public String getHeight() { + return height; + } +} diff --git a/src/main/java/model/Ladder.java b/src/main/java/model/Ladder.java new file mode 100644 index 0000000000..a8b2fff06e --- /dev/null +++ b/src/main/java/model/Ladder.java @@ -0,0 +1,21 @@ +package model; + +import util.RandomFootholdGenerator; + +public class Ladder { + private Lines lines; + + public Ladder() { + this.lines = new Lines(); + } + + public Lines generateLadder(Height height, int size) { + for (int i = 0; i < Integer.parseInt(height.getHeight()); i++) { + Line line = new Line(new RandomFootholdGenerator(), size); + line.makeLine(); + lines.addLine(line); + } + + return lines; + } +} diff --git a/src/main/java/model/Line.java b/src/main/java/model/Line.java new file mode 100644 index 0000000000..1748298cb0 --- /dev/null +++ b/src/main/java/model/Line.java @@ -0,0 +1,36 @@ +package model; + +import util.FootholdGenerator; + +public class Line { + + private static final int INITIAL_INDEX = 0; + private static final int STEP_SIZE = 2; + + private final boolean[] line; + private final FootholdGenerator footholdGenerator; + + public Line(FootholdGenerator footholdGenerator, int size) { + this.line = new boolean[size]; + this.footholdGenerator = footholdGenerator; + } + + public void makeLine() { + for (int i = INITIAL_INDEX; i < line.length; i++) { + tryToPlaceFootholdAt(i); + } + } + + private boolean tryToPlaceFootholdAt(int index) { + if (!line[index] && footholdGenerator.generate()) { + line[index] = true; + return true; + } + return false; + } + + public boolean[] getLine() { + return line; + } + +} \ No newline at end of file diff --git a/src/main/java/model/Lines.java b/src/main/java/model/Lines.java new file mode 100644 index 0000000000..6258b37ece --- /dev/null +++ b/src/main/java/model/Lines.java @@ -0,0 +1,38 @@ +package model; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class Lines { + + private final List lines = new ArrayList<>(); + + public void addLine(Line line) { + lines.add(line); + } + + public List> toBooleanList() { + return lines.stream() + .map(this::convertLineToBooleanList) + .collect(Collectors.toList()); + } + + public List getLines() { + return lines; + } + + private List convertLineToBooleanList(Line line) { + boolean[] points = line.getLine(); + return convertArrayToBooleanList(points); + } + + private List convertArrayToBooleanList(boolean[] points) { + List booleanList = new ArrayList<>(); + for (boolean point : points) { + booleanList.add(point); + } + return booleanList; + } + +} diff --git a/src/main/java/model/Player.java b/src/main/java/model/Player.java new file mode 100644 index 0000000000..708024a3b6 --- /dev/null +++ b/src/main/java/model/Player.java @@ -0,0 +1,15 @@ +package model; + +public class Player { + + private final String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public String getName() { + return this.playerName; + } + +} \ No newline at end of file diff --git a/src/main/java/model/Players.java b/src/main/java/model/Players.java new file mode 100644 index 0000000000..e2071cc885 --- /dev/null +++ b/src/main/java/model/Players.java @@ -0,0 +1,26 @@ +package model; + +import java.util.List; + +public class Players { + + private final List players; + + public Players(List playerNames) { + this.players = makePlayers(playerNames); + } + + public int findNumberOfPlayers() { + return this.players.size(); + } + + public List getPlayers() { + return players; + } + + private List makePlayers(final List playerNames) { + return playerNames.stream() + .map(Player::new) + .toList(); + } +} diff --git a/src/main/java/util/FootholdGenerator.java b/src/main/java/util/FootholdGenerator.java new file mode 100644 index 0000000000..b23b2b2d60 --- /dev/null +++ b/src/main/java/util/FootholdGenerator.java @@ -0,0 +1,5 @@ +package util; + +public interface FootholdGenerator { + boolean generate(); +} diff --git a/src/main/java/util/RandomFootholdGenerator.java b/src/main/java/util/RandomFootholdGenerator.java new file mode 100644 index 0000000000..a30aef12ab --- /dev/null +++ b/src/main/java/util/RandomFootholdGenerator.java @@ -0,0 +1,13 @@ +package util; + +import java.util.Random; + +public class RandomFootholdGenerator implements FootholdGenerator{ + + private static Random random = new Random(); + + @Override + public boolean generate() { + return random.nextBoolean(); + } +} diff --git a/src/main/java/util/validator/LadderHeightValidator.java b/src/main/java/util/validator/LadderHeightValidator.java new file mode 100644 index 0000000000..c9c0384778 --- /dev/null +++ b/src/main/java/util/validator/LadderHeightValidator.java @@ -0,0 +1,33 @@ +package util.validator; + +import exception.LadderGameValidationException; + +public class LadderHeightValidator { + + public static void validateLadderHeightNumberIsCorrect(String ladderNumber) { + validateLadderHeightNumberIsEmpty(ladderNumber); + validateLadderHeightNumberIsNotInt(ladderNumber); + validateLadderHeightNumberIsNaturalNumber(ladderNumber); + } + + private static void validateLadderHeightNumberIsNotInt(String ladderNumber) { + try { + Integer.parseInt(ladderNumber); + } catch (NumberFormatException e) { + throw new LadderGameValidationException.NotNumericException(); + } + } + + private static void validateLadderHeightNumberIsNaturalNumber(String ladderNumber) { + int number = Integer.parseInt(ladderNumber); + if (number <= 0) { + throw new LadderGameValidationException.NotNaturalNumberException(); + } + } + + private static void validateLadderHeightNumberIsEmpty(String ladderNumber) { + if (ladderNumber.isEmpty()) { + throw new LadderGameValidationException.NotAllowEmptyInputException(); + } + } +} diff --git a/src/main/java/util/validator/PlayerValidator.java b/src/main/java/util/validator/PlayerValidator.java new file mode 100644 index 0000000000..e5737a4d5c --- /dev/null +++ b/src/main/java/util/validator/PlayerValidator.java @@ -0,0 +1,43 @@ +package util.validator; + +import exception.LadderGameValidationException; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class PlayerValidator { + + public static void validatePlayerNameIsCorrect(String[] playerNames) { + validatePlayerNameHasNoSpace(playerNames); + validatePlayerNameIsNotEmpty(playerNames); + validatePlayerNameLength(playerNames); + validatePlayerNameDuplicated(playerNames); + } + + private static void validatePlayerNameDuplicated(String[] playerNames) { + Set playerNameSet = new HashSet<>(); + if (Arrays.stream(playerNames).anyMatch(name -> !playerNameSet.add(name))) { + throw new LadderGameValidationException.DuplicatedPlayerNameException(); + } + } + + private static void validatePlayerNameIsNotEmpty(String[] playerNames) { + if (Arrays.stream(playerNames).anyMatch(String::isEmpty)) { + throw new LadderGameValidationException.NotAllowEmptyInputException(); + } + } + + private static void validatePlayerNameHasNoSpace(String[] playerNames) { + if (Arrays.stream(playerNames).anyMatch(name -> name.contains(" "))) { + throw new LadderGameValidationException.BlankPlayerNameException(); + } + } + + private static void validatePlayerNameLength(String[] playNames) { + final int LENGTH_LIMIT_NUMBER = 5; + if (Arrays.stream(playNames).anyMatch(name -> name.length() > LENGTH_LIMIT_NUMBER)) { + throw new LadderGameValidationException.InvalidPlayerNameLengthException(); + } + } +} diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java new file mode 100644 index 0000000000..e6346b0111 --- /dev/null +++ b/src/main/java/view/InputView.java @@ -0,0 +1,37 @@ +package view; + +import util.validator.LadderHeightValidator; +import util.validator.PlayerValidator; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class InputView { + + private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + + public String[] readPlayerNames() throws IOException { + System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); + String input = reader.readLine(); + + String[] playerNames = convertInputStringToArray(input); + PlayerValidator.validatePlayerNameIsCorrect(playerNames); + + return playerNames; + } + + public String readLadderHeightNumber() throws IOException { + System.out.println("최대 사다리 높이는 몇 개인가요?"); + String ladderHeightNumber = reader.readLine(); + + LadderHeightValidator.validateLadderHeightNumberIsCorrect(ladderHeightNumber); + + return ladderHeightNumber; + } + + private String[] convertInputStringToArray(String input) { + return input.split(","); + } + +} \ No newline at end of file diff --git a/src/main/java/view/LadderView.java b/src/main/java/view/LadderView.java new file mode 100644 index 0000000000..896b0b5621 --- /dev/null +++ b/src/main/java/view/LadderView.java @@ -0,0 +1,18 @@ +package view; + +public enum LadderView { + + BLANK(" "), + FOOTHOLD("-----"), + BAR("|"); + + private final String symbol; + + LadderView(final String symbol) { + this.symbol = symbol; + } + + public String getSymbol() { + return this.symbol; + } +} diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java new file mode 100644 index 0000000000..2256dc5cc8 --- /dev/null +++ b/src/main/java/view/OutputView.java @@ -0,0 +1,40 @@ +package view; + +import java.util.List; + +public class OutputView { + + public void printResultSentence(List> lines, List playerNames) { + System.out.println("실행결과"); + printPlayerNames(playerNames); + printLadder(lines); + } + + private void printPlayerNames(List playerNames) { + playerNames.forEach(name -> System.out.printf("%-6s", name)); + System.out.println(); + } + + private void printLadder(List> lines) { + lines.forEach(line -> { + printLine(line); + System.out.println("|"); + }); + } + + private void printLine(List line) { + for (int i = 0; i < line.size(); i++) { + System.out.print("|"); + printFoothold(line, i); + } + } + + private void printFoothold(List line, int index) { + if (index < line.size() && line.get(index)) { + System.out.print("-----"); + return; + } + + System.out.print(" "); + } +} diff --git a/src/test/java/model/HeightTest.java b/src/test/java/model/HeightTest.java new file mode 100644 index 0000000000..ab43934f07 --- /dev/null +++ b/src/test/java/model/HeightTest.java @@ -0,0 +1,21 @@ +package model; + + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class HeightTest { + + @Test + @DisplayName("객체 생성 테스트") + void heightObjectCreateTest() { + + String inputHeight = "5"; + + Height height = new Height(inputHeight); + + assertEquals(inputHeight, height.getHeight()); + } +} diff --git a/src/test/java/model/LadderTest.java b/src/test/java/model/LadderTest.java new file mode 100644 index 0000000000..595f9a2136 --- /dev/null +++ b/src/test/java/model/LadderTest.java @@ -0,0 +1,35 @@ +package model; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Arrays; + +class LadderTest { + + private Ladder ladder; + private Height height; + + @BeforeEach + void setUp() { + ladder = new Ladder(); + height = new Height("5"); + } + + @Test + @DisplayName("generateLadder 메소드가 정상적으로 작동하는지 테스트 합니다.") + void generateLadderTest() { + + int size = 5; + Lines lines = ladder.generateLadder(height, size); + + lines.getLines().forEach(line -> { + assertEquals(size, line.getLine().length); + }); + + assertEquals(Integer.parseInt(height.getHeight()), lines.getLines().size()); + } +} diff --git a/src/test/java/model/LineTest.java b/src/test/java/model/LineTest.java new file mode 100644 index 0000000000..33aa9fafbe --- /dev/null +++ b/src/test/java/model/LineTest.java @@ -0,0 +1,56 @@ +package model; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import util.FootholdGenerator; +import util.RandomFootholdGenerator; +import util.TestFootholdGenerator; + +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +public class LineTest { + + @Test + void makeLineWithNotFoothold() { + TestFootholdGenerator generator = new TestFootholdGenerator(false); + + int size = 5; + Line line = new Line(generator, size); + + line.makeLine(); + + boolean[] result = new boolean[size]; + assertArrayEquals(result, line.getLine()); + } + + @Test + @DisplayName("Foothold가 있는 라인 생성 테스트") + void makeLineWithFoothold() { + TestFootholdGenerator generator = new TestFootholdGenerator(true); + int size = 5; + Line line = new Line(generator, size); + + line.makeLine(); + + boolean[] result = new boolean[size]; + for (int i = 0; i < size; i++) { + result[i] = true; + } + + assertArrayEquals(result, line.getLine()); + } + + @Test + @DisplayName("정상적으로 라인 객체 생성") + void Line_Object_Create_Success_Test() { + int size = 5; + Line line = new Line(new RandomFootholdGenerator(), size); + + assertNotNull(line); + } +} diff --git a/src/test/java/model/LinesTest.java b/src/test/java/model/LinesTest.java new file mode 100644 index 0000000000..e81911ebe1 --- /dev/null +++ b/src/test/java/model/LinesTest.java @@ -0,0 +1,48 @@ +package model; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import util.FootholdGenerator; +import util.TestFootholdGenerator; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class LinesTest { + + @Test + @DisplayName("addLine 메소드가 정상적으로 작동하는지 테스트 합니다.") + void addLineTest() { + FootholdGenerator footholdGenerator = new TestFootholdGenerator(true); + + Line line1 = new Line(footholdGenerator, 5); + line1.makeLine(); + Line line2 = new Line(footholdGenerator, 5); + line2.makeLine(); + + Lines lines = new Lines(); + lines.addLine(line1); + lines.addLine(line2); + + assertEquals(2, lines.getLines().size()); + } + + @Test + @DisplayName("Lines의 Line을 boolean 리스트로 변환을 테스트 합니다.") + void convertLinesToBooleanArrayTest() { + FootholdGenerator footholdGenerator = new TestFootholdGenerator(true); + + Line line = new Line(footholdGenerator, 3); + line.makeLine(); + + Lines lines = new Lines(); + lines.addLine(line); + + List> result = lines.toBooleanList(); + + List expected = List.of(true, true, true); + + assertEquals(expected, result.get(0)); + } +} \ No newline at end of file diff --git a/src/test/java/model/PlayerTest.java b/src/test/java/model/PlayerTest.java new file mode 100644 index 0000000000..c281024e43 --- /dev/null +++ b/src/test/java/model/PlayerTest.java @@ -0,0 +1,18 @@ +package model; + + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class PlayerTest { + + @Test + @DisplayName("정상적으로 참여자 객체 생성되는지 테스트합니다") + void createPlayerObjectTest() { + Player player = new Player("pobix"); + + assertEquals("pobix", player.getName()); + } +} diff --git a/src/test/java/model/PlayersTest.java b/src/test/java/model/PlayersTest.java new file mode 100644 index 0000000000..8a84e93110 --- /dev/null +++ b/src/test/java/model/PlayersTest.java @@ -0,0 +1,34 @@ +package model; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PlayersTest { + @Test + @DisplayName("리스트를 사용해 정상적으로 Players 객체를 생성하고, 값이 같은지 확인") + void playersCreateObjectFromListTest() { + // given + List playerNames = new ArrayList<>(); + + playerNames.add("Player1"); + playerNames.add("Player2"); + playerNames.add("Player3"); + + // when + Players players = new Players(playerNames); + + // then + assertThat(players.getPlayers()).hasSize(playerNames.size()); + + List extractedNames = players.getPlayers().stream() + .map(Player::getName) + .toList(); + + assertThat(extractedNames).containsExactlyElementsOf(playerNames); + } +} diff --git a/src/test/java/util/TestFootholdGenerator.java b/src/test/java/util/TestFootholdGenerator.java new file mode 100644 index 0000000000..752cb202e3 --- /dev/null +++ b/src/test/java/util/TestFootholdGenerator.java @@ -0,0 +1,15 @@ +package util; + +public class TestFootholdGenerator implements FootholdGenerator { + + private final boolean testGenerateFoothold; + + public TestFootholdGenerator(boolean testGenerateFoothold) { + this.testGenerateFoothold = testGenerateFoothold; + } + + @Override + public boolean generate() { + return testGenerateFoothold; + } +} diff --git a/src/test/java/util/validator/LadderHeightValidatorTest.java b/src/test/java/util/validator/LadderHeightValidatorTest.java new file mode 100644 index 0000000000..8ed6b65305 --- /dev/null +++ b/src/test/java/util/validator/LadderHeightValidatorTest.java @@ -0,0 +1,37 @@ +package util.validator; + +import exception.LadderGameValidationException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class LadderHeightValidatorTest { + + @Test + void ladderHeightValidator_정상_입력() { + String ladderHeight = "5"; + assertDoesNotThrow(() -> LadderHeightValidator.validateLadderHeightNumberIsCorrect(ladderHeight)); + } + + @Test + void ladderHeightValidator_정수가_아닌_입력() { + String ladderHeight = "abc"; + assertThrows(LadderGameValidationException.NotNumericException.class, + () -> LadderHeightValidator.validateLadderHeightNumberIsCorrect(ladderHeight)); + } + + @Test + void ladderHeightValidator_음수_입력() { + String ladderHeight = "-1"; + assertThrows(LadderGameValidationException.NotNaturalNumberException.class, + () -> LadderHeightValidator.validateLadderHeightNumberIsCorrect(ladderHeight)); + } + + @Test + void ladderHeightValidator_공백_입력() { + String ladderHeight = ""; + assertThrows(LadderGameValidationException.NotAllowEmptyInputException.class, + () -> LadderHeightValidator.validateLadderHeightNumberIsCorrect(ladderHeight)); + } +} diff --git a/src/test/java/util/validator/PlayerValidatorTest.java b/src/test/java/util/validator/PlayerValidatorTest.java new file mode 100644 index 0000000000..ed19ae7f23 --- /dev/null +++ b/src/test/java/util/validator/PlayerValidatorTest.java @@ -0,0 +1,44 @@ +package util.validator; + +import exception.LadderGameValidationException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class PlayerValidatorTest { + + @Test + void playerNameValidator_정상_입력() { + String[] playerNames = {"pobi", "honux", "crong", "jk"}; + assertDoesNotThrow(() -> PlayerValidator.validatePlayerNameIsCorrect(playerNames)); + } + + @Test + void playerNameValidator_공백_포함() { + String[] playerNames = {"pobi", "ho nux", "crong", "jk"}; + assertThrows(LadderGameValidationException.BlankPlayerNameException.class, + () -> PlayerValidator.validatePlayerNameIsCorrect(playerNames)); + } + + @Test + void playerNameValidator_공백() { + String[] playerNames = {"pobi", "", "crong", "jk"}; + assertThrows(LadderGameValidationException.NotAllowEmptyInputException.class, + () -> PlayerValidator.validatePlayerNameIsCorrect(playerNames)); + } + + @Test + void playerNameValidator_이름_길이_초과() { + String[] playerNames = {"pobi", "honux", "crong", "123456"}; + assertThrows(LadderGameValidationException.InvalidPlayerNameLengthException.class, + () -> PlayerValidator.validatePlayerNameIsCorrect(playerNames)); + } + + @Test + void playerNameValidator_중복_입력() { + String[] playerNames = {"pobi", "pobi", "crong", "jk"}; + assertThrows(LadderGameValidationException.DuplicatedPlayerNameException.class, + () -> PlayerValidator.validatePlayerNameIsCorrect(playerNames)); + } +}