|
| 1 | +package racingcar; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.BeforeEach; |
| 4 | +import org.junit.jupiter.api.DisplayName; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import racingcar.domain.Car; |
| 7 | +import racingcar.domain.Cars; |
| 8 | +import racingcar.generator.RandomGenerator; |
| 9 | +import racingcar.ui.InputView; |
| 10 | + |
| 11 | +import java.io.ByteArrayInputStream; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Scanner; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 18 | + |
| 19 | +public class RacingCarTest { |
| 20 | + |
| 21 | + private static final int CAR = 3; |
| 22 | + Car car; |
| 23 | + Cars cars; |
| 24 | + |
| 25 | + @BeforeEach |
| 26 | + void setUp() { |
| 27 | + car = new Car(); // 1λ‘ μ΄κΈ°ν |
| 28 | + cars = new Cars(RacingCar.getCars(CAR)); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + @DisplayName("μ μ§ μ‘°κ±΄μ λ§μ‘±νλ©΄ μ μ§") |
| 33 | + void move(){ |
| 34 | + car.move(()->true); |
| 35 | + assertThat(car.getMoveCount()).isEqualTo(2); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + @DisplayName("μ μ§ μ‘°κ±΄μ λ§μ‘±νμ§ μμΌλ©΄ λ©μΆ€") |
| 40 | + void stop(){ |
| 41 | + car.move(()->false); |
| 42 | + assertThat(car.getMoveCount()).isEqualTo(1); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + @DisplayName("μ μ§ μ‘°κ±΄μ λ§μ‘±νλ©΄ λͺ¨λ μ μ§") |
| 47 | + void moveAll(){ |
| 48 | + cars.moveAll(()->true); |
| 49 | + |
| 50 | + assertAll( |
| 51 | + () -> assertThat(cars.getCurrentStatus().get(0).getMoveCount()).isEqualTo(2), |
| 52 | + () -> assertThat(cars.getCurrentStatus().get(1).getMoveCount()).isEqualTo(2), |
| 53 | + () -> assertThat(cars.getCurrentStatus().get(2).getMoveCount()).isEqualTo(2) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + @DisplayName("μ μ§ μ‘°κ±΄μ λ§μ‘±νμ§ μμΌλ©΄ λͺ¨λ λ©μΆ€") |
| 59 | + void stopAll(){ |
| 60 | + cars.moveAll(()->false); |
| 61 | + |
| 62 | + assertAll( |
| 63 | + () -> assertThat(cars.getCurrentStatus().get(0).getMoveCount()).isEqualTo(1), |
| 64 | + () -> assertThat(cars.getCurrentStatus().get(1).getMoveCount()).isEqualTo(1), |
| 65 | + () -> assertThat(cars.getCurrentStatus().get(2).getMoveCount()).isEqualTo(1) |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + @DisplayName("random κ°μ 0κ³Ό 9μ¬μ΄ κ°") |
| 71 | + void random(){ |
| 72 | + assertThat(RandomGenerator.generate()).isBetween(0, 9); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @DisplayName("μλμ°¨ λμκ° 0λ³΄λ€ μμ κ²½μ° μ€λ₯ 리ν΄") |
| 77 | + void inputCar(){ |
| 78 | + String input = "0"; |
| 79 | + InputView.setScanner(new Scanner(new ByteArrayInputStream(input.getBytes()))); |
| 80 | + |
| 81 | + assertThatThrownBy(InputView::inputValidatedNumberOfCar) |
| 82 | + .isInstanceOf(IllegalArgumentException.class); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @DisplayName("μλν νμκ° 0λ³΄λ€ μμ κ²½μ° μ€λ₯ 리ν΄") |
| 87 | + void inputAttempts(){ |
| 88 | + String input = "0"; |
| 89 | + InputView.setScanner(new Scanner(new ByteArrayInputStream(input.getBytes()))); |
| 90 | + |
| 91 | + assertThatThrownBy(InputView::inputValidatedNumberOfAttempts) |
| 92 | + .isInstanceOf(IllegalArgumentException.class); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + @DisplayName("λ°νλ μλμ°¨ Listμ ν¬κΈ°λ μ
λ ₯λ ν¬κΈ°μ κ°λ€.") |
| 97 | + public void getCars() { |
| 98 | + ArrayList<Car> cars = RacingCar.getCars(CAR); |
| 99 | + assertThat(cars.size()).isEqualTo(CAR); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | +} |
0 commit comments