From 01ee1f19a396a2686da5f470444060b7f1a2e615 Mon Sep 17 00:00:00 2001 From: Matt Scilipoti Date: Tue, 17 May 2011 12:15:41 -0400 Subject: [PATCH 1/2] stdio: dependency inject the interface Using dependency injection was the only way I could see to make write testable. Interface defaults to InterfaceHelper.new.get_interface --- lib/kidsruby/stdio.rb | 10 +++++---- spec/models/std_io_spec.rb | 44 +++++++++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/lib/kidsruby/stdio.rb b/lib/kidsruby/stdio.rb index e2b26d7..4b25c49 100644 --- a/lib/kidsruby/stdio.rb +++ b/lib/kidsruby/stdio.rb @@ -1,6 +1,8 @@ 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) @@ -11,14 +13,14 @@ def puts(data) class StdOut < KidsRubyStdIo def write(data) t = data.gsub(/\n/,"
") - @iface.call("append", t) + interface.call("append", t) end end class StdErr < KidsRubyStdIo def write(data) t = data.gsub(/\n/,"
") - @iface.call("appendError", t) + interface.call("appendError", t) end end diff --git a/spec/models/std_io_spec.rb b/spec/models/std_io_spec.rb index b2cda2c..0f2d43c 100644 --- a/spec/models/std_io_spec.rb +++ b/spec/models/std_io_spec.rb @@ -1,22 +1,50 @@ 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 +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
") + + 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
") + + StdErr.new(test_interface).write("234\n") end end end From 509d949d0f1064fd4b48a75700d1209c9dc02f8d Mon Sep 17 00:00:00 2001 From: Matt Scilipoti Date: Tue, 17 May 2011 12:25:44 -0400 Subject: [PATCH 2/2] stdio: extract simple_textilize --- lib/kidsruby/stdio.rb | 16 +++++++++++----- spec/models/std_io_spec.rb | 8 ++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/kidsruby/stdio.rb b/lib/kidsruby/stdio.rb index 4b25c49..e142543 100644 --- a/lib/kidsruby/stdio.rb +++ b/lib/kidsruby/stdio.rb @@ -8,19 +8,25 @@ def initialize(interface = InterfaceHelper.new.get_interface) 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/,"
") + end end class StdOut < KidsRubyStdIo - def write(data) - t = data.gsub(/\n/,"
") - interface.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/,"
") - interface.call("appendError", t) + interface.call("appendError", simple_textilize(data)) end end diff --git a/spec/models/std_io_spec.rb b/spec/models/std_io_spec.rb index 0f2d43c..8295e31 100644 --- a/spec/models/std_io_spec.rb +++ b/spec/models/std_io_spec.rb @@ -9,6 +9,14 @@ 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
2
c
" + KidsRubyStdIo.new.simple_textilize(input).must_equal expected + end + end end describe StdOut do