diff --git a/src/main/java/nextstep/ladder/Main.java b/src/main/java/nextstep/ladder/Main.java index 0965b25dc4..e5c60c47b9 100644 --- a/src/main/java/nextstep/ladder/Main.java +++ b/src/main/java/nextstep/ladder/Main.java @@ -1,6 +1,6 @@ package nextstep.ladder; -import nextstep.ladder.domain.Ladder; +import nextstep.ladder.domain.*; import nextstep.ladder.view.InputView; import nextstep.ladder.view.ResultView; @@ -8,8 +8,22 @@ public class Main { public static void main(String[] args) { InputView inputView = new InputView(); - Ladder ladder = new Ladder(inputView.people(), inputView.height()); - ResultView.printResult(ladder); + People people = inputView.people(); + ExecuteResults executeResults = inputView.executeResults(); + InputOutput inputOutput = new InputOutput(people, executeResults); + + int verticalLineCount = people.value().size(); + int height = inputView.height(); + + Ladder ladder = new Ladder(verticalLineCount, height); + + ResultView.printLadder(inputOutput, ladder); + + Match match = new Match(inputOutput, ladder); + Result result = match.makeResult(); + String person = inputView.person(); + + ResultView.printResult(result, person); inputView.close(); } diff --git a/src/main/java/nextstep/ladder/README.md b/src/main/java/nextstep/ladder/README.md index 6189ef2e5f..e0ce62edc4 100644 --- a/src/main/java/nextstep/ladder/README.md +++ b/src/main/java/nextstep/ladder/README.md @@ -11,6 +11,19 @@ -[x] 라인을 생성 할지/말지 판단하는 1/2 랜덤 함수 사용 -[x] 가로 라인 확인 (겹치지 않기 위해) -[x] 사다리(Ladder) 객체 구현 --[ ] 실행결과 출력 +-[x] 실행결과 출력 - [x] 이름 출력 - [x] 사다리 출력 + +## 3단계 - 사다리(게임 실행) + +-[x] 실행 결과 입력 추가 (,로 구분) + - [x] 입력받은 실행 결과 수와 참여할 사람 이름의 수가 불일치 할 경우 예외처리 +-[x] 사다리 출력시 입력 받은 실행 결과를 포함하여 출력 +-[x] 사다리 결과 로직 구현 +-[x] 결과를 보고 싶은 사람 입력 +- all 아닐 경우 +- [x] 입력받은 결과를 보고 싶은 사람의 실행 결과 출력 +- [x] 입력 받은 사람 이름이 참여할 사람 이름 목록에 없을 경우 예외처리 +- all 일 경우 +- [x] 모든 사람의 실행 결과 출력 diff --git a/src/main/java/nextstep/ladder/domain/ExecuteResult.java b/src/main/java/nextstep/ladder/domain/ExecuteResult.java new file mode 100644 index 0000000000..85457ded3d --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/ExecuteResult.java @@ -0,0 +1,25 @@ +package nextstep.ladder.domain; + +public class ExecuteResult { + private static final int NAME_LENGTH_MAX = 5; + + private final String name; + + public ExecuteResult(String name) { + if (name.length() > NAME_LENGTH_MAX) { + throw new IllegalArgumentException("글자수는 최대 5글자까지 부여할 수 있습니다."); + } + this.name = name; + } + + public String name() { + return this.name; + } + + @Override + public String toString() { + return "ExecuteResult{" + + "name='" + name + '\'' + + '}'; + } +} diff --git a/src/main/java/nextstep/ladder/domain/ExecuteResults.java b/src/main/java/nextstep/ladder/domain/ExecuteResults.java new file mode 100644 index 0000000000..105fb5e117 --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/ExecuteResults.java @@ -0,0 +1,23 @@ +package nextstep.ladder.domain; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class ExecuteResults { + + private final List executeResults; + + public ExecuteResults(String[] executeResults) { + if (executeResults.length == 0) { + throw new IllegalStateException("실행 결과가 입력되지 않았습니다."); + } + this.executeResults = Arrays.stream(executeResults). + map(ExecuteResult::new). + collect(Collectors.toList()); + } + + public List value() { + return executeResults; + } +} diff --git a/src/main/java/nextstep/ladder/domain/InputOutput.java b/src/main/java/nextstep/ladder/domain/InputOutput.java new file mode 100644 index 0000000000..a5ae02e2c1 --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/InputOutput.java @@ -0,0 +1,23 @@ +package nextstep.ladder.domain; + +public class InputOutput { + + private final People people; + private final ExecuteResults executeResults; + + public InputOutput(People people, ExecuteResults executeResults) { + if (people.value().size() != executeResults.value().size()) { + throw new IllegalArgumentException("참여할 사람 수와 실행 결과 수는 일치해야 합니다."); + } + this.people = people; + this.executeResults = executeResults; + } + + public People people() { + return people; + } + + public ExecuteResults executeResults() { + return executeResults; + } +} diff --git a/src/main/java/nextstep/ladder/domain/Ladder.java b/src/main/java/nextstep/ladder/domain/Ladder.java index 148ccb928b..e24ac0c1a1 100644 --- a/src/main/java/nextstep/ladder/domain/Ladder.java +++ b/src/main/java/nextstep/ladder/domain/Ladder.java @@ -2,29 +2,63 @@ import java.util.List; import java.util.stream.Collectors; +import java.util.stream.IntStream; import java.util.stream.Stream; public class Ladder { - private final People people; + private static final int BEGIN_INDEX = 0; + + private final int verticalLineCount; private final Lines lines; - public Ladder(People people, int height) { - this.people = people; - this.lines = new Lines(generateLadder(height)); + public Ladder(int verticalLineCount, Lines lines) { + this.verticalLineCount = verticalLineCount; + this.lines = lines; } - private List generateLadder(int height) { - return Stream.generate(() -> new Line(people.value().size())) - .limit(height) - .collect(Collectors.toList()); + public Ladder(int verticalLineCount, int height) { + this(verticalLineCount, new Lines(generateLines(verticalLineCount, height))); } - public People people() { - return people; + private static List generateLines(int verticalLineCount, int height) { + return Stream.generate(() -> new Line(verticalLineCount)) + .limit(height) + .collect(Collectors.toList()); } public Lines lines() { return lines; } + + public String[][] result() { + List lines = this.lines.value(); + + int width = verticalLineCount * 2 - 1; + int height = this.lines.value().size(); + + String[][] ladder = new String[height][width]; + + IntStream.range(BEGIN_INDEX, height) + .forEach(i -> IntStream.range(BEGIN_INDEX, width) + .forEach(j -> { + if (isVerticalLine(j)) { + ladder[i][j] = "v"; + return; + } + if (isHorizontalLine(lines, i, j)) { + ladder[i][j] = "h"; + } + })); + return ladder; + } + + private boolean isVerticalLine(int j) { + return j % 2 == 0; + } + + private Boolean isHorizontalLine(List lines, int i, int j) { + return lines.get(i).value().get((j - 1) / 2); + } + } diff --git a/src/main/java/nextstep/ladder/domain/Line.java b/src/main/java/nextstep/ladder/domain/Line.java index b451344a85..60282e9869 100644 --- a/src/main/java/nextstep/ladder/domain/Line.java +++ b/src/main/java/nextstep/ladder/domain/Line.java @@ -10,44 +10,47 @@ public class Line { private static final boolean EMPTY_POINT = false; private static final double HALF = 0.5; - private final List points; + private final List horizontalLines; - public Line(int countOfPerson) { - points = generateLine(countOfPerson); + public Line(List horizontalLines) { + this.horizontalLines = horizontalLines; } - public List generateLine(int countOfPerson) { - List points = new ArrayList<>(); + public Line(int countOfPerson) { + this(generateLine(countOfPerson)); + } - IntStream.range(BEGIN_INDEX, countOfPerson) - .forEach(idx -> points.add(createPoint(idx, points))); + public static List generateLine(int countOfPerson) { + List horizontalLines = new ArrayList<>(); - return points; + IntStream.range(BEGIN_INDEX, countOfPerson - 1) + .forEach(idx -> horizontalLines.add(createPoint(idx, horizontalLines))); + return horizontalLines; } - private Boolean createPoint(int idx, List points) { + private static Boolean createPoint(int idx, List horizontalLines) { if (BEGIN_INDEX == idx) { - return EMPTY_POINT; + return isCurrPointNonEmpty(); } - return isPrevPointEmpty(idx, points) && isCurrPointNonEmpty(); + return isPrevPointEmpty(idx, horizontalLines) && isCurrPointNonEmpty(); } - private boolean isPrevPointEmpty(int idx, List points) { - return EMPTY_POINT == points.get(idx - 1); + private static boolean isPrevPointEmpty(int idx, List horizontalLines) { + return EMPTY_POINT == horizontalLines.get(idx - 1); } - private boolean isCurrPointNonEmpty() { + private static boolean isCurrPointNonEmpty() { return HALF < Math.random(); } - public List points() { - return points; + public List value() { + return horizontalLines; } @Override public String toString() { return "Line{" + - "points=" + points + + "horizontalLines=" + horizontalLines + '}'; } } diff --git a/src/main/java/nextstep/ladder/domain/Match.java b/src/main/java/nextstep/ladder/domain/Match.java new file mode 100644 index 0000000000..df779ccd1b --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/Match.java @@ -0,0 +1,61 @@ +package nextstep.ladder.domain; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class Match { + + private static final int BEGIN_IDX = 0; + + private final InputOutput inputOutput; + private final Ladder ladder; + + public Match(InputOutput inputOutput, Ladder ladder) { + this.inputOutput = inputOutput; + this.ladder = ladder; + } + + public Match(Ladder ladder) { + this(null, ladder); + } + + public Result makeResult() { + List people = inputOutput.people().value(); + List executeResults = inputOutput.executeResults().value(); + + return new Result(IntStream.range(BEGIN_IDX, people.size()) + .boxed() + .collect(Collectors.toMap(i -> people.get(i).name(), i -> executeResults.get(findOutputIdx(i)).name(), + (v1, v2) -> v1, LinkedHashMap::new))); + } + + int findOutputIdx(int startIdx) { + int currentIdx = startIdx * 2; + String[][] ladder = this.ladder.result(); + int rowCount = ladder.length; + int columnLength = ladder[BEGIN_IDX].length; + + currentIdx = IntStream.range(BEGIN_IDX, rowCount) + .reduce(currentIdx, (idx, i) -> { + String[] row = ladder[i]; + if (isMovableToLeft(idx, row)) { + return idx - 2; + } + if (isMovableToRight(idx, row, columnLength)) { + return idx + 2; + } + return idx; + }); + return currentIdx / 2; + } + + private boolean isMovableToLeft(int idx, String[] row) { + return idx > BEGIN_IDX && "h".equals(row[idx - 1]); + } + + private boolean isMovableToRight(int idx, String[] row, int columnLength) { + return idx < columnLength - 1 && "h".equals(row[idx + 1]); + } +} diff --git a/src/main/java/nextstep/ladder/domain/Person.java b/src/main/java/nextstep/ladder/domain/Person.java index 00c8814058..c9705f123a 100644 --- a/src/main/java/nextstep/ladder/domain/Person.java +++ b/src/main/java/nextstep/ladder/domain/Person.java @@ -1,5 +1,7 @@ package nextstep.ladder.domain; +import java.util.Objects; + public class Person { private static final int NAME_LENGTH_MAX = 5; @@ -17,6 +19,19 @@ public String name() { return this.name; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Person person = (Person) o; + return Objects.equals(name, person.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } + @Override public String toString() { return "Person{" + diff --git a/src/main/java/nextstep/ladder/domain/Result.java b/src/main/java/nextstep/ladder/domain/Result.java new file mode 100644 index 0000000000..85424b55f8 --- /dev/null +++ b/src/main/java/nextstep/ladder/domain/Result.java @@ -0,0 +1,23 @@ +package nextstep.ladder.domain; + +import java.util.Map; + +public class Result { + + private final Map result; + + public Result(Map result) { + this.result = result; + } + + public String get(String person) { + if (result.get(person) == null) { + throw new IllegalArgumentException("해당하는 사람이 존재하지 않습니다."); + } + return result.get(person); + } + + public Map value() { + return this.result; + } +} diff --git a/src/main/java/nextstep/ladder/view/InputView.java b/src/main/java/nextstep/ladder/view/InputView.java index 8bb4c76b5f..e5daec952c 100644 --- a/src/main/java/nextstep/ladder/view/InputView.java +++ b/src/main/java/nextstep/ladder/view/InputView.java @@ -1,5 +1,6 @@ package nextstep.ladder.view; +import nextstep.ladder.domain.ExecuteResults; import nextstep.ladder.domain.People; import java.util.Scanner; @@ -13,11 +14,21 @@ public People people() { return new People(nextLine().split(",")); } + public ExecuteResults executeResults() { + ResultView.printExecuteResultCommand(); + return new ExecuteResults(nextLine().split(",")); + } + public int height() { ResultView.printHeightInputCommand(); return Integer.parseInt(nextLine()); } + public String person() { + ResultView.printPersonInputCommand(); + return nextLine(); + } + private String nextLine() { return scanner.nextLine(); } diff --git a/src/main/java/nextstep/ladder/view/ResultView.java b/src/main/java/nextstep/ladder/view/ResultView.java index 5afa636ceb..bdb38b20ff 100644 --- a/src/main/java/nextstep/ladder/view/ResultView.java +++ b/src/main/java/nextstep/ladder/view/ResultView.java @@ -15,26 +15,27 @@ static void printPeopleInputCommand() { System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); } - static void printHeightInputCommand() { - System.out.println("\n최대 사다리 높이는 몇 개인가요?"); + static void printExecuteResultCommand() { + System.out.println("\n실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); } - public static void printResult(Ladder ladder) { - System.out.println("\n실행결과\n"); - printLadder(ladder); + static void printHeightInputCommand() { + System.out.println("\n최대 사다리 높이는 몇 개인가요?"); } - private static void printLadder(Ladder ladder) { - printNames(ladder); - printLines(ladder); + public static void printLadder(InputOutput inputOutput, Ladder ladder) { + System.out.println("\n사다리 결과\n"); + printNames(inputOutput); + printLines(inputOutput, ladder); + printExecuteResults(inputOutput); } - private static void printNames(Ladder ladder) { - System.out.println(makeNames(ladder)); + private static void printNames(InputOutput inputOutput) { + System.out.println(makeNames(inputOutput)); } - private static String makeNames(Ladder ladder) { - List people = ladder.people().value(); + private static String makeNames(InputOutput inputOutput) { + List people = inputOutput.people().value(); return IntStream.range(BEGIN_INDEX, people.size()) .mapToObj(i -> { @@ -43,42 +44,103 @@ private static String makeNames(Ladder ladder) { if (BEGIN_INDEX == i) { return name; } - StringBuilder sb = new StringBuilder(); - sb.append(" ".repeat(NAME_SPACE - name.length())); - sb.append(name); - return sb.toString(); + return generateSpace(name) + name; }) .collect(Collectors.joining()); } - private static void printLines(Ladder ladder) { - System.out.println(makeLines(ladder)); + private static void printLines(InputOutput inputOutput, Ladder ladder) { + System.out.println(makeLines(inputOutput, ladder)); + } + + private static String makeLines(InputOutput inputOutput, Ladder ladder) { + int firstPersonNameLength = inputOutput.people().firstPersonNameLength(); + String[][] result = ladder.result(); + + return IntStream.range(BEGIN_INDEX, result.length) + .mapToObj(i -> IntStream.range(BEGIN_INDEX, result[BEGIN_INDEX].length) + .mapToObj(j -> { + if (BEGIN_INDEX == j) { + return generateSpace(firstPersonNameLength) + "|"; + } + return generateLine(result[i][j]); + }) + .collect(Collectors.joining())) + .collect(Collectors.joining("\n")); + } + + private static String generateSpace(int firstPersonNameLength) { + return " ".repeat(firstPersonNameLength); } - private static String makeLines(Ladder ladder) { - People people = ladder.people(); - Lines lines = ladder.lines(); + private static String generateLine(String result) { + if (isVerticalLine(result)) { + return "|"; + } + if (isHorizontalLine(result)) { + return "-----"; + } + return " "; + } + + private static boolean isVerticalLine(String result) { + return "v".equals(result); + } + + private static boolean isHorizontalLine(String result) { + return "h".equals(result); + } - return lines.value().stream() - .map(line -> { - StringBuilder sb = new StringBuilder(); - IntStream.range(BEGIN_INDEX, line.points().size()) - .mapToObj(idx -> generatePointString(people, line, idx) - ) - .forEach(sb::append); + private static void printExecuteResults(InputOutput inputOutput) { + System.out.println(makeExecuteResults(inputOutput)); + } - sb.append("\n"); - return sb.toString(); + private static String makeExecuteResults(InputOutput inputOutput) { + List executeResults = inputOutput.executeResults().value(); + List people = inputOutput.people().value(); + + return IntStream.range(BEGIN_INDEX, executeResults.size()) + .mapToObj(i -> { + String executeName = executeResults.get(i).name(); + String peopleName = people.get(i).name(); + + if (BEGIN_INDEX == i) { + return generateSpace(executeName, peopleName) + executeName; + } + return generateSpace(executeName) + executeName; }) .collect(Collectors.joining()); } - private static String generatePointString(People people, Line line, int idx) { - if (BEGIN_INDEX == idx) { - return " ".repeat(people.firstPersonNameLength()) + "|"; + private static String generateSpace(String executeName, String peopleName) { + if (peopleName.length() - executeName.length() > 0) { + return " ".repeat(peopleName.length() - executeName.length()); } - return line.points().get(idx) ? "-----|" : " |"; + return ""; + } + + private static String generateSpace(String executeName) { + return " ".repeat(NAME_SPACE - executeName.length()); } + public static void printPersonInputCommand() { + System.out.println("\n결과를 보고 싶은 사람은?"); + } + + public static void printResult(Result result, String person) { + printResultCommand(); + + if ("all".equals(person)) { + result.value().entrySet().stream() + .forEachOrdered(entry -> System.out.println(entry.getKey() + " : " + entry.getValue())); + return; + } + System.out.println(result.get(person)); + + } + + private static void printResultCommand() { + System.out.println("\n실행 결과"); + } } diff --git a/src/test/java/nextstep/ladder/domain/ExecuteResultTest.java b/src/test/java/nextstep/ladder/domain/ExecuteResultTest.java new file mode 100644 index 0000000000..54ddf534e8 --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/ExecuteResultTest.java @@ -0,0 +1,22 @@ +package nextstep.ladder.domain; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +public class ExecuteResultTest { + + @Test + void create() { + ExecuteResult result = new ExecuteResult("꽝"); + assertThat(result.name()).isEqualTo(new ExecuteResult("꽝").name()); + } + + @Test + void 글자수_5이상시_예외() { + assertThatIllegalArgumentException() + .isThrownBy(() -> new ExecuteResult("500000")) + .withMessageContaining("글자수는 최대 5글자까지 부여할 수 있습니다."); + } +} diff --git a/src/test/java/nextstep/ladder/domain/ExecuteResultsTest.java b/src/test/java/nextstep/ladder/domain/ExecuteResultsTest.java new file mode 100644 index 0000000000..db3c41685c --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/ExecuteResultsTest.java @@ -0,0 +1,15 @@ +package nextstep.ladder.domain; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; + +public class ExecuteResultsTest { + + @Test + void 입력된_사람_없을시_예외() { + assertThatIllegalStateException() + .isThrownBy(() -> new ExecuteResults(new String[]{})) + .withMessageContaining("실행 결과가 입력되지 않았습니다."); + } +} diff --git a/src/test/java/nextstep/ladder/domain/InputOutputTest.java b/src/test/java/nextstep/ladder/domain/InputOutputTest.java new file mode 100644 index 0000000000..efec4b208c --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/InputOutputTest.java @@ -0,0 +1,21 @@ +package nextstep.ladder.domain; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +public class InputOutputTest { + + @Test + void input_output_개수_다를경우_예외처리() { + //given + People people = new People(new String[]{"a", "b"}); + ExecuteResults executeResults = new ExecuteResults(new String[]{"꽝"}); + + //when + //then + assertThatIllegalArgumentException() + .isThrownBy(() -> new InputOutput(people, executeResults)) + .withMessageContaining("참여할 사람 수와 실행 결과 수는 일치해야 합니다."); + } +} diff --git a/src/test/java/nextstep/ladder/domain/LadderTest.java b/src/test/java/nextstep/ladder/domain/LadderTest.java index 606daf4d3a..5f623e0622 100644 --- a/src/test/java/nextstep/ladder/domain/LadderTest.java +++ b/src/test/java/nextstep/ladder/domain/LadderTest.java @@ -2,16 +2,55 @@ import org.junit.jupiter.api.Test; +import java.util.Arrays; + import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; public class LadderTest { @Test void create() { - People people = new People(new String[]{"pobi", "honux", "crong", "jk"}); + int verticalLineCount = 4; int height = 5; - Ladder ladder = new Ladder(people, height); + Ladder ladder = new Ladder(verticalLineCount, height); assertThat(ladder.lines().value().size()).isEqualTo(height); + + } + + @Test + void 사다리_2차원배열_만들기() { + //given + int verticalLineCount = 4; + Lines lines = new Lines(Arrays.asList( + new Line(Arrays.asList(true, false, true)), + new Line(Arrays.asList(false, true, false)), + new Line(Arrays.asList(true, false, false)), + new Line(Arrays.asList(false, true, false)), + new Line(Arrays.asList(true, false, true)) + )); + + //when + Ladder ladder = new Ladder(verticalLineCount, lines); + String[][] result = ladder.result(); + + //then + assertTrue(arrayContainsValue(result, "v")); + assertTrue(arrayContainsValue(result, "h")); + assertTrue(arrayContainsValue(result, null)); + assertFalse(arrayContainsValue(result, "x")); + } + + private boolean arrayContainsValue(String[][] array, String value) { + return Arrays.stream(array) + .flatMap(Arrays::stream) + .anyMatch(element -> { + if (value == null) { + return element == null; + } + return value.equals(element); + }); } } diff --git a/src/test/java/nextstep/ladder/domain/LineTest.java b/src/test/java/nextstep/ladder/domain/LineTest.java index 6d36b4ee5d..1eb079e88e 100644 --- a/src/test/java/nextstep/ladder/domain/LineTest.java +++ b/src/test/java/nextstep/ladder/domain/LineTest.java @@ -18,14 +18,14 @@ void setup() { @Test void create() { - assertThat(line.points().size()).isEqualTo(4); + assertThat(line.value().size()).isEqualTo(3); } @ParameterizedTest - @ValueSource(ints = {1, 2, 3}) + @ValueSource(ints = {1, 2}) void 라인이_겹치지_않는지_확인(int idx) { - Boolean prevPoint = line.points().get(idx - 1); - Boolean currPoint = line.points().get(idx); + Boolean prevPoint = line.value().get(idx - 1); + Boolean currPoint = line.value().get(idx); if (prevPoint) assertThat(true).isNotEqualTo(currPoint); diff --git a/src/test/java/nextstep/ladder/domain/MatchResultTest.java b/src/test/java/nextstep/ladder/domain/MatchResultTest.java new file mode 100644 index 0000000000..bafeea8ced --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/MatchResultTest.java @@ -0,0 +1,61 @@ +package nextstep.ladder.domain; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.Arrays; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +class MatchResultTest { + + private People people; + private Lines lines; + private ExecuteResults executeResults; + private Ladder ladder; + private InputOutput inputOutput; + + @BeforeEach + void setup() { + people = new People(new String[]{"pobi", "honux", "crong", "jk"}); + executeResults = new ExecuteResults(new String[]{"꽝1", "5000", "꽝2", "3000"}); + inputOutput = new InputOutput(people, executeResults); + + lines = new Lines(Arrays.asList( + new Line(Arrays.asList(true, false, true)), + new Line(Arrays.asList(false, true, false)), + new Line(Arrays.asList(true, false, false)), + new Line(Arrays.asList(false, true, false)), + new Line(Arrays.asList(true, false, true)) + )); + int verticalLineSize = people.value().size(); + ladder = new Ladder(verticalLineSize, lines); + } + + @ParameterizedTest + @CsvSource(value = {"0:0", "1:3", "2:2", "3:1"}, delimiter = ':') + void findOutputIdx(int inputIdx, int outputIdx) { + //given + Match match = new Match(ladder); + + //when + int resultIdx = match.findOutputIdx(inputIdx); + + //then + assertThat(resultIdx).isEqualTo(outputIdx); + } + + @ParameterizedTest + @CsvSource(value = {"pobi:꽝1", "honux:3000", "crong:꽝2", "jk:5000"}, delimiter = ':') + void 사다리_결과(String input, String output) { + //given + Match match = new Match(inputOutput, ladder); + + //when + Result result = match.makeResult(); + + //then + assertThat(result.value().get(input)).isEqualTo(output); + } +} diff --git a/src/test/java/nextstep/ladder/domain/ResultTest.java b/src/test/java/nextstep/ladder/domain/ResultTest.java new file mode 100644 index 0000000000..9ecb6db0c8 --- /dev/null +++ b/src/test/java/nextstep/ladder/domain/ResultTest.java @@ -0,0 +1,19 @@ +package nextstep.ladder.domain; + +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +class ResultTest { + + @Test + void 입력받은_이름이_참여할_이름목록에_없을경우_예외처리() { + Result result = new Result(Map.of("pobi", "꽝")); + + assertThatIllegalArgumentException() + .isThrownBy(() -> result.get("crong")) + .withMessageContaining("해당하는 사람이 존재하지 않습니다."); + } +}