Skip to content

Commit f79f7d4

Browse files
committed
Refactor tests to use @ArgumentsSource
1 parent fb1c0c7 commit f79f7d4

8 files changed

Lines changed: 117 additions & 36 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2026 Prospect Robotics SWENext Club
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package com.team2813.lib2813.testing.truth;
17+
18+
import java.util.List;
19+
import java.util.stream.Stream;
20+
import org.junit.jupiter.api.extension.ExtensionContext;
21+
import org.junit.jupiter.params.provider.Arguments;
22+
import org.junit.jupiter.params.provider.ArgumentsProvider;
23+
import org.junit.jupiter.params.support.ParameterDeclarations;
24+
25+
interface Component {
26+
27+
Type getType();
28+
29+
enum Type {
30+
TRANSLATION,
31+
ROTATION
32+
}
33+
34+
abstract class ComponentArgumentsProvider<T extends Component> implements ArgumentsProvider {
35+
private final List<T> values;
36+
37+
protected ComponentArgumentsProvider(Type componentType, Stream<T> allValues) {
38+
values = allValues.filter(c -> c.getType() == componentType).toList();
39+
}
40+
41+
@Override
42+
public final Stream<? extends Arguments> provideArguments(
43+
ParameterDeclarations parameters, ExtensionContext context) {
44+
return values.stream().map(Arguments::of);
45+
}
46+
}
47+
}

testing/src/test/java/com/team2813/lib2813/testing/truth/Pose2dComponent.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,48 @@
1818
import edu.wpi.first.math.geometry.Pose2d;
1919
import edu.wpi.first.math.geometry.Rotation2d;
2020
import edu.wpi.first.math.geometry.Translation2d;
21+
import java.util.stream.Stream;
2122

