2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2004-03-01 15:24:42 +03:00
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# scanf for Ruby
|
|
|
|
#
|
|
|
|
# Some not very comprehensive tests of block behavior.
|
|
|
|
|
|
|
|
|
|
|
|
require 'test/unit'
|
2004-03-02 14:21:32 +03:00
|
|
|
require 'scanf'
|
2007-09-28 20:51:41 +04:00
|
|
|
require 'tmpdir'
|
2004-03-01 15:24:42 +03:00
|
|
|
|
2004-03-02 14:21:32 +03:00
|
|
|
class TestScanfBlock < Test::Unit::TestCase
|
2004-03-01 15:24:42 +03:00
|
|
|
|
|
|
|
def setup
|
|
|
|
@str = <<-EOS
|
|
|
|
Beethoven 1770
|
|
|
|
Bach 1685
|
|
|
|
Handel 1685
|
|
|
|
Scarlatti 1685
|
|
|
|
Brahms 1833
|
|
|
|
EOS
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2004-03-01 15:24:42 +03:00
|
|
|
alias set_up setup
|
|
|
|
def test_str1
|
|
|
|
res = @str.scanf("%s%d") { |name, year| "#{name} was born in #{year}." }
|
|
|
|
assert_equal(res,
|
|
|
|
[ "Beethoven was born in 1770.",
|
|
|
|
"Bach was born in 1685.",
|
|
|
|
"Handel was born in 1685.",
|
|
|
|
"Scarlatti was born in 1685.",
|
|
|
|
"Brahms was born in 1833." ])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str2
|
|
|
|
names = @str.scanf("%s%d") { |name, year| name.upcase }
|
|
|
|
assert_equal(names, ["BEETHOVEN", "BACH", "HANDEL", "SCARLATTI", "BRAHMS"])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str3
|
|
|
|
assert_equal("".scanf("%d%f%s") {}, [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str4
|
|
|
|
assert_equal("abc".scanf("%d%f%s") {}, [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_str5
|
|
|
|
assert_equal("abc".scanf("") {}, [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_io1
|
2007-09-28 22:22:23 +04:00
|
|
|
fn = "#{Dir.tmpdir}/iotest.dat.#{$$}"
|
|
|
|
File.open(fn, "w") { |fh| fh.puts(@str) }
|
|
|
|
fh = File.open(fn, "rb")
|
2004-03-01 15:24:42 +03:00
|
|
|
res = fh.scanf("%s%d") { |name, year| "#{name} was born in #{year}." }
|
|
|
|
|
|
|
|
assert_equal(
|
|
|
|
[ "Beethoven was born in 1770.",
|
|
|
|
"Bach was born in 1685.",
|
|
|
|
"Handel was born in 1685.",
|
|
|
|
"Scarlatti was born in 1685.",
|
|
|
|
"Brahms was born in 1833." ],res)
|
|
|
|
fh.close
|
2004-03-02 14:21:32 +03:00
|
|
|
ensure
|
2009-03-06 06:56:38 +03:00
|
|
|
File.delete(fn)
|
2007-09-28 22:22:23 +04:00
|
|
|
end
|
2004-03-01 15:24:42 +03:00
|
|
|
|
|
|
|
def test_io2
|
2007-09-28 22:22:23 +04:00
|
|
|
fn = "#{Dir.tmpdir}/iotest.dat.#{$$}"
|
|
|
|
File.open(fn, "w").close
|
|
|
|
fh = File.open(fn,"rb")
|
2004-03-01 15:24:42 +03:00
|
|
|
assert_equal(fh.scanf("") {}, [])
|
|
|
|
fh.seek(0)
|
|
|
|
assert_equal(fh.scanf("%d%f%s") {}, [])
|
|
|
|
fh.close
|
2004-03-02 14:21:32 +03:00
|
|
|
ensure
|
2009-03-06 06:56:38 +03:00
|
|
|
File.delete(fn)
|
2004-03-01 15:24:42 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|