aboutsummaryrefslogtreecommitdiffstats
path: root/test/scanf/test_scanfblocks.rb
diff options
context:
space:
mode:
authordblack <dblack@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-03-01 12:24:42 +0000
committerdblack <dblack@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-03-01 12:24:42 +0000
commit933fb96a12978ca76cb2538a1daf19f0e968226e (patch)
treebae3eb03d241c5a22ecf00ed4bee4ea8da47ff80 /test/scanf/test_scanfblocks.rb
parentcac6f19906cd88161c306baeca66802c021c7735 (diff)
downloadruby-933fb96a12978ca76cb2538a1daf19f0e968226e.tar.gz
First commit of scanf test files
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/scanf/test_scanfblocks.rb')
-rw-r--r--test/scanf/test_scanfblocks.rb77
1 files changed, 77 insertions, 0 deletions
diff --git a/test/scanf/test_scanfblocks.rb b/test/scanf/test_scanfblocks.rb
new file mode 100644
index 0000000000..6b9945a159
--- /dev/null
+++ b/test/scanf/test_scanfblocks.rb
@@ -0,0 +1,77 @@
+# $Id$
+#
+# scanf for Ruby
+#
+# Some not very comprehensive tests of block behavior.
+
+
+$:.unshift("..")
+require 'test/unit'
+require 'scanf.rb'
+
+class TestMe < Test::Unit::TestCase
+
+ def setup
+ @str = <<-EOS
+ Beethoven 1770
+ Bach 1685
+ Handel 1685
+ Scarlatti 1685
+ Brahms 1833
+ EOS
+ end
+
+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
+ File.open("iotest.dat", "w") { |fh| fh.puts(@str) }
+ fh = File.open("iotest.dat", "rb")
+ 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
+# File.delete("iotest.dat")
+ end
+
+ def test_io2
+ File.open("iotest.dat", "w").close
+ fh = File.open("iotest.dat","rb")
+ assert_equal(fh.scanf("") {}, [])
+ fh.seek(0)
+ assert_equal(fh.scanf("%d%f%s") {}, [])
+ fh.close
+ # File.delete("iotest.dat")
+ end
+
+end