Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Add ParameterType message source reference #2739

Merged
merged 4 commits into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [JUnit Platform Engine] Add constant for fixed.max-pool-size property ([#2713](https://github.com/cucumber/cucumber-jvm/pull/2713) M.P. Korstanje)
- [Core] Support directories containing exclusively rerun files using the `@path/to/rerun` syntax ([#2710](https://github.com/cucumber/cucumber-jvm/pull/2710) Daniel Whitney, M.P. Korstanje)
- [Core] Improved event bus performance using UUID generator selectable through SPI ([#2703](https://github.com/cucumber/cucumber-jvm/pull/2703) Julien Kronegg)
- [Core] Added source reference in parameter type messages ([#2719](https://github.com/cucumber/cucumber-jvm/issues/2719) Julien Kronegg)

### Fixed
- [Pico] Improve performance ([#2724](https://github.com/cucumber/cucumber-jvm/issues/2724) Julien Kronegg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void prepareGlue(StepTypeRegistry stepTypeRegistry) throws DuplicateStepDefiniti
parameterTypeDefinitions.forEach(ptd -> {
ParameterType<?> parameterType = ptd.parameterType();
stepTypeRegistry.defineParameterType(parameterType);
emitParameterTypeDefined(parameterType);
emitParameterTypeDefined(ptd);
});
dataTableTypeDefinitions.forEach(dtd -> stepTypeRegistry.defineDataTableType(dtd.dataTableType()));
docStringTypeDefinitions.forEach(dtd -> stepTypeRegistry.defineDocStringType(dtd.docStringType()));
Expand Down Expand Up @@ -285,14 +285,17 @@ void prepareGlue(StepTypeRegistry stepTypeRegistry) throws DuplicateStepDefiniti
afterHooks.forEach(this::emitHook);
}

private void emitParameterTypeDefined(ParameterType<?> parameterType) {
private void emitParameterTypeDefined(ParameterTypeDefinition parameterTypeDefinition) {
ParameterType<?> parameterType = parameterTypeDefinition.parameterType();
io.cucumber.messages.types.ParameterType messagesParameterType = new io.cucumber.messages.types.ParameterType(
parameterType.getName(),
parameterType.getRegexps(),
parameterType.preferForRegexpMatch(),
parameterType.useForSnippets(),
bus.generateId().toString(),
null);
parameterTypeDefinition.getSourceReference()
.map(this::createSourceReference)
.orElseGet(this::emptySourceReference));
bus.send(Envelope.of(messagesParameterType));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -464,7 +462,7 @@ void scenario_scoped_hooks_have_higher_order() {
}

@Test
public void emits_hook_messages_to_bus() {
void emits_hook_messages_to_bus() {

List<Envelope> events = new ArrayList<>();
EventHandler<Envelope> messageEventHandler = e -> events.add(e);
Expand All @@ -482,6 +480,50 @@ public void emits_hook_messages_to_bus() {
assertThat(events.size(), is(4));
}

@Test
void parameterTypeDefinition_without_source_reference_emits_parameterType_with_empty_source_reference() {
// Given
List<Envelope> events = new ArrayList<>();
EventHandler<Envelope> messageEventHandler = events::add;

EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID);
bus.registerHandlerFor(Envelope.class, messageEventHandler);
CachingGlue glue = new CachingGlue(bus);

glue.addParameterType(new MockedParameterTypeDefinition());

// When
glue.prepareGlue(stepTypeRegistry);

// Then
assertThat(events.size(), is(1));
io.cucumber.messages.types.SourceReference sourceReference = events.get(0).getParameterType().get()
.getSourceReference().get();
assertEquals(new io.cucumber.messages.types.SourceReference(null, null, null, null), sourceReference);
}

@Test
void parameterTypeDefinition_with_source_reference_emits_parameterType_with_non_empty_source_reference() {
// Given
List<Envelope> events = new ArrayList<>();
EventHandler<Envelope> messageEventHandler = events::add;

EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID);
bus.registerHandlerFor(Envelope.class, messageEventHandler);
CachingGlue glue = new CachingGlue(bus);

glue.addParameterType(new MockedParameterTypeDefinitionWithSourceReference());

// When
glue.prepareGlue(stepTypeRegistry);

// Then
assertThat(events.size(), is(1));
io.cucumber.messages.types.SourceReference sourceReference = events.get(0).getParameterType().get()
.getSourceReference().get();
assertNotNull(sourceReference.getJavaStackTraceElement());
}

private static class MockedScenarioScopedStepDefinition extends StubStepDefinition implements ScenarioScoped {

MockedScenarioScopedStepDefinition(String pattern, Type... types) {
Expand Down Expand Up @@ -564,6 +606,17 @@ public boolean isDisposed() {

}

private static class MockedParameterTypeDefinitionWithSourceReference extends MockedParameterTypeDefinition {
@Override
public Optional<SourceReference> getSourceReference() {
return Optional.of(SourceReference.fromStackTraceElement(new StackTraceElement(
"MockedParameterTypeDefinition",
"getSourceReference",
"CachingGlueTest.java",
593)));
}
}

private static class MockedHookDefinition implements HookDefinition {

private final int order;
Expand Down