summaryrefslogtreecommitdiff
path: root/ruby_1_8_5/lib/test/unit/util
diff options
context:
space:
mode:
Diffstat (limited to 'ruby_1_8_5/lib/test/unit/util')
-rw-r--r--ruby_1_8_5/lib/test/unit/util/backtracefilter.rb40
-rw-r--r--ruby_1_8_5/lib/test/unit/util/observable.rb90
-rw-r--r--ruby_1_8_5/lib/test/unit/util/procwrapper.rb48
3 files changed, 178 insertions, 0 deletions
diff --git a/ruby_1_8_5/lib/test/unit/util/backtracefilter.rb b/ruby_1_8_5/lib/test/unit/util/backtracefilter.rb
new file mode 100644
index 0000000000..7ebec2dfef
--- /dev/null
+++ b/ruby_1_8_5/lib/test/unit/util/backtracefilter.rb
@@ -0,0 +1,40 @@
+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/
+
+ 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
diff --git a/ruby_1_8_5/lib/test/unit/util/observable.rb b/ruby_1_8_5/lib/test/unit/util/observable.rb
new file mode 100644
index 0000000000..3567d34271
--- /dev/null
+++ b/ruby_1_8_5/lib/test/unit/util/observable.rb
@@ -0,0 +1,90 @@
+#--
+#
+# 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
diff --git a/ruby_1_8_5/lib/test/unit/util/procwrapper.rb b/ruby_1_8_5/lib/test/unit/util/procwrapper.rb
new file mode 100644
index 0000000000..ad7252186d
--- /dev/null
+++ b/ruby_1_8_5/lib/test/unit/util/procwrapper.rb
@@ -0,0 +1,48 @@
+#--
+#
+# 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