Skip to content

Commit 400b2a4

Browse files
committed
test: 중위 연산식을 후위 연산식으로 변환하는 Converter 테스트 작성 및 통과
1 parent 95306f6 commit 400b2a4

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.wonu606.calculator.converter;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.api.Test;
9+
10+
class InfixToPostfixConverterTest {
11+
12+
@Test
13+
@DisplayName("연산자가 1개일 경우")
14+
void testSingle() {
15+
// given
16+
Converter<String, List<String>> converter = new InfixToPostfixConverter();
17+
List<String> expectedList = Arrays.asList("-3", "5.0", "+");
18+
String infixExpression = "-3 + 5.0";
19+
20+
// when
21+
List<String> actualList = converter.convert(infixExpression);
22+
23+
// then
24+
assertThat(actualList).isEqualTo(expectedList);
25+
}
26+
27+
@Test
28+
@DisplayName("같은 우선 순위일 경우")
29+
void testPrecedenceEquals() {
30+
// given
31+
Converter<String, List<String>> converter = new InfixToPostfixConverter();
32+
List<String> expectedList = Arrays.asList("3", "5", "+", "2", "-");
33+
String infixExpression = "3 + 5 - 2";
34+
35+
// when
36+
List<String> actualList = converter.convert(infixExpression);
37+
38+
// then
39+
assertThat(actualList).isEqualTo(expectedList);
40+
}
41+
42+
@Test
43+
@DisplayName("높은 우선 순위가 뒤에 올 경우")
44+
void testHigherPrecedenceFollows() {
45+
// given
46+
Converter<String, List<String>> converter = new InfixToPostfixConverter();
47+
List<String> expectedList = Arrays.asList("3", "5", "2", "*", "+");
48+
String infixExpression = "3 + 5 * 2";
49+
50+
// when
51+
List<String> actualList = converter.convert(infixExpression);
52+
53+
// then
54+
assertThat(actualList).isEqualTo(expectedList);
55+
}
56+
57+
@Test
58+
@DisplayName("높은 우선 순위가 앞에 올 경우")
59+
void testHigherPrecedenceComesFirst() {
60+
// given
61+
Converter<String, List<String>> converter = new InfixToPostfixConverter();
62+
List<String> expectedList = Arrays.asList("3", "5", "*", "2", "+");
63+
String infixExpression = "3 * 5 + 2";
64+
65+
// when
66+
List<String> actualList = converter.convert(infixExpression);
67+
68+
// then
69+
assertThat(actualList).isEqualTo(expectedList);
70+
}
71+
}

calculator/src/test/java/com/wonu606/validator/InfixValidatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void testNotInfix() {
1414
Validator validator = new InfixValidator();
1515

1616
// when
17-
String expression = "+ 1 3";
17+
String expression = "1 3 +";
1818

1919
// then
2020
assertThat(validator.isValid(expression)).isFalse();

0 commit comments

Comments
 (0)