These are chat archives for Behat/Behat

23rd
Nov 2016
kimberv
@kimberv_twitter
Nov 23 2016 15:33 UTC

Hello there! I was hoping someone could give me tips on how the behat parser is breaking out arguments from steps. I came across something unexpected the other day, and while I could resort to a regex to deal with this, I wondered if this was expected behavior or a bug? It seems like the parser isn't treating double quotes as a container consistently.

scenario and test run results:
https://gist.github.com/kimberv/d25a140765b2f01a880a14fb9cbb0330

step definitions:
https://gist.github.com/kimberv/570230f611fae7362ae699a7c9967090

(i'm using behat version 3.0.15)
kimberv
@kimberv_twitter
Nov 23 2016 15:40 UTC
mmm. that version has fallen behind behat's latest. i just retried with behat 3.2.2 and it behaves the same
Gildas Quéméner
@gquemener
Nov 23 2016 15:59 UTC
Hello, AFAIK you can use a single step like so:
    /** 
     * @Given The child received a :gift
     * @Given The child received a :gift :at
     */
    public function theChildReceivedAGift($gift, \DateTime $at = null)
    {
    }
and avoid the ambiguous match error you get
Would that be acceptable @kimberv_twitter ?
kimberv
@kimberv_twitter
Nov 23 2016 16:21 UTC
sadly no. the parser still isn't sure which step to use, even when you combine them for one function. so it will still throw an ambiguous match error (using something similar to the above for step definition):
    And The child received a "doll house"
      Ambiguous match of "The child received a "doll house"":
      to `The child received a :arg1` from FeatureContext::theChildReceivedAGift()
      to `The child received a :arg1 :arg2` from FeatureContext::theChildReceivedAGift()
Gildas Quéméner
@gquemener
Nov 23 2016 17:30 UTC
ah yes indeed
What about
```
    /** 
     * @Given The child received a :gift :at
     */
    public function theChildReceivedAGift($gift, \DateTime $at = null)
    {
    }
then?
it should match both with and without "at"
kimberv
@kimberv_twitter
Nov 23 2016 17:39 UTC
the function is aware that the final param is optional, but the step definition isn't. so using that will cause it to throw exceptions that it's missing a step definition for The child received a "ball"
Gildas Quéméner
@gquemener
Nov 23 2016 17:43 UTC

Given the following feature:

Feature:

  Scenario:
    Given The child received a "doll"
    And The child received a "cat" "yesterday"

and the following step:


    /**
     * @Given The child received a :arg1
     * @Given The child received a :arg1 :arg2
     */
    public function theChildReceivedA($arg1, $arg2 = null)
    {
        var_dump(func_get_args());
    }

When I run bin/behat
Then I see:

"""
Feature:

  Scenario:                                    # features/billing/test.feature:3
    Given The child received a "doll"          # FeatureContext::theChildReceivedA()
      │ /data/www/features/bootstrap/FeatureContext.php:374:
      │ array(2) {
      │   [0] =>
      │   string(4) "doll"
      │   [1] =>
      │   NULL
      │ }
      │
    And The child received a "cat" "yesterday" # FeatureContext::theChildReceivedA()
      │ /data/www/features/bootstrap/FeatureContext.php:374:
      │ array(2) {
      │   [0] =>
      │   string(3) "cat"
      │   [1] =>
      │   string(9) "yesterday"
      │ }
      │

1 scenario (1 passed)
2 steps (2 passed)
0m2.10s (22.86Mb)
"""
This is what you're trying to achieve, don't you?
Simply perform a null check on the $arg2 and you'll know if it's been provided or not @kimberv_twitter
I'm using Behat v3.2.2 FYI
Gildas Quéméner
@gquemener
Nov 23 2016 17:48 UTC

BTW you don't have to wrap the values with quotes when it is a single word:

Feature:

  Scenario:
    Given The child received a doll
    And The child received a cat yesterday

will also match the step definitions

kimberv
@kimberv_twitter
Nov 23 2016 18:14 UTC

Thanks, Gildas. I know the dbl quotes arenot necessary for single word. I just wanted to be consistent. And yes, your above example will work - for just those steps you gave. but it will not work for all the scenario steps I gave (which involve spaces within the dbl quoted tokens).

using:
https://gist.github.com/kimberv/d1f3f55e3b92d4c27a7cf0a5126a0f4e

test run results for the full scenario:
https://gist.github.com/kimberv/f401f2591e0ea8e7f36b10020405ccf0