module Test::Unit::Fixture::ClassMethods

Public Instance Methods

after_cleanup_callbacks() click to toggle source
# File lib/test/unit/fixture.rb, line 85
def after_cleanup_callbacks
  collect_fixture_callbacks(:cleanup, :after)
end
after_setup_callbacks() click to toggle source
# File lib/test/unit/fixture.rb, line 77
def after_setup_callbacks
  collect_fixture_callbacks(:setup, :after)
end
after_teardown_callbacks() click to toggle source
# File lib/test/unit/fixture.rb, line 93
def after_teardown_callbacks
  collect_fixture_callbacks(:teardown, :after)
end
before_cleanup_callbacks() click to toggle source
# File lib/test/unit/fixture.rb, line 81
def before_cleanup_callbacks
  collect_fixture_callbacks(:cleanup, :before)
end
before_setup_callbacks() click to toggle source
# File lib/test/unit/fixture.rb, line 73
def before_setup_callbacks
  collect_fixture_callbacks(:setup, :before)
end
before_teardown_callbacks() click to toggle source
# File lib/test/unit/fixture.rb, line 89
def before_teardown_callbacks
  collect_fixture_callbacks(:teardown, :before)
end
cleanup(*method_names, &callback) click to toggle source
# File lib/test/unit/fixture.rb, line 30
def cleanup(*method_names, &callback)
  register_fixture(:cleanup, *method_names, &callback)
end
register_cleanup_callback(method_name_or_callback, options) click to toggle source
# File lib/test/unit/fixture.rb, line 55
def register_cleanup_callback(method_name_or_callback, options)
  register_fixture_callback(:cleanup, method_name_or_callback,
                            options, :before, :prepend)
end
register_setup_callback(method_name_or_callback, options) click to toggle source
# File lib/test/unit/fixture.rb, line 46
def register_setup_callback(method_name_or_callback, options)
  register_fixture_callback(:setup, method_name_or_callback,
                            options, :after, :append)
end
register_teardown_callback(method_name_or_callback, options) click to toggle source
# File lib/test/unit/fixture.rb, line 64
def register_teardown_callback(method_name_or_callback, options)
  register_fixture_callback(:teardown, method_name_or_callback,
                            options, :before, :prepend)
end
setup(*method_names, &callback) click to toggle source
# File lib/test/unit/fixture.rb, line 22
def setup(*method_names, &callback)
  register_fixture(:setup, *method_names, &callback)
end
teardown(*method_names, &callback) click to toggle source
# File lib/test/unit/fixture.rb, line 38
def teardown(*method_names, &callback)
  register_fixture(:teardown, *method_names, &callback)
end
unregister_cleanup(*method_names_or_callbacks) click to toggle source
# File lib/test/unit/fixture.rb, line 34
def unregister_cleanup(*method_names_or_callbacks)
  unregister_fixture(:cleanup, *method_names_or_callbacks)
end
unregister_cleanup_callback(method_name_or_callback) click to toggle source
# File lib/test/unit/fixture.rb, line 60
def unregister_cleanup_callback(method_name_or_callback)
  unregister_fixture_callback(:cleanup, method_name_or_callback)
end
unregister_setup(*method_names_or_callbacks) click to toggle source
# File lib/test/unit/fixture.rb, line 26
def unregister_setup(*method_names_or_callbacks)
  unregister_fixture(:setup, *method_names_or_callbacks)
end
unregister_setup_callback(method_name_or_callback) click to toggle source
# File lib/test/unit/fixture.rb, line 51
def unregister_setup_callback(method_name_or_callback)
  unregister_fixture_callback(:setup, method_name_or_callback)
end
unregister_teardown(*method_names_or_callbacks) click to toggle source
# File lib/test/unit/fixture.rb, line 42
def unregister_teardown(*method_names_or_callbacks)
  unregister_fixture(:teardown, *method_names_or_callbacks)
