Skip to content

Commit

Permalink
Fixed issue ID and disabled test
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbechtold committed Jan 20, 2018
1 parent d1ff989 commit f9ce4c7
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +50,8 @@ public Stream<TestTemplateInvocationContext> 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) {
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
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;

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<Object> 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<Object> 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<? extends Annotation> 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<Object> 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<Object> 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<? extends Annotation> annotationType() {
return GoldenMasterRun.class;
}

@Override
public int repetitions() {
return GoldenMasterRun.DEFAULT_REPETITIONS;
}

@Override
public String id() {
return testId;
}
};
}
}

0 comments on commit f9ce4c7

Please sign in to comment.