|
| 1 | +package com.wonu606.calculator.strategy; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.wonu606.calculator.calculator.PostfixCalculator; |
| 6 | +import com.wonu606.calculator.converter.InfixToPostfixConverter; |
| 7 | +import com.wonu606.calculator.storage.ResultStore; |
| 8 | +import com.wonu606.calculator.util.CalculatorMessage; |
| 9 | +import com.wonu606.calculator.validator.InfixValidator; |
| 10 | +import com.wonu606.io.ConsoleInput; |
| 11 | +import com.wonu606.io.ConsolePrinter; |
| 12 | +import java.io.ByteArrayInputStream; |
| 13 | +import java.io.ByteArrayOutputStream; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.PrintStream; |
| 16 | +import org.junit.jupiter.api.DisplayName; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +class CalculationStrategyTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + @DisplayName("정상 입력") |
| 23 | + void testNormalInput() { |
| 24 | + // given |
| 25 | + CalculatorStrategy calculator = new CalculationStrategy( |
| 26 | + new InfixValidator(), new InfixToPostfixConverter(), new PostfixCalculator()); |
| 27 | + |
| 28 | + String expression = "2 + 3"; |
| 29 | + InputStream in = new ByteArrayInputStream(expression.getBytes()); |
| 30 | + System.setIn(in); |
| 31 | + |
| 32 | + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
| 33 | + PrintStream out = System.out; |
| 34 | + System.setOut(new PrintStream(outContent)); |
| 35 | + |
| 36 | + // when |
| 37 | + calculator.execute(new ConsoleInput(), new ConsolePrinter(), new ResultStore()); |
| 38 | + |
| 39 | + // then |
| 40 | + String expectedResult = 5 + "\n"; |
| 41 | + assertThat(expectedResult).isEqualTo(outContent.toString()); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + @DisplayName("Double 오버 플로우 발생") |
| 46 | + void testDoubleOverflowOccurs() { |
| 47 | + // given |
| 48 | + CalculatorStrategy calculator = new CalculationStrategy( |
| 49 | + new InfixValidator(), new InfixToPostfixConverter(), new PostfixCalculator()); |
| 50 | + |
| 51 | + String expression = Double.MAX_VALUE + " + 1"; |
| 52 | + InputStream in = new ByteArrayInputStream(expression.getBytes()); |
| 53 | + System.setIn(in); |
| 54 | + |
| 55 | + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
| 56 | + PrintStream out = System.out; |
| 57 | + System.setOut(new PrintStream(outContent)); |
| 58 | + |
| 59 | + // when |
| 60 | + calculator.execute(new ConsoleInput(), new ConsolePrinter(), new ResultStore()); |
| 61 | + |
| 62 | + // then |
| 63 | + String expectedResult = CalculatorMessage.OVERFLOW_OCCURS.message + "\n"; |
| 64 | + assertThat(expectedResult).isEqualTo(outContent.toString()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @DisplayName("Integer 오버 플로우 발생") |
| 69 | + void testIntegerOverflowOccurs() { |
| 70 | + // given |
| 71 | + CalculatorStrategy calculator = new CalculationStrategy( |
| 72 | + new InfixValidator(), new InfixToPostfixConverter(), new PostfixCalculator()); |
| 73 | + |
| 74 | + String expression = Integer.MAX_VALUE + " + 1"; |
| 75 | + InputStream in = new ByteArrayInputStream(expression.getBytes()); |
| 76 | + System.setIn(in); |
| 77 | + |
| 78 | + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
| 79 | + PrintStream out = System.out; |
| 80 | + System.setOut(new PrintStream(outContent)); |
| 81 | + |
| 82 | + // when |
| 83 | + calculator.execute(new ConsoleInput(), new ConsolePrinter(), new ResultStore()); |
| 84 | + |
| 85 | + // then |
| 86 | + String expectedResult = CalculatorMessage.OVERFLOW_OCCURS.message + "\n"; |
| 87 | + assertThat(expectedResult).isEqualTo(outContent.toString()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + @DisplayName("0으로 나눌 경우 오류 발생") |
| 92 | + void testDividedByZero() { |
| 93 | + // given |
| 94 | + CalculatorStrategy calculator = new CalculationStrategy( |
| 95 | + new InfixValidator(), new InfixToPostfixConverter(), new PostfixCalculator()); |
| 96 | + |
| 97 | + String expression = "10 / 0"; |
| 98 | + InputStream in = new ByteArrayInputStream(expression.getBytes()); |
| 99 | + System.setIn(in); |
| 100 | + |
| 101 | + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
| 102 | + PrintStream out = System.out; |
| 103 | + System.setOut(new PrintStream(outContent)); |
| 104 | + |
| 105 | + // when |
| 106 | + calculator.execute(new ConsoleInput(), new ConsolePrinter(), new ResultStore()); |
| 107 | + |
| 108 | + // then |
| 109 | + String expectedResult = CalculatorMessage.NOT_DIVISIBLE_BY_ZERO.message + "\n"; |
| 110 | + assertThat(expectedResult).isEqualTo(outContent.toString()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + @DisplayName("중위 연산식이 아닌 경우") |
| 115 | + void testIsNotInfixExpression() { |
| 116 | + // given |
| 117 | + CalculatorStrategy calculator = new CalculationStrategy( |
| 118 | + new InfixValidator(), new InfixToPostfixConverter(), new PostfixCalculator()); |
| 119 | + |
| 120 | + String expression = "3 5 +"; |
| 121 | + InputStream in = new ByteArrayInputStream(expression.getBytes()); |
| 122 | + System.setIn(in); |
| 123 | + |
| 124 | + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
| 125 | + PrintStream out = System.out; |
| 126 | + System.setOut(new PrintStream(outContent)); |
| 127 | + |
| 128 | + // when |
| 129 | + calculator.execute(new ConsoleInput(), new ConsolePrinter(), new ResultStore()); |
| 130 | + |
| 131 | + // then |
| 132 | + String expectedResult = CalculatorMessage.INVALID_INPUT.message + "\n"; |
| 133 | + assertThat(expectedResult).isEqualTo(outContent.toString()); |
| 134 | + } |
| 135 | +} |
0 commit comments