Module: QuickTest::Tracer

Included in:
Module
Defined in:
lib/quicktest.rb

Overview

if the class under test (or one of its ancestors) is overriding method tracing then it must include QuickTest::Tracer

Class Method Summary collapse

Class Method Details

.included(into) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/quicktest.rb', line 125

def self.included(into)
  if into == Class or into == Module # default include in in Module
    self.tracer_include into
  else
    self.tracer_include(class<<into;self;end)
  end
end

.tracer_include(meta) ⇒ Object



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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/quicktest.rb', line 133

def self.tracer_include(meta)
  meta.module_eval do

    # monkeypatch the two hook methods called on method creation
    # so they will still be called for any method other than _quicktest_
    alias_method :__quicktest_singleton_method_added__, :singleton_method_added
    alias_method :__quicktest_method_added__, :method_added

    # ruby tracing hook
    def singleton_method_added traced
      # avoid infinite recursion if module is included into a class by a user
      return if traced == QuickTest.runner.methods.last

      if QuickTest.last_self != self
        QuickTest.last_self = self
        QuickTest.include_module_into = Object
      end

      if traced == QuickTest.test_method
        QuickTest.runner.new self

      elsif traced == :quicktest_include_into
        QuickTest.include_module_into = self.quicktest_include_into

      else
        QuickTest.runner.add_singleton_method traced
        __quicktest_singleton_method_added__ traced
      end
    end

    QuickTest.ignore_first_method_added = true

    # ruby tracing hook
    def method_added traced
      qt = QuickTest
      # avoid infinite recursion if module is included into a class by a user
      return if traced == qt.runner.methods.last

      if traced == QuickTest.test_method
        begin
          qt.runner.new( *
            if self.class != Module
              [self.new]
            else
              [Class.new(QuickTest.include_module_into).extend(self), self]
            end
          )
        rescue ArgumentError
          puts <<-EOS
method:
  #{qt.runner.methods.last}
object initialization requires arguments, use:
  "def self.quicktest"
instead of:
  "def quicktest"
EOS
          exit(1)
        end

      else
        unless qt.ignore_first_method_added or
          qt.runner_module::QuickTestIgnoreClasses.detect {|m| self.to_s =~ m}
            qt.runner.add_method traced
        end

        qt.ignore_first_method_added = false
        __quicktest_method_added__ traced
      end
    end
  end
end