Permalink
Browse files

Add spec for irb/driver

  • Loading branch information...
1 parent abfd9df commit 939594e31310a7b9794458bc0ec16e5539163039 @alloy committed Jul 14, 2010
Showing with 96 additions and 24 deletions.
  1. +1 −1 bin/dietrb
  2. +16 −17 lib/irb/driver.rb
  3. +1 −1 lib/irb/driver/readline.rb
  4. +1 −1 lib/irb/driver/socket.rb
  5. +2 −2 lib/irb/driver/tty.rb
  6. +69 −0 spec/driver_spec.rb
  7. +6 −2 spec/spec_helper.rb
View
2 bin/dietrb
@@ -38,7 +38,7 @@ if ARGV.empty?
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)
View
33 lib/irb/driver.rb
@@ -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
View
2 lib/irb/driver/readline.rb
@@ -22,4 +22,4 @@ def readline(context)
end
end
-IRB.driver = IRB::Driver::Readline.new
+IRB::Driver.current = IRB::Driver::Readline.new
View
2 lib/irb/driver/socket.rb
@@ -24,7 +24,7 @@ def run
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
View
4 lib/irb/driver/tty.rb
@@ -58,12 +58,12 @@ def ensure_output_redirector
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
View
69 spec/driver_spec.rb
@@ -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
View
8 spec/spec_helper.rb
@@ -21,12 +21,16 @@ def printed
@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)

0 comments on commit 939594e

Please sign in to comment.