-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathPointTest.java
135 lines (109 loc) · 2.99 KB
/
PointTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package org.codefx.demo.junit5.dynamic;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import java.util.List;
import java.util.stream.Stream;
import static java.lang.Math.sqrt;
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
import static org.junit.jupiter.api.DynamicTest.stream;
class PointTest {
@TestFactory
List<DynamicTest> testPointsDynamically() {
return List.of(
dynamicTest(
"A Great Test For Point",
() -> {
// test code
}),
dynamicTest(
"Another Great Test For Point",
() -> {
// test code
})
);
}
/*
* NOTE: In essence, these are parameterized tests and outside of a demo
* that feature should be used instead for such tests.
*/
void testDistanceComputation(Point p1, Point p2, double distance) {
assertEquals(distance, p1.distanceTo(p2));
}
@Test
void testDistanceComputations_loop() {
List<PointPointDistance> testData = createTestData();
for (PointPointDistance datum : testData) {
testDistanceComputation(
datum.point1(), datum.point2(), datum.distance());
}
}
@TestFactory
Stream<DynamicTest> testDistanceComputations_testFactory1() {
List<PointPointDistance> testData = createTestData();
return testData.stream()
.map(datum -> dynamicTest(
"Testing " + datum,
() -> testDistanceComputation(
datum.point1(), datum.point2(), datum.distance()
)));
}
@TestFactory
Stream<DynamicTest> testDistanceComputations_testFactory2() {
return stream(
createTestData().iterator(),
datum -> "Testing " + datum,
datum -> testDistanceComputation(
datum.point1(), datum.point2(), datum.distance()));
}
// INNER CLASSES
private List<PointPointDistance> createTestData() {
return asList(
new PointPointDistance(0, 0, 0, 0, 0), // pass
new PointPointDistance(0, 0, 1, 1, 1), // fail
new PointPointDistance(1, 2, 3, 4, 5), // fail
new PointPointDistance(1, 2, 4, 6, 5) // pass
);
}
static class PointPointDistance {
private final Point p1, p2;
private final double distance;
PointPointDistance(int x1, int y1, int x2, int y2, double distance) {
this(new Point(x1, y1), new Point(x2, y2), distance);
}
PointPointDistance(Point p1, Point p2, double distance) {
this.p1 = p1;
this.p2 = p2;
this.distance = distance;
}
Point point1() {
return p1;
}
Point point2() {
return p2;
}
double distance() {
return distance;
}
@Override
public String toString() {
return "| " + p1 + " - " + p2 + " | = " + distance;
}
}
static class Point {
private final int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
double distanceTo(Point other) {
return sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
}