Skip to content

Commit aa8b80d

Browse files
committed
feat: create Enums from String in TestCases
Refs: #127
1 parent 30c2191 commit aa8b80d

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/main/java/com/github/nylle/javafixture/annotations/testcases/ReflectedTestCase.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public ReflectedTestCase(TestCase testCase) {
3838
@SuppressWarnings("unchecked")
3939
public <T> T getTestCaseValueFor(Class<T> type, int i) {
4040
validate(type, i);
41+
if(type.isEnum()) {
42+
return (T) Enum.valueOf((Class<Enum>) type, (String)matrix.get(asPrimitive(String.class)).get(i));
43+
}
4144
return (T) matrix.get(asPrimitive(type)).get(i);
4245
}
4346

@@ -80,6 +83,9 @@ private static <T> boolean isInvalid(Class<T> type, List<?> nonDefaultValues) {
8083
if (nonDefaultValues.size() > 1) {
8184
return true;
8285
}
86+
if(type.isEnum()) {
87+
return false;
88+
}
8389
return asPrimitive(type) != asPrimitive(nonDefaultValues.get(0).getClass());
8490
}
8591

src/test/java/com/github/nylle/javafixture/annotations/testcases/ReflectedTestCaseTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.nylle.javafixture.annotations.testcases;
22

3+
import com.github.nylle.javafixture.testobjects.TestEnum;
34
import com.github.nylle.javafixture.testobjects.TestObject;
45
import org.junit.jupiter.api.Test;
56

@@ -11,7 +12,7 @@
1112
class ReflectedTestCaseTest {
1213

1314
@Test
14-
void getTestCaseValueFor_ThrowsIfTwoDifferentValuesAreCustomized() {
15+
void throwsIfTwoDifferentValuesAreCustomized() {
1516
var testCaseWithDuplicateCustomizationsAt_1_2_3_4_6 = createCustomTestCase(
1617
"foo", true,
1718
2.0f, 2L,
@@ -148,6 +149,25 @@ void createsReflectedTestCaseWithDefaultValues() {
148149
assertThat(actual.getTestCaseValueFor(Byte.class, 5)).isZero();
149150
}
150151

152+
@Test
153+
void createsEnumFromString() {
154+
var testCaseWithStringOnPosition0 = createCustomTestCase("VALUE3", false, 0.0f, 0L, Object.class, 0, (short) 0, Character.MIN_VALUE, (byte) 0, 0.0d);
155+
156+
var actual = new ReflectedTestCase(testCaseWithStringOnPosition0);
157+
158+
assertThat(actual.getTestCaseValueFor(TestEnum.class, 0)).isEqualTo(TestEnum.VALUE3);
159+
}
160+
161+
@Test
162+
void throwsForInvalidEnumValue() {
163+
var testCaseWithStringOnPosition0 = createCustomTestCase("INVALID", false, 0.0f, 0L, Object.class, 0, (short) 0, Character.MIN_VALUE, (byte) 0, 0.0d);
164+
165+
var actual = new ReflectedTestCase(testCaseWithStringOnPosition0);
166+
167+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> actual.getTestCaseValueFor(TestEnum.class, 0))
168+
.withMessage("No enum constant com.github.nylle.javafixture.testobjects.TestEnum.INVALID");
169+
}
170+
151171
private static TestCase createDefaultTestCase() {
152172
return new TestCase() {
153173
@Override

src/test/java/com/github/nylle/javafixture/annotations/testcases/TestCaseTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.nylle.javafixture.annotations.testcases;
22

3+
import com.github.nylle.javafixture.testobjects.TestEnum;
34
import org.junit.jupiter.api.DisplayName;
45
import org.junit.jupiter.api.Nested;
56

@@ -38,6 +39,13 @@ void testClassIsAssignable(Class type, Class interfaceType, boolean expected) {
3839
assertThat(interfaceType.isAssignableFrom(type)).isEqualTo(expected);
3940
}
4041

42+
@TestWithCases
43+
@TestCase(str1 = "VALUE1", str2 = "VALUE1")
44+
@TestCase(str1 = "VALUE2", str2 = "VALUE2")
45+
void canCreateEnumFromString(TestEnum testEnum, String string) {
46+
assertThat(testEnum).isEqualTo(TestEnum.valueOf(string));
47+
}
48+
4149
@Nested
4250
@DisplayName("verify that primitive wrapper classes are supported as method arguments")
4351
class PrimitiveAutoboxing {

0 commit comments

Comments
 (0)