Class: Test::Unit::Fixture::HookPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/test/unit/fixture.rb

Instance Method Summary collapse

Constructor Details

#initialize(test_case, type, default_options) ⇒ HookPoint

Returns a new instance of HookPoint.



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/test/unit/fixture.rb', line 102

def initialize(test_case, type, default_options)
  @test_case = test_case
  @type = type
  @default_options = default_options
  @callbacks = {}
  @before_prepend_callbacks = []
  @before_append_callbacks = []
  @after_prepend_callbacks = []
  @after_append_callbacks = []
  @unregistered_callbacks = []
end

Instance Method Details

#after_append_callbacksObject



168
169
170
# File 'lib/test/unit/fixture.rb', line 168

def after_append_callbacks
  @after_append_callbacks - @unregistered_callbacks
end

#after_prepend_callbacksObject



164
165
166
# File 'lib/test/unit/fixture.rb', line 164

def after_prepend_callbacks
  @after_prepend_callbacks - @unregistered_callbacks
end

#before_append_callbacksObject



160
161
162
# File 'lib/test/unit/fixture.rb', line 160

def before_append_callbacks
  @before_append_callbacks - @unregistered_callbacks
end

#before_prepend_callbacksObject



156
157
158
# File 'lib/test/unit/fixture.rb', line 156

def before_prepend_callbacks
  @before_prepend_callbacks - @unregistered_callbacks
end

#register(method_name_or_callback, options = nil) ⇒ Object



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
# File 'lib/test/unit/fixture.rb', line 114

def register(method_name_or_callback, options=nil)
  options ||= {}
  unless valid_register_options?(options)
    message = "must be {:before => :prepend}, " +
      "{:before => :append}, {:after => :prepend} or " +
      "{:after => :append}: #{options.inspect}"
    raise ArgumentError, message
  end

  if options.empty?
    options = @default_options
  end
  before_how = options[:before]
  after_how = options[:after]
  if method_name_or_callback.respond_to?(:call)
    callback = method_name_or_callback
    method_name = callback_method_name(callback)
    @test_case.attribute(:source_location,
                         callback.source_location,
                         method_name)
    # For Ruby 2.6 or earlier. callback may be GC-ed. If
    # callback is GC-ed, callback_method_name may be
    # duplicated because callback_method_name uses callback.object_id.
    # See also: https://github.com/test-unit/test-unit/issues/179
    @callbacks[callback] = true
    @test_case.__send__(:define_method, method_name, &callback)
  else
    method_name = method_name_or_callback
  end
  add_callback(method_name, before_how, after_how)
end

#unregister(method_name_or_callback) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/test/unit/fixture.rb', line 146

def unregister(method_name_or_callback)
  if method_name_or_callback.respond_to?(:call)
    callback = method_name_or_callback
    method_name = callback_method_name(callback)
  else
    method_name = method_name_or_callback
  end
  @unregistered_callbacks << method_name
end