-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added runtime class search when testing
- Loading branch information
1 parent
cb03545
commit 5c25d02
Showing
7 changed files
with
103 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 31 additions & 23 deletions
54
src/test/java/com/belellou/kevin/advent/DaySolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,54 @@ | ||
package com.belellou.kevin.advent; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
|
||
import org.junit.jupiter.api.DynamicTest; | ||
import org.junit.jupiter.api.TestFactory; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; | ||
import org.springframework.core.type.filter.AssignableTypeFilter; | ||
|
||
import com.belellou.kevin.advent.generic.DaySolver; | ||
import com.belellou.kevin.advent.year2015.Day1; | ||
import com.belellou.kevin.advent.year2015.Day2; | ||
import com.belellou.kevin.advent.year2015.Day3; | ||
import com.belellou.kevin.advent.year2015.Day4; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class DaySolverTest { | ||
|
||
private static final List<TestCase> TEST_CASES = List.of(new TestCase(new Day1(), 74, 1_795), | ||
new TestCase(new Day2(), 1_588_178, 3_783_758), | ||
new TestCase(new Day3(), 2_081, 2_341), | ||
new TestCase(new Day4(), 346_386, 9_958_218)); | ||
|
||
private static final String FIRST_SOLUTION = " - First solution"; | ||
private static final String SECOND_SOLUTION = " - Second solution"; | ||
|
||
@TestFactory | ||
List<DynamicTest> testDaySolvers() { | ||
return TEST_CASES.stream() | ||
.<DynamicTest>mapMulti((testCase, consumer) -> { | ||
consumer.accept(dynamicTestOf(testCase, true)); | ||
consumer.accept(dynamicTestOf(testCase, false)); | ||
}).toList(); | ||
private static void createDynamicTest(BeanDefinition component, Consumer<DynamicTest> consumer) { | ||
try { | ||
Class<?> clazz = Class.forName(component.getBeanClassName()); | ||
DaySolver daySolver = (DaySolver) clazz.getDeclaredConstructor().newInstance(); | ||
|
||
consumer.accept(dynamicTestOf(daySolver, true)); | ||
consumer.accept(dynamicTestOf(daySolver, false)); | ||
} catch (ClassNotFoundException | InvocationTargetException | InstantiationException | | ||
IllegalAccessException | NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static DynamicTest dynamicTestOf(TestCase testCase, boolean firstSolution) { | ||
return DynamicTest.dynamicTest(testCase.daySolver + (firstSolution ? FIRST_SOLUTION : SECOND_SOLUTION), | ||
private static DynamicTest dynamicTestOf(DaySolver daySolver, boolean firstSolution) { | ||
return DynamicTest.dynamicTest(daySolver + (firstSolution ? FIRST_SOLUTION : SECOND_SOLUTION), | ||
firstSolution | ||
? () -> assertThat(testCase.daySolver.solveFirstStar()) | ||
.isEqualTo(testCase.firstSolution) | ||
: () -> assertThat(testCase.daySolver.solveSecondStar()) | ||
.isEqualTo(testCase.secondSolution) | ||
? () -> assertThat(daySolver.solveFirstStar()) | ||
.isEqualTo(daySolver.getFirstStarSolution()) | ||
: () -> assertThat(daySolver.solveSecondStar()) | ||
.isEqualTo(daySolver.getSecondStarSolution()) | ||
); | ||
} | ||
|
||
protected record TestCase(DaySolver daySolver, int firstSolution, int secondSolution) {} | ||
@TestFactory | ||
List<DynamicTest> testDaySolvers() { | ||
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); | ||
provider.addIncludeFilter(new AssignableTypeFilter(DaySolver.class)); | ||
|
||
Set<BeanDefinition> components = provider.findCandidateComponents(getClass().getPackageName()); | ||
return components.stream().mapMulti(DaySolverTest::createDynamicTest).toList(); | ||
} | ||
} |