Hi,
I have a question.
How to change a stream of ‘print’ to a string? Is it able?
Background:
I want to test my program which print data to standard output.
And I have no good idea to test output data.
···
##
## main.rb
##
def print_list(list)
list.each_with_index do |item, index|
print "#{index+1}: #{item}\n"
end
end
----------------------------------------
----------------------------------------
##
## test program
##
require 'main'
require 'runit/testcase'
require 'runit/cui/testrunner'
class MainTest < RUNIT::TestCase
def test_print_list
list = [10, 20, 30]
print_list(list) # <== how to test?
end
end
testcases = MainTest.test_cases()
RUNIT::CUI::TestRunner.run(testcases)
----------------------------------------
If I could change a stream of print to a string, I would write it
like the following:
----------------------------------------
...(snip)...
def test_print_list
list = [10, 20, 30]
expected = <<END
1: 10
2: 20
3: 30
END
result = ''
result << print_list(list) # if I could
assert_equal(expected, result)
end
...(snip)...
----------------------------------------
Please give me an advice.
–
regards,
makotz kuwata
I’m not sure I understand your question, but the code you are trying to
write seems to work fine:
$ irb
result = “”
=> “”
result << “Hello”
=> “Hello”
Is this what you want?
···
On Wed, Mar 26, 2003 at 09:45:21AM +0900, makotz wrote:
Hi,
I have a question.
How to change a stream of ‘print’ to a string? Is it able?
Background:
I want to test my program which print data to standard output.
And I have no good idea to test output data.
----------------------------------------
##
## main.rb
##
def print_list(list)
list.each_with_index do |item, index|
print "#{index+1}: #{item}\n"
end
end
----------------------------------------
----------------------------------------
##
## test program
##
require 'main'
require 'runit/testcase'
require 'runit/cui/testrunner'
class MainTest < RUNIT::TestCase
def test_print_list
list = [10, 20, 30]
print_list(list) # <== how to test?
end
end
testcases = MainTest.test_cases()
RUNIT::CUI::TestRunner.run(testcases)
----------------------------------------
If I could change a stream of print to a string, I would write it
like the following:
----------------------------------------
...(snip)...
def test_print_list
list = [10, 20, 30]
expected = <<END
1: 10
2: 20
3: 30
END
result = ''
result << print_list(list) # if I could
assert_equal(expected, result)
end
...(snip)...
----------------------------------------
Please give me an advice.
–
regards,
makotz kuwata
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
en banc: en banc (ahn-BAHNK) adjective, adverb
Having all the judges of a court present in a hearing.
def print_list(list, out=STDOUT)
list.each_with_index do |item, index|
out.print “#{index+1}: #{item}\n”
end
end
In your test case:
require ‘stringio’
buf = “”
StringIO.open(buf, ‘w’) do |io|
print_list(list, io)
end
assert_equal(expected_result, buf)
I think I got that right.
Gavin
···
On Wednesday, March 26, 2003, 11:45:21 AM, makotz wrote:
I have a question.
How to change a stream of ‘print’ to a string? Is it able?
Background:
I want to test my program which print data to standard output.
And I have no good idea to test output data.
----------------------------------------
##
## main.rb
##
def print_list(list)
list.each_with_index do |item, index|
print "#{index+1}: #{item}\n"
end
end
----------------------------------------
Or you can just do
$defout = StringIO.open(…)
I think that should let everything work without changes, except putc under
1.6.8 (but then you don’t get stringio with 1.6.8 unless you install the
1.6->1.8 shim)
Regards,
Brian.
···
On Wed, Mar 26, 2003 at 09:58:11AM +0900, Gavin Sinclair wrote:
def print_list(list, out=STDOUT)
list.each_with_index do |item, index|
out.print “#{index+1}: #{item}\n”
end
end
In your test case:
require ‘stringio’
buf = “”
StringIO.open(buf, ‘w’) do |io|
print_list(list, io)
end
assert_equal(expected_result, buf)
Thank you, Gavin.
‘stringio’ is very useful. I didn’t know it.
It seemed to be a feature of Ruby 1.8, so I installed Ruby Shim.
http://raa.ruby-lang.org/list.rhtml?name=shim-ruby16_18
regards,
makotz
···
Gavin Sinclair gsinclair@soyabean.com.au wrote:
def print_list(list, out=STDOUT)
list.each_with_index do |item, index|
out.print “#{index+1}: #{item}\n”
end
end
In your test case:
require ‘stringio’
buf = “”
StringIO.open(buf, ‘w’) do |io|
print_list(list, io)
end
assert_equal(expected_result, buf)
I think I got that right.
Gavin