[macruby-changes] [4603] DietRB/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Fri Oct 8 03:47:13 PDT 2010
Revision: 4603
http://trac.macosforge.org/projects/ruby/changeset/4603
Author: eloy.de.enige at gmail.com
Date: 2010-10-08 03:47:12 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Use Readline, with prompt and history, to get input and buffer the source.
From: Eloy Duran <eloy.de.enige at gmail.com>
Modified Paths:
--------------
DietRB/trunk/lib/irb/context.rb
DietRB/trunk/spec/context_spec.rb
Modified: DietRB/trunk/lib/irb/context.rb
===================================================================
--- DietRB/trunk/lib/irb/context.rb 2010-10-08 10:47:03 UTC (rev 4602)
+++ DietRB/trunk/lib/irb/context.rb 2010-10-08 10:47:12 UTC (rev 4603)
@@ -1,7 +1,7 @@
+require 'readline'
+
module IRB
class Context
- PROMPT = "irb(%s):%03d:%d> "
-
attr_reader :object, :binding, :line, :source
def initialize(object)
@@ -15,6 +15,18 @@
eval(source.to_s, @binding)
end
+ def readline
+ Readline.readline(prompt, true)
+ end
+
+ def run
+ while line = readline
+ @source << line
+ end
+ end
+
+ PROMPT = "irb(%s):%03d:%d> "
+
def prompt
PROMPT % [@object.inspect, @line, @source.level]
end
Modified: DietRB/trunk/spec/context_spec.rb
===================================================================
--- DietRB/trunk/spec/context_spec.rb 2010-10-08 10:47:03 UTC (rev 4602)
+++ DietRB/trunk/spec/context_spec.rb 2010-10-08 10:47:12 UTC (rev 4603)
@@ -1,4 +1,5 @@
require File.expand_path('../spec_helper', __FILE__)
+require 'tempfile'
main = self
@@ -48,4 +49,35 @@
o = Object.new
IRB::Context.new(o).prompt.should == "irb(#{o.inspect}):001:0> "
end
+end
+
+class << Readline
+ attr_reader :received
+
+ def stub_input(*input)
+ @input = input
+ end
+
+ def readline(prompt, history)
+ @received = [prompt, history]
+ @input.shift
+ end
+end
+
+describe "IRB::Context, running in a simple readline loop" do
+ before do
+ @context = IRB::Context.new(main)
+ 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]
+ end
+
+ it "adds the received code, as long as available, to the source buffer while running" do
+ Readline.stub_input("def foo", "p :ok")
+ @context.run
+ @context.source.to_s.should == "def foo\np :ok"
+ 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/7971db88/attachment.html>
More information about the macruby-changes
mailing list