Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add tests, fix bugs

  • Loading branch information...
commit 67d68d87d76ae7f93e89329797831c57c7432106 1 parent 277696c
@ConradIrwin authored
View
2  ext/interception.c
@@ -48,5 +48,5 @@ Init_interception()
{
rb_mInterception = rb_define_module("Interception");
rb_define_singleton_method(rb_mInterception, "start", interception_start, 0);
- rb_define_singleton_method(rb_mInterception, "stop", interception_start, 0);
+ rb_define_singleton_method(rb_mInterception, "stop", interception_stop, 0);
}
View
12 lib/interception.rb
@@ -3,7 +3,7 @@
module Interception
class << self
- attr_accessor :mutex, :listeners
+ attr_accessor :mutex, :listeners, :rescueing
end
self.mutex = Mutex.new
@@ -12,8 +12,8 @@ class << self
def self.listen(for_block=nil, &listen_block)
raise "no block given" unless listen_block || for_block
mutex.synchronize{
+ start if listeners.empty?
listeners << listen_block || for_block
- start
}
if listen_block && for_block
@@ -33,9 +33,13 @@ def self.unlisten(listen_block)
end
def self.rescue(e, binding)
+ return if rescueing
+ self.rescueing = true
listeners.each do |l|
l.call(e, binding)
end
+ ensure
+ self.rescueing = false
end
if defined? Rubinius
@@ -66,7 +70,11 @@ class << Rubinius
java_import org.pryrepl.InterceptionEventHook
def self.start
+ old_verbose = $VERBOSE
+ $VERBOSE = nil
JRuby.runtime.add_event_hook(hook)
+ ensure
+ $VERBOSE = old_verbose
end
def self.stop
View
39 spec/interception_spec.rb
@@ -22,4 +22,43 @@
@exceptions.map(&:first).should == [e]
end
+
+ it "should catch the correct binding" do
+ shoulder = :bucket
+ begin
+ raise "foo"
+ rescue => e
+ #
+ end
+
+ @exceptions.map{ |e, b| b.eval('shoulder') }.should == [:bucket]
+ end
+
+ it "should catch the binding on the correct line" do
+ shoulder = :bucket
+
+ line = nil
+ begin
+ line = __LINE__; raise "foo"
+ rescue => e
+ #
+ end
+
+ @exceptions.map{ |e, b| b.eval('__LINE__') }.should == [line]
+ end
+
+ it "should catch all nested exceptions" do
+
+ begin
+ begin
+ raise "foo"
+ rescue => e1
+ raise "bar"
+ end
+ rescue => e2
+ #
+ end
+
+ @exceptions.map(&:first).should == [e1, e2]
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.