Moved String additions from filearchive.rb to zip.rb (and moved tests along too to ziptest.rb). Added ZipEntry.parentAsString and ZipEntrySet.parent

This commit is contained in:
thomas 2002-09-08 14:07:49 +00:00
parent 2668152853
commit 4580ff889d
4 changed files with 100 additions and 34 deletions

View File

@ -3,17 +3,6 @@
require 'ftools'
require 'zip'
class String
def endsWith(aString)
aStringSize = aString.size
slice(-aStringSize, aStringSize) == aString
end
def ensureEnd(aString)
endsWith(aString) ? self : self + aString
end
end
module Glob

View File

@ -6,24 +6,6 @@ require 'rubyunit'
require 'filearchive'
require 'fileutils'
class StringExtensionsTest < RUNIT::TestCase
def test_endsWith
assert("hello".endsWith("o"))
assert("hello".endsWith("lo"))
assert("hello".endsWith("hello"))
assert(!"howdy".endsWith("o"))
assert(!"howdy".endsWith("oy"))
assert(!"howdy".endsWith("howdy doody"))
assert(!"howdy".endsWith("doody howdy"))
end
def test_ensureEnd
assert_equals("hello!", "hello!".ensureEnd("!"))
assert_equals("hello!", "hello!".ensureEnd("o!"))
assert_equals("hello!", "hello".ensureEnd("!"))
assert_equals("hello!", "hel".ensureEnd("lo!"))
end
end
class GlobTest < RUNIT::TestCase

35
zip.rb
View File

@ -16,6 +16,22 @@ unless Enumerable.instance_methods.include?("inject")
end
end
class String
def startsWith(aString)
slice(0, aString.size) == aString
end
def endsWith(aString)
aStringSize = aString.size
slice(-aStringSize, aStringSize) == aString
end
def ensureEnd(aString)
endsWith(aString) ? self : self + aString
end
end
class Time
#MS-DOS File Date and Time format as used in Interrupt 21H Function 57H:
@ -324,6 +340,9 @@ module Zip
compressedSize = 0, crc = 0,
compressionMethod = ZipEntry::DEFLATED, size = 0,
time = Time.now)
if name.startsWith("/")
raise ZipEntryNameError, "Illegal ZipEntry name '#{name}', name must not start with /"
end
@localHeaderOffset = 0
@zipfile, @comment, @compressedSize, @crc, @extra, @compressionMethod,
@name, @size = zipfile, comment, compressedSize, crc,
@ -553,6 +572,11 @@ module Zip
}
end
def parentAsString
val = name[/.*(?=[^\/](\/)?)/]
val == "" ? nil : val
end
private
def getRawInputStream(&aProc)
File.open(@zipfile, "rb", &aProc)
@ -762,13 +786,13 @@ module Zip
return @entrySet == other.entrySet
end
def parent(entry)
@entrySet[entry.parentAsString]
end
#TODO attr_accessor :autoCreateDirectories
protected
attr_accessor :entrySet
# attr_accessor :autoCreateDirectories
# def parent(entry)
# end
end
@ -886,6 +910,7 @@ module Zip
class ZipEntryExistsError < ZipError; end
class ZipDestinationFileExistsError < ZipError; end
class ZipCompressionMethodError < ZipError; end
class ZipEntryNameError < ZipError; end
class ZipFile < ZipCentralDirectory
CREATE = 1

View File

@ -7,6 +7,37 @@ require 'zip'
include Zip
class StringExtensionsTest < RUNIT::TestCase
def test_startsWith
assert("hello".startsWith(""))
assert("hello".startsWith("h"))
assert("hello".startsWith("he"))
assert(! "hello".startsWith("hello there"))
assert(! "hello".startsWith(" he"))
assert_exception(NameError, "undefined method 'size' for nil") {
"hello".startsWith(nil)
}
end
def test_endsWith
assert("hello".endsWith("o"))
assert("hello".endsWith("lo"))
assert("hello".endsWith("hello"))
assert(!"howdy".endsWith("o"))
assert(!"howdy".endsWith("oy"))
assert(!"howdy".endsWith("howdy doody"))
assert(!"howdy".endsWith("doody howdy"))
end
def test_ensureEnd
assert_equals("hello!", "hello!".ensureEnd("!"))
assert_equals("hello!", "hello!".ensureEnd("o!"))
assert_equals("hello!", "hello".ensureEnd("!"))
assert_equals("hello!", "hel".ensureEnd("lo!"))
end
end
class AbstractInputStreamTest < RUNIT::TestCase
# AbstractInputStream subclass that provides a read method
@ -184,6 +215,26 @@ class ZipEntryTest < RUNIT::TestCase
assert_equals("4", entries[4].to_s)
assert_equals("5", entries[5].to_s)
end
def testParentAsString
entry1 = ZipEntry.new("zf.zip", "a")
entry2 = ZipEntry.new("zf.zip", "a/")
entry3 = ZipEntry.new("zf.zip", "a/b")
entry4 = ZipEntry.new("zf.zip", "a/b/")
entry5 = ZipEntry.new("zf.zip", "a/b/c")
entry6 = ZipEntry.new("zf.zip", "a/b/c/")
assert_equals(nil, entry1.parentAsString)
assert_equals(nil, entry2.parentAsString)
assert_equals("a/", entry3.parentAsString)
assert_equals("a/", entry4.parentAsString)
assert_equals("a/b/", entry5.parentAsString)
assert_equals("a/b/", entry6.parentAsString)
end
def testEntryNameCannotStartWithSlash
assert_exception(ZipEntryNameError) { ZipEntry.new("zf.zip", "/hej/der") }
end
end
module IOizeString
@ -1021,6 +1072,25 @@ class ZipEntrySetTest < RUNIT::TestCase
copy.entries[0].name = "a totally different name"
assert(@zipEntrySet != copy)
end
def testParent
entries = [
ZipEntry.new("zf.zip", "a"),
ZipEntry.new("zf.zip", "a/"),
ZipEntry.new("zf.zip", "a/b"),
ZipEntry.new("zf.zip", "a/b/"),
ZipEntry.new("zf.zip", "a/b/c"),
ZipEntry.new("zf.zip", "a/b/c/")
]
entrySet = ZipEntrySet.new(entries)
assert_equals(nil, entrySet.parent(entries[0]))
assert_equals(nil, entrySet.parent(entries[1]))
assert_equals(entries[1], entrySet.parent(entries[2]))
assert_equals(entries[1], entrySet.parent(entries[3]))
assert_equals(entries[3], entrySet.parent(entries[4]))
assert_equals(entries[3], entrySet.parent(entries[5]))
end
end