Skip to content

Commit

Permalink
Merge pull request hybridgroup#8 from mattscilipoti/stdio_write
Browse files Browse the repository at this point in the history
Wrap Stdio write in some tests and do a little cleanup.
  • Loading branch information
Kia Kroas committed May 17, 2011
2 parents 329330b + 509d949 commit f0e8cd8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
22 changes: 15 additions & 7 deletions lib/kidsruby/stdio.rb
@@ -1,24 +1,32 @@
class KidsRubyStdIo
def initialize
@iface = InterfaceHelper.new.get_interface
attr_reader :interface

def initialize(interface = InterfaceHelper.new.get_interface)
@interface = interface
end

def puts(data)
write(data.to_s + "\n")
end

# (very) Simple textilize-like converter
# Converts newlines to html line breaks
def simple_textilize(text)
# TODO: should we use an actual textilize/markdown filter?
# Or simple_format from actionpack/lib/action_view/helpers/text_helper.rb
text.gsub(/\n/,"<br/>")
end
end

class StdOut < KidsRubyStdIo
def write(data)
t = data.gsub(/\n/,"<br/>")
@iface.call("append", t)
def write(data, as_error = false)
interface.call("append", simple_textilize(data))
end
end

class StdErr < KidsRubyStdIo
def write(data)
t = data.gsub(/\n/,"<br/>")
@iface.call("appendError", t)
interface.call("appendError", simple_textilize(data))
end
end

Expand Down
52 changes: 44 additions & 8 deletions spec/models/std_io_spec.rb
@@ -1,22 +1,58 @@
require_relative "../spec_helper"
require_relative "../../lib/kidsruby"

describe KidsRubyStdIo do
it "should default the interface to a new one from InterfaceHelper" do
# We can not test that interface == InterfaceHelper.new.get_interface
# since it will be a different instance of get_interface.
# Therefore, we stub get_interface
InterfaceHelper.any_instance.stubs(:get_interface => "TEST INTERFACE")
KidsRubyStdIo.new().interface.must_equal "TEST INTERFACE"
end

describe '.simple_textilize' do
it "should convert newlines to html line break" do
input = "a\n2\nc\n"
expected = "a<br/>2<br/>c<br/>"
KidsRubyStdIo.new.simple_textilize(input).must_equal expected
end
end
end

describe StdOut do
describe ".puts" do
it "should delegate the coerced, new-lined data to .write" do
stdout_ut = StdOut.new
stdout_ut.expects(:write).with("123\n")
stdout_ut.puts(123)
it "should forward the coerced, new-lined data to .write" do
subject = StdOut.new
subject.expects(:write).with("123\n")
subject.puts(123)
end
end

describe ".write" do
it "should convert the text to html and append it to the interface" do
test_interface = mock()
test_interface.expects(:call).with("append", "123<br/>")

StdOut.new(test_interface).write("123\n")
end
end
end

describe StdErr do
describe ".puts" do
it "should delegate the coerced, newlined data to .write" do
stderr_ut = StdErr.new
stderr_ut.expects(:write).with("234\n")
stderr_ut.puts(234)
it "should forward the coerced, newlined data to .write" do
subject = StdErr.new
subject.expects(:write).with("234\n")
subject.puts(234)
end
end

describe ".write" do
it "should convert the text to html and append it (as error) to the interface" do
test_interface = mock()
test_interface.expects(:call).with("appendError", "234<br/>")

StdErr.new(test_interface).write("234\n")
end
end
end

0 comments on commit f0e8cd8

Please sign in to comment.