Skip to content

Commit 490ce1d

Browse files
committed
test: 저장소 구현체 테스트 작성
1 parent 6fa2360 commit 490ce1d

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-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: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
@Test
17+
@DisplayName("저장")
18+
void testSave() {
19+
// given
20+
Persistence store;
21+
22+
// when
23+
CalculationResult calculationResult = new CalculationResult("2 + 2", 4.0d);
24+
store.saveResult(calculationResult);
25+
26+
// then
27+
assertThat(store.findAllResult().size()).isEqualTo(1);
28+
}
29+
30+
@Test
31+
@DisplayName("순서로 결과 찾기")
32+
void testFind() {
33+
// given
34+
Persistence store;
35+
36+
// when
37+
CalculationResult saveResult = new CalculationResult("5 + 10", 15.0d);
38+
store.saveResult(saveResult);
39+
40+
// then
41+
CalculationResult foundResult = store.findResult(1);
42+
assertThat(foundResult).isEqualTo(saveResult);
43+
}
44+
45+
@Test
46+
@DisplayName("1 미만의 수로 찾은 경우 예외 발생")
47+
void testFindNumberLessThan1() {
48+
// given
49+
Persistence store;
50+
51+
// then
52+
CalculationResult saveResult = new CalculationResult("5 + 10", 15.0d);
53+
store.saveResult(saveResult);
54+
55+
// then
56+
assertThatThrownBy(() -> store.findResult(0))
57+
.isInstanceOf(IllegalArgumentException.class)
58+
.hasMessageContaining("잘못된 순번입니다.");
59+
}
60+
61+
@Test
62+
@DisplayName("사이즈 초과의 수로 찾은 경우 예외 발생")
63+
void testFindNumberMoreThanSize() {
64+
// given
65+
Persistence store;
66+
67+
// when
68+
CalculationResult saveResult = new CalculationResult("5 + 10", 15.0d);
69+
store.saveResult(saveResult);
70+
71+
// then
72+
assertThatThrownBy(() -> store.findResult(store.findAllResult().size() + 1))
73+
.isInstanceOf(IllegalArgumentException.class)
74+
.hasMessageContaining("잘못된 순번입니다.");
75+
}
76+
77+
@Test
78+
@DisplayName("모두 찾기")
79+
void testFindAll() {
80+
// given
81+
Persistence store;
82+
83+
// when
84+
List<CalculationResult> results = new ArrayList<>();
85+
results.add(new CalculationResult("5 + 6", 11.0d));
86+
results.add(new CalculationResult("5 + 6", 11.0d));
87+
88+
for (CalculationResult result : results) {
89+
store.saveResult(result);
90+
}
91+
92+
// then
93+
List<CalculationResult> foundResults = store.findAllResult();
94+
for (CalculationResult foundResult : foundResults) {
95+
assertThat(results.indexOf(foundResult)).isNotEqualTo(-1);
96+
}
97+
}
98+
99+
@Test
100+
@DisplayName("클리어")
101+
void testClear() {
102+
// given
103+
Persistence store;
104+
105+
// when
106+
store.saveResult(new CalculationResult("11 + 6", 17.0d));
107+
store.saveResult(new CalculationResult("11 + 10", 21.0d));
108+
109+
// then
110+
assertThat(store.findAllResult().size()).isNotEqualTo(0);
111+
store.clear();
112+
assertThat(store.findAllResult().size()).isEqualTo(0);
113+
}
114+
}

0 commit comments

Comments
 (0)