[macruby-changes] [4722] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 04:04:51 PDT 2010


Revision: 4722
          http://trac.macosforge.org/projects/ruby/changeset/4722
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 04:04:50 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Make specs green. Now uses a pure IO stub instead of monkey patching Readline.

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

Modified Paths:
--------------
    DietRB/trunk/lib/irb/context.rb
    DietRB/trunk/lib/irb/io/readline.rb
    DietRB/trunk/spec/context_spec.rb
    DietRB/trunk/spec/ext/history_spec.rb
    DietRB/trunk/spec/spec_helper.rb

Modified: DietRB/trunk/lib/irb/context.rb
===================================================================
--- DietRB/trunk/lib/irb/context.rb	2010-10-08 11:04:39 UTC (rev 4721)
+++ DietRB/trunk/lib/irb/context.rb	2010-10-08 11:04:50 UTC (rev 4722)
@@ -58,16 +58,14 @@
     # Prints the prompt to, and reads input from, the + at io+ object and passes
     # it to all processors.
     #
-    # If the object returned by  is +nil+ the buffer is
-    # cleared. This could, for example, be when the user sends a SIGINT.
+    # The buffer is cleared if an Interrupt exception is raised.
     def readline_from_io
-      if input = @io.readline(formatter.prompt(self))
-        @processors.each { |processor| input = processor.input(input) }
-        input
-      else
-        clear_buffer
-        ""
-      end
+      input = @io.readline(formatter.prompt(self))
+      @processors.each { |processor| input = processor.input(input) }
+      input
+    rescue Interrupt
+      clear_buffer
+      ""
     end
     
     def run

Modified: DietRB/trunk/lib/irb/io/readline.rb
===================================================================
--- DietRB/trunk/lib/irb/io/readline.rb	2010-10-08 11:04:39 UTC (rev 4721)
+++ DietRB/trunk/lib/irb/io/readline.rb	2010-10-08 11:04:50 UTC (rev 4722)
@@ -10,7 +10,6 @@
       
       def readline(prompt)
         ::Readline.readline(prompt, true)
-      rescue Interrupt
       end
       
       def puts(*args)

Modified: DietRB/trunk/spec/context_spec.rb
===================================================================
--- DietRB/trunk/spec/context_spec.rb	2010-10-08 11:04:39 UTC (rev 4721)
+++ DietRB/trunk/spec/context_spec.rb	2010-10-08 11:04:50 UTC (rev 4722)
@@ -1,22 +1,6 @@
 require File.expand_path('../spec_helper', __FILE__)
 require 'tempfile'
 
-def stub_Readline
-  class << Readline
-    attr_reader :received
-    
-    def stub_input(*input)
-      @input = input
-    end
-    
-    def readline(prompt, history)
-      @received = [prompt, history]
-      @input.shift
-    end
-  end
-end
-stub_Readline
-
 class TestProcessor
   def input(s)
     s * 2
@@ -28,6 +12,7 @@
 describe "IRB::Context" do
   before do
     @context = IRB::Context.new(main)
+    @context.io = @io = CaptureIO.new
   end
   
   it "initializes with an object and stores a copy of its binding" do
@@ -65,22 +50,21 @@
     lambda { eval("x", @context.binding) }.should raise_error(NameError)
   end
   
-  # it "makes itself the current running context during the runloop and resigns once it's done" do
-  #   IRB::Context.current.should == nil
-  #   
-  #   Readline.stub_input("current_during_run = IRB::Context.current")
-  #   @context.run
-  #   eval('current_during_run', @context.binding).should == @context
-  #   
-  #   IRB::Context.current.should == nil
-  # end
+  it "makes itself the current running context during the runloop and resigns once it's done" do
+    IRB::Context.current.should == nil
+    
+    @io.stub_input("current_during_run = IRB::Context.current")
+    @context.run
+    eval('current_during_run', @context.binding).should == @context
+    
+    IRB::Context.current.should == nil
+  end
 end
 
 describe "IRB::Context, when evaluating source" do
   before do
     @context = IRB::Context.new(main)
