* test/stringio/test_stringio.rb: add tests to achieve over 95% test

coverage of stringio.

* test/strscan/test_stringscanner.rb: ditto for strscan.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16847 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2008-06-05 14:33:01 +00:00
Родитель cbbc06dcf7
Коммит 437af4f46f
3 изменённых файлов: 444 добавлений и 0 удалений

Просмотреть файл

@ -1,3 +1,10 @@
Thu Jun 5 23:31:21 2008 Yusuke Endoh <mame@tsg.ne.jp>
* test/stringio/test_stringio.rb: add tests to achieve over 95% test
coverage of stringio.
* test/strscan/test_stringscanner.rb: ditto for strscan.
Thu Jun 5 23:25:34 2008 Yusuke Endoh <mame@tsg.ne.jp>
* eval.c (ruby_finalize_0): clear trace_func after executing END

Просмотреть файл

@ -21,6 +21,9 @@ class TestStringIO < Test::Unit::TestCase
io.truncate(0)
io.puts "def"
assert_equal("\0\0\0\0def\n", io.string, "[ruby-dev:24190]")
assert_raise(Errno::EINVAL) { io.truncate(-1) }
io.truncate(10)
assert_equal("\0\0\0\0def\n\0\0", io.string)
end
def test_seek_beyond_eof
@ -65,4 +68,315 @@ class TestStringIO < Test::Unit::TestCase
assert_equal(["abc\n\n", "def\n"], StringIO.new("abc\n\ndef\n").readlines(""))
end
def test_write
s = ""
f = StringIO.new(s, "w")
f.print("foo")
f.close
assert_equal("foo", s)
f = StringIO.new(s, File::WRONLY)
f.print("bar")
f.close
assert_equal("bar", s)
f = StringIO.new(s, "a")
o = Object.new
def o.to_s; "baz"; end
f.print(o)
f.close
assert_equal("barbaz", s)
ensure
f.close unless f.closed?
end
def test_mode_error
f = StringIO.new("", "r")
assert_raise(IOError) { f.write("foo") }
f = StringIO.new("", "w")
assert_raise(IOError) { f.read }
assert_raise(Errno::EACCES) { StringIO.new("".freeze, "w") }
s = ""
f = StringIO.new(s, "w")
s.freeze
assert_raise(IOError) { f.write("foo") }
assert_raise(IOError) { StringIO.allocate.read }
ensure
f.close unless f.closed?
end
def test_open
s = ""
StringIO.open("foo") {|f| s = f.read }
assert_equal("foo", s)
end
def test_isatty
assert_equal(false, StringIO.new("").isatty)
end
def test_path
assert_equal(nil, StringIO.new("").path)
end
def test_fsync
assert_equal(0, StringIO.new("").fsync)
end
def test_sync
assert_equal(true, StringIO.new("").sync)
assert_equal(false, StringIO.new("").sync = false)
end
def test_set_fcntl
assert_raise(NotImplementedError) { StringIO.new("").fcntl }
end
def test_close
f = StringIO.new("")
f.close
assert_raise(IOError) { f.close }
f = StringIO.new("")
f.close_read
f.close_write
assert_raise(IOError) { f.close }
ensure
f.close unless f.closed?
end
def test_close_read
f = StringIO.new("")
f.close_read
assert_raise(IOError) { f.read }
assert_raise(IOError) { f.close_read }
f.close
f = StringIO.new("", "w")
assert_raise(IOError) { f.close_read }
f.close
ensure
f.close unless f.closed?
end
def test_close_write
f = StringIO.new("")
f.close_write
assert_raise(IOError) { f.write("foo") }
assert_raise(IOError) { f.close_write }
f.close
f = StringIO.new("", "r")
assert_raise(IOError) { f.close_write }
f.close
ensure
f.close unless f.closed?
end
def test_closed
f = StringIO.new("")
assert_equal(false, f.closed?)
f.close
assert_equal(true, f.closed?)
ensure
f.close unless f.closed?
end
def test_closed_read
f = StringIO.new("")
assert_equal(false, f.closed_read?)
f.close_write
assert_equal(false, f.closed_read?)
f.close_read
assert_equal(true, f.closed_read?)
ensure
f.close unless f.closed?
end
def test_closed_write
f = StringIO.new("")
assert_equal(false, f.closed_write?)
f.close_read
assert_equal(false, f.closed_write?)
f.close_write
assert_equal(true, f.closed_write?)
ensure
f.close unless f.closed?
end
def test_dup
f1 = StringIO.new("1234")
assert_equal("1", f1.getc)
f2 = f1.dup
assert_equal("2", f2.getc)
assert_equal("3", f1.getc)
assert_equal("4", f2.getc)
assert_equal(nil, f1.getc)
assert_equal(true, f2.eof?)
f1.close
assert_equal(true, f2.closed?)
ensure
f1.close unless f1.closed?
f2.close unless f2.closed?
end
def test_lineno
f = StringIO.new("foo\nbar\nbaz\n")
assert_equal([0, "foo\n"], [f.lineno, f.gets])
assert_equal([1, "bar\n"], [f.lineno, f.gets])
f.lineno = 1000
assert_equal([1000, "baz\n"], [f.lineno, f.gets])
assert_equal([1001, nil], [f.lineno, f.gets])
ensure
f.close unless f.closed?
end
def test_pos
f = StringIO.new("foo\nbar\nbaz\n")
assert_equal([0, "foo\n"], [f.pos, f.gets])
assert_equal([4, "bar\n"], [f.pos, f.gets])
assert_raise(Errno::EINVAL) { f.pos = -1 }
f.pos = 1
assert_equal([1, "oo\n"], [f.pos, f.gets])
assert_equal([4, "bar\n"], [f.pos, f.gets])
assert_equal([8, "baz\n"], [f.pos, f.gets])
assert_equal([12, nil], [f.pos, f.gets])
ensure
f.close unless f.closed?
end
def test_reopen
f = StringIO.new("foo\nbar\nbaz\n")
assert_equal("foo\n", f.gets)
f.reopen("qux\nquux\nquuux\n")
assert_equal("quux\n", f.gets)
f2 = StringIO.new("")
f2.reopen(f)
assert_equal("quuux\n", f2.gets)
ensure
f.close unless f.closed?
end
def test_seek
f = StringIO.new("1234")
assert_raise(Errno::EINVAL) { f.seek(-1) }
f.seek(-1, 2)
assert_equal("4", f.getc)
assert_raise(Errno::EINVAL) { f.seek(1, 3) }
f.close
assert_raise(IOError) { f.seek(0) }
ensure
f.close unless f.closed?
end
def test_each_byte
f = StringIO.new("1234")
a = []
f.each_byte {|c| a << c }
assert_equal(%w(1 2 3 4).map {|c| c.ord }, a)
ensure
f.close unless f.closed?
end
def test_getbyte
f = StringIO.new("1234")
assert_equal("1".ord, f.getbyte)
assert_equal("2".ord, f.getbyte)
assert_equal("3".ord, f.getbyte)
assert_equal("4".ord, f.getbyte)
assert_equal(nil, f.getbyte)
ensure
f.close unless f.closed?
end
def test_ungetc
s = "1234"
f = StringIO.new(s, "r")
assert_nothing_raised { f.ungetc("x") }
assert_equal("x", f.getc) # bug?
assert_equal("1", f.getc)
s = "1234"
f = StringIO.new(s, "r")
assert_equal("1", f.getc)
f.ungetc("y".ord)
assert_equal("y", f.getc)
assert_equal("2", f.getc)
ensure
f.close unless f.closed?
end
def test_readchar
f = StringIO.new("1234")
a = ""
assert_raise(EOFError) { loop { a << f.readchar } }
assert_equal("1234", a)
end
def test_readbyte
f = StringIO.new("1234")
a = []
assert_raise(EOFError) { loop { a << f.readbyte } }
assert_equal("1234".unpack("C*"), a)
end
def test_each_char
f = StringIO.new("1234")
assert_equal(%w(1 2 3 4), f.each_char.to_a)
end
def test_gets2
f = StringIO.new("foo\nbar\nbaz\n")
assert_equal("fo", f.gets(2))
o = Object.new
def o.to_str; "z"; end
assert_equal("o\nbar\nbaz", f.gets(o))
f = StringIO.new("foo\nbar\nbaz\n")
assert_equal("foo\nbar\nbaz", f.gets("az"))
f = StringIO.new("a" * 10000 + "zz!")
assert_equal("a" * 10000 + "zz", f.gets("zz"))
f = StringIO.new("a" * 10000 + "zz!")
assert_equal("a" * 10000 + "zz!", f.gets("zzz"))
end
def test_each
f = StringIO.new("foo\nbar\nbaz\n")
assert_equal(["foo\n", "bar\n", "baz\n"], f.each.to_a)
end
def test_putc
s = ""
f = StringIO.new(s, "w")
f.putc("1")
f.putc("2")
f.putc("3")
f.close
assert_equal("123", s)
s = "foo"
f = StringIO.new(s, "a")
f.putc("1")
f.putc("2")
f.putc("3")
f.close
assert_equal("foo123", s)
end
def test_read
f = StringIO.new("1234")
assert_raise(ArgumentError) { f.read(-1) }
assert_raise(ArgumentError) { f.read(1, 2, 3) }
end
def test_size
f = StringIO.new("1234")
assert_equal(4, f.size)
end
end

