class Test::Unit::Fixture::HookPoint - test-unit: Ruby Standard Library Documentation

# class Test::Unit::Fixture::HookPoint

### Public Class Methods

new(test_case, type, default_options) click to toggle source

    # File test-unit-3.6.7/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

### Public Instance Methods

after_append_callbacks() click to toggle source

    # File test-unit-3.6.7/lib/test/unit/fixture.rb, line 168
    def after_append_callbacks
      @after_append_callbacks - @unregistered_callbacks
    end

after_prepend_callbacks() click to toggle source

    # File test-unit-3.6.7/lib/test/unit/fixture.rb, line 164
    def after_prepend_callbacks
      @after_prepend_callbacks - @unregistered_callbacks
    end

before_append_callbacks() click to toggle source

    # File test-unit-3.6.7/lib/test/unit/fixture.rb, line 160
    def before_append_callbacks
      @before_append_callbacks - @unregistered_callbacks
    end

before_prepend_callbacks() click to toggle source

    # File test-unit-3.6.7/lib/test/unit/fixture.rb, line 156
    def before_prepend_callbacks
      @before_prepend_callbacks - @unregistered_callbacks
    end

register(method_name_or_callback, options=nil) click to toggle source

    # File test-unit-3.6.7/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) click to toggle source

    # File test-unit-3.6.7/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

### Private Instance Methods

add_callback(method_name_or_callback, before_how, after_how) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/fixture.rb, line 186
    def add_callback(method_name_or_callback, before_how, after_how)
      case before_how
      when :prepend
        @before_prepend_callbacks =
          [method_name_or_callback] | @before_prepend_callbacks
      when :append
        @before_append_callbacks |= [method_name_or_callback]
      else
        case after_how
        when :prepend
          @after_prepend_callbacks =
            [method_name_or_callback] | @after_prepend_callbacks
        when :append
          @after_append_callbacks |= [method_name_or_callback]
        end
      end
    end

callback_method_name(callback) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/fixture.rb, line 182
    def callback_method_name(callback)
      "#{@type}_#{callback.object_id}"
    end

valid_register_options?(options) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/fixture.rb, line 173
    def valid_register_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