[macruby-changes] [4764] DietRB/trunk

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


Revision: 4764
          http://trac.macosforge.org/projects/ruby/changeset/4764
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 04:11:26 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Context prints to the current driver?\226?\128?\153s output, if one is available, this way the output doesn't have to be redirected to return to the proper output. Think server apps.

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

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

Modified: DietRB/trunk/TODO
===================================================================
--- DietRB/trunk/TODO	2010-10-08 11:11:16 UTC (rev 4763)
+++ DietRB/trunk/TODO	2010-10-08 11:11:26 UTC (rev 4764)
@@ -1,4 +1,3 @@
-* Make Context take a ‘driver’ again, because we do not always want to redirect $stdout, but still receive output from the context. Ie, result, exception etc. A situation where we don’t want to redirect $stdout is in a server app.
 * Write docs for using a as library. Probably right after creating a Cocoa client.
 * Add specs for irb/driver/socket, and possibly for bin/dietrb
 * Configurable history file? (:HISTORY_FILE) Configurable number of saved history lines? (:SAVE_HISTORY)

Modified: DietRB/trunk/lib/irb/context.rb
===================================================================
--- DietRB/trunk/lib/irb/context.rb	2010-10-08 11:11:16 UTC (rev 4763)
+++ DietRB/trunk/lib/irb/context.rb	2010-10-08 11:11:26 UTC (rev 4764)
@@ -31,12 +31,12 @@
       result = __evaluate__(source.to_s, '(irb)', @line - @source.buffer.size + 1)
       unless result == IGNORE_RESULT
         store_result(result)
-        puts(formatter.result(result))
+        output(formatter.result(result))
         result
       end
     rescue Exception => e
       store_exception(e)
-      puts(formatter.exception(e))
+      output(formatter.exception(e))
     end
     
     # Returns whether or not the user wants to continue the current runloop.
@@ -56,7 +56,7 @@
       return false if @source.terminate?
       
       if @source.syntax_error?
-        puts(formatter.syntax_error(@line, @source.syntax_error))
+        output(formatter.syntax_error(@line, @source.syntax_error))
         @source.pop
       elsif @source.code_block?
         evaluate(@source)
@@ -67,12 +67,22 @@
       true
     end
     
+    # Output is directed to the IRB::Driver.current driver’s output if a
+    # current driver is available. Otherwise it’s simply printed to $stdout.
+    def output(string)
+      if driver = IRB::Driver.current
+        driver.output.puts(string)
+      else
+        puts(string)
+      end
+    end
+    
     def prompt
       formatter.prompt(self)
     end
     
     def input_line(line)
-      puts(formatter.prompt(self) + line)
+      output(formatter.prompt(self) + line)
       process_line(line)
     end
     
@@ -92,4 +102,4 @@
       @exception_assigner.call(exception)
     end
   end
-end
\ No newline at end of file
+end

Modified: DietRB/trunk/lib/irb.rb
===================================================================
--- DietRB/trunk/lib/irb.rb	2010-10-08 11:11:16 UTC (rev 4763)
+++ DietRB/trunk/lib/irb.rb	2010-10-08 11:11:26 UTC (rev 4764)
@@ -5,6 +5,7 @@
 # Copyright (C) 2009-2010, Eloy Duran <eloy.de.enige at gmail.com>
 
 require 'irb/context'
+require 'irb/driver'
 require 'irb/source'
 require 'irb/version'
 
@@ -22,4 +23,4 @@
     end
     alias_method :setup, :start
   end
-end
\ No newline at end of file
+end

Modified: DietRB/trunk/spec/context_spec.rb
===================================================================
--- DietRB/trunk/spec/context_spec.rb	2010-10-08 11:11:16 UTC (rev 4763)
+++ DietRB/trunk/spec/context_spec.rb	2010-10-08 11:11:26 UTC (rev 4764)
@@ -1,16 +1,11 @@
 require File.expand_path('../spec_helper', __FILE__)
 require 'tempfile'
 
-class TestProcessor
-  def input(s)
-    s * 2
-  end
-end
-
 main = self
 
 describe "IRB::Context" do
   before do
+    @output = setup_current_driver.output
     @context = IRB::Context.new(main)
     @context.extend(OutputStubMixin)
   end
@@ -38,12 +33,25 @@
   it "does not use the same binding copy of the top level object" do
     lambda { eval("x", @context.binding) }.should raise_error(NameError)
   end
+  
+  it "prints to the output object of the current driver" do
+    @context.output("croque monsieur")
+    @output.printed.should == "croque monsieur\n"
+    @context.printed.should == ""
+  end
+
+  it "prints as normal when no current driver is available" do
+    IRB::Driver.current = nil
+    @context.output("croque monsieur")
+    @output.printed.should == ""
+    @context.printed.should == "croque monsieur\n"
+  end
 end
 
 describe "IRB::Context, when evaluating source" do
   before do
+    @output = setup_current_driver.output
     @context = IRB::Context.new(main)
-    @context.extend(OutputStubMixin)
     IRB.formatter = IRB::Formatter.new
   end
   
@@ -53,7 +61,7 @@
   
   it "prints the result" do
     @context.evaluate("Hash[:foo, :foo]")
-    @context.printed.should == "=> {:foo=>:foo}\n"
+    @output.printed.should == "=> {:foo=>:foo}\n"
   end
   
   it "assigns the result to the local variable `_'" do
@@ -84,31 +92,31 @@
   
   it "prints the exception that occurs" do
     @context.evaluate("DoesNotExist")
-    @context.printed.should =~ /^NameError:.+DoesNotExist/
+    @output.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/
+    @output.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
+    @output.printed.should =~ /\(irb\):3:in.+\(irb\):2:in/m
   end
   
   it "ignores the result if it's IRB::Context::IGNORE_RESULT" do
     @context.evaluate(":bananas")
     @context.evaluate("IRB::Context::IGNORE_RESULT").should == nil
-    @context.printed.should == "=> :bananas\n"
+    @output.printed.should == "=> :bananas\n"
     @context.evaluate("_").should == :bananas
   end
 end
 
 describe "IRB::Context, when receiving input" do
   before do
+    @output = setup_current_driver.output
     @context = IRB::Context.new(main)
     @context.extend(InputStubMixin)
-    @context.extend(OutputStubMixin)
   end
   
   it "adds the received code to the source buffer" do
@@ -147,7 +155,7 @@
     @context.process_line("  };")
     
     @context.source.to_s.should == "def foo"
-    @context.printed.should == "SyntaxError: compile error\n(irb):2: syntax error, unexpected '}'\n"
+    @output.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
@@ -161,6 +169,6 @@
   it "inputs a line to be processed" do
     expected = "#{@context.formatter.prompt(@context)}2 * 21\n=> 42\n"
     @context.input_line("2 * 21")
-    @context.printed.should == expected
+    @output.printed.should == expected
   end
 end

Modified: DietRB/trunk/spec/spec_helper.rb
===================================================================
--- DietRB/trunk/spec/spec_helper.rb	2010-10-08 11:11:16 UTC (rev 4763)
+++ DietRB/trunk/spec/spec_helper.rb	2010-10-08 11:11:26 UTC (rev 4764)
@@ -72,4 +72,14 @@
   def output
     @output || $stdout
   end
+
+  def printed
+    @output.printed
+  end
 end
+
+def setup_current_driver
+  driver = StubDriver.new
+  driver.output = OutputStub.new
+  IRB::Driver.current = driver
+end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/7b7b9832/attachment-0001.html>


More information about the macruby-changes mailing list