Skip to content

Commit 1ff745b

Browse files
authored
[Core] Add ParameterType message source reference (#2739)
1 parent 2f90ebf commit 1ff745b

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- [JUnit Platform Engine] Add constant for fixed.max-pool-size property ([#2713](https://github.com/cucumber/cucumber-jvm/pull/2713) M.P. Korstanje)
1515
- [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)
1616
- [Core] Improved event bus performance using UUID generator selectable through SPI ([#2703](https://github.com/cucumber/cucumber-jvm/pull/2703) Julien Kronegg)
17+
- [Core] Added source reference in parameter type messages ([#2719](https://github.com/cucumber/cucumber-jvm/issues/2719) Julien Kronegg)
1718

1819
### Fixed
1920
- [Pico] Improve performance ([#2724](https://github.com/cucumber/cucumber-jvm/issues/2724) Julien Kronegg)

cucumber-core/src/main/java/io/cucumber/core/runner/CachingGlue.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ void prepareGlue(StepTypeRegistry stepTypeRegistry) throws DuplicateStepDefiniti
235235
parameterTypeDefinitions.forEach(ptd -> {
236236
ParameterType<?> parameterType = ptd.parameterType();
237237
stepTypeRegistry.defineParameterType(parameterType);
238-
emitParameterTypeDefined(parameterType);
238+
emitParameterTypeDefined(ptd);
239239
});
240240
dataTableTypeDefinitions.forEach(dtd -> stepTypeRegistry.defineDataTableType(dtd.dataTableType()));
241241
docStringTypeDefinitions.forEach(dtd -> stepTypeRegistry.defineDocStringType(dtd.docStringType()));
@@ -285,14 +285,17 @@ void prepareGlue(StepTypeRegistry stepTypeRegistry) throws DuplicateStepDefiniti
285285
afterHooks.forEach(this::emitHook);
286286
}
287287

288-
private void emitParameterTypeDefined(ParameterType<?> parameterType) {
288+
private void emitParameterTypeDefined(ParameterTypeDefinition parameterTypeDefinition) {
289+
ParameterType<?> parameterType = parameterTypeDefinition.parameterType();
289290
io.cucumber.messages.types.ParameterType messagesParameterType = new io.cucumber.messages.types.ParameterType(
290291
parameterType.getName(),
291292
parameterType.getRegexps(),
292293
parameterType.preferForRegexpMatch(),
293294
parameterType.useForSnippets(),
294295
bus.generateId().toString(),
295-
null);
296+
parameterTypeDefinition.getSourceReference()
297+
.map(this::createSourceReference)
298+
.orElseGet(this::emptySourceReference));
296299
bus.send(Envelope.of(messagesParameterType));
297300
}
298301

cucumber-core/src/test/java/io/cucumber/core/runner/CachingGlueTest.java

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@
4343
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
4444
import static org.hamcrest.core.Is.is;
4545
import static org.hamcrest.core.IsEqual.equalTo;
46-
import static org.junit.jupiter.api.Assertions.assertAll;
47-
import static org.junit.jupiter.api.Assertions.assertThrows;
48-
import static org.junit.jupiter.api.Assertions.assertTrue;
46+
import static org.junit.jupiter.api.Assertions.*;
4947
import static org.mockito.Mockito.mock;
5048
import static org.mockito.Mockito.when;
5149

@@ -464,7 +462,7 @@ void scenario_scoped_hooks_have_higher_order() {
464462
}
465463

466464
@Test
467-
public void emits_hook_messages_to_bus() {
465+
void emits_hook_messages_to_bus() {
468466

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

483+
@Test
484+
void parameterTypeDefinition_without_source_reference_emits_parameterType_with_empty_source_reference() {
485+
// Given
486+
List<Envelope> events = new ArrayList<>();
487+
EventHandler<Envelope> messageEventHandler = events::add;
488+
489+
EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID);
490+
bus.registerHandlerFor(Envelope.class, messageEventHandler);
491+
CachingGlue glue = new CachingGlue(bus);
492+
493+
glue.addParameterType(new MockedParameterTypeDefinition());
494+
495+
// When
496+
glue.prepareGlue(stepTypeRegistry);
497+
498+
// Then
499+
assertThat(events.size(), is(1));
500+
io.cucumber.messages.types.SourceReference sourceReference = events.get(0).getParameterType().get()
501+
.getSourceReference().get();
502+
assertEquals(new io.cucumber.messages.types.SourceReference(null, null, null, null), sourceReference);
503+
}
504+
505+
@Test
506+
void parameterTypeDefinition_with_source_reference_emits_parameterType_with_non_empty_source_reference() {
507+
// Given
508+
List<Envelope> events = new ArrayList<>();
509+
EventHandler<Envelope> messageEventHandler = events::add;
510+
511+
EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID);
512+
bus.registerHandlerFor(Envelope.class, messageEventHandler);
513+
CachingGlue glue = new CachingGlue(bus);
514+
515+
glue.addParameterType(new MockedParameterTypeDefinitionWithSourceReference());
516+
517+
// When
518+
glue.prepareGlue(stepTypeRegistry);
519+
520+
// Then
521+
assertThat(events.size(), is(1));
522+
io.cucumber.messages.types.SourceReference sourceReference = events.get(0).getParameterType().get()
523+
.getSourceReference().get();
524+
assertNotNull(sourceReference.getJavaStackTraceElement());
525+
}
526+
485527
private static class MockedScenarioScopedStepDefinition extends StubStepDefinition implements ScenarioScoped {
486528

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

565607
}
566608

609+
private static class MockedParameterTypeDefinitionWithSourceReference extends MockedParameterTypeDefinition {
610+
@Override
611+
public Optional<SourceReference> getSourceReference() {
612+
return Optional.of(SourceReference.fromStackTraceElement(new StackTraceElement(
613+
"MockedParameterTypeDefinition",
614+
"getSourceReference",
615+
"CachingGlueTest.java",
616+
593)));
617+
}
618+
}
619+
567620
private static class MockedHookDefinition implements HookDefinition {
568621

569622
private final int order;

0 commit comments

Comments
 (0)