Skip to content

Commit

Permalink
Finish tty spec and simplified a bit more
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Jul 16, 2010
1 parent b1f3607 commit af45782
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
5 changes: 4 additions & 1 deletion .gitignore
@@ -1 +1,4 @@
pkg pkg
*.swp
extensions
t.rb
4 changes: 2 additions & 2 deletions bin/dietrb
Expand Up @@ -32,9 +32,9 @@ end
IRB.formatter.filter_from_backtrace << /^#{__FILE__}/ IRB.formatter.filter_from_backtrace << /^#{__FILE__}/


if ARGV.empty? if ARGV.empty?
# require 'irb/driver/readline' require 'irb/driver/readline'
# require 'irb/driver/tty' # require 'irb/driver/tty'
require 'irb/driver/socket' # require 'irb/driver/socket'
else else
path = ARGV.shift path = ARGV.shift
require 'irb/driver/file' require 'irb/driver/file'
Expand Down
23 changes: 8 additions & 15 deletions lib/irb/driver/tty.rb
Expand Up @@ -23,22 +23,15 @@ def consume(context)
"" ""
end end


# Feeds input into a given context.
#
# Ensures that the standard output object is a OutputRedirector, or a
# subclass thereof.
def run(context) def run(context)
ensure_output_redirector do before, $stdout = $stdout, OutputRedirector.new unless $stdout.is_a?(OutputRedirector)
while line = consume(context) while line = consume(context)
continue = context.process_line(line) break unless context.process_line(line)
break unless continue
end
end end
end

# Ensure that the standard output object is a OutputRedirector. If it's
# already a OutputRedirector, do nothing.
def ensure_output_redirector
unless $stdout.is_a?(IRB::Driver::OutputRedirector)
before, $stdout = $stdout, IRB::Driver::OutputRedirector.new
end
yield
ensure ensure
$stdout = before if before $stdout = before if before
end end
Expand All @@ -55,4 +48,4 @@ def irb(object, binding = nil)
end end


private :irb private :irb
end end
18 changes: 17 additions & 1 deletion spec/driver/tty_spec.rb
Expand Up @@ -24,4 +24,20 @@ def @driver.readline(_); raise Interrupt; end
@driver.consume(@context).should == "" @driver.consume(@context).should == ""
@context.source.to_s.should == "" @context.source.to_s.should == ""
end end
end
it "feeds input into a given context" do
$from_context = false
@driver.input.stub_input "$from_context = true", "exit"
@driver.run(@context)
$from_context.should == true
end

it "makes sure there's one global output redirector while running a context" do
before = $stdout
$from_context = nil
@driver.input.stub_input "$from_context = $stdout", "exit"
@driver.run(@context)
$from_context.class == IRB::Driver::OutputRedirector
$stdout.should == before
end
end

0 comments on commit af45782

Please sign in to comment.