From e4c12f3230db2d29be2eebdc0bee1b2f16ca18b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Tue, 14 Jul 2015 14:32:49 +0200 Subject: [PATCH] DocString arguments can be converted to scalar types just like capture group arguments --- History.md | 1 + .../cucumber/runtime/StepDefinitionMatch.java | 4 +++- .../runtime/StepDefinitionMatchTest.java | 17 +++++++++++++++++ .../runtime/groovy/date_stepdefs.groovy | 7 ++++--- .../cucumber/runtime/groovy/a_feature.feature | 5 ++++- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/History.md b/History.md index d3079e6812..60d495dabe 100644 --- a/History.md +++ b/History.md @@ -1,5 +1,6 @@ ## [1.2.4-SNAPSHOT](https://github.com/cucumber/cucumber-jvm/compare/v1.2.3...master) (In Git) +* [Core] DocString arguments can be converted to scalar types just like capture group arguments (Aslak Hellesøy) * [Guice] The `cucumber-guice.properties` file is no longer used. Use `cucumber.properties` instead. * [Guice] The `guice.injector-source` property can be overridden as a System property or environment variable ([#881](https://github.com/cucumber/cucumber-jvm/issues/881) Aslak Hellesøy) * [Java] `ObjectFactory.addClass` returns a boolean indicating whether or not stepdefs/hooks for that class should be registered. (Aslak Hellesøy) diff --git a/core/src/main/java/cucumber/runtime/StepDefinitionMatch.java b/core/src/main/java/cucumber/runtime/StepDefinitionMatch.java index 77605189f9..ff8fe392f2 100644 --- a/core/src/main/java/cucumber/runtime/StepDefinitionMatch.java +++ b/core/src/main/java/cucumber/runtime/StepDefinitionMatch.java @@ -73,7 +73,9 @@ private Object[] transformedArgs(Step step, LocalizedXStreams.LocalizedXStream x if (step.getRows() != null) { result.add(tableArgument(step, n, xStream)); } else if (step.getDocString() != null) { - result.add(step.getDocString().getValue()); + ParameterInfo parameterInfo = getParameterType(n, String.class); + Object arg = parameterInfo.convert(step.getDocString().getValue(), xStream); + result.add(arg); } return result.toArray(new Object[result.size()]); } diff --git a/core/src/test/java/cucumber/runtime/StepDefinitionMatchTest.java b/core/src/test/java/cucumber/runtime/StepDefinitionMatchTest.java index 37c7669e51..b3cd4968e6 100644 --- a/core/src/test/java/cucumber/runtime/StepDefinitionMatchTest.java +++ b/core/src/test/java/cucumber/runtime/StepDefinitionMatchTest.java @@ -57,6 +57,23 @@ public void converts_with_explicit_converter() throws Throwable { verify(stepDefinition).execute(ENGLISH, new Object[]{new Thing("the thing")}); } + @Test + public void converts_doc_string_with_explicit_converter() throws Throwable { + StepDefinition stepDefinition = mock(StepDefinition.class); + when(stepDefinition.getParameterCount()).thenReturn(1); + when(stepDefinition.getParameterType(0, String.class)).thenReturn(new ParameterInfo(Thing.class, null, null, + null)); + + Step stepWithDocString = mock(Step.class); + DocString docString = new DocString("test", "the thing", 999); + when(stepWithDocString.getDocString()).thenReturn(docString); + when(stepWithDocString.getRows()).thenReturn(null); + + StepDefinitionMatch stepDefinitionMatch = new StepDefinitionMatch(new ArrayList(), stepDefinition, "some.feature", stepWithDocString, new LocalizedXStreams(classLoader)); + stepDefinitionMatch.runStep(ENGLISH); + verify(stepDefinition).execute(ENGLISH, new Object[]{new Thing("the thing")}); + } + @XStreamConverter(ThingConverter.class) public static class Thing { public final String name; diff --git a/groovy/src/test/groovy/cucumber/runtime/groovy/date_stepdefs.groovy b/groovy/src/test/groovy/cucumber/runtime/groovy/date_stepdefs.groovy index 77e785ffbd..5eafd0cbd4 100644 --- a/groovy/src/test/groovy/cucumber/runtime/groovy/date_stepdefs.groovy +++ b/groovy/src/test/groovy/cucumber/runtime/groovy/date_stepdefs.groovy @@ -16,11 +16,12 @@ class DateWrapper { class DateWrapperConverter extends Transformer { def DateWrapper transform(String string) { - def df = new SimpleDateFormat("dd-MM-yyyy") + def df = new SimpleDateFormat("yyyy-MM-dd") return new DateWrapper(date:df.parse(string)); } } -Given(~'^today\'s date is "(.*)"') { DateWrapper dw -> - assertEquals(71, dw.date.year) +Given(~'^today\'s date is "(.*)" and tomorrow is:') { DateWrapper today, DateWrapper tomorrow -> + assertEquals(3, today.date.date) + assertEquals(4, tomorrow.date.date) } diff --git a/groovy/src/test/resources/cucumber/runtime/groovy/a_feature.feature b/groovy/src/test/resources/cucumber/runtime/groovy/a_feature.feature index bc86c1e9ab..9a15004237 100644 --- a/groovy/src/test/resources/cucumber/runtime/groovy/a_feature.feature +++ b/groovy/src/test/resources/cucumber/runtime/groovy/a_feature.feature @@ -24,7 +24,10 @@ Feature: Cucumber Runner Rocks | 2012 | Cucumber-JVM | Scenario: A date - Given today's date is "10-03-1971" + Given today's date is "1971-10-03" and tomorrow is: + """ + 1971-10-04 + """ Scenario: Call a method or property from second world