Просмотреть файл

@ -548,4 +548,127 @@ class TestStringScanner < Test::Unit::TestCase
assert_equal("\xa1\xa2".force_encoding("euc-jp"), t)
end
def test_set_pos
s = StringScanner.new("test string")
s.pos = 7
assert_equal("ring", s.rest)
end
def test_match_p
s = StringScanner.new("test string")
assert_equal(4, s.match?(/\w+/))
assert_equal(4, s.match?(/\w+/))
assert_equal(nil, s.match?(/\s+/))
end
def test_check
s = StringScanner.new("Foo Bar Baz")
assert_equal("Foo", s.check(/Foo/))
assert_equal(0, s.pos)
assert_equal("Foo", s.matched)
assert_equal(nil, s.check(/Bar/))
assert_equal(nil, s.matched)
end
def test_scan_full
s = StringScanner.new("Foo Bar Baz")
assert_equal(4, s.scan_full(/Foo /, false, false))
assert_equal(0, s.pos)
assert_equal(nil, s.scan_full(/Baz/, false, false))
assert_equal("Foo ", s.scan_full(/Foo /, false, true))
assert_equal(0, s.pos)
assert_equal(nil, s.scan_full(/Baz/, false, false))
assert_equal(4, s.scan_full(/Foo /, true, false))
assert_equal(4, s.pos)
assert_equal(nil, s.scan_full(/Baz /, false, false))
assert_equal("Bar ", s.scan_full(/Bar /, true, true))
assert_equal(8, s.pos)
assert_equal(nil, s.scan_full(/az/, false, false))
end
def test_exist_p
s = StringScanner.new("test string")
assert_equal(3, s.exist?(/s/))
assert_equal(0, s.pos)
s.scan(/test/)
assert_equal(2, s.exist?(/s/))
assert_equal(4, s.pos)
assert_equal(nil, s.exist?(/e/))
end
def test_skip_until
s = StringScanner.new("Foo Bar Baz")
assert_equal(3, s.skip_until(/Foo/))
assert_equal(3, s.pos)
assert_equal(4, s.skip_until(/Bar/))
assert_equal(7, s.pos)
assert_equal(nil, s.skip_until(/Qux/))
end
def test_check_until
s = StringScanner.new("Foo Bar Baz")
assert_equal("Foo", s.check_until(/Foo/))
assert_equal(0, s.pos)
assert_equal("Foo Bar", s.check_until(/Bar/))
assert_equal(0, s.pos)
assert_equal(nil, s.check_until(/Qux/))
end
def test_search_full
s = StringScanner.new("Foo Bar Baz")
assert_equal(8, s.search_full(/Bar /, false, false))
assert_equal(0, s.pos)
assert_equal("Foo Bar ", s.search_full(/Bar /, false, true))
assert_equal(0, s.pos)
assert_equal(8, s.search_full(/Bar /, true, false))
assert_equal(8, s.pos)
assert_equal("Baz", s.search_full(/az/, true, true))
assert_equal(11, s.pos)
end
def test_peek
s = StringScanner.new("test string")
assert_equal("test st", s.peek(7))
assert_equal("test st", s.peek(7))
s.scan(/test/)
assert_equal(" stri", s.peek(5))
assert_equal(" string", s.peek(10))
s.scan(/ string/)
assert_equal("", s.peek(10))
end
def test_unscan
s = StringScanner.new('test string')
assert_equal("test", s.scan(/\w+/))
s.unscan
assert_equal("te", s.scan(/../))
assert_equal(nil, s.scan(/\d/))
assert_raise(ScanError) { s.unscan }
end
def test_rest
s = StringScanner.new('test string')
assert_equal("test string", s.rest)
s.scan(/test/)
assert_equal(" string", s.rest)
s.scan(/ string/)
assert_equal("", s.rest)
s.scan(/ string/)
end
def test_rest_size
s = StringScanner.new('test string')
assert_equal(11, s.rest_size)
s.scan(/test/)
assert_equal(7, s.rest_size)
s.scan(/ string/)
assert_equal(0, s.rest_size)
s.scan(/ string/)
end
def test_inspect
s = StringScanner.new('test string test')
s.scan(/test strin/)
assert_equal('#<StringScanner 10/16 "...strin" @ "g tes...">', s.inspect)
end
end