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



111
112
113
114
115
116
117
# File 'lib/quicktest.rb', line 111

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



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
176
177
178
179
180
# File 'lib/quicktest.rb', line 119

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 = nil
      end

      if traced == :quicktest
        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
        qt.runner.new(
          if self.class != Module
            self.new
          else
            imi = QuickTest.include_module_into
            o = (imi ? Class.new(imi) : Class.new).new
            o.extend(self)
            o
          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