[macruby-changes] [4594] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:45:49 PDT 2010


Revision: 4594
          http://trac.macosforge.org/projects/ruby/changeset/4594
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:45:48 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Add the source related code, as it basically is, from napkin.rb to IRB::Source.

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

Added Paths:
-----------
    DietRB/trunk/lib/
    DietRB/trunk/lib/irb/
    DietRB/trunk/lib/irb/source.rb
    DietRB/trunk/spec/
    DietRB/trunk/spec/source_spec.rb

Added: DietRB/trunk/lib/irb/source.rb
===================================================================
--- DietRB/trunk/lib/irb/source.rb	                        (rev 0)
+++ DietRB/trunk/lib/irb/source.rb	2010-10-08 10:45:48 UTC (rev 4594)
@@ -0,0 +1,33 @@
+require 'ripper'
+
+module IRB
+  class Source
+    attr_reader :buffer
+    
+    def initialize(buffer = [])
+      @buffer = buffer
+    end
+    
+    def <<(source)
+      @buffer << source.chomp
+    end
+    
+    def source
+      @buffer.join("\n")
+    end
+    
+    # This does not take syntax errors in account, but only whether or not the
+    # accumulated source up till now is a valid code block.
+    #
+    # For example, this is not a valid full code block:
+    #
+    #   def foo; p :ok
+    #
+    # This however is:
+    #
+    #   def foo; p :ok; end
+    def valid?
+      !!Ripper::SexpBuilder.new(source).parse
+    end
+  end
+end
\ No newline at end of file

Added: DietRB/trunk/spec/source_spec.rb
===================================================================
--- DietRB/trunk/spec/source_spec.rb	                        (rev 0)
+++ DietRB/trunk/spec/source_spec.rb	2010-10-08 10:45:48 UTC (rev 4594)
@@ -0,0 +1,49 @@
+require 'rubygems'
+require 'bacon'
+
+ROOT = File.expand_path('../../', __FILE__)
+$:.unshift File.join(ROOT, 'lib')
+
+require 'irb/source'
+
+describe "IRB::Source" do
+  before do
+    @source = IRB::Source.new
+  end
+  
+  it "initializes with an empty buffer" do
+    @source.buffer.should == []
+  end
+  
+  it "appends source to the buffer, removing trailing newlines" do
+    @source << "foo\n"
+    @source << "bar\r\n"
+    @source.buffer.should == %w{ foo bar }
+  end
+  
+  it "returns the full buffered source, joined by a newline" do
+    @source.source.should == ""
+    @source << "foo\n"
+    @source.source.should == "foo"
+    @source << "bar\r\n"
+    @source.source.should == "foo\nbar"
+  end
+  
+  it "returns that the accumulated source is a valid code block" do
+    [
+      ["def foo", "p :ok", "end"],
+      ["class A; def", "foo(x); p x", "end; end"]
+    ].each do |buffer|
+      IRB::Source.new(buffer).should.be.valid
+    end
+  end
+  
+  it "returns that the accumulated source is not a valid code block" do
+    [
+      ["def foo", "p :ok"],
+      ["class A; def", "foo(x); p x", "end"]
+    ].each do |buffer|
+      IRB::Source.new(buffer).should.not.be.valid
+    end
+  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/31967ad6/attachment.html>


More information about the macruby-changes mailing list