end
unregister_teardown_callback(method_name_or_callback) click to toggle source
# File lib/test/unit/fixture.rb, line 69
def unregister_teardown_callback(method_name_or_callback)
  unregister_fixture_callback(:teardown, method_name_or_callback)
end

Private Instance Methods

add_fixture_callback(how, variable_name, method_name_or_callback) click to toggle source
# File lib/test/unit/fixture.rb, line 119
def add_fixture_callback(how, variable_name, method_name_or_callback)
  callbacks = instance_eval("#{variable_name} ||= []")

  if how == :prepend
    callbacks = [method_name_or_callback] | callbacks
  else
    callbacks = callbacks | [method_name_or_callback]
  end
  instance_variable_set(variable_name, callbacks)
end
collect_fixture_callbacks(fixture, order) click to toggle source
# File lib/test/unit/fixture.rb, line 161
def collect_fixture_callbacks(fixture, order)
  callbacks_variable = registered_callbacks_variable_name(fixture, order)
  unregistered_callbacks_variable =
    unregistered_callbacks_variable_name(fixture)

  base_index = ancestors.index(Fixture)
  interested_ancestors = ancestors[0, base_index].reverse
  interested_ancestors.inject([]) do |result, ancestor|
    if ancestor.is_a?(Class)
      ancestor.class_eval do
        callbacks = instance_eval("#{callbacks_variable} ||= []")
        unregistered_callbacks =
          instance_eval("#{unregistered_callbacks_variable} ||= []")
        (result | callbacks) - unregistered_callbacks
      end
    else
      result
    end
  end
end
register_fixture(fixture, *method_names, &callback) click to toggle source
# File lib/test/unit/fixture.rb, line 98
def register_fixture(fixture, *method_names, &callback)
  options = {}
  options = method_names.pop if method_names.last.is_a?(Hash)
  callbacks = method_names
  callbacks << callback if callback
  attribute(fixture, options, *callbacks)
end
register_fixture_callback(fixture, method_name_or_callback, options, default_order, default_how) click to toggle source
# File lib/test/unit/fixture.rb, line 138
def register_fixture_callback(fixture, method_name_or_callback, options,
                              default_order, default_how)
  unless valid_register_fixture_options?(options)
    message = "must be {:before => :prepend}, " +
      "{:before => :append}, {:after => :prepend} or " +
      "{:after => :append}: #{options.inspect}"
    raise ArgumentError, message
  end

  if options.empty?
    order, how = default_order, default_how
  else
    order, how = options.to_a.first
  end
  variable_name = registered_callbacks_variable_name(fixture, order)
  add_fixture_callback(how, variable_name, method_name_or_callback)
end
registered_callbacks_variable_name(fixture, order) click to toggle source
# File lib/test/unit/fixture.rb, line 130
def registered_callbacks_variable_name(fixture, order)
  "@#{order}_#{fixture}_callbacks"
end
unregister_fixture(fixture, *method_names_or_callbacks) click to toggle source
# File lib/test/unit/fixture.rb, line 106
def unregister_fixture(fixture, *method_names_or_callbacks)
  attribute(fixture, nil, *method_names_or_callbacks)
end
unregister_fixture_callback(fixture, method_name_or_callback) click to toggle source
# File lib/test/unit/fixture.rb, line 156
def unregister_fixture_callback(fixture, method_name_or_callback)
  variable_name = unregistered_callbacks_variable_name(fixture)
  add_fixture_callback(:append, variable_name, method_name_or_callback)
end
unregistered_callbacks_variable_name(fixture) click to toggle source
# File lib/test/unit/fixture.rb, line 134
def unregistered_callbacks_variable_name(fixture)
  "@unregistered_#{fixture}_callbacks"
end
valid_register_fixture_options?(options) click to toggle source
# File lib/test/unit/fixture.rb, line 110
def valid_register_fixture_options?(options)
  return true if options.empty?
  return false if options.size > 1

  key = options.keys.first
  [:before, :after].include?(key) and
    [:prepend, :append].include?(options[key])
end