[macruby-changes] [4645] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:53:22 PDT 2010


Revision: 4645
          http://trac.macosforge.org/projects/ruby/changeset/4645
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:53:21 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Move formatting code to its own class.

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

Modified Paths:
--------------
    DietRB/trunk/lib/irb/context.rb
    DietRB/trunk/spec/context_spec.rb

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

Modified: DietRB/trunk/lib/irb/context.rb
===================================================================
--- DietRB/trunk/lib/irb/context.rb	2010-10-08 10:53:11 UTC (rev 4644)
+++ DietRB/trunk/lib/irb/context.rb	2010-10-08 10:53:21 UTC (rev 4645)
@@ -1,3 +1,4 @@
+require 'irb/formatter'
 require 'readline'
 
 module IRB
@@ -28,14 +29,14 @@
     
     def evaluate(source)
       result = __evaluate__("_ = (#{source})")
-      puts format_result(result)
+      puts formatter.result(result)
       result
     rescue Exception => e
-      puts format_exception(e)
+      puts formatter.exception(e)
     end
     
     def readline
-      Readline.readline(prompt, true)
+      Readline.readline(formatter.prompt(self), true)
     end
     
     def run
@@ -64,7 +65,7 @@
       return false if @source.to_s == "quit"
       
       if @source.syntax_error?
-        puts format_syntax_error(@source.syntax_error)
+        puts formatter.syntax_error(@line, @source.syntax_error)
         @source.pop
       elsif @source.code_block?
         evaluate(@source)
@@ -75,26 +76,12 @@
       true
     end
     
-    PROMPT = "irb(%s):%03d:%d> "
+    private
     
-    def prompt
-      PROMPT % [@object.inspect, @line, @source.level]
+    def formatter
+      IRB.formatter
     end
     
-    def format_result(result)
-      "=> #{result.inspect}"
-    end
-    
-    def format_exception(e)
-      "#{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
-    end
-    
-    def format_syntax_error(e)
-      "SyntaxError: compile error\n(irb):#{@line}: #{e}"
-    end
-    
-    private
-    
     def clear_buffer
       @source = Source.new
     end

Added: DietRB/trunk/lib/irb/formatter.rb
===================================================================
--- DietRB/trunk/lib/irb/formatter.rb	                        (rev 0)
+++ DietRB/trunk/lib/irb/formatter.rb	2010-10-08 10:53:21 UTC (rev 4645)
@@ -0,0 +1,31 @@
+module IRB
+  class << self
+    def formatter
+      @formatter ||= Formatter::Default.new
+    end
+  end
+  
+  module Formatter
+    class Default
+      PROMPT = "irb(%s):%03d:%d> "
+      
+      def prompt(context)
+        PROMPT % [context.object.inspect, context.line, context.source.level]
+      end
+      
+      def exception(exception)
+        "#{exception.class.name}: #{exception.message}\n\t#{exception.backtrace.join("\n\t")}"
+      end
+      
+      def result(object)
+        "=> #{object.inspect}"
+      end
+      
+      SYNTAX_ERROR = "SyntaxError: compile error\n(irb):%d: %s"
+      
+      def syntax_error(line, message)
+        SYNTAX_ERROR % [line, message]
+      end
+    end
+  end
+end
\ No newline at end of file

Modified: DietRB/trunk/spec/context_spec.rb
===================================================================
--- DietRB/trunk/spec/context_spec.rb	2010-10-08 10:53:11 UTC (rev 4644)
+++ DietRB/trunk/spec/context_spec.rb	2010-10-08 10:53:21 UTC (rev 4645)
@@ -45,26 +45,6 @@
     lambda { eval("x", @context.binding) }.should.raise NameError
   end
   
-  it "returns a prompt string, displaying line number and code indentation level" do
-    @context.prompt.should == "irb(main):001:0> "
-    @context.instance_variable_set(:@line, 23)
-    @context.prompt.should == "irb(main):023:0> "
-    @context.source << "def foo"
-    @context.prompt.should == "irb(main):023:1> "
-  end
-  
-  it "describes the context's object in the prompt" do
-    @context.prompt.should == "irb(main):001:0> "
-    o = Object.new
-    IRB::Context.new(o).prompt.should == "irb(#{o.inspect}):001:0> "
-  end
-  
-  it "returns a formatted exception message" do
-    begin; DoesNotExist; rescue NameError => e; exception = e; end
-    @context.format_exception(exception).should ==
-      "NameError: uninitialized constant Bacon::Context::DoesNotExist\n\t#{exception.backtrace.join("\n\t")}"
-  end
-  
   it "makes itself the current running context during the runloop and resigns once it's done" do
     IRB::Context.current.should == nil
     

Added: DietRB/trunk/spec/formatter_spec.rb
===================================================================
--- DietRB/trunk/spec/formatter_spec.rb	                        (rev 0)
+++ DietRB/trunk/spec/formatter_spec.rb	2010-10-08 10:53:21 UTC (rev 4645)
@@ -0,0 +1,38 @@
+require File.expand_path('../spec_helper', __FILE__)
+
+main = self
+
+describe "IRB::Formatter::Default" do
+  before do
+    @formatter = IRB::Formatter::Default.new
+    @context = IRB::Context.new(main)
+  end
+  
+  it "returns a prompt string, displaying line number and code indentation level" do
+    @formatter.prompt(@context).should == "irb(main):001:0> "
+    @context.instance_variable_set(:@line, 23)
+    @formatter.prompt(@context).should == "irb(main):023:0> "
+    @context.source << "def foo"
+    @formatter.prompt(@context).should == "irb(main):023:1> "
+  end
+  
+  it "describes the context's object in the prompt" do
+    o = Object.new
+    @formatter.prompt(IRB::Context.new(o)).should == "irb(#{o.inspect}):001:0> "
+  end
+  
+  it "returns a formatted exception message" do
+    begin; DoesNotExist; rescue NameError => e; exception = e; end
+    @formatter.exception(exception).should ==
+      "NameError: uninitialized constant Bacon::Context::DoesNotExist\n\t#{exception.backtrace.join("\n\t")}"
+  end
+  
+  it "prints the result" do
+    @formatter.result(:foo => :foo).should == "=> {:foo=>:foo}"
+  end
+  
+  it "prints that a syntax error occurred on the last line and reset the buffer to the previous line" do
+    @formatter.syntax_error(2, "syntax error, unexpected '}'").should ==
+      "SyntaxError: compile error\n(irb):2: syntax error, unexpected '}'"
+  end
+end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/37b91ba6/attachment.html>


More information about the macruby-changes mailing list