[macruby-changes] [4733] DietRB/trunk

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


Revision: 4733
          http://trac.macosforge.org/projects/ruby/changeset/4733
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 04:06:51 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Add spec for irb/driver

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

Modified Paths:
--------------
    DietRB/trunk/bin/dietrb
    DietRB/trunk/lib/irb/driver/readline.rb
    DietRB/trunk/lib/irb/driver/socket.rb
    DietRB/trunk/lib/irb/driver/tty.rb
    DietRB/trunk/lib/irb/driver.rb
    DietRB/trunk/spec/spec_helper.rb

Added Paths:
-----------
    DietRB/trunk/spec/driver_spec.rb

Modified: DietRB/trunk/bin/dietrb
===================================================================
--- DietRB/trunk/bin/dietrb	2010-10-08 11:06:38 UTC (rev 4732)
+++ DietRB/trunk/bin/dietrb	2010-10-08 11:06:51 UTC (rev 4733)
@@ -38,7 +38,7 @@
 else
   path = ARGV.shift
   require 'irb/driver/file'
-  IRB.driver = IRB::Driver::File.new(path)
+  IRB::Driver.current = IRB::Driver::File.new(path)
 end
 
 irb(self, TOPLEVEL_BINDING.dup)

Modified: DietRB/trunk/lib/irb/driver/readline.rb
===================================================================
--- DietRB/trunk/lib/irb/driver/readline.rb	2010-10-08 11:06:38 UTC (rev 4732)
+++ DietRB/trunk/lib/irb/driver/readline.rb	2010-10-08 11:06:51 UTC (rev 4733)
@@ -22,4 +22,4 @@
   end
 end
 
-IRB.driver = IRB::Driver::Readline.new
\ No newline at end of file
+IRB::Driver.current = IRB::Driver::Readline.new
\ No newline at end of file

Modified: DietRB/trunk/lib/irb/driver/socket.rb
===================================================================
--- DietRB/trunk/lib/irb/driver/socket.rb	2010-10-08 11:06:38 UTC (rev 4732)
+++ DietRB/trunk/lib/irb/driver/socket.rb	2010-10-08 11:06:51 UTC (rev 4733)
@@ -24,7 +24,7 @@
           connection = @server.accept
           Thread.new do
             # assign driver with connection to current thread and start runloop
-            IRB.driver = TTY.new(connection, connection)
+            IRB::Driver.current = TTY.new(connection, connection)
             irb(@object, @binding)
             connection.close
           end

Modified: DietRB/trunk/lib/irb/driver/tty.rb
===================================================================
--- DietRB/trunk/lib/irb/driver/tty.rb	2010-10-08 11:06:38 UTC (rev 4732)
+++ DietRB/trunk/lib/irb/driver/tty.rb	2010-10-08 11:06:51 UTC (rev 4733)
@@ -58,12 +58,12 @@
   end
 end
 
-IRB.driver = IRB::Driver::TTY.new
+IRB::Driver.current = IRB::Driver::TTY.new
 
 module Kernel
   # Creates a new IRB::Context with the given +object+ and runs it.
   def irb(object, binding = nil)
-    IRB.driver.run(IRB::Context.new(object, binding))
+    IRB::Driver.current.run(IRB::Context.new(object, binding))
   end
   
   private :irb

Modified: DietRB/trunk/lib/irb/driver.rb
===================================================================
--- DietRB/trunk/lib/irb/driver.rb	2010-10-08 11:06:38 UTC (rev 4732)
+++ DietRB/trunk/lib/irb/driver.rb	2010-10-08 11:06:51 UTC (rev 4733)
@@ -1,27 +1,26 @@
 module IRB
-  class << self
-    attr_accessor :driver_class
-    
-    def driver=(driver)
-      Thread.current[:irb_driver] = driver
-    end
-    
-    def driver
-      current_thread = Thread.current
-      current_thread[:irb_driver] ||= begin
-        if group = current_thread.group
-          group.list.each do |thread|
-            break(driver) if driver = thread[:irb_driver]
+  module Driver
+    class << self
+      def current=(driver)
+        Thread.current[:irb_driver] = driver
+      end
+      
+      def current
+        current_thread = Thread.current
+        current_thread[:irb_driver] ||= begin
+          if group = current_thread.group
+            group.list.each do |thread|
+              driver = thread[:irb_driver]
+              break(driver)
+            end
           end
         end
       end
     end
-  end
-  
-  module Driver
+    
     class OutputRedirector
       def self.target
-        if driver = IRB.driver
+        if driver = IRB::Driver.current
           driver.output
         else
           $stderr

Added: DietRB/trunk/spec/driver_spec.rb
===================================================================
--- DietRB/trunk/spec/driver_spec.rb	                        (rev 0)
+++ DietRB/trunk/spec/driver_spec.rb	2010-10-08 11:06:51 UTC (rev 4733)
@@ -0,0 +1,69 @@
+require File.expand_path('../spec_helper', __FILE__)
+require 'irb/driver'
+
+class StubDriver
+  attr_writer :output
+  
+  def output
+    @output || $stdout
+  end
+end
+
+describe "IRB::Driver" do
+  before :all do
+    @driver = StubDriver.new
+    IRB::Driver.current = @driver
+  end
+  
+  it "assigns the driver for the current thread" do
+    Thread.current[:irb_driver].should == @driver
+  end
+  
+  it "returns the same driver for child threads" do
+    Thread.new { IRB::Driver.current.should == @driver }.join
+  end
+end
+
+describe "IRB::Driver::OutputRedirector" do
+  before :each do
+    @driver = StubDriver.new
+    @driver.output = CaptureIO.new
+    IRB::Driver.current = @driver
+    
+    @redirector = IRB::Driver::OutputRedirector.new
+  end
+  
+  it "returns $stderr as the target if no current driver could be found" do
+    IRB::Driver.current = nil
+    IRB::Driver::OutputRedirector.target.should == $stderr
+  end
+  
+  it "returns the current driver's output as the target" do
+    IRB::Driver::OutputRedirector.target.should == @driver.output
+  end
+  
+  it "forwards method calls to the current target" do
+    @redirector.send_to_target(:eql?, @driver.output).should == true
+  end
+  
+  it "writes to the current target's output" do
+    @redirector.write("strawberry coupe")
+    @driver.output.printed.should == "strawberry coupe"
+  end
+  
+  it "returns the amount of bytes written" do
+    @redirector.write("banana coupe").should == 12
+  end
+  
+  it "coerces an object to a string before writing" do
+    o = Object.new
+    def o.to_s; "cherry coupe"; end
+    @redirector.write(o)
+    @driver.output.printed.should == "cherry coupe"
+  end
+  
+  it "forwards puts to the current target's output" do
+    @redirector.puts("double", "coupe")
+    @driver.output.printed.should == "double\ncoupe\n"
+  end
+end
\ No newline at end of file

Modified: DietRB/trunk/spec/spec_helper.rb
===================================================================
--- DietRB/trunk/spec/spec_helper.rb	2010-10-08 11:06:38 UTC (rev 4732)
+++ DietRB/trunk/spec/spec_helper.rb	2010-10-08 11:06:51 UTC (rev 4733)
@@ -21,12 +21,16 @@
     @printed ||= ''
   end
   
+  def write(string)
+    printed << string
+  end
+  
   def print(string)
     printed << string
   end
   
-  def puts(string)
-    print "#{string}\n"
+  def puts(*args)
+    print "#{args.join("\n")}\n"
   end
   
   def stub_input(*input)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/0ca5594d/attachment.html>


More information about the macruby-changes mailing list