[cairo-commit] rcairo/test-unit/lib/test/unit/util backtracefilter.rb, NONE, 1.1 observable.rb, NONE, 1.1 procwrapper.rb, NONE, 1.1

Kouhei Sutou commit at pdx.freedesktop.org
Wed Aug 13 01:20:59 PDT 2008


Committed by: kou

Update of /cvs/cairo/rcairo/test-unit/lib/test/unit/util
In directory kemper:/tmp/cvs-serv10309/test-unit/lib/test/unit/util

Added Files:
	backtracefilter.rb observable.rb procwrapper.rb 
Log Message:
* test-unit: imported Test::Unit 2.x.


--- NEW FILE: backtracefilter.rb ---
module Test
  module Unit
    module Util
      module BacktraceFilter
        TESTUNIT_FILE_SEPARATORS = %r{[\\/:]}
        TESTUNIT_PREFIX = __FILE__.split(TESTUNIT_FILE_SEPARATORS)[0..-3]
        TESTUNIT_RB_FILE = /\.rb\Z/

        module_function
        def filter_backtrace(backtrace, prefix=nil)
          return ["No backtrace"] unless(backtrace)
          split_p = if(prefix)
            prefix.split(TESTUNIT_FILE_SEPARATORS)
          else
            TESTUNIT_PREFIX
          end
          match = proc do |e|
            split_e = e.split(TESTUNIT_FILE_SEPARATORS)[0, split_p.size]
            next false unless(split_e[0..-2] == split_p[0..-2])
            split_e[-1].sub(TESTUNIT_RB_FILE, '') == split_p[-1]
          end
          return backtrace unless(backtrace.detect(&match))
          found_prefix = false
          new_backtrace = backtrace.reverse.reject do |e|
            if(match[e])
              found_prefix = true
              true
            elsif(found_prefix)
              false
            else
              true
            end
          end.reverse
          new_backtrace = (new_backtrace.empty? ? backtrace : new_backtrace)
          new_backtrace = new_backtrace.reject(&match)
          new_backtrace.empty? ? backtrace : new_backtrace
        end
      end
    end
  end
end

--- NEW FILE: observable.rb ---
#--
#
# Author:: Nathaniel Talbott.
# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
# License:: Ruby license.

require 'test/unit/util/procwrapper'

module Test
  module Unit
    module Util

      # This is a utility class that allows anything mixing
      # it in to notify a set of listeners about interesting
      # events.
      module Observable
        # We use this for defaults since nil might mean something
        NOTHING = "NOTHING/#{__id__}"

        # Adds the passed proc as a listener on the
        # channel indicated by channel_name. listener_key
        # is used to remove the listener later; if none is
        # specified, the proc itself is used.
        #
        # Whatever is used as the listener_key is
        # returned, making it very easy to use the proc
        # itself as the listener_key:
        #
        #  listener = add_listener("Channel") { ... }
        #  remove_listener("Channel", listener)
        def add_listener(channel_name, listener_key=NOTHING, &listener) # :yields: value
          unless(block_given?)
            raise ArgumentError.new("No callback was passed as a listener")
          end
      
          key = listener_key
          if (listener_key == NOTHING)
            listener_key = listener
            key = ProcWrapper.new(listener)
          end
      
          channels[channel_name] ||= {}
          channels[channel_name][key] = listener
          return listener_key
        end

        # Removes the listener indicated by listener_key
        # from the channel indicated by
        # channel_name. Returns the registered proc, or
        # nil if none was found.
        def remove_listener(channel_name, listener_key)
          channel = channels[channel_name]
          return nil unless (channel)
          key = listener_key
          if (listener_key.instance_of?(Proc))
            key = ProcWrapper.new(listener_key)
          end
          if (channel.has_key?(key))
            return channel.delete(key)
          end
          return nil
        end

        # Calls all the procs registered on the channel
        # indicated by channel_name. If value is
        # specified, it is passed in to the procs,
        # otherwise they are called with no arguments.
        #
        #--
        #
        # Perhaps this should be private? Would it ever
        # make sense for an external class to call this
        # method directly?
        def notify_listeners(channel_name, *arguments)
          channel = channels[channel_name]
          return 0 unless (channel)
          listeners = channel.values
          listeners.each { |listener| listener.call(*arguments) }
          return listeners.size
        end

        private
        def channels
          @channels ||= {}
          return @channels
        end
      end
    end
  end
end

--- NEW FILE: procwrapper.rb ---
#--
#
# Author:: Nathaniel Talbott.
# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
# License:: Ruby license.

module Test
  module Unit
    module Util

      # Allows the storage of a Proc passed through '&' in a
      # hash.
      #
      # Note: this may be inefficient, since the hash being
      # used is not necessarily very good. In Observable,
      # efficiency is not too important, since the hash is
      # only accessed when adding and removing listeners,
      # not when notifying.

      class ProcWrapper

        # Creates a new wrapper for a_proc.
        def initialize(a_proc)
          @a_proc = a_proc
          @hash = a_proc.inspect.sub(/^(#<#{a_proc.class}:)/){''}.sub(/(>)$/){''}.hex
        end

        def hash
          return @hash
        end

        def ==(other)
          case(other)
            when ProcWrapper
              return @a_proc == other.to_proc
            else
              return super
          end
        end
        alias :eql? :==

        def to_proc
          return @a_proc
        end
      end
    end
  end
end



More information about the cairo-commit mailing list