-
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.
add first normal junit test style + assert
- Loading branch information
Showing
19 changed files
with
209 additions
and
22 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
2 changes: 1 addition & 1 deletion
2
...snippets_test_junit5/ActualGenerator.java → ..._test_junit5/snippet/ActualGenerator.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
2 changes: 1 addition & 1 deletion
2
...ippets_test_junit5/SnippetFileFormat.java → ...est_junit5/snippet/SnippetFileFormat.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
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
2 changes: 1 addition & 1 deletion
2
...snippets_test_junit5/SnippetTestFile.java → ..._test_junit5/snippet/SnippetTestFile.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
2 changes: 1 addition & 1 deletion
2
...s_test_junit5/TestCaseFilenameFilter.java → ...unit5/snippet/TestCaseFilenameFilter.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
2 changes: 1 addition & 1 deletion
2
...ippets_test_junit5/TestCaseProcessor.java → ...est_junit5/snippet/TestCaseProcessor.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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/laamella/snippets_test_junit5/snipsert/ActualGenerator.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.laamella.snippets_test_junit5.snipsert; | ||
|
||
/** | ||
* Takes the actual that was passed to Snipsert.snipsert, and outputs an "actual" which | ||
* will be compared with the "expected" in the snippet file. | ||
*/ | ||
@FunctionalInterface | ||
public interface ActualGenerator<T> { | ||
String generate(T actual); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/laamella/snippets_test_junit5/snipsert/PostProcessor.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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.laamella.snippets_test_junit5.snipsert; | ||
|
||
public interface PostProcessor<T> { | ||
T process(Object actual); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/laamella/snippets_test_junit5/snipsert/RegenerateExpectations.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.laamella.snippets_test_junit5.snipsert; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
public @interface RegenerateExpectations { | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/laamella/snippets_test_junit5/snipsert/Snipsert.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.laamella.snippets_test_junit5.snipsert; | ||
|
||
public class Snipsert { | ||
static class AssertSnippet extends RuntimeException { | ||
final Object actual; | ||
|
||
AssertSnippet(Object actual) { | ||
this.actual = actual; | ||
} | ||
} | ||
|
||
public static void snipsert(Object actual) { | ||
throw new AssertSnippet(actual); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/main/java/com/laamella/snippets_test_junit5/snipsert/SnipsertExtension.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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.laamella.snippets_test_junit5.snipsert; | ||
|
||
import com.laamella.snippets_test_junit5.BasePath; | ||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.nio.file.Files.createDirectories; | ||
import static java.nio.file.Files.readAllBytes; | ||
import static java.nio.file.StandardOpenOption.*; | ||
import static java.util.Arrays.asList; | ||
import static java.util.Collections.singletonList; | ||
import static java.util.stream.Collectors.joining; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
public class SnipsertExtension implements BeforeAllCallback, TestExecutionExceptionHandler { | ||
private Path testCasesPath; | ||
|
||
private final BasePath basePath; | ||
private final String fileExtension; | ||
private final List<ActualGenerator<?>> actualGenerators; | ||
private final String separatorBetweenExpectations; | ||
private final PostProcessor<?> postProcessor; | ||
|
||
@SafeVarargs | ||
public <T> SnipsertExtension( | ||
BasePath basePath, | ||
String fileExtension, | ||
String separatorBetweenExpectations, | ||
PostProcessor<T> postProcessor, | ||
ActualGenerator<T>... actualGenerators) { | ||
this.basePath = basePath; | ||
this.fileExtension = fileExtension; | ||
this.separatorBetweenExpectations = separatorBetweenExpectations; | ||
this.postProcessor = postProcessor; | ||
if (actualGenerators.length == 0) { | ||
this.actualGenerators = singletonList(Object::toString); | ||
} else { | ||
this.actualGenerators = asList(actualGenerators); | ||
} | ||
} | ||
|
||
@Override | ||
public void beforeAll(ExtensionContext context) throws IOException { | ||
testCasesPath = basePath.toPath().toAbsolutePath(); | ||
|
||
if (!Files.exists(testCasesPath)) { | ||
createDirectories(testCasesPath); | ||
} | ||
} | ||
|
||
@Override | ||
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable { | ||
if (throwable.getClass() != Snipsert.AssertSnippet.class) { | ||
throw throwable; | ||
} | ||
Object rawActual = ((Snipsert.AssertSnippet) throwable).actual; | ||
Object actual = postProcessor.process(rawActual); | ||
final String actualText = actualGenerators.stream() | ||
.map(ag -> ((ActualGenerator) ag).generate(actual)) | ||
.collect(joining(separatorBetweenExpectations)); | ||
|
||
Path path = testCasesPath.resolve(context.getRequiredTestMethod().getName() + "." + fileExtension); | ||
if (!Files.exists(path) || shouldRegenerate(context)) { | ||
Files.write(path, actualText.getBytes(UTF_8), CREATE, WRITE, TRUNCATE_EXISTING); | ||
Path relativize = basePath.toPath().relativize(path); | ||
|
||
fail("Generated new expection file: " + relativize); | ||
} | ||
|
||
String completeFile = new String(readAllBytes(path), UTF_8); | ||
assertEquals(completeFile, actual); | ||
} | ||
|
||
public static boolean shouldRegenerate(ExtensionContext context) { | ||
return context.getRequiredTestMethod().isAnnotationPresent(RegenerateExpectations.class) || | ||
context.getRequiredTestClass().isAnnotationPresent(RegenerateExpectations.class) || | ||
System.getProperties().containsKey("REGENERATE_EXPECTATIONS"); | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
...s_test_junit5/SnippetTestFactoryTest.java → ...unit5/snippet/SnippetTestFactoryTest.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
4 changes: 2 additions & 2 deletions
4
...st_junit5/TestCaseFilenameFilterTest.java → ...5/snippet/TestCaseFilenameFilterTest.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
31 changes: 31 additions & 0 deletions
31
src/test/java/com/laamella/snippets_test_junit5/snipsert/SnipsertTest.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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.laamella.snippets_test_junit5.snipsert; | ||
|
||
import com.laamella.snippets_test_junit5.BasePath; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class SnipsertTest { | ||
private static final BasePath basePath = BasePath.fromMavenModuleRoot(SnipsertTest.class).inSrcTestResources(); | ||
|
||
@RegisterExtension | ||
static SnipsertExtension snipsertExtension = new SnipsertExtension( | ||
basePath.inClassPackageAndNameSubDirectory(SnipsertTest.class), | ||
"txt", | ||
"\n---\n", | ||
ac -> ac); | ||
|
||
@Test | ||
void abc() { | ||
Snipsert.snipsert("Henkie"); | ||
} | ||
|
||
@Test | ||
void def() { | ||
Snipsert.snipsert("Klaas"); | ||
} | ||
|
||
@Test | ||
void ghi() { | ||
Snipsert.snipsert("Pietje"); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/test/resources/com/laamella/snippets_test_junit5/snipsert/snipsert_test/abc.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Henkie |
1 change: 1 addition & 0 deletions
1
src/test/resources/com/laamella/snippets_test_junit5/snipsert/snipsert_test/def.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Klaas |
1 change: 1 addition & 0 deletions
1
src/test/resources/com/laamella/snippets_test_junit5/snipsert/snipsert_test/ghi.txt
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Pietje |