File: transforms.feature

package info (click to toggle)
cucumber 2.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,076 kB
  • sloc: ruby: 17,016; javascript: 4,641; makefile: 12; sh: 10; tcl: 3
file content (98 lines) | stat: -rw-r--r-- 2,904 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Feature: Transforms

  Transforms allow you to convert primitive string arguments captured in step definitions
  into more meaningful data types.

  Background:
    Let's just create a simple feature for testing out Transforms.
    We also have a Person class that we need to be able to build.

    Given a file named "features/foo.feature" with:
      """
      Feature:
        Scenario:
          Given a Person aged 15 with blonde hair
      """
    And a file named "features/support/person.rb" with:
      """
      class Person
        attr_accessor :age
      
        def to_s
          "I am #{age} years old"
        end
      end
      """

  Scenario: Basic Transform
    This is the most basic way to use a transform. Notice that the regular
    expression is pretty much duplicated between the step defintion and the 
    transform itself.

    And a file named "features/step_definitions/steps.rb" with:
      """
      Transform(/a Person aged (\d+)/) do |age|
        person = Person.new
        person.age = age.to_i
        person
      end
      
      Given /^(a Person aged \d+) with blonde hair$/ do |person|
        expect(person.age).to eq 15
      end
      """
    When I run `cucumber features/foo.feature`
    Then it should pass

  Scenario: Re-use Transform's Regular Expression
    If you keep a reference to the transform, you can use it in your
    regular expressions to avoid repeating the regular expression.

    And a file named "features/step_definitions/steps.rb" with:
      """
      A_PERSON = Transform(/a Person aged (\d+)/) do |age|
        person = Person.new
        person.age = age.to_i
        person
      end

      Given /^(#{A_PERSON}) with blonde hair$/ do |person|
        expect(person.age).to eq 15
      end
      """
    When I run `cucumber features/foo.feature`
    Then it should pass

  Scenario: Use Transform from code
    You can also call the `Transform` method from within a step definition to
    dynamically run a tranform.

    Note that this is rather useless at present, unless the transform itself has a side-effect, since the `Transform`
    function still returns the regular expression pattern.

    Given a file named "features/people.feature" with:
      """
      Feature:
        Scenario:
          Given people with these ages:
            | name  | age |
            | sue   | 25  |
            | sally | 77  |
      """
    And a file named "features/step_definitions/steps.rb" with:
      """
      Transform(/a Person aged (\d+)/) do |age|
        $person = Person.new
        $person.age = age.to_i
        $person
      end

      Given(/^people with these ages:$/) do |table|
        table.rows.each do |name, age|
          Transform("a Person aged #{age}")
          expect($person.age).to eq age.to_i
        end
      end
      """
    When I run `cucumber features/people.feature`
    Then it should pass