Skip to content

Commit

Permalink
[Core] Include hook type in cucumber message
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje committed Feb 2, 2025
1 parent 5c990e6 commit edd03fe
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.cucumber.core.backend;

import io.cucumber.messages.types.HookType;
import org.apiguardian.api.API;

import java.util.Optional;

@API(status = API.Status.STABLE)
public interface HookDefinition extends Located {

Expand All @@ -11,4 +14,7 @@ public interface HookDefinition extends Located {

int getOrder();

default Optional<HookType> getHookType() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ private void emitHook(CoreHookDefinition coreHook) {
coreHook.getDefinitionLocation()
.map(this::createSourceReference)
.orElseGet(this::emptySourceReference),
coreHook.getTagExpression(), null);
coreHook.getTagExpression(),
coreHook.getHookType().orElse(null));
bus.send(Envelope.of(messagesHook));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.cucumber.core.backend.ScenarioScoped;
import io.cucumber.core.backend.SourceReference;
import io.cucumber.core.backend.TestCaseState;
import io.cucumber.messages.types.HookType;
import io.cucumber.tagexpressions.Expression;
import io.cucumber.tagexpressions.TagExpressionException;
import io.cucumber.tagexpressions.TagExpressionParser;
Expand Down Expand Up @@ -71,6 +72,10 @@ String getTagExpression() {
return delegate.getTagExpression();
}

Optional<HookType> getHookType() {
return delegate.getHookType();
}

static class ScenarioScopedCoreHookDefinition extends CoreHookDefinition implements ScenarioScoped {

private ScenarioScopedCoreHookDefinition(UUID id, HookDefinition delegate) {
Expand Down
15 changes: 11 additions & 4 deletions cucumber-java/src/main/java/io/cucumber/java/GlueAdaptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import static io.cucumber.messages.types.HookType.AFTER_TEST_CASE;
import static io.cucumber.messages.types.HookType.AFTER_TEST_STEP;
import static io.cucumber.messages.types.HookType.BEFORE_TEST_CASE;
import static io.cucumber.messages.types.HookType.BEFORE_TEST_STEP;

final class GlueAdaptor {

private final Lookup lookup;
Expand All @@ -24,25 +29,27 @@ void addDefinition(Method method, Annotation annotation) {
} else if (annotationType.equals(Before.class)) {
Before before = (Before) annotation;
String tagExpression = before.value();
glue.addBeforeHook(new JavaHookDefinition(method, tagExpression, before.order(), lookup));
glue.addBeforeHook(new JavaHookDefinition(BEFORE_TEST_CASE, method, tagExpression, before.order(), lookup));
} else if (annotationType.equals(BeforeAll.class)) {
BeforeAll beforeAll = (BeforeAll) annotation;
glue.addBeforeAllHook(new JavaStaticHookDefinition(method, beforeAll.order(), lookup));
} else if (annotationType.equals(After.class)) {
After after = (After) annotation;
String tagExpression = after.value();
glue.addAfterHook(new JavaHookDefinition(method, tagExpression, after.order(), lookup));
glue.addAfterHook(new JavaHookDefinition(AFTER_TEST_CASE, method, tagExpression, after.order(), lookup));
} else if (annotationType.equals(AfterAll.class)) {
AfterAll afterAll = (AfterAll) annotation;
glue.addAfterAllHook(new JavaStaticHookDefinition(method, afterAll.order(), lookup));
} else if (annotationType.equals(BeforeStep.class)) {
BeforeStep beforeStep = (BeforeStep) annotation;
String tagExpression = beforeStep.value();
glue.addBeforeStepHook(new JavaHookDefinition(method, tagExpression, beforeStep.order(), lookup));
glue.addBeforeStepHook(
new JavaHookDefinition(BEFORE_TEST_STEP, method, tagExpression, beforeStep.order(), lookup));
} else if (annotationType.equals(AfterStep.class)) {
AfterStep afterStep = (AfterStep) annotation;
String tagExpression = afterStep.value();
glue.addAfterStepHook(new JavaHookDefinition(method, tagExpression, afterStep.order(), lookup));
glue.addAfterStepHook(
new JavaHookDefinition(AFTER_TEST_STEP, method, tagExpression, afterStep.order(), lookup));
} else if (annotationType.equals(ParameterType.class)) {
ParameterType parameterType = (ParameterType) annotation;
String pattern = parameterType.value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import io.cucumber.core.backend.HookDefinition;
import io.cucumber.core.backend.Lookup;
import io.cucumber.core.backend.TestCaseState;
import io.cucumber.messages.types.HookType;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Optional;

import static io.cucumber.java.InvalidMethodSignatureException.builder;
import static java.util.Objects.requireNonNull;
Expand All @@ -14,9 +16,11 @@ final class JavaHookDefinition extends AbstractGlueDefinition implements HookDef

private final String tagExpression;
private final int order;
private final HookType hookType;

JavaHookDefinition(Method method, String tagExpression, int order, Lookup lookup) {
JavaHookDefinition(HookType hookType, Method method, String tagExpression, int order, Lookup lookup) {
super(requireValidMethod(method), lookup);
this.hookType = requireNonNull(hookType);
this.tagExpression = requireNonNull(tagExpression, "tag-expression may not be null");
this.order = order;
}
Expand Down Expand Up @@ -74,4 +78,8 @@ public int getOrder() {
return order;
}

@Override
public Optional<HookType> getHookType() {
return Optional.of(hookType);
}
}

0 comments on commit edd03fe

Please sign in to comment.