-    def @context.printed;      @printed ||= ''          end
-    def @context.puts(string); printed << "#{string}\n" end
+    @context.io = @io = CaptureIO.new
     IRB.formatter = IRB::Formatter.new
   end
   
@@ -90,7 +74,7 @@
   
   it "prints the result" do
     @context.evaluate("Hash[:foo, :foo]")
-    @context.printed.should == "=> {:foo=>:foo}\n"
+    @io.printed.should == "=> {:foo=>:foo}\n"
   end
   
   it "assigns the result to the local variable `_'" do
@@ -121,44 +105,45 @@
   
   it "prints the exception that occurs" do
     @context.evaluate("DoesNotExist")
-    @context.printed.should =~ /^NameError:.+DoesNotExist/
+    @io.printed.should =~ /^NameError:.+DoesNotExist/
   end
   
   it "uses the line number of the *first* line in the buffer, for the line parameter of eval" do
     @context.process_line("DoesNotExist")
-    @context.printed.should =~ /\(irb\):1:in/
+    @io.printed.should =~ /\(irb\):1:in/
     @context.process_line("class A")
     @context.process_line("DoesNotExist")
     @context.process_line("end")
-    @context.printed.should =~ /\(irb\):3:in.+\(irb\):2:in/m
+    @io.printed.should =~ /\(irb\):3:in.+\(irb\):2:in/m
   end
   
   it "inputs a line to be processed, skipping readline" do
     expected = "#{@context.formatter.prompt(@context)}2 * 21\n=> 42\n"
     @context.input_line("2 * 21")
-    @context.printed.should == expected
+    @io.printed.should == expected
   end
 end
 
 describe "IRB::Context, when receiving input" do
   before do
     @context = IRB::Context.new(main)
+    @context.io = @io = CaptureIO.new
   end
   
-  it "prints the prompt, reads a line, saves it to the history and returns it" do
-    Readline.stub_input("def foo")
-    @context.readline.should == "def foo"
-    Readline.received.should == ["irb(main):001:0> ", true]
+  it "prints the prompt and reads a line" do
+    @io.stub_input("def foo")
+    @context.readline_from_io.should == "def foo"
+    @io.printed.should == "irb(main):001:0> "
   end
   
   it "passes the input to all processors, which may return a new value" do
     @context.processors << TestProcessor.new
-    Readline.stub_input("foo")
-    @context.readline.should == "foofoo"
+    @io.stub_input("foo")
+    @context.readline_from_io.should == "foofoo"
   end
   
   it "processes the output" do
-    Readline.stub_input("def foo")
+    @io.stub_input("def foo")
     def @context.process_line(line); @received = line; false; end
     @context.run
     @context.instance_variable_get(:@received).should == "def foo"
@@ -170,23 +155,19 @@
     @context.source.to_s.should == "def foo\np :ok"
   end
   
-  # it "clears the source buffer when an Interrupt signal is received" do
-  #   begin
-  #     @context.process_line("def foo")
-  #     
-  #     def Readline.readline(*args)
-  #       unless @raised
-  #         @raised = true
-  #         raise Interrupt
-  #       end
-  #     end
-  #     
-  #     lambda { @context.run }.should_not raise_error(Interrupt)
-  #     @context.source.to_s.should == ""
-  #   ensure
-  #     stub_Readline
-  #   end
-  # end
+  it "clears the source buffer when an Interrupt signal is received" do
+    @context.process_line("def foo")
+    
+    def @io.readline(prompt)
+      unless @raised
+        @raised = true
+        raise Interrupt
+      end
+    end
+    
+    lambda { @context.run }.should_not raise_error(Interrupt)
+    @context.source.to_s.should == ""
+  end
   
   it "increases the current line number" do
     @context.line.should == 1
