File: cucumber.rb

package info (click to toggle)
ruby-vcr 6.0.0%2Breally5.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,320 kB
  • sloc: ruby: 8,456; sh: 177; makefile: 7
file content (98 lines) | stat: -rw-r--r-- 2,825 bytes parent folder | download | duplicates (3)
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
module VCR
  # Provides integration with Cucumber using tags.
  class CucumberTags
    class << self
      # @private
      def tags
        @tags.dup
      end

      # @private
      def add_tag(tag)
        @tags << tag
      end
    end

    @tags = []

    # @private
    def initialize(main_object)
      @main_object = main_object
    end

    # Adds `Before` and `After` cucumber hooks for the named tags that
    # will cause a VCR cassette to be used for scenarios with matching tags.
    #
    # @param [Array<String>] tag_names the cucumber scenario tags
    # @param [(optional) Hash] options the cassette options. Specify
    #   `:use_scenario_name => true` to automatically name the
    #   cassette according to the scenario name.
    def tags(*tag_names)
      original_options = tag_names.last.is_a?(::Hash) ? tag_names.pop : {}
      tag_names.each do |tag_name|
        tag_name = "@#{tag_name}" unless tag_name =~ /\A@/

        # It would be nice to use an Around hook here, but
        # cucumber has a bug: background steps do not run
        # within an around hook.
        # https://gist.github.com/652968
        @main_object.Before(tag_name) do |scenario|
          options = original_options.dup

          cassette_name = if options.delete(:use_scenario_name)
            if scenario.respond_to?(:outline?) && scenario.outline?
              ScenarioNameBuilder.new(scenario).cassette_name
            elsif scenario.respond_to?(:scenario_outline)
              [ scenario.scenario_outline.feature.name.split("\n").first,
                scenario.scenario_outline.name,
                scenario.name.split("\n").first
              ].join("/")
            else
              [ scenario.feature.name.split("\n").first,
                scenario.name.split("\n").first
              ].join("/")
            end
          else
            "cucumber_tags/#{tag_name.gsub(/\A@/, '')}"
          end

          VCR.insert_cassette(cassette_name, options)
        end

        @main_object.After(tag_name) do |scenario|
          VCR.eject_cassette(:skip_no_unused_interactions_assertion => scenario.failed?)
        end

        self.class.add_tag(tag_name)
      end
    end
    alias :tag :tags

    # Constructs a cassette name from a Cucumber 2 scenario outline
    # @private
    class ScenarioNameBuilder
      def initialize(test_case)
        @parts = []
        test_case.describe_source_to self
      end

      def cassette_name
        @parts.join("/")
      end

      def feature(feature)
        @parts.unshift feature.name
        self
      end
      alias scenario_outline feature

      def scenario(*) self end
      alias examples_table scenario

      def examples_table_row(row)
        @parts.unshift "| %s |" % row.values.join(" | ")
        self
      end
    end
  end
end