22-
enum Pose2dComponent {
23-
X {
23+
/** Represents component in a two-dimensional coordinate system. */
24+
enum Pose2dComponent implements Component {
25+
X(Type.TRANSLATION) {
2426
@Override
2527
Pose2d add(Pose2d pose, double value) {
2628
return new Pose2d(pose.getX() + value, pose.getY(), pose.getRotation());
2729
}
2830
},
29-
Y {
31+
Y(Type.TRANSLATION) {
3032
@Override
3133
Pose2d add(Pose2d pose, double value) {
3234
return new Pose2d(pose.getX(), pose.getY() + value, pose.getRotation());
3335
}
3436
},
35-
R {
37+
R(Type.ROTATION) {
3638
@Override
3739
Pose2d add(Pose2d pose, double value) {
3840
return new Pose2d(
3941
pose.getTranslation(), new Rotation2d(pose.getRotation().getRadians() + value));
4042
}
4143
};
4244

45+
/** An arguments provider for Pose3dComponent values that represent translations. */
46+
static class TranslationsArgumentsProvider extends ComponentArgumentsProvider<Pose2dComponent> {
47+
TranslationsArgumentsProvider() {
48+
super(Type.TRANSLATION, Stream.of(Pose2dComponent.values()));
49+
}
50+
}
51+
52+
private final Type componentType;
53+
54+
Pose2dComponent(Type componentType) {
55+
this.componentType = componentType;
56+
}
57+
58+
@Override
59+
public final Type getType() {
60+
return componentType;
61+
}
62+
4363
abstract Pose2d add(Pose2d pose, double value);
4464

4565
final Translation2d add(Translation2d translation, double value) {

testing/src/test/java/com/team2813/lib2813/testing/truth/Pose2dSubjectTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/** Tests for {@link Pose2dSubject}. */
2626
class Pose2dSubjectTest {
2727
private static final Pose2d POSE =
28-
new Pose2d(7.353, 0.706, new Rotation2d(Math.toRadians(6.81), Math.toRadians(-25.67)));
28+
new Pose2d(7.353, 0.706, new Rotation2d(Math.PI / 6));
2929

3030
@ParameterizedTest
3131
@EnumSource(Pose2dComponent.class)

testing/src/test/java/com/team2813/lib2813/testing/truth/Pose3dComponent.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,29 @@
1818
import edu.wpi.first.math.geometry.Pose3d;
1919
import edu.wpi.first.math.geometry.Rotation3d;
2020
import edu.wpi.first.math.geometry.Translation3d;
21+
import java.util.stream.Stream;
2122

22-
enum Pose3dComponent {
23-
X {
23+
/** Represents component in a three-dimensional coordinate system. */
24+
enum Pose3dComponent implements Component {
25+
X(Type.TRANSLATION) {
2426
@Override
2527
Pose3d add(Pose3d pose, double value) {
2628
return new Pose3d(pose.getX() + value, pose.getY(), pose.getZ(), pose.getRotation());
2729
}
2830
},
29-
Y {
31+
Y(Type.TRANSLATION) {
3032
@Override
3133
Pose3d add(Pose3d pose, double value) {
3234
return new Pose3d(pose.getX(), pose.getY() + value, pose.getZ(), pose.getRotation());
3335
}
3436
},
35-
Z {
37+
Z(Type.TRANSLATION) {
3638
@Override
3739
Pose3d add(Pose3d pose, double value) {
3840
return new Pose3d(pose.getX(), pose.getY(), pose.getZ() + value, pose.getRotation());
3941
}
4042
},
41-
ROLL {
43+
ROLL(Type.ROTATION) {
4244
@Override
4345
Pose3d add(Pose3d pose, double value) {
4446
Rotation3d rotation = pose.getRotation();
@@ -47,7 +49,7 @@ Pose3d add(Pose3d pose, double value) {
4749
new Rotation3d(rotation.getX() + value, rotation.getY(), rotation.getZ()));
4850
}
4951
},
50-
PITCH {
52+
PITCH(Type.ROTATION) {
5153
@Override
5254
Pose3d add(Pose3d pose, double value) {
5355
Rotation3d rotation = pose.getRotation();
@@ -56,7 +58,7 @@ Pose3d add(Pose3d pose, double value) {
5658
new Rotation3d(rotation.getX(), rotation.getY() + value, rotation.getZ()));
5759
}
5860
},
59-
YAW {
61+
YAW(Type.ROTATION) {
6062
@Override
6163
Pose3d add(Pose3d pose, double value) {
6264
Rotation3d rotation = pose.getRotation();
@@ -66,6 +68,31 @@ Pose3d add(Pose3d pose, double value) {
6668
}
6769
};
6870

71+
/** An arguments provider for Pose3dComponent values that represent translations. */
72+
static class TranslationsArgumentsProvider extends ComponentArgumentsProvider<Pose3dComponent> {
73+
TranslationsArgumentsProvider() {
74+
super(Type.TRANSLATION, Stream.of(Pose3dComponent.values()));
75+
}
76+
}
77+
78+
/** An arguments provider for Pose3dComponent values that represent rotations. */
79+
static class RotationsArgumentsProvider extends ComponentArgumentsProvider<Pose3dComponent> {
80+
RotationsArgumentsProvider() {
81+
super(Type.ROTATION, Stream.of(Pose3dComponent.values()));
82+
}
83+
}
84+
85+
private final Type componentType;
86+
87+
Pose3dComponent(Type componentType) {
88+
this.componentType = componentType;
89+
}
90+
91+
@Override
92+
public final Type getType() {
93+
return componentType;
94+
}
95+
6996
abstract Pose3d add(Pose3d pose, double value);
7097

7198
final Translation3d add(Translation3d translation, double value) {

testing/src/test/java/com/team2813/lib2813/testing/truth/Rotation2dSubjectTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
/** Tests for {@link Rotation2dSubject}. */
2424
class Rotation2dSubjectTest {
25-
private static final Rotation2d ROTATION =
26-
new Rotation2d(Math.toRadians(6.81), Math.toRadians(-25.67));
25+
private static final Rotation2d ROTATION = new Rotation2d(Math.PI / 6);
2726

2827
@Test
2928
public void isWithin_valueWithinTolerance_doesNotThrow() {

testing/src/test/java/com/team2813/lib2813/testing/truth/Rotation3dSubjectTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,22 @@
1919

2020
import edu.wpi.first.math.geometry.Rotation3d;
2121
import org.junit.jupiter.params.ParameterizedTest;
22-
import org.junit.jupiter.params.provider.EnumSource;
22+
import org.junit.jupiter.params.provider.ArgumentsSource;
2323

2424
/** Tests for {@link Rotation3dSubject}. */
2525
class Rotation3dSubjectTest {
2626
private static final Rotation3d ROTATION = new Rotation3d(6.81, -25.67, 3.16);
2727

2828
@ParameterizedTest
29-
@EnumSource(
30-
value = Pose3dComponent.class,
31-
names = {"ROLL", "PITCH", "YAW"})
29+
@ArgumentsSource(Pose3dComponent.RotationsArgumentsProvider.class)
3230
public void isWithin_valueWithinTolerance_doesNotThrow(Pose3dComponent component) {
3331
Rotation3d closeRotation = component.add(ROTATION, 0.009);
3432

3533
Rotation3dSubject.assertThat(closeRotation).isWithin(0.01).of(ROTATION);
3634
}
3735

3836
@ParameterizedTest
39-
@EnumSource(
40-
value = Pose3dComponent.class,
41-
names = {"ROLL", "PITCH", "YAW"})
37+
@ArgumentsSource(Pose3dComponent.RotationsArgumentsProvider.class)
4238
public void isWithin_valueNotWithinTolerance_throws(Pose3dComponent component) {
4339
Rotation3d closeRotation = component.add(ROTATION, 0.011);
4440

testing/src/test/java/com/team2813/lib2813/testing/truth/Translation2dSubjectTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,22 @@
1919

2020
import edu.wpi.first.math.geometry.Translation2d;
2121
import org.junit.jupiter.params.ParameterizedTest;
22-
import org.junit.jupiter.params.provider.EnumSource;
22+
import org.junit.jupiter.params.provider.ArgumentsSource;
2323

2424
/** Tests for {@link Translation2dSubject}. */
2525
class Translation2dSubjectTest {
2626
private static final Translation2d TRANSLATION = new Translation2d(7.353, 0.706);
2727

2828
@ParameterizedTest
29-
@EnumSource(
30-
value = Pose2dComponent.class,
31-
names = {"X", "Y"})
29+
@ArgumentsSource(Pose2dComponent.TranslationsArgumentsProvider.class)
3230
public void isWithin_valueWithinTolerance_doesNotThrow(Pose2dComponent component) {
3331
Translation2d closeTranslation = component.add(TRANSLATION, 0.009);
3432

3533
Translation2dSubject.assertThat(closeTranslation).isWithin(0.01).of(TRANSLATION);
3634
}
3735

3836
@ParameterizedTest
39-
@EnumSource(
40-
value = Pose2dComponent.class,
41-
names = {"X", "Y"})
37+
@ArgumentsSource(Pose2dComponent.TranslationsArgumentsProvider.class)
4238
public void isWithin_valueNotWithinTolerance_throws(Pose2dComponent component) {
4339
Translation2d closeTranslation = component.add(TRANSLATION, 0.011);
4440

testing/src/test/java/com/team2813/lib2813/testing/truth/Translation3dSubjectTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,26 @@
1515
*/
1616
package com.team2813.lib2813.testing.truth;
1717

18-
import static org.junit.jupiter.api.Assertions.*;
18+
import static org.junit.jupiter.api.Assertions.assertThrows;
1919

2020
import edu.wpi.first.math.geometry.Translation3d;
2121
import org.junit.jupiter.params.ParameterizedTest;
22-
import org.junit.jupiter.params.provider.EnumSource;
22+
import org.junit.jupiter.params.provider.ArgumentsSource;
2323

2424
/** Tests for {@link Translation3dSubject}. */
2525
class Translation3dSubjectTest {
2626
private static final Translation3d TRANSLATION = new Translation3d(7.353, 0.706, 42.00);
2727

2828
@ParameterizedTest
29-
@EnumSource(
30-
value = Pose3dComponent.class,
31-
names = {"X", "Y", "Z"})
29+
@ArgumentsSource(Pose3dComponent.TranslationsArgumentsProvider.class)
3230
public void isWithin_valueWithinTolerance_doesNotThrow(Pose3dComponent component) {
3331
Translation3d closeTranslation = component.add(TRANSLATION, 0.009);
3432

3533
Translation3dSubject.assertThat(closeTranslation).isWithin(0.01).of(TRANSLATION);
3634
}
3735

3836
@ParameterizedTest
39-
@EnumSource(
40-
value = Pose3dComponent.class,
41-
names = {"X", "Y", "Z"})
37+
@ArgumentsSource(Pose3dComponent.TranslationsArgumentsProvider.class)
4238
public void isWithin_valueNotWithinTolerance_throws(Pose3dComponent component) {
4339
Translation3d closeTranslation = component.add(TRANSLATION, 0.011);
4440

0 commit comments

Comments
 (0)