class Test::Advice

The Advice class is an observer that can be customized to initiate before, after and upon procedures for all of RubyTests observable points.

Only one procedure is allowed per-point.

Public Class Methods

joinpoint(name) click to toggle source
# File lib/rubytest/advice.rb, line 21
def self.joinpoint(name)
  joinpoints << name.to_sym

  class_eval %{
    def #{name}(*args)
      procedure = @table[:#{name}]
      procedure.call(*args) if procedure
    end
  }
end
joinpoints() click to toggle source
# File lib/rubytest/advice.rb, line 12
def self.joinpoints
  @joinpoints ||= []
end
new() click to toggle source
# File lib/rubytest/advice.rb, line 33
def initialize
  @table = {}
end

Public Instance Methods

join(type, &block) click to toggle source

Add a procedure to one of the join-points.

# File lib/rubytest/advice.rb, line 76
def join(type, &block)
  type = valid_type(type) 
  @table[type] = block
end
join_after(type, &block) click to toggle source

Add a procedure to one of the after join-points.

# File lib/rubytest/advice.rb, line 87
def join_after(type, &block)
  join("end_#{type}", &block)
end
join_before(type, &block) click to toggle source

Add a procedure to one of the before join-points.

# File lib/rubytest/advice.rb, line 82
def join_before(type, &block)
  join("begin_#{type}", &block) 
end
method_missing(*) click to toggle source

Ignore any other signals (precautionary).

# File lib/rubytest/advice.rb, line 92
def method_missing(*)
end

Private Instance Methods

message_type(type) click to toggle source
# File lib/rubytest/advice.rb, line 113
def message_type(type)
  type = type.to_sym
  case type
  when :each
    type = :test
  when :all
    type = :case
  when :begin_each
    type = :begin_test
  when :begin_all
    type = :begin_case
  when :end_each
    type = :end_test
  when :end_all
    type = :end_case
  end
  return type
end
valid_type(type) click to toggle source
# File lib/rubytest/advice.rb, line 104
def valid_type(type)
  type = message_type(type)
  unless self.class.joinpoints.include?(type)
    raise ArgumentError, "not a valid advice type -- #{type}"
  end
  type
end