[macruby-changes] [4654] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:54:46 PDT 2010


Revision: 4654
          http://trac.macosforge.org/projects/ruby/changeset/4654
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:54:45 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Filter out lines about dietrb files from the backtrace.

From: Eloy Duran <eloy.de.enige at gmail.com>

Modified Paths:
--------------
    DietRB/trunk/lib/irb/formatter.rb
    DietRB/trunk/spec/formatter_spec.rb

Modified: DietRB/trunk/lib/irb/formatter.rb
===================================================================
--- DietRB/trunk/lib/irb/formatter.rb	2010-10-08 10:54:37 UTC (rev 4653)
+++ DietRB/trunk/lib/irb/formatter.rb	2010-10-08 10:54:45 UTC (rev 4654)
@@ -7,13 +7,15 @@
     DEFAULT_PROMPT = "irb(%s):%03d:%d> "
     SIMPLE_PROMPT  = ">> "
     NO_PROMPT      = ""
+    SYNTAX_ERROR   = "SyntaxError: compile error\n(irb):%d: %s"
+    SOURCE_ROOT    = /^#{File.expand_path('../../../', __FILE__)}/
     
-    SYNTAX_ERROR = "SyntaxError: compile error\n(irb):%d: %s"
-    
     attr_writer :prompt
+    attr_reader :filter_from_backtrace
     
     def initialize
       @prompt = :default
+      @filter_from_backtrace = [SOURCE_ROOT]
     end
     
     def prompt(context)
@@ -25,10 +27,6 @@
       end
     end
     
-    def exception(exception)
-      "#{exception.class.name}: #{exception.message}\n\t#{exception.backtrace.join("\n\t")}"
-    end
-    
     def result(object)
       "=> #{object.inspect}"
     end
@@ -36,5 +34,16 @@
     def syntax_error(line, message)
       SYNTAX_ERROR % [line, message]
     end
+    
+    def exception(exception)
+      backtrace = $DEBUG ? exception.backtrace : filter_backtrace(exception.backtrace)
+      "#{exception.class.name}: #{exception.message}\n\t#{backtrace.join("\n\t")}"
+    end
+    
+    def filter_backtrace(backtrace)
+      backtrace.reject do |line|
+        @filter_from_backtrace.any? { |pattern| pattern.match(line) }
+      end
+    end
   end
 end
\ No newline at end of file

Modified: DietRB/trunk/spec/formatter_spec.rb
===================================================================
--- DietRB/trunk/spec/formatter_spec.rb	2010-10-08 10:54:37 UTC (rev 4653)
+++ DietRB/trunk/spec/formatter_spec.rb	2010-10-08 10:54:45 UTC (rev 4654)
@@ -31,12 +31,25 @@
     @formatter.prompt(@context).should == ""
   end
   
-  it "returns a formatted exception message" do
-    begin; DoesNotExist; rescue NameError => e; exception = e; end
+  it "returns a formatted exception message, with the lines, regarding dietrb, filtered out of the backtrace" do
+    begin; @context.__evaluate__('DoesNotExist'); rescue NameError => e; exception = e; end
+    backtrace = exception.backtrace.reject { |f| f =~ /#{ROOT}/ }
     @formatter.exception(exception).should ==
-      "NameError: uninitialized constant Bacon::Context::DoesNotExist\n\t#{exception.backtrace.join("\n\t")}"
+      "NameError: uninitialized constant IRB::Context::DoesNotExist\n\t#{backtrace.join("\n\t")}"
   end
   
+  it "does not filter the backtrace if $DEBUG is true" do
+    begin
+      before, $DEBUG = $DEBUG, true
+      
+      begin; @context.__evaluate__('DoesNotExist'); rescue NameError => e; exception = e; end
+      @formatter.exception(exception).should ==
+        "NameError: uninitialized constant IRB::Context::DoesNotExist\n\t#{exception.backtrace.join("\n\t")}"
+    ensure
+      $DEBUG = before
+    end
+  end
+  
   it "prints the result" do
     @formatter.result(:foo => :foo).should == "=> {:foo=>:foo}"
   end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/51b722b5/attachment.html>


More information about the macruby-changes mailing list