Skip to content

Commit cbe8dc9

Browse files
committed
test: 중위 연산식을 입력 받아 계산하는 클래스 테스트 작성
1 parent c56bf89 commit cbe8dc9

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

calculator/src/main/java/com/wonu606/calculator/util/CalculatorMessage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
public enum CalculatorMessage {
44
INVALID_ORDER("잘못된 순번입니다."),
55
INVALID_INPUT("잘못된 입력입니다."),
6-
NOT_DIVISIBLE_BY_ZERO("0으로 나눌 수 없습니다.");
6+
NOT_DIVISIBLE_BY_ZERO("0으로 나눌 수 없습니다."),
7+
OVERFLOW_OCCURS("계산 중 Overflow가 발생했습니다.");
78

89
public final String message;
910

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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

Comments
 (0)