Skip to content

Commit a2a903f

Browse files
committed
test: 저장소 구현체 테스트 작성
1 parent 8da7207 commit a2a903f

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.wonu606.calculator.model;
2+
3+
import java.util.Objects;
4+
5+
public class CalculationResult {
6+
7+
private final String expression;
8+
private final double result;
9+
10+
public CalculationResult(String expression, double result) {
11+
this.expression = expression;
12+
this.result = result;
13+
}
14+
15+
public String getExpression() {
16+
return expression;
17+
}
18+
19+
public double getResult() {
20+
return result;
21+
}
22+
23+
@Override
24+
public boolean equals(Object o) {
25+
if (this == o) {
26+
return true;
27+
}
28+
if (o == null || getClass() != o.getClass()) {
29+
return false;
30+
}
31+
CalculationResult that = (CalculationResult) o;
32+
return Double.compare(that.getResult(), getResult()) == 0 && Objects.equals(
33+
getExpression(), that.getExpression());
34+
}
35+
36+
@Override
37+
public int hashCode() {
38+
return Objects.hash(getExpression(), getResult());
39+
}
40+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.wonu606.storage;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
6+
import com.wonu606.calculator.model.CalculationResult;
7+
import com.wonu606.calculator.storage.Persistence;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.DisplayName;
12+
import org.junit.jupiter.api.Test;
13+
14+
public class ResultStoreTest {
15+
16+
private Persistence store;
17+
18+
@BeforeEach
19+
void setUp() {
20+
store.clear();
21+
}
22+
23+
@Test
24+
@DisplayName("저장")
25+
void testSave() {
26+
assertThat(store.findAllResult().size()).isEqualTo(0);
27+
28+
CalculationResult calculationResult = new CalculationResult("2 + 2", 4.0d);
29+
store.saveResult(calculationResult);
30+
31+
assertThat(store.findAllResult().size()).isEqualTo(1);
32+
}
33+
34+
@Test
35+
@DisplayName("순서로 결과 찾기")
36+
void testFind() {
37+
CalculationResult saveResult = new CalculationResult("5 + 10", 15.0d);
38+
store.saveResult(saveResult);
39+
40+
CalculationResult foundResult = store.findResult(1);
41+
assertThat(foundResult).isEqualTo(saveResult);
42+
}
43+
44+
@Test
45+
@DisplayName("1 미만의 수로 찾은 경우 예외 발생")
46+
void testFindNumberLessThan1() {
47+
CalculationResult saveResult = new CalculationResult("5 + 10", 15.0d);
48+
store.saveResult(saveResult);
49+
50+
assertThatThrownBy(() -> store.findResult(0))
51+
.isInstanceOf(IllegalArgumentException.class)
52+
.hasMessageContaining("잘못된 순번입니다.");
53+
}
54+
55+
@Test
56+
@DisplayName("사이즈 초과의 수로 찾은 경우 예외 발생")
57+
void testFindNumberMoreThanSize() {
58+
CalculationResult saveResult = new CalculationResult("5 + 10", 15.0d);
59+
store.saveResult(saveResult);
60+
61+
assertThatThrownBy(() -> store.findResult(store.findAllResult().size() + 1))
62+
.isInstanceOf(IllegalArgumentException.class)
63+
.hasMessageContaining("잘못된 순번입니다.");
64+
}
65+
66+
@Test
67+
@DisplayName("모두 찾기")
68+
void testFindAll() {
69+
List<CalculationResult> results = new ArrayList<>();
70+
results.add(new CalculationResult("5 + 6", 11.0d));
71+
results.add(new CalculationResult("5 + 6", 11.0d));
72+
73+
for (CalculationResult result : results) {
74+
store.saveResult(result);
75+
}
76+
77+
List<CalculationResult> foundResults = store.findAllResult();
78+
for (CalculationResult foundResult : foundResults) {
79+
assertThat(results.indexOf(foundResult)).isNotEqualTo(-1);
80+
}
81+
}
82+
83+
@Test
84+
@DisplayName("클리어")
85+
void testClear() {
86+
store.saveResult(new CalculationResult("11 + 6", 17.0d));
87+
store.saveResult(new CalculationResult("11 + 10", 21.0d));
88+
assertThat(store.findAllResult().size()).isNotEqualTo(0);
89+
90+
store.clear();
91+
assertThat(store.findAllResult().size()).isEqualTo(0);
92+
}
93+
}

0 commit comments

Comments
 (0)