Class: TurnipExt

Inherits:
Object
  • Object
show all
Defined in:
lib/acceptance_test/turnip_ext.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_dynamic_steps(page_set, context) ⇒ Object



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
99
100
101
# File 'lib/acceptance_test/turnip_ext.rb', line 47

def self.build_dynamic_steps page_set, context
  # if context
  #   context.class.send(:define_method, :method_missing) do |meth, *args, &block|
  #     page_set.send meth, *args, &block
  #   end
  # end

  ##self.class.step :enter_word, "I enter word :word"

  # turnip_rspec_execute = Turnip::RSpec::Execute
  #
  # turnip_rspec_execute.class_eval do
  #   alias_method :old_run_step, :run_step
  #
  #   def run_step(feature_file, step)
  #     begin
  #       instance_eval <<-EOS, feature_file, step.line
  #         step(step)
  #       EOS
  #     rescue Turnip::Pending => e
  #
  #       # instance_eval <<-EOS, feature_file, step.line
  #       #   step(:visit_home_page, "I am on wikipedia.com")
  #       # EOS
  #
  #       # page_set.pages.each do |page|
  #       #   page_set.page_methods(page).each do |method|
  #       #     context.class.step "I #{method.to_s.gsub('_', ' ')}" do
  #       #       send method
  #       #     end
  #       #
  #       #     # context.class.step method, "I #{method.to_s.gsub('_', ' ')}"
  #       #     #
  #       #     # context.class.step method, "#{method.to_s.gsub('_', ' ')}"
  #       #   end
  #       # end
  #
  #       old_run_step feature_file, step
  #     end
  #   end
  # end


  # page_set.pages.each do |page|
  #   page_set.page_methods(page).each do |method|
  #     page_set.class.step "I #{method.to_s.gsub('_', ' ')}" do
  #       send method
  #     end
  #
  #     page_set.class.step "#{method.to_s.gsub('_', ' ')}" do
  #       send method
  #     end
  #   end
  # end
end

.shared_context_with_turnip(context_name) ⇒ Object



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
# File 'lib/acceptance_test/turnip_ext.rb', line 4

def self.shared_context_with_turnip context_name
  turnip_rspec = Turnip::RSpec

  turnip_rspec.class_eval do
    @context_name = context_name # class instance variable

    def self.context_name # access to class instance variable
      @context_name
    end

    class << self
      def run(feature_file)
        Turnip::Builder.build(feature_file).features.each do |feature|
          ::RSpec.describe feature.name, feature. do

            include_context Turnip::RSpec.context_name

            before do
              example = Turnip::RSpec.fetch_current_example(self)
              # This is kind of a hack, but it will make RSpec throw way nicer exceptions
              example.[:file_path] ||= feature_file

              feature.backgrounds.map(&:steps).flatten.each do |step|
                run_step(feature_file, step)
              end
            end
            feature.scenarios.each do |scenario|
              instance_eval <<-EOS, feature_file, scenario.line
            describe scenario.name, scenario.metadata_hash do it(scenario.steps.map(&:to_s).join(' -> ')) do
                scenario.steps.each do |step|
                  run_step(feature_file, step)
                end
              end
            end
              EOS
            end
          end
        end
      end
    end
  end
end

Instance Method Details

#extend_turnipObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/acceptance_test/turnip_ext.rb', line 103

def extend_turnip
  turnip_define = Turnip::Define

  turnip_define.class_eval do
    def before &block
      send(:define_method, "before",  &block) if block
    end

    def after &block
      send(:define_method, "after",  &block) if block
    end
  end

  turnip_rspec_execute = Turnip::RSpec::Execute

  turnip_rspec_execute.class_eval do
    def run_before rspec_root
      self.class.send(:define_method, :rspec_root, lambda { rspec_root })

      before
    end

    def run_after rspec_root
      self.class.send(:define_method, :rspec_root, lambda { rspec_root })

      after
    end
  end

  turnip_rspec = Turnip::RSpec

  turnip_rspec.class_eval do
    @shared_context_name = shared_context_name # class instance variable

    def self.shared_context_name # access to class instance variable
      @shared_context_name
    end

    class << self
      def run(feature_file)
        Turnip::Builder.build(feature_file).features.each do |feature|
          ::RSpec.describe feature.name, feature. do
            rspec_root = self

            before do
              run_before rspec_root
              example = Turnip::RSpec.fetch_current_example(self)
              # This is kind of a hack, but it will make RSpec throw way nicer exceptions
              example.[:file_path] ||= feature_file

              feature.backgrounds.map(&:steps).flatten.each do |step|
                run_step(feature_file, step)
              end
            end
            feature.scenarios.each do |scenario|
              instance_eval <<-EOS, feature_file, scenario.line
            describe scenario.name, scenario.metadata_hash do it(scenario.steps.map(&:to_s).join(' -> ')) do
                scenario.steps.each do |step|
                  run_step(feature_file, step)
                end
              end
            end
              EOS
            end
            after do
              run_after rspec_root
            end
          end
        end
      end
    end
  end
end