@@ -208,14 +189,11 @@
   end
   
   it "prints that a syntax error occurred on the last line and reset the buffer to the previous line" do
-    def @context.puts(str); @printed = str; end
-    
     @context.process_line("def foo")
     @context.process_line("  };")
     
     @context.source.to_s.should == "def foo"
-    printed = @context.instance_variable_get(:@printed)
-    printed.should == "SyntaxError: compile error\n(irb):2: syntax error, unexpected '}'"
+    @io.printed.should == "SyntaxError: compile error\n(irb):2: syntax error, unexpected '}'\n"
   end
   
   it "returns whether or not the runloop should continue, but only if the level is 0" do
@@ -227,7 +205,7 @@
   end
   
   it "exits the runloop if the user wishes so" do
-    Readline.stub_input("quit", "def foo")
+    @io.stub_input("quit", "def foo")
     def @context.process_line(line); @received = line; super; end
     @context.run
     @context.instance_variable_get(:@received).should_not == "def foo"

Modified: DietRB/trunk/spec/ext/history_spec.rb
===================================================================
--- DietRB/trunk/spec/ext/history_spec.rb	2010-10-08 11:04:39 UTC (rev 4721)
+++ DietRB/trunk/spec/ext/history_spec.rb	2010-10-08 11:04:50 UTC (rev 4722)
@@ -71,21 +71,10 @@
 end
 
 class IRB::History
-  def printed
-    @printed ||= ""
-  end
-  
-  def print(s)
-    printed << s
-  end
-  
-  def puts(s)
-    printed << "#{s}\n"
-  end
-  
   def clear!
     @cleared = true
   end
+  
   def cleared?
     @cleared
   end
@@ -116,6 +105,7 @@
     IRB::History.max_entries_in_overview = 5
     
     @context = IRB::Context.new(Object.new)
+    @context.io = @io = CaptureIO.new
     IRB::Context.current = @context
     
     @history = @context.processors.find { |p| p.is_a?(IRB::History) }
@@ -129,10 +119,10 @@
     history.should == nil
   end
   
-  it "prints a formatted list with, by default IRB::History.max_entries_in_overview, number of history entries" do
+  it "prints a formatted list with IRB::History.max_entries_in_overview number of history entries" do
     history
     
-    @history.printed.should == %{
+    @io.printed.should == %{
 2: class AAA
 3:   def bar
 4:     :ok
@@ -144,7 +134,7 @@
   it "prints a formatted list of N most recent history entries" do
     history(7)
     
-    @history.printed.should == %{
+    @io.printed.should == %{
 0: puts :ok
 1: x = foo(x)
 2: class AAA
@@ -158,7 +148,7 @@
   it "prints a formatted list of all history entries if the request number of entries is more than there is" do
     history(777)
 
-    @history.printed.should == %{
+    @io.printed.should == %{
 0: puts :ok
 1: x = foo(x)
 2: class AAA

Modified: DietRB/trunk/spec/spec_helper.rb
===================================================================
--- DietRB/trunk/spec/spec_helper.rb	2010-10-08 11:04:39 UTC (rev 4721)
+++ DietRB/trunk/spec/spec_helper.rb	2010-10-08 11:04:50 UTC (rev 4722)
@@ -14,4 +14,27 @@
 end
 $:.unshift File.join(ROOT, 'lib')
 
-require 'irb'
\ No newline at end of file
+require 'irb'
+
+class CaptureIO
+  def printed
+    @printed ||= ''
+  end
+  
+  def print(string)
+    printed << string
+  end
+  
+  def puts(string)
+    print "#{string}\n"
+  end
+  
+  def stub_input(*input)
+    @input = input
+  end
+  
+  def readline(prompt)
+    print prompt
+    @input.shift
+  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/289b963a/attachment-0001.html>


More information about the macruby-changes mailing list