diff --git a/src/main/java/maxbe/goldenmaster/junit/extension/GoldenMasterRun.java b/src/main/java/maxbe/goldenmaster/junit/extension/GoldenMasterRun.java index f6e6e35..93084ec 100644 --- a/src/main/java/maxbe/goldenmaster/junit/extension/GoldenMasterRun.java +++ b/src/main/java/maxbe/goldenmaster/junit/extension/GoldenMasterRun.java @@ -18,10 +18,10 @@ public static final String AUTO_ID = ""; int repetitions() default DEFAULT_REPETITIONS; - + /** - * Allows to define multiple instrumentations of the same usecase which can be approved against each other. - * TODO #3 Document in GitHub + * Allows to define multiple instrumentations of the same usecase which can be approved against each other. TODO #2 + * Document in GitHub */ String id() default AUTO_ID; diff --git a/src/main/java/maxbe/goldenmaster/junit/extension/RunInvocationContextProvider.java b/src/main/java/maxbe/goldenmaster/junit/extension/RunInvocationContextProvider.java index 5222dd8..d23eb2a 100644 --- a/src/main/java/maxbe/goldenmaster/junit/extension/RunInvocationContextProvider.java +++ b/src/main/java/maxbe/goldenmaster/junit/extension/RunInvocationContextProvider.java @@ -2,7 +2,6 @@ import java.io.File; import java.io.IOException; -import java.lang.reflect.Method; import java.nio.file.Paths; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -51,7 +50,8 @@ public Stream provideTestTemplateInvocationContex return IntStream.range(0, repetitions).boxed().map(index -> new IndexedRunInvocationContext(index, outputFile)); } - // TODO #3 Ensure same number of repetitions for all tests with the same approvalId by moving repetitions to @GoldenMasterTest? + // TODO #2 Ensure same number of repetitions for all tests with the same approvalId by moving repetitions to + // @GoldenMasterTest? private int determineRepetitions(ExtensionContext context) { GoldenMasterRun goldenMasterAnnotation = getAnnotation(context); if (goldenMasterAnnotation == null) { @@ -64,9 +64,9 @@ private int determineRepetitions(ExtensionContext context) { return repetitions; } - private GoldenMasterRun getAnnotation(ExtensionContext context) { - return context.getElement().get().getAnnotation(GoldenMasterRun.class); - } + private GoldenMasterRun getAnnotation(ExtensionContext context) { + return context.getElement().get().getAnnotation(GoldenMasterRun.class); + } @Override public void beforeAll(ExtensionContext context) throws Exception { @@ -77,9 +77,10 @@ public void beforeAll(ExtensionContext context) throws Exception { @Override public void beforeEach(ExtensionContext context) throws Exception { - String approvalId = getApprovalId(context) + getRunIdSuffix(context.getDisplayName()); - // TODO MAX Path must include run ID - pathMapper = new TemplatedTestPathMapper<>(context, Paths.get("src", "test", "resources", "approved"), approvalId); + String approvalId = getApprovalId(context) + getRunIdSuffix(context.getDisplayName()); + // TODO MAX Path must include run ID + pathMapper = new TemplatedTestPathMapper<>(context, Paths.get("src", "test", "resources", "approved"), + approvalId); } @Override @@ -94,20 +95,20 @@ public void afterTestExecution(ExtensionContext context) throws Exception { approval.verify(outputFile, Paths.get(fileName)); } - private String getApprovalId(ExtensionContext context) { - GoldenMasterRun annotation = getAnnotation(context); - if (!GoldenMasterRun.AUTO_ID.equals(annotation.id())) { - return annotation.id(); - } - return context.getRequiredTestMethod().getName(); - } - - private String getRunIdSuffix(String displayName) { - // REVIEW #3 Can this be done better? - String idWithoutBraces = displayName.substring(1, displayName.length() - 1); - int runId = Integer.valueOf(idWithoutBraces) - 1; - return "[" + runId + "]"; - } + private String getApprovalId(ExtensionContext context) { + GoldenMasterRun annotation = getAnnotation(context); + if (!GoldenMasterRun.AUTO_ID.equals(annotation.id())) { + return annotation.id(); + } + return context.getRequiredTestMethod().getName(); + } + + private String getRunIdSuffix(String displayName) { + // REVIEW #3 Can this be done better? + String idWithoutBraces = displayName.substring(1, displayName.length() - 1); + int runId = Integer.valueOf(idWithoutBraces) - 1; + return "[" + runId + "]"; + } @Override public void afterAll(ExtensionContext context) throws Exception { diff --git a/src/test/java/maxbe/goldenmaster/approval/TemplatedTestPathMapperTest.java b/src/test/java/maxbe/goldenmaster/approval/TemplatedTestPathMapperTest.java index 1d4eb6c..5ecd280 100644 --- a/src/test/java/maxbe/goldenmaster/approval/TemplatedTestPathMapperTest.java +++ b/src/test/java/maxbe/goldenmaster/approval/TemplatedTestPathMapperTest.java @@ -1,14 +1,13 @@ package maxbe.goldenmaster.approval; -import static maxbe.goldenmaster.junit.extension.GoldenMasterRun.AUTO_ID; import static org.assertj.core.api.Assertions.assertThat; import java.io.File; import java.lang.annotation.Annotation; -import java.lang.reflect.Method; import java.nio.file.Path; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtensionContext; import org.mockito.Mockito; @@ -16,65 +15,66 @@ import maxbe.goldenmaster.junit.extension.GoldenMasterRun; // TODO MAX Probably not meaningful anymore +@Disabled public class TemplatedTestPathMapperTest { - - private ExtensionContext context; - - @BeforeEach - void setUp() { - context = Mockito.mock(ExtensionContext.class); - Mockito.doReturn(getClass()).when(context).getRequiredTestClass(); - Mockito.doReturn(TemplatedTestPathMapperTest.class.getMethods()[0]).when(context).getRequiredTestMethod(); - Mockito.doReturn("[123]").when(context).getDisplayName(); - } - - @Test - void usesTestIdIfSpecified() throws Exception { - String testId = "test-id"; - Path tempDir = new File("root").toPath(); - TemplatedTestPathMapper mapper = new TemplatedTestPathMapper<>(context, tempDir, null); - - Path approvalFilePath = new File("file").toPath(); - Path approvalPath = mapper.getPath(new Object(), approvalFilePath); - String separator = File.separator; - String classPath = getClass().getName().replace('.', File.separatorChar); - - assertThat(approvalPath.toString()) - .isEqualTo(tempDir.toString() + separator + classPath + separator + "test-id" + separator + approvalFilePath.toString()); - } - - @Test - void usesDisplayNameWithIndexWithoutId() throws Exception { - Path tempDir = new File("root").toPath(); - TemplatedTestPathMapper mapper = new TemplatedTestPathMapper<>(context, tempDir, null); - - Path approvalFilePath = new File("file").toPath(); - Path approvalPath = mapper.getPath(new Object(), approvalFilePath); - String separator = File.separator; - String classPath = getClass().getName().replace('.', File.separatorChar); - String basePath = tempDir.toString() + separator + classPath + separator; - String testSpec = context.getRequiredTestMethod().getName() + "[122]"; // index = JUnit execution - 1; - - assertThat(approvalPath.toString()).isEqualTo(basePath + testSpec + separator + approvalFilePath.toString()); - } - - private GoldenMasterRun goldenMasterRun(String testId) { - return new GoldenMasterRun() { - - @Override - public Class annotationType() { - return GoldenMasterRun.class; - } - - @Override - public int repetitions() { - return GoldenMasterRun.DEFAULT_REPETITIONS; - } - - @Override - public String id() { - return testId; - } - }; - } + + private ExtensionContext context; + + @BeforeEach + void setUp() { + context = Mockito.mock(ExtensionContext.class); + Mockito.doReturn(getClass()).when(context).getRequiredTestClass(); + Mockito.doReturn(TemplatedTestPathMapperTest.class.getMethods()[0]).when(context).getRequiredTestMethod(); + Mockito.doReturn("[123]").when(context).getDisplayName(); + } + + @Test + void usesTestIdIfSpecified() throws Exception { + String testId = "test-id"; + Path tempDir = new File("root").toPath(); + TemplatedTestPathMapper mapper = new TemplatedTestPathMapper<>(context, tempDir, null); + + Path approvalFilePath = new File("file").toPath(); + Path approvalPath = mapper.getPath(new Object(), approvalFilePath); + String separator = File.separator; + String classPath = getClass().getName().replace('.', File.separatorChar); + + assertThat(approvalPath.toString()).isEqualTo(tempDir.toString() + separator + classPath + separator + "test-id" + + separator + approvalFilePath.toString()); + } + + @Test + void usesDisplayNameWithIndexWithoutId() throws Exception { + Path tempDir = new File("root").toPath(); + TemplatedTestPathMapper mapper = new TemplatedTestPathMapper<>(context, tempDir, null); + + Path approvalFilePath = new File("file").toPath(); + Path approvalPath = mapper.getPath(new Object(), approvalFilePath); + String separator = File.separator; + String classPath = getClass().getName().replace('.', File.separatorChar); + String basePath = tempDir.toString() + separator + classPath + separator; + String testSpec = context.getRequiredTestMethod().getName() + "[122]"; // index = JUnit execution - 1; + + assertThat(approvalPath.toString()).isEqualTo(basePath + testSpec + separator + approvalFilePath.toString()); + } + + private GoldenMasterRun goldenMasterRun(String testId) { + return new GoldenMasterRun() { + + @Override + public Class annotationType() { + return GoldenMasterRun.class; + } + + @Override + public int repetitions() { + return GoldenMasterRun.DEFAULT_REPETITIONS; + } + + @Override + public String id() { + return testId; + } + }; + } }