added newline sugar to pipe library

This commit is contained in:
Nick Gauthier 2010-01-26 15:35:58 -05:00
parent b491f767a0
commit b85a565be0
2 changed files with 18 additions and 7 deletions

View File

@ -34,12 +34,15 @@ module Hydra #:nodoc:
# Read a line from a pipe. It will have a trailing newline.
def gets
force_identification
@reader.gets
@reader.gets.chomp
end
# Write a line to a pipe. It must have a trailing newline.
def write(str)
force_identification
unless str =~ /\n$/
str += "\n"
end
begin
@writer.write(str)
return str

View File

@ -8,15 +8,15 @@ class TestPipe < Test::Unit::TestCase
should "be able to write messages" do
Process.fork do
@pipe.identify_as_child
assert_equal "Test Message\n", @pipe.gets
@pipe.write "Message Received\n"
@pipe.write "Second Message\n"
assert_equal "Test Message", @pipe.gets
@pipe.write "Message Received"
@pipe.write "Second Message"
@pipe.close
end
@pipe.identify_as_parent
@pipe.write "Test Message\n"
assert_equal "Message Received\n", @pipe.gets
assert_equal "Second Message\n", @pipe.gets
@pipe.write "Test Message"
assert_equal "Message Received", @pipe.gets
assert_equal "Second Message", @pipe.gets
assert_raise Hydra::PipeError::Broken do
@pipe.write "anybody home?"
end
@ -32,5 +32,13 @@ class TestPipe < Test::Unit::TestCase
@pipe.gets
end
end
should "handle newlines" do
Process.fork do
@pipe.identify_as_child
@pipe.write "Message\n"
end
@pipe.identify_as_parent
assert_equal "Message", @pipe.gets
end
end
end