Skip to content

Commit d547f04

Browse files
committed
feat, test: Points 도메인 생성 구현 및 테스트
1 parent ab7f642 commit d547f04

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

src/main/java/com/dongko/coordiate/domain/Point.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.dongko.coordiate.domain.exception.IllegalPointException;
44

5+
import java.util.Objects;
6+
57
public class Point {
68
private final int x;
79
private final int y;
@@ -15,4 +17,17 @@ public Point(int x, int y) {
1517
this.x = x;
1618
this.y = y;
1719
}
20+
21+
@Override
22+
public boolean equals(Object o) {
23+
if (this == o) return true;
24+
if (o == null || getClass() != o.getClass()) return false;
25+
Point point = (Point) o;
26+
return x == point.x && y == point.y;
27+
}
28+
29+
@Override
30+
public int hashCode() {
31+
return Objects.hash(x, y);
32+
}
1833
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.dongko.coordiate.domain;
2+
3+
import com.dongko.coordiate.domain.exception.IllegalPointsException;
4+
5+
import java.util.List;
6+
import java.util.Objects;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
10+
public class Points {
11+
private static String regex = "\\((\\d+),(\\d+)\\)-\\((\\d+),(\\d+)\\)";
12+
13+
private final List<Point> points;
14+
public Points(String pointsString) {
15+
Pattern pattern = Pattern.compile(regex);
16+
Matcher matcher = pattern.matcher(pointsString);
17+
if (!matcher.matches()) {
18+
throw new IllegalPointsException();
19+
}
20+
int x1 = Integer.parseInt(matcher.group(1));
21+
int y1 = Integer.parseInt(matcher.group(2));
22+
int x2 = Integer.parseInt(matcher.group(3));
23+
int y2 = Integer.parseInt(matcher.group(4));
24+
25+
points = List.of(new Point(x1, y1), new Point(x2, y2));
26+
}
27+
28+
@Override
29+
public boolean equals(Object o) {
30+
if (this == o) return true;
31+
if (o == null || getClass() != o.getClass()) return false;
32+
Points points1 = (Points) o;
33+
return Objects.equals(points, points1.points);
34+
}
35+
36+
@Override
37+
public int hashCode() {
38+
return Objects.hash(points);
39+
}
40+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.dongko.coordiate.domain.exception;
2+
3+
public class IllegalPointsException extends CoordinateCalculatorDomainException{
4+
public IllegalPointsException() {
5+
super("잘못된 좌표 값 입니다.");
6+
}
7+
}

src/test/java/com/dongko/coordiate/PointTest.java renamed to src/test/java/com/dongko/coordiate/domain/PointTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package com.dongko.coordiate;
1+
package com.dongko.coordiate.domain;
22

33
import com.dongko.coordiate.domain.Point;
44
import com.dongko.coordiate.domain.exception.IllegalPointException;
55
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.api.Test;
67
import org.junit.jupiter.params.ParameterizedTest;
78
import org.junit.jupiter.params.provider.CsvSource;
89

@@ -20,4 +21,9 @@ void test(String xString, String yString) {
2021
new Point(x, y)
2122
);
2223
}
24+
25+
@Test
26+
void 객체비교테스트() {
27+
assertEquals(new Point(1,2), new Point(1,2));
28+
}
2329
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.dongko.coordiate.domain;
2+
3+
import com.dongko.coordiate.domain.exception.IllegalPointsException;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.CsvSource;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class PointsTest {
12+
13+
@ParameterizedTest
14+
@CsvSource({"ashjkd", "(10,10,123)-(14,15)", "(10,10)-(14,151,123)", "12379612679"})
15+
@DisplayName("(x1,y1)-(x2,y2) 형식의 입력 값만 받을 수 있다.")
16+
void test(String pointsString) {
17+
assertThrows(IllegalPointsException.class, () ->
18+
new Points(pointsString)
19+
);
20+
}
21+
22+
@Test
23+
void 객체비교테스트() {
24+
assertEquals(new Points("(10,10)-(14,15)"), new Points("(10,10)-(14,15)"));
25+
}
26+
27+
@Test
28+
void 객체비교테스트2() {
29+
assertNotEquals(new Points("(10,10)-(14,15)"), new Points("(10,9)-(14,15)"));
30+
}
31+
}

0 commit comments

Comments
 (0)