Added more rdoc and changed the remaining tests to Test::Unit

This commit is contained in:
thomas 2005-02-19 20:30:33 +00:00
parent 6cf67cd4ab
commit adaaf8337c
10 changed files with 216 additions and 192 deletions

View File

@ -1,4 +1,4 @@
module IOExtras module IOExtras #:nodoc:
# Implements kind_of? in order to pretend to be an IO object # Implements kind_of? in order to pretend to be an IO object
module FakeIO module FakeIO

View File

@ -16,7 +16,7 @@ module Enumerable #:nodoc:all
end end
unless Object.method_defined?(:object_id) unless Object.method_defined?(:object_id)
class Object class Object #:nodoc:all
# Using object_id which is the new thing, so we need # Using object_id which is the new thing, so we need
# to make that work in versions prior to 1.8.0 # to make that work in versions prior to 1.8.0
alias object_id id alias object_id id
@ -32,7 +32,7 @@ unless File.respond_to?(:read)
end end
end end
class String class String #:nodoc:all
def starts_with(aString) def starts_with(aString)
rindex(aString, 0) == 0 rindex(aString, 0) == 0
end end
@ -50,7 +50,7 @@ class String
end end
end end
class Time class Time #:nodoc:all
#MS-DOS File Date and Time format as used in Interrupt 21H Function 57H: #MS-DOS File Date and Time format as used in Interrupt 21H Function 57H:
# #
@ -95,7 +95,7 @@ class Time
end end
end end
class Module class Module #:nodoc:all
def forward_message(forwarder, *messagesToForward) def forward_message(forwarder, *messagesToForward)
methodDefs = messagesToForward.map { methodDefs = messagesToForward.map {
|msg| |msg|

View File

@ -1,13 +1,13 @@
# #
# tempfile - manipulates temporary files # tempfile - manipulates temporary files
# #
# $Id: tempfile_bugfixed.rb,v 1.1 2005/02/15 21:10:32 thomas Exp $ # $Id: tempfile_bugfixed.rb,v 1.2 2005/02/19 20:30:33 thomas Exp $
# #
require 'delegate' require 'delegate'
require 'tmpdir' require 'tmpdir'
module BugFix module BugFix #:nodoc:all
# A class for managing temporary files. This library is written to be # A class for managing temporary files. This library is written to be
# thread safe. # thread safe.

View File

@ -11,7 +11,7 @@ if Tempfile.superclass == SimpleDelegator
Tempfile = BugFix::Tempfile Tempfile = BugFix::Tempfile
end end
module Zlib module Zlib #:nodoc:all
if ! const_defined? :MAX_WBITS if ! const_defined? :MAX_WBITS
MAX_WBITS = Zlib::Deflate.MAX_WBITS MAX_WBITS = Zlib::Deflate.MAX_WBITS
end end
@ -36,12 +36,12 @@ module Zip
# ZipFileSystem interface) object for a particular entry in the zip # ZipFileSystem interface) object for a particular entry in the zip
# archive. # archive.
# #
# A ZipInputStream inherits AbstractInputStream in order to provide an # A ZipInputStream inherits IOExtras::AbstractInputStream in order
# IO-like interface for reading from a single zip entry. Beyond methods # to provide an IO-like interface for reading from a single zip
# for mimicking an IO-object it contains the method get_next_entry for # entry. Beyond methods for mimicking an IO-object it contains
# iterating through the entries of an archive. get_next_entry returns a # the method get_next_entry for iterating through the entries of
# ZipEntry object that describes the zip entry the ZipInputStream is # an archive. get_next_entry returns a ZipEntry object that describes
# currently reading from. # the zip entry the ZipInputStream is currently reading from.
# #
# Example that creates a zip archive with ZipOutputStream and reads it # Example that creates a zip archive with ZipOutputStream and reads it
# back again with a ZipInputStream. # back again with a ZipInputStream.
@ -73,6 +73,9 @@ module Zip
class ZipInputStream class ZipInputStream
include IOExtras::AbstractInputStream include IOExtras::AbstractInputStream
# Opens the indicated zip file. An exception is thrown
# if the specified offset in the specified filename is
# not a local zip entry header.
def initialize(filename, offset = 0) def initialize(filename, offset = 0)
super() super()
@archiveIO = File.open(filename, "rb") @archiveIO = File.open(filename, "rb")
@ -84,7 +87,10 @@ module Zip
def close def close
@archiveIO.close @archiveIO.close
end end
# Same as #initialize but if a block is passed the opened
# stream is passed to the block and closed when the block
# returns.
def ZipInputStream.open(filename) def ZipInputStream.open(filename)
return new(filename) unless block_given? return new(filename) unless block_given?
@ -94,12 +100,17 @@ module Zip
zio.close if zio zio.close if zio
end end
# Returns a ZipEntry object. It is necessary to call this
# method on a newly created ZipInputStream before reading from
# the first entry in the archive. Returns nil when there are
# no more entries.
def get_next_entry def get_next_entry
@archiveIO.seek(@currentEntry.next_header_offset, @archiveIO.seek(@currentEntry.next_header_offset,
IO::SEEK_SET) if @currentEntry IO::SEEK_SET) if @currentEntry
open_entry open_entry
end end
# Rewinds the stream to the beginning of the current entry
def rewind def rewind
return if @currentEntry.nil? return if @currentEntry.nil?
@lineno = 0 @lineno = 0
@ -108,6 +119,13 @@ module Zip
open_entry open_entry
end end
# Modeled after IO.read
def read(numberOfBytes = nil)
@decompressor.read(numberOfBytes)
end
protected
def open_entry def open_entry
@currentEntry = ZipEntry.read_local_entry(@archiveIO) @currentEntry = ZipEntry.read_local_entry(@archiveIO)
if (@currentEntry == nil) if (@currentEntry == nil)
@ -125,10 +143,6 @@ module Zip
return @currentEntry return @currentEntry
end end
def read(numberOfBytes = nil)
@decompressor.read(numberOfBytes)
end
protected
def produce_input def produce_input
@decompressor.produce_input @decompressor.produce_input
end end
@ -549,16 +563,17 @@ module Zip
# ZipOutputStream is the basic class for writing zip files. It is # ZipOutputStream is the basic class for writing zip files. It is
# possible to create a ZipInputStream object directly, passing # possible to create a ZipOutputStream object directly, passing
# the zip file name to the constructor, but more often than not # the zip file name to the constructor, but more often than not
# the ZipOutputStream will be obtained from a ZipFile (perhaps using the # the ZipOutputStream will be obtained from a ZipFile (perhaps using the
# ZipFileSystem interface) object for a particular entry in the zip # ZipFileSystem interface) object for a particular entry in the zip
# archive. # archive.
# #
# A ZipOutputStream inherits AbstractOutputStream in order to provide an # A ZipOutputStream inherits IOExtras::AbstractOutputStream in order
# IO-like interface for writing to a single zip entry. Beyond methods # to provide an IO-like interface for writing to a single zip
# for mimicking an IO-object it contains the method put_next_entry that # entry. Beyond methods for mimicking an IO-object it contains
# closes the current entry and creates a new. # the method put_next_entry that closes the current entry
# and creates a new.
# #
# Please refer to ZipInputStream for example code. # Please refer to ZipInputStream for example code.
# #
@ -570,6 +585,8 @@ module Zip
attr_accessor :comment attr_accessor :comment
# Opens the indicated zip file. If a file with that name already
# exists it will be overwritten.
def initialize(fileName) def initialize(fileName)
super() super()
@fileName = fileName @fileName = fileName
@ -581,6 +598,9 @@ module Zip
@comment = nil @comment = nil
end end
# Same as #initialize but if a block is passed the opened
# stream is passed to the block and closed when the block
# returns.
def ZipOutputStream.open(fileName) def ZipOutputStream.open(fileName)
return new(fileName) unless block_given? return new(fileName) unless block_given?
zos = new(fileName) zos = new(fileName)
@ -589,6 +609,7 @@ module Zip
zos.close if zos zos.close if zos
end end
# Closes the stream and writes the central directory to the zip file
def close def close
return if @closed return if @closed
finalize_current_entry finalize_current_entry
@ -598,6 +619,8 @@ module Zip
@closed = true @closed = true
end end
# Closes the current entry and opens a new for writing.
# +entry+ can be a ZipEntry object or a string.
def put_next_entry(entry, level = Zlib::DEFAULT_COMPRESSION) def put_next_entry(entry, level = Zlib::DEFAULT_COMPRESSION)
raise ZipError, "zip stream is closed" if @closed raise ZipError, "zip stream is closed" if @closed
newEntry = entry.kind_of?(ZipEntry) ? entry : ZipEntry.new(@fileName, entry.to_s) newEntry = entry.kind_of?(ZipEntry) ? entry : ZipEntry.new(@fileName, entry.to_s)
@ -673,6 +696,7 @@ module Zip
end end
public public
# Modeled after IO.<<
def << (data) def << (data)
@compressor << data @compressor << data
end end
@ -738,7 +762,7 @@ module Zip
end end
class ZipEntrySet class ZipEntrySet #:nodoc:all
include Enumerable include Enumerable
def initialize(anEnumerable = []) def initialize(anEnumerable = [])

View File

@ -1,6 +1,6 @@
require 'zip/zip' require 'zip/zip'
class ZipList class ZipList #:nodoc:all
def initialize(zipFileList) def initialize(zipFileList)
@zipFileList = zipFileList @zipFileList = zipFileList
end end
@ -23,7 +23,7 @@ class ZipList
end end
module Kernel module Kernel #:nodoc:all
alias :oldRequire :require alias :oldRequire :require
def require(moduleName) def require(moduleName)

View File

@ -4,12 +4,12 @@ $VERBOSE = true
$: << "../lib" $: << "../lib"
require 'rubyunit' require 'test/unit'
require 'zip/ioextras' require 'zip/ioextras'
include IOExtras include IOExtras
class FakeIOTest < RUNIT::TestCase class FakeIOTest < Test::Unit::TestCase
class FakeIOUsingClass class FakeIOUsingClass
include FakeIO include FakeIO
end end
@ -25,7 +25,7 @@ class FakeIOTest < RUNIT::TestCase
end end
end end
class AbstractInputStreamTest < RUNIT::TestCase class AbstractInputStreamTest < Test::Unit::TestCase
# AbstractInputStream subclass that provides a read method # AbstractInputStream subclass that provides a read method
TEST_LINES = [ "Hello world#{$/}", TEST_LINES = [ "Hello world#{$/}",
@ -60,32 +60,32 @@ class AbstractInputStreamTest < RUNIT::TestCase
end end
def test_gets def test_gets
assert_equals(TEST_LINES[0], @io.gets) assert_equal(TEST_LINES[0], @io.gets)
assert_equals(1, @io.lineno) assert_equal(1, @io.lineno)
assert_equals(TEST_LINES[1], @io.gets) assert_equal(TEST_LINES[1], @io.gets)
assert_equals(2, @io.lineno) assert_equal(2, @io.lineno)
assert_equals(TEST_LINES[2], @io.gets) assert_equal(TEST_LINES[2], @io.gets)
assert_equals(3, @io.lineno) assert_equal(3, @io.lineno)
assert_equals(nil, @io.gets) assert_equal(nil, @io.gets)
assert_equals(4, @io.lineno) assert_equal(4, @io.lineno)
end end
def test_getsMultiCharSeperator def test_getsMultiCharSeperator
assert_equals("Hell", @io.gets("ll")) assert_equal("Hell", @io.gets("ll"))
assert_equals("o world#{$/}this is the second l", @io.gets("d l")) assert_equal("o world#{$/}this is the second l", @io.gets("d l"))
end end
def test_each_line def test_each_line
lineNumber=0 lineNumber=0
@io.each_line { @io.each_line {
|line| |line|
assert_equals(TEST_LINES[lineNumber], line) assert_equal(TEST_LINES[lineNumber], line)
lineNumber+=1 lineNumber+=1
} }
end end
def test_readlines def test_readlines
assert_equals(TEST_LINES, @io.readlines) assert_equal(TEST_LINES, @io.readlines)
end end
def test_readline def test_readline
@ -98,7 +98,7 @@ class AbstractInputStreamTest < RUNIT::TestCase
end end
end end
class AbstractOutputStreamTest < RUNIT::TestCase class AbstractOutputStreamTest < Test::Unit::TestCase
class TestOutputStream class TestOutputStream
include AbstractOutputStream include AbstractOutputStream
@ -128,77 +128,77 @@ class AbstractOutputStreamTest < RUNIT::TestCase
def test_write def test_write
count = @outputStream.write("a little string") count = @outputStream.write("a little string")
assert_equals("a little string", @outputStream.buffer) assert_equal("a little string", @outputStream.buffer)
assert_equals("a little string".length, count) assert_equal("a little string".length, count)
count = @outputStream.write(". a little more") count = @outputStream.write(". a little more")
assert_equals("a little string. a little more", @outputStream.buffer) assert_equal("a little string. a little more", @outputStream.buffer)
assert_equals(". a little more".length, count) assert_equal(". a little more".length, count)
end end
def test_print def test_print
$\ = nil # record separator set to nil $\ = nil # record separator set to nil
@outputStream.print("hello") @outputStream.print("hello")
assert_equals("hello", @outputStream.buffer) assert_equal("hello", @outputStream.buffer)
@outputStream.print(" world.") @outputStream.print(" world.")
assert_equals("hello world.", @outputStream.buffer) assert_equal("hello world.", @outputStream.buffer)
@outputStream.print(" You ok ", "out ", "there?") @outputStream.print(" You ok ", "out ", "there?")
assert_equals("hello world. You ok out there?", @outputStream.buffer) assert_equal("hello world. You ok out there?", @outputStream.buffer)
$\ = "\n" $\ = "\n"
@outputStream.print @outputStream.print
assert_equals("hello world. You ok out there?\n", @outputStream.buffer) assert_equal("hello world. You ok out there?\n", @outputStream.buffer)
@outputStream.print("I sure hope so!") @outputStream.print("I sure hope so!")
assert_equals("hello world. You ok out there?\nI sure hope so!\n", @outputStream.buffer) assert_equal("hello world. You ok out there?\nI sure hope so!\n", @outputStream.buffer)
$, = "X" $, = "X"
@outputStream.buffer = "" @outputStream.buffer = ""
@outputStream.print("monkey", "duck", "zebra") @outputStream.print("monkey", "duck", "zebra")
assert_equals("monkeyXduckXzebra\n", @outputStream.buffer) assert_equal("monkeyXduckXzebra\n", @outputStream.buffer)
$\ = nil $\ = nil
@outputStream.buffer = "" @outputStream.buffer = ""
@outputStream.print(20) @outputStream.print(20)
assert_equals("20", @outputStream.buffer) assert_equal("20", @outputStream.buffer)
end end
def test_printf def test_printf
@outputStream.printf("%d %04x", 123, 123) @outputStream.printf("%d %04x", 123, 123)
assert_equals("123 007b", @outputStream.buffer) assert_equal("123 007b", @outputStream.buffer)
end end
def test_putc def test_putc
@outputStream.putc("A") @outputStream.putc("A")
assert_equals("A", @outputStream.buffer) assert_equal("A", @outputStream.buffer)
@outputStream.putc(65) @outputStream.putc(65)
assert_equals("AA", @outputStream.buffer) assert_equal("AA", @outputStream.buffer)
end end
def test_puts def test_puts
@outputStream.puts @outputStream.puts
assert_equals("\n", @outputStream.buffer) assert_equal("\n", @outputStream.buffer)
@outputStream.puts("hello", "world") @outputStream.puts("hello", "world")
assert_equals("\nhello\nworld\n", @outputStream.buffer) assert_equal("\nhello\nworld\n", @outputStream.buffer)
@outputStream.buffer = "" @outputStream.buffer = ""
@outputStream.puts("hello\n", "world\n") @outputStream.puts("hello\n", "world\n")
assert_equals("hello\nworld\n", @outputStream.buffer) assert_equal("hello\nworld\n", @outputStream.buffer)
@outputStream.buffer = "" @outputStream.buffer = ""
@outputStream.puts(["hello\n", "world\n"]) @outputStream.puts(["hello\n", "world\n"])
assert_equals("hello\nworld\n", @outputStream.buffer) assert_equal("hello\nworld\n", @outputStream.buffer)
@outputStream.buffer = "" @outputStream.buffer = ""
@outputStream.puts(["hello\n", "world\n"], "bingo") @outputStream.puts(["hello\n", "world\n"], "bingo")
assert_equals("hello\nworld\nbingo\n", @outputStream.buffer) assert_equal("hello\nworld\nbingo\n", @outputStream.buffer)
@outputStream.buffer = "" @outputStream.buffer = ""
@outputStream.puts(16, 20, 50, "hello") @outputStream.puts(16, 20, 50, "hello")
assert_equals("16\n20\n50\nhello\n", @outputStream.buffer) assert_equal("16\n20\n50\nhello\n", @outputStream.buffer)
end end
end end

View File

@ -4,18 +4,18 @@ $VERBOSE = true
$: << "../lib" $: << "../lib"
require 'rubyunit' require 'test/unit'
require 'zip/stdrubyext' require 'zip/stdrubyext'
class ModuleTest < RUNIT::TestCase class ModuleTest < Test::Unit::TestCase
def test_select_map def test_select_map
assert_equals([2, 4, 8, 10], [1, 2, 3, 4, 5].select_map { |e| e == 3 ? nil : 2*e }) assert_equal([2, 4, 8, 10], [1, 2, 3, 4, 5].select_map { |e| e == 3 ? nil : 2*e })
end end
end end
class StringExtensionsTest < RUNIT::TestCase class StringExtensionsTest < Test::Unit::TestCase
def test_starts_with def test_starts_with
assert("hello".starts_with("")) assert("hello".starts_with(""))
@ -24,7 +24,7 @@ class StringExtensionsTest < RUNIT::TestCase
assert(! "hello".starts_with("hello there")) assert(! "hello".starts_with("hello there"))
assert(! "hello".starts_with(" he")) assert(! "hello".starts_with(" he"))
assert_exception(TypeError, "type mismatch: NilClass given") { assert_raise(TypeError, "type mismatch: NilClass given") {
"hello".starts_with(nil) "hello".starts_with(nil)
} }
end end
@ -40,10 +40,10 @@ class StringExtensionsTest < RUNIT::TestCase
end end
def test_ensure_end def test_ensure_end
assert_equals("hello!", "hello!".ensure_end("!")) assert_equal("hello!", "hello!".ensure_end("!"))
assert_equals("hello!", "hello!".ensure_end("o!")) assert_equal("hello!", "hello!".ensure_end("o!"))
assert_equals("hello!", "hello".ensure_end("!")) assert_equal("hello!", "hello".ensure_end("!"))
assert_equals("hello!", "hel".ensure_end("lo!")) assert_equal("hello!", "hel".ensure_end("lo!"))
end end
end end

View File

@ -5,7 +5,7 @@ $VERBOSE = true
$: << "../lib" $: << "../lib"
require 'zip/zipfilesystem' require 'zip/zipfilesystem'
require 'rubyunit' require 'test/unit'
module ExtraAssertions module ExtraAssertions
@ -20,8 +20,8 @@ module ExtraAssertions
end end
end_eval end_eval
assert_equals(retVal, yield) # Invoke test assert_equal(retVal, yield) # Invoke test
assert_equals(expectedArgs, callArgs) assert_equal(expectedArgs, callArgs)
ensure ensure
anObject.instance_eval "alias #{method} #{method}_org" anObject.instance_eval "alias #{method} #{method}_org"
end end
@ -30,7 +30,7 @@ end
include Zip include Zip
class ZipFsFileNonmutatingTest < RUNIT::TestCase class ZipFsFileNonmutatingTest < Test::Unit::TestCase
def setup def setup
@zipFile = ZipFile.new("data/zipWithDirs.zip") @zipFile = ZipFile.new("data/zipWithDirs.zip")
end end
@ -40,7 +40,7 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
end end
def test_umask def test_umask
assert_equals(File.umask, @zipFile.file.umask) assert_equal(File.umask, @zipFile.file.umask)
@zipFile.file.umask(0006) @zipFile.file.umask(0006)
end end
@ -62,7 +62,7 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
@zipFile.file.open("file1", "r") { @zipFile.file.open("file1", "r") {
|f| |f|
blockCalled = true blockCalled = true
assert_equals("this is the entry 'file1' in my test archive!", assert_equal("this is the entry 'file1' in my test archive!",
f.readline.chomp) f.readline.chomp)
} }
assert(blockCalled) assert(blockCalled)
@ -72,19 +72,19 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
@zipFile.file.open("file21", "r") { @zipFile.file.open("file21", "r") {
|f| |f|
blockCalled = true blockCalled = true
assert_equals("this is the entry 'dir2/file21' in my test archive!", assert_equal("this is the entry 'dir2/file21' in my test archive!",
f.readline.chomp) f.readline.chomp)
} }
assert(blockCalled) assert(blockCalled)
@zipFile.dir.chdir "/" @zipFile.dir.chdir "/"
assert_exception(Errno::ENOENT) { assert_raise(Errno::ENOENT) {
@zipFile.file.open("noSuchEntry") @zipFile.file.open("noSuchEntry")
} }
begin begin
is = @zipFile.file.open("file1") is = @zipFile.file.open("file1")
assert_equals("this is the entry 'file1' in my test archive!", assert_equal("this is the entry 'file1' in my test archive!",
is.readline.chomp) is.readline.chomp)
ensure ensure
is.close if is is.close if is
@ -94,7 +94,7 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
def test_new def test_new
begin begin
is = @zipFile.file.new("file1") is = @zipFile.file.new("file1")
assert_equals("this is the entry 'file1' in my test archive!", assert_equal("this is the entry 'file1' in my test archive!",
is.readline.chomp) is.readline.chomp)
ensure ensure
is.close if is is.close if is
@ -109,27 +109,27 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
end end
def test_symlink def test_symlink
assert_exception(NotImplementedError) { assert_raise(NotImplementedError) {
@zipFile.file.symlink("file1", "aSymlink") @zipFile.file.symlink("file1", "aSymlink")
} }
end end
def test_size def test_size
assert_exception(Errno::ENOENT) { @zipFile.file.size("notAFile") } assert_raise(Errno::ENOENT) { @zipFile.file.size("notAFile") }
assert_equals(72, @zipFile.file.size("file1")) assert_equal(72, @zipFile.file.size("file1"))
assert_equals(0, @zipFile.file.size("dir2/dir21")) assert_equal(0, @zipFile.file.size("dir2/dir21"))
assert_equals(72, @zipFile.file.stat("file1").size) assert_equal(72, @zipFile.file.stat("file1").size)
assert_equals(0, @zipFile.file.stat("dir2/dir21").size) assert_equal(0, @zipFile.file.stat("dir2/dir21").size)
end end
def test_size? def test_size?
assert_equals(nil, @zipFile.file.size?("notAFile")) assert_equal(nil, @zipFile.file.size?("notAFile"))
assert_equals(72, @zipFile.file.size?("file1")) assert_equal(72, @zipFile.file.size?("file1"))
assert_equals(nil, @zipFile.file.size?("dir2/dir21")) assert_equal(nil, @zipFile.file.size?("dir2/dir21"))
assert_equals(72, @zipFile.file.stat("file1").size?) assert_equal(72, @zipFile.file.stat("file1").size?)
assert_equals(nil, @zipFile.file.stat("dir2/dir21").size?) assert_equal(nil, @zipFile.file.stat("dir2/dir21").size?)
end end
@ -166,19 +166,19 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
end end
def test_join def test_join
assert_equals("a/b/c", @zipFile.file.join("a/b", "c")) assert_equal("a/b/c", @zipFile.file.join("a/b", "c"))
assert_equals("a/b/c/d", @zipFile.file.join("a/b", "c/d")) assert_equal("a/b/c/d", @zipFile.file.join("a/b", "c/d"))
assert_equals("/c/d", @zipFile.file.join("", "c/d")) assert_equal("/c/d", @zipFile.file.join("", "c/d"))
assert_equals("a/b/c/d", @zipFile.file.join("a", "b", "c", "d")) assert_equal("a/b/c/d", @zipFile.file.join("a", "b", "c", "d"))
end end
def test_utime def test_utime
t_now = Time.now t_now = Time.now
t_bak = @zipFile.file.mtime("file1") t_bak = @zipFile.file.mtime("file1")
@zipFile.file.utime(t_now, "file1") @zipFile.file.utime(t_now, "file1")
assert_equals(t_now, @zipFile.file.mtime("file1")) assert_equal(t_now, @zipFile.file.mtime("file1"))
@zipFile.file.utime(t_bak, "file1") @zipFile.file.utime(t_bak, "file1")
assert_equals(t_bak, @zipFile.file.mtime("file1")) assert_equal(t_bak, @zipFile.file.mtime("file1"))
end end
@ -219,26 +219,26 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
end end
def test_truncate def test_truncate
assert_exception(StandardError, "truncate not supported") { assert_raise(StandardError, "truncate not supported") {
@zipFile.file.truncate("file1", 100) @zipFile.file.truncate("file1", 100)
} }
end end
def assert_e_n_o_e_n_t(operation, args = ["NoSuchFile"]) def assert_e_n_o_e_n_t(operation, args = ["NoSuchFile"])
assert_exception(Errno::ENOENT) { assert_raise(Errno::ENOENT) {
@zipFile.file.send(operation, *args) @zipFile.file.send(operation, *args)
} }
end end
def test_ftype def test_ftype
assert_e_n_o_e_n_t(:ftype) assert_e_n_o_e_n_t(:ftype)
assert_equals("file", @zipFile.file.ftype("file1")) assert_equal("file", @zipFile.file.ftype("file1"))
assert_equals("directory", @zipFile.file.ftype("dir1/dir11")) assert_equal("directory", @zipFile.file.ftype("dir1/dir11"))
assert_equals("directory", @zipFile.file.ftype("dir1/dir11/")) assert_equal("directory", @zipFile.file.ftype("dir1/dir11/"))
end end
def test_link def test_link
assert_exception(NotImplementedError) { assert_raise(NotImplementedError) {
@zipFile.file.link("file1", "someOtherString") @zipFile.file.link("file1", "someOtherString")
} }
end end
@ -259,10 +259,10 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
end end
def test_chown def test_chown
assert_equals(2, @zipFile.file.chown(1,2, "dir1", "file1")) assert_equal(2, @zipFile.file.chown(1,2, "dir1", "file1"))
assert_equals(1, @zipFile.file.stat("dir1").uid) assert_equal(1, @zipFile.file.stat("dir1").uid)
assert_equals(2, @zipFile.file.stat("dir1").gid) assert_equal(2, @zipFile.file.stat("dir1").gid)
assert_equals(2, @zipFile.file.chown(nil, nil, "dir1", "file1")) assert_equal(2, @zipFile.file.chown(nil, nil, "dir1", "file1"))
end end
def test_zero? def test_zero?
@ -291,27 +291,27 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
def test_expand_path def test_expand_path
ZipFile.open("data/zipWithDirs.zip") { ZipFile.open("data/zipWithDirs.zip") {
|zf| |zf|
assert_equals("/", zf.file.expand_path(".")) assert_equal("/", zf.file.expand_path("."))
zf.dir.chdir "dir1" zf.dir.chdir "dir1"
assert_equals("/dir1", zf.file.expand_path(".")) assert_equal("/dir1", zf.file.expand_path("."))
assert_equals("/dir1/file12", zf.file.expand_path("file12")) assert_equal("/dir1/file12", zf.file.expand_path("file12"))
assert_equals("/", zf.file.expand_path("..")) assert_equal("/", zf.file.expand_path(".."))
assert_equals("/dir2/dir21", zf.file.expand_path("../dir2/dir21")) assert_equal("/dir2/dir21", zf.file.expand_path("../dir2/dir21"))
} }
end end
def test_mtime def test_mtime
assert_equals(Time.at(1027694306), assert_equal(Time.at(1027694306),
@zipFile.file.mtime("dir2/file21")) @zipFile.file.mtime("dir2/file21"))
assert_equals(Time.at(1027690863), assert_equal(Time.at(1027690863),
@zipFile.file.mtime("dir2/dir21")) @zipFile.file.mtime("dir2/dir21"))
assert_exception(Errno::ENOENT) { assert_raise(Errno::ENOENT) {
@zipFile.file.mtime("noSuchEntry") @zipFile.file.mtime("noSuchEntry")
} }
assert_equals(Time.at(1027694306), assert_equal(Time.at(1027694306),
@zipFile.file.stat("dir2/file21").mtime) @zipFile.file.stat("dir2/file21").mtime)
assert_equals(Time.at(1027690863), assert_equal(Time.at(1027690863),
@zipFile.file.stat("dir2/dir21").mtime) @zipFile.file.stat("dir2/dir21").mtime)
end end
@ -394,7 +394,7 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
end end
def test_readlink def test_readlink
assert_exception(NotImplementedError) { assert_raise(NotImplementedError) {
@zipFile.file.readlink("someString") @zipFile.file.readlink("someString")
} }
end end
@ -402,7 +402,7 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
def test_stat def test_stat
s = @zipFile.file.stat("file1") s = @zipFile.file.stat("file1")
assert(s.kind_of?(File::Stat)) # It pretends assert(s.kind_of?(File::Stat)) # It pretends
assert_exception(Errno::ENOENT, "No such file or directory - noSuchFile") { assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
@zipFile.file.stat("noSuchFile") @zipFile.file.stat("noSuchFile")
} }
end end
@ -413,14 +413,14 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
def test_chmod def test_chmod
assert_exception(Errno::ENOENT, "No such file or directory - noSuchFile") { assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
@zipFile.file.chmod(0644, "file1", "NoSuchFile") @zipFile.file.chmod(0644, "file1", "NoSuchFile")
} }
assert_equals(2, @zipFile.file.chmod(0644, "file1", "dir1")) assert_equal(2, @zipFile.file.chmod(0644, "file1", "dir1"))
end end
def test_pipe def test_pipe
assert_exception(NotImplementedError) { assert_raise(NotImplementedError) {
@zipFile.file.pipe @zipFile.file.pipe
} }
end end
@ -434,10 +434,10 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
index = 0 index = 0
zf.file.foreach("data/file1.txt") { zf.file.foreach("data/file1.txt") {
|l| |l|
assert_equals(ref[index], l) assert_equal(ref[index], l)
index = index.next index = index.next
} }
assert_equals(ref.size, index) assert_equal(ref.size, index)
} }
ZipFile.open("data/generated/zipWithDir.zip") { ZipFile.open("data/generated/zipWithDir.zip") {
@ -448,15 +448,15 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
index = 0 index = 0
zf.file.foreach("data/file1.txt", " ") { zf.file.foreach("data/file1.txt", " ") {
|l| |l|
assert_equals(ref[index], l) assert_equal(ref[index], l)
index = index.next index = index.next
} }
assert_equals(ref.size, index) assert_equal(ref.size, index)
} }
end end
def test_popen def test_popen
assert_equals(File.popen("ls") { |f| f.read }, assert_equal(File.popen("ls") { |f| f.read },
@zipFile.file.popen("ls") { |f| f.read }) @zipFile.file.popen("ls") { |f| f.read })
end end
@ -468,7 +468,7 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
def test_readlines def test_readlines
ZipFile.open("data/generated/zipWithDir.zip") { ZipFile.open("data/generated/zipWithDir.zip") {
|zf| |zf|
assert_equals(File.readlines("data/file1.txt"), assert_equal(File.readlines("data/file1.txt"),
zf.file.readlines("data/file1.txt")) zf.file.readlines("data/file1.txt"))
} }
end end
@ -476,14 +476,14 @@ class ZipFsFileNonmutatingTest < RUNIT::TestCase
def test_read def test_read
ZipFile.open("data/generated/zipWithDir.zip") { ZipFile.open("data/generated/zipWithDir.zip") {
|zf| |zf|
assert_equals(File.read("data/file1.txt"), assert_equal(File.read("data/file1.txt"),
zf.file.read("data/file1.txt")) zf.file.read("data/file1.txt"))
} }
end end
end end
class ZipFsFileStatTest < RUNIT::TestCase class ZipFsFileStatTest < Test::Unit::TestCase
def setup def setup
@zipFile = ZipFile.new("data/zipWithDirs.zip") @zipFile = ZipFile.new("data/zipWithDirs.zip")
@ -494,51 +494,51 @@ class ZipFsFileStatTest < RUNIT::TestCase
end end
def test_blocks def test_blocks
assert_equals(nil, @zipFile.file.stat("file1").blocks) assert_equal(nil, @zipFile.file.stat("file1").blocks)
end end
def test_ino def test_ino
assert_equals(0, @zipFile.file.stat("file1").ino) assert_equal(0, @zipFile.file.stat("file1").ino)
end end
def test_uid def test_uid
assert_equals(0, @zipFile.file.stat("file1").uid) assert_equal(0, @zipFile.file.stat("file1").uid)
end end
def test_gid def test_gid
assert_equals(0, @zipFile.file.stat("file1").gid) assert_equal(0, @zipFile.file.stat("file1").gid)
end end
def test_ftype def test_ftype
assert_equals("file", @zipFile.file.stat("file1").ftype) assert_equal("file", @zipFile.file.stat("file1").ftype)
assert_equals("directory", @zipFile.file.stat("dir1").ftype) assert_equal("directory", @zipFile.file.stat("dir1").ftype)
end end
def test_mode def test_mode
assert_equals(0600, @zipFile.file.stat("file1").mode & 0777) assert_equal(0600, @zipFile.file.stat("file1").mode & 0777)
assert_equals(0600, @zipFile.file.stat("file1").mode & 0777) assert_equal(0600, @zipFile.file.stat("file1").mode & 0777)
assert_equals(0755, @zipFile.file.stat("dir1").mode & 0777) assert_equal(0755, @zipFile.file.stat("dir1").mode & 0777)
assert_equals(0755, @zipFile.file.stat("dir1").mode & 0777) assert_equal(0755, @zipFile.file.stat("dir1").mode & 0777)
end end
def test_dev def test_dev
assert_equals(0, @zipFile.file.stat("file1").dev) assert_equal(0, @zipFile.file.stat("file1").dev)
end end
def test_rdev def test_rdev
assert_equals(0, @zipFile.file.stat("file1").rdev) assert_equal(0, @zipFile.file.stat("file1").rdev)
end end
def test_rdev_major def test_rdev_major
assert_equals(0, @zipFile.file.stat("file1").rdev_major) assert_equal(0, @zipFile.file.stat("file1").rdev_major)
end end
def test_rdev_minor def test_rdev_minor
assert_equals(0, @zipFile.file.stat("file1").rdev_minor) assert_equal(0, @zipFile.file.stat("file1").rdev_minor)
end end
def test_nlink def test_nlink
assert_equals(1, @zipFile.file.stat("file1").nlink) assert_equal(1, @zipFile.file.stat("file1").nlink)
end end
def test_blksize def test_blksize
@ -547,7 +547,7 @@ class ZipFsFileStatTest < RUNIT::TestCase
end end
class ZipFsFileMutatingTest < RUNIT::TestCase class ZipFsFileMutatingTest < Test::Unit::TestCase
TEST_ZIP = "zipWithDirs_copy.zip" TEST_ZIP = "zipWithDirs_copy.zip"
def setup def setup
File.copy("data/zipWithDirs.zip", TEST_ZIP) File.copy("data/zipWithDirs.zip", TEST_ZIP)
@ -573,7 +573,7 @@ class ZipFsFileMutatingTest < RUNIT::TestCase
blockCalled = true blockCalled = true
f.write "This is what I'm writing" f.write "This is what I'm writing"
} }
assert_equals("This is what I'm writing", assert_equal("This is what I'm writing",
zf.file.read("test_open_write_entry")) zf.file.read("test_open_write_entry"))
# Test with existing entry # Test with existing entry
@ -582,7 +582,7 @@ class ZipFsFileMutatingTest < RUNIT::TestCase
blockCalled = true blockCalled = true
f.write "This is what I'm writing too" f.write "This is what I'm writing too"
} }
assert_equals("This is what I'm writing too", assert_equal("This is what I'm writing too",
zf.file.read("file1")) zf.file.read("file1"))
} }
end end
@ -590,7 +590,7 @@ class ZipFsFileMutatingTest < RUNIT::TestCase
def test_rename def test_rename
ZipFile.open(TEST_ZIP) { ZipFile.open(TEST_ZIP) {
|zf| |zf|
assert_exception(Errno::ENOENT, "") { assert_raise(Errno::ENOENT, "") {
zf.file.rename("NoSuchFile", "bimse") zf.file.rename("NoSuchFile", "bimse")
} }
zf.file.rename("file1", "newNameForFile1") zf.file.rename("file1", "newNameForFile1")
@ -616,9 +616,9 @@ class ZipFsFileMutatingTest < RUNIT::TestCase
assert(! zf.file.exists?("dir1/file11")) assert(! zf.file.exists?("dir1/file11"))
assert(! zf.file.exists?("dir1/file12")) assert(! zf.file.exists?("dir1/file12"))
assert_exception(Errno::ENOENT) { zf.file.send(symbol, "noSuchFile") } assert_raise(Errno::ENOENT) { zf.file.send(symbol, "noSuchFile") }
assert_exception(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11") } assert_raise(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11") }
assert_exception(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11/") } assert_raise(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11/") }
} }
ZipFile.open(TEST_ZIP) { ZipFile.open(TEST_ZIP) {
@ -634,7 +634,7 @@ class ZipFsFileMutatingTest < RUNIT::TestCase
end end
class ZipFsDirectoryTest < RUNIT::TestCase class ZipFsDirectoryTest < Test::Unit::TestCase
TEST_ZIP = "zipWithDirs_copy.zip" TEST_ZIP = "zipWithDirs_copy.zip"
def setup def setup
@ -644,10 +644,10 @@ class ZipFsDirectoryTest < RUNIT::TestCase
def test_delete def test_delete
ZipFile.open(TEST_ZIP) { ZipFile.open(TEST_ZIP) {
|zf| |zf|
assert_exception(Errno::ENOENT, "No such file or directory - NoSuchFile.txt") { assert_raise(Errno::ENOENT, "No such file or directory - NoSuchFile.txt") {
zf.dir.delete("NoSuchFile.txt") zf.dir.delete("NoSuchFile.txt")
} }
assert_exception(Errno::EINVAL, "Invalid argument - file1") { assert_raise(Errno::EINVAL, "Invalid argument - file1") {
zf.dir.delete("file1") zf.dir.delete("file1")
} }
assert(zf.file.exists?("dir1")) assert(zf.file.exists?("dir1"))
@ -659,10 +659,10 @@ class ZipFsDirectoryTest < RUNIT::TestCase
def test_mkdir def test_mkdir
ZipFile.open(TEST_ZIP) { ZipFile.open(TEST_ZIP) {
|zf| |zf|
assert_exception(Errno::EEXIST, "File exists - dir1") { assert_raise(Errno::EEXIST, "File exists - dir1") {
zf.dir.mkdir("file1") zf.dir.mkdir("file1")
} }
assert_exception(Errno::EEXIST, "File exists - dir1") { assert_raise(Errno::EEXIST, "File exists - dir1") {
zf.dir.mkdir("dir1") zf.dir.mkdir("dir1")
} }
assert(!zf.file.exists?("newDir")) assert(!zf.file.exists?("newDir"))
@ -677,24 +677,24 @@ class ZipFsDirectoryTest < RUNIT::TestCase
def test_pwd_chdir_entries def test_pwd_chdir_entries
ZipFile.open(TEST_ZIP) { ZipFile.open(TEST_ZIP) {
|zf| |zf|
assert_equals("/", zf.dir.pwd) assert_equal("/", zf.dir.pwd)
assert_exception(Errno::ENOENT, "No such file or directory - no such dir") { assert_raise(Errno::ENOENT, "No such file or directory - no such dir") {
zf.dir.chdir "no such dir" zf.dir.chdir "no such dir"
} }
assert_exception(Errno::EINVAL, "Invalid argument - file1") { assert_raise(Errno::EINVAL, "Invalid argument - file1") {
zf.dir.chdir "file1" zf.dir.chdir "file1"
} }
assert_equals(["dir1", "dir2", "file1"].sort, zf.dir.entries(".").sort) assert_equal(["dir1", "dir2", "file1"].sort, zf.dir.entries(".").sort)
zf.dir.chdir "dir1" zf.dir.chdir "dir1"
assert_equals("/dir1", zf.dir.pwd) assert_equal("/dir1", zf.dir.pwd)
assert_equals(["dir11", "file11", "file12"], zf.dir.entries(".").sort) assert_equal(["dir11", "file11", "file12"], zf.dir.entries(".").sort)
zf.dir.chdir "../dir2/dir21" zf.dir.chdir "../dir2/dir21"
assert_equals("/dir2/dir21", zf.dir.pwd) assert_equal("/dir2/dir21", zf.dir.pwd)
assert_equals(["dir221"].sort, zf.dir.entries(".").sort) assert_equal(["dir221"].sort, zf.dir.entries(".").sort)
} }
end end
@ -703,30 +703,30 @@ class ZipFsDirectoryTest < RUNIT::TestCase
|zf| |zf|
blockCalled = false blockCalled = false
assert_exception(Errno::ENOENT, "No such file or directory - noSuchDir") { assert_raise(Errno::ENOENT, "No such file or directory - noSuchDir") {
zf.dir.foreach("noSuchDir") { |e| blockCalled = true } zf.dir.foreach("noSuchDir") { |e| blockCalled = true }
} }
assert(! blockCalled) assert(! blockCalled)
assert_exception(Errno::ENOTDIR, "Not a directory - file1") { assert_raise(Errno::ENOTDIR, "Not a directory - file1") {
zf.dir.foreach("file1") { |e| blockCalled = true } zf.dir.foreach("file1") { |e| blockCalled = true }
} }
assert(! blockCalled) assert(! blockCalled)
entries = [] entries = []
zf.dir.foreach(".") { |e| entries << e } zf.dir.foreach(".") { |e| entries << e }
assert_equals(["dir1", "dir2", "file1"].sort, entries.sort) assert_equal(["dir1", "dir2", "file1"].sort, entries.sort)
entries = [] entries = []
zf.dir.foreach("dir1") { |e| entries << e } zf.dir.foreach("dir1") { |e| entries << e }
assert_equals(["dir11", "file11", "file12"], entries.sort) assert_equal(["dir11", "file11", "file12"], entries.sort)
} }
end end
def test_chroot def test_chroot
ZipFile.open(TEST_ZIP) { ZipFile.open(TEST_ZIP) {
|zf| |zf|
assert_exception(NotImplementedError) { assert_raise(NotImplementedError) {
zf.dir.chroot zf.dir.chroot
} }
} }
@ -742,28 +742,28 @@ class ZipFsDirectoryTest < RUNIT::TestCase
ZipFile.open(TEST_ZIP) { ZipFile.open(TEST_ZIP) {
|zf| |zf|
assert_exception(Errno::ENOTDIR, "Not a directory - file1") { assert_raise(Errno::ENOTDIR, "Not a directory - file1") {
zf.dir.new("file1") zf.dir.new("file1")
} }
assert_exception(Errno::ENOENT, "No such file or directory - noSuchFile") { assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
zf.dir.new("noSuchFile") zf.dir.new("noSuchFile")
} }
d = zf.dir.new(".") d = zf.dir.new(".")
assert_equals(["file1", "dir1", "dir2"].sort, d.entries.sort) assert_equal(["file1", "dir1", "dir2"].sort, d.entries.sort)
d.close d.close
zf.dir.open("dir1") { zf.dir.open("dir1") {
|d| |d|
assert_equals(["dir11", "file11", "file12"].sort, d.entries.sort) assert_equal(["dir11", "file11", "file12"].sort, d.entries.sort)
} }
} }
end end
end end
class ZipFsDirIteratorTest < RUNIT::TestCase class ZipFsDirIteratorTest < Test::Unit::TestCase
FILENAME_ARRAY = [ "f1", "f2", "f3", "f4", "f5", "f6" ] FILENAME_ARRAY = [ "f1", "f2", "f3", "f4", "f5", "f6" ]
@ -773,19 +773,19 @@ class ZipFsDirIteratorTest < RUNIT::TestCase
def test_close def test_close
@dirIt.close @dirIt.close
assert_exception(IOError, "closed directory") { assert_raise(IOError, "closed directory") {
@dirIt.each { |e| p e } @dirIt.each { |e| p e }
} }
assert_exception(IOError, "closed directory") { assert_raise(IOError, "closed directory") {
@dirIt.read @dirIt.read
} }
assert_exception(IOError, "closed directory") { assert_raise(IOError, "closed directory") {
@dirIt.rewind @dirIt.rewind
} }
assert_exception(IOError, "closed directory") { assert_raise(IOError, "closed directory") {
@dirIt.seek(0) @dirIt.seek(0)
} }
assert_exception(IOError, "closed directory") { assert_raise(IOError, "closed directory") {
@dirIt.tell @dirIt.tell
} }
@ -793,22 +793,22 @@ class ZipFsDirIteratorTest < RUNIT::TestCase
def test_each def test_each
# Tested through Enumerable.entries # Tested through Enumerable.entries
assert_equals(FILENAME_ARRAY, @dirIt.entries) assert_equal(FILENAME_ARRAY, @dirIt.entries)
end end
def test_read def test_read
FILENAME_ARRAY.size.times { FILENAME_ARRAY.size.times {
|i| |i|
assert_equals(FILENAME_ARRAY[i], @dirIt.read) assert_equal(FILENAME_ARRAY[i], @dirIt.read)
} }
end end
def test_rewind def test_rewind
@dirIt.read @dirIt.read
@dirIt.read @dirIt.read
assert_equals(FILENAME_ARRAY[2], @dirIt.read) assert_equal(FILENAME_ARRAY[2], @dirIt.read)
@dirIt.rewind @dirIt.rewind
assert_equals(FILENAME_ARRAY[0], @dirIt.read) assert_equal(FILENAME_ARRAY[0], @dirIt.read)
end end
def test_tell_seek def test_tell_seek
@ -818,7 +818,7 @@ class ZipFsDirIteratorTest < RUNIT::TestCase
valAtPos = @dirIt.read valAtPos = @dirIt.read
@dirIt.read @dirIt.read
@dirIt.seek(pos) @dirIt.seek(pos)
assert_equals(valAtPos, @dirIt.read) assert_equal(valAtPos, @dirIt.read)
end end
end end

View File

@ -4,12 +4,12 @@ $VERBOSE = true
$: << "../lib" $: << "../lib"
require 'rubyunit' require 'test/unit'
require 'zip/ziprequire' require 'zip/ziprequire'
$: << 'data/rubycode.zip' << 'data/rubycode2.zip' $: << 'data/rubycode.zip' << 'data/rubycode2.zip'
class ZipRequireTest < RUNIT::TestCase class ZipRequireTest < Test::Unit::TestCase
def test_require def test_require
assert(require('data/notzippedruby')) assert(require('data/notzippedruby'))
assert(!require('data/notzippedruby')) assert(!require('data/notzippedruby'))
@ -27,13 +27,13 @@ class ZipRequireTest < RUNIT::TestCase
assert(c1.returnTrue) assert(c1.returnTrue)
assert(ZippedRuby1.returnTrue) assert(ZippedRuby1.returnTrue)
assert(!ZippedRuby2.returnFalse) assert(!ZippedRuby2.returnFalse)
assert_equals(4, ZippedRuby3.multiplyValues(2, 2)) assert_equal(4, ZippedRuby3.multiplyValues(2, 2))
end end
def test_get_resource def test_get_resource
get_resource("aResource.txt") { get_resource("aResource.txt") {
|f| |f|
assert_equals("Nothing exciting in this file!", f.read) assert_equal("Nothing exciting in this file!", f.read)
} }
end end
end end

View File

@ -4,7 +4,7 @@ $VERBOSE = true
$: << "../lib" $: << "../lib"
require 'rubyunit' require 'test/unit'
require 'zip/zip' require 'zip/zip'
require 'gentestfiles' require 'gentestfiles'