viva-rubyzip 0.9.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ class NotZippedRuby
4
+ def returnTrue
5
+ true
6
+ end
7
+ end
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,157 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = true
4
+
5
+ class TestFiles
6
+ RANDOM_ASCII_FILE1 = "data/generated/randomAscii1.txt"
7
+ RANDOM_ASCII_FILE2 = "data/generated/randomAscii2.txt"
8
+ RANDOM_ASCII_FILE3 = "data/generated/randomAscii3.txt"
9
+ RANDOM_BINARY_FILE1 = "data/generated/randomBinary1.bin"
10
+ RANDOM_BINARY_FILE2 = "data/generated/randomBinary2.bin"
11
+
12
+ EMPTY_TEST_DIR = "data/generated/emptytestdir"
13
+
14
+ ASCII_TEST_FILES = [ RANDOM_ASCII_FILE1, RANDOM_ASCII_FILE2, RANDOM_ASCII_FILE3 ]
15
+ BINARY_TEST_FILES = [ RANDOM_BINARY_FILE1, RANDOM_BINARY_FILE2 ]
16
+ TEST_DIRECTORIES = [ EMPTY_TEST_DIR ]
17
+ TEST_FILES = [ ASCII_TEST_FILES, BINARY_TEST_FILES, EMPTY_TEST_DIR ].flatten!
18
+
19
+ def TestFiles.create_test_files(recreate)
20
+ if (recreate ||
21
+ ! (TEST_FILES.inject(true) { |accum, element| accum && File.exists?(element) }))
22
+
23
+ Dir.mkdir "data/generated" rescue Errno::EEXIST
24
+
25
+ ASCII_TEST_FILES.each_with_index {
26
+ |filename, index|
27
+ create_random_ascii(filename, 1E4 * (index+1))
28
+ }
29
+
30
+ BINARY_TEST_FILES.each_with_index {
31
+ |filename, index|
32
+ create_random_binary(filename, 1E4 * (index+1))
33
+ }
34
+
35
+ ensure_dir(EMPTY_TEST_DIR)
36
+ end
37
+ end
38
+
39
+ private
40
+ def TestFiles.create_random_ascii(filename, size)
41
+ File.open(filename, "wb") {
42
+ |file|
43
+ while (file.tell < size)
44
+ file << rand
45
+ end
46
+ }
47
+ end
48
+
49
+ def TestFiles.create_random_binary(filename, size)
50
+ File.open(filename, "wb") {
51
+ |file|
52
+ while (file.tell < size)
53
+ file << [rand].pack("V")
54
+ end
55
+ }
56
+ end
57
+
58
+ def TestFiles.ensure_dir(name)
59
+ if File.exists?(name)
60
+ return if File.stat(name).directory?
61
+ File.delete(name)
62
+ end
63
+ Dir.mkdir(name)
64
+ end
65
+
66
+ end
67
+
68
+
69
+
70
+ # For representation and creation of
71
+ # test data
72
+ class TestZipFile
73
+ attr_accessor :zip_name, :entry_names, :comment
74
+
75
+ def initialize(zip_name, entry_names, comment = "")
76
+ @zip_name=zip_name
77
+ @entry_names=entry_names
78
+ @comment = comment
79
+ end
80
+
81
+ def TestZipFile.create_test_zips(recreate)
82
+ files = Dir.entries("data/generated")
83
+ if (recreate ||
84
+ ! (files.index(File.basename(TEST_ZIP1.zip_name)) &&
85
+ files.index(File.basename(TEST_ZIP2.zip_name)) &&
86
+ files.index(File.basename(TEST_ZIP3.zip_name)) &&
87
+ files.index(File.basename(TEST_ZIP4.zip_name)) &&
88
+ files.index("empty.txt") &&
89
+ files.index("empty_chmod640.txt") &&
90
+ files.index("short.txt") &&
91
+ files.index("longAscii.txt") &&
92
+ files.index("longBinary.bin") ))
93
+ raise "failed to create test zip '#{TEST_ZIP1.zip_name}'" unless
94
+ system("zip #{TEST_ZIP1.zip_name} data/file2.txt")
95
+ raise "failed to remove entry from '#{TEST_ZIP1.zip_name}'" unless
96
+ system("zip #{TEST_ZIP1.zip_name} -d data/file2.txt")
97
+
98
+ File.open("data/generated/empty.txt", "w") {}
99
+ File.open("data/generated/empty_chmod640.txt", "w") { |f| f.chmod(0640) }
100
+
101
+ File.open("data/generated/short.txt", "w") { |file| file << "ABCDEF" }
102
+ ziptestTxt=""
103
+ File.open("data/file2.txt") { |file| ziptestTxt=file.read }
104
+ File.open("data/generated/longAscii.txt", "w") {
105
+ |file|
106
+ while (file.tell < 1E5)
107
+ file << ziptestTxt
108
+ end
109
+ }
110
+
111
+ testBinaryPattern=""
112
+ File.open("data/generated/empty.zip") { |file| testBinaryPattern=file.read }
113
+ testBinaryPattern *= 4
114
+
115
+ File.open("data/generated/longBinary.bin", "wb") {
116
+ |file|
117
+ while (file.tell < 3E5)
118
+ file << testBinaryPattern << rand << "\0"
119
+ end
120
+ }
121
+ raise "failed to create test zip '#{TEST_ZIP2.zip_name}'" unless
122
+ system("zip #{TEST_ZIP2.zip_name} #{TEST_ZIP2.entry_names.join(' ')}")
123
+
124
+ # without bash system interprets everything after echo as parameters to
125
+ # echo including | zip -z ...
126
+ raise "failed to add comment to test zip '#{TEST_ZIP2.zip_name}'" unless
127
+ system("bash -c \"echo #{TEST_ZIP2.comment} | zip -z #{TEST_ZIP2.zip_name}\"")
128
+
129
+ raise "failed to create test zip '#{TEST_ZIP3.zip_name}'" unless
130
+ system("zip #{TEST_ZIP3.zip_name} #{TEST_ZIP3.entry_names.join(' ')}")
131
+
132
+ raise "failed to create test zip '#{TEST_ZIP4.zip_name}'" unless
133
+ system("zip #{TEST_ZIP4.zip_name} #{TEST_ZIP4.entry_names.join(' ')}")
134
+ end
135
+ rescue
136
+ raise $!.to_s +
137
+ "\n\nziptest.rb requires the Info-ZIP program 'zip' in the path\n" +
138
+ "to create test data. If you don't have it you can download\n" +
139
+ "the necessary test files at http://sf.net/projects/rubyzip."
140
+ end
141
+
142
+ TEST_ZIP1 = TestZipFile.new("data/generated/empty.zip", [])
143
+ TEST_ZIP2 = TestZipFile.new("data/generated/5entry.zip", %w{ data/generated/longAscii.txt data/generated/empty.txt data/generated/empty_chmod640.txt data/generated/short.txt data/generated/longBinary.bin},
144
+ "my zip comment")
145
+ TEST_ZIP3 = TestZipFile.new("data/generated/test1.zip", %w{ data/file1.txt })
146
+ TEST_ZIP4 = TestZipFile.new("data/generated/zipWithDir.zip", [ "data/file1.txt",
147
+ TestFiles::EMPTY_TEST_DIR])
148
+ end
149
+
150
+
151
+ END {
152
+ TestFiles::create_test_files(ARGV.index("recreate") != nil ||
153
+ ARGV.index("recreateonly") != nil)
154
+ TestZipFile::create_test_zips(ARGV.index("recreate") != nil ||
155
+ ARGV.index("recreateonly") != nil)
156
+ exit if ARGV.index("recreateonly") != nil
157
+ }
@@ -0,0 +1,208 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = true
4
+
5
+ $: << "../lib"
6
+
7
+ require 'test/unit'
8
+ require 'zip/ioextras'
9
+
10
+ include IOExtras
11
+
12
+ class FakeIOTest < Test::Unit::TestCase
13
+ class FakeIOUsingClass
14
+ include FakeIO
15
+ end
16
+
17
+ def test_kind_of?
18
+ obj = FakeIOUsingClass.new
19
+
20
+ assert(obj.kind_of?(Object))
21
+ assert(obj.kind_of?(FakeIOUsingClass))
22
+ assert(obj.kind_of?(IO))
23
+ assert(!obj.kind_of?(Fixnum))
24
+ assert(!obj.kind_of?(String))
25
+ end
26
+ end
27
+
28
+ class AbstractInputStreamTest < Test::Unit::TestCase
29
+ # AbstractInputStream subclass that provides a read method
30
+
31
+ TEST_LINES = [ "Hello world#{$/}",
32
+ "this is the second line#{$/}",
33
+ "this is the last line"]
34
+ TEST_STRING = TEST_LINES.join
35
+ class TestAbstractInputStream
36
+ include AbstractInputStream
37
+ def initialize(aString)
38
+ super()
39
+ @contents = aString
40
+ @readPointer = 0
41
+ end
42
+
43
+ def read(charsToRead)
44
+ retVal=@contents[@readPointer, charsToRead]
45
+ @readPointer+=charsToRead
46
+ return retVal
47
+ end
48
+
49
+ def produce_input
50
+ read(100)
51
+ end
52
+
53
+ def input_finished?
54
+ @contents[@readPointer] == nil
55
+ end
56
+ end
57
+
58
+ def setup
59
+ @io = TestAbstractInputStream.new(TEST_STRING)
60
+ end
61
+
62
+ def test_gets
63
+ assert_equal(TEST_LINES[0], @io.gets)
64
+ assert_equal(1, @io.lineno)
65
+ assert_equal(TEST_LINES[1], @io.gets)
66
+ assert_equal(2, @io.lineno)
67
+ assert_equal(TEST_LINES[2], @io.gets)
68
+ assert_equal(3, @io.lineno)
69
+ assert_equal(nil, @io.gets)
70
+ assert_equal(4, @io.lineno)
71
+ end
72
+
73
+ def test_getsMultiCharSeperator
74
+ assert_equal("Hell", @io.gets("ll"))
75
+ assert_equal("o world#{$/}this is the second l", @io.gets("d l"))
76
+ end
77
+
78
+ def test_each_line
79
+ lineNumber=0
80
+ @io.each_line {
81
+ |line|
82
+ assert_equal(TEST_LINES[lineNumber], line)
83
+ lineNumber+=1
84
+ }
85
+ end
86
+
87
+ def test_readlines
88
+ assert_equal(TEST_LINES, @io.readlines)
89
+ end
90
+
91
+ def test_readline
92
+ test_gets
93
+ begin
94
+ @io.readline
95
+ fail "EOFError expected"
96
+ rescue EOFError
97
+ end
98
+ end
99
+ end
100
+
101
+ class AbstractOutputStreamTest < Test::Unit::TestCase
102
+ class TestOutputStream
103
+ include AbstractOutputStream
104
+
105
+ attr_accessor :buffer
106
+
107
+ def initialize
108
+ @buffer = ""
109
+ end
110
+
111
+ def << (data)
112
+ @buffer << data
113
+ self
114
+ end
115
+ end
116
+
117
+ def setup
118
+ @outputStream = TestOutputStream.new
119
+
120
+ @origCommaSep = $,
121
+ @origOutputSep = $\
122
+ end
123
+
124
+ def teardown
125
+ $, = @origCommaSep
126
+ $\ = @origOutputSep
127
+ end
128
+
129
+ def test_write
130
+ count = @outputStream.write("a little string")
131
+ assert_equal("a little string", @outputStream.buffer)
132
+ assert_equal("a little string".length, count)
133
+
134
+ count = @outputStream.write(". a little more")
135
+ assert_equal("a little string. a little more", @outputStream.buffer)
136
+ assert_equal(". a little more".length, count)
137
+ end
138
+
139
+ def test_print
140
+ $\ = nil # record separator set to nil
141
+ @outputStream.print("hello")
142
+ assert_equal("hello", @outputStream.buffer)
143
+
144
+ @outputStream.print(" world.")
145
+ assert_equal("hello world.", @outputStream.buffer)
146
+
147
+ @outputStream.print(" You ok ", "out ", "there?")
148
+ assert_equal("hello world. You ok out there?", @outputStream.buffer)
149
+
150
+ $\ = "\n"
151
+ @outputStream.print
152
+ assert_equal("hello world. You ok out there?\n", @outputStream.buffer)
153
+
154
+ @outputStream.print("I sure hope so!")
155
+ assert_equal("hello world. You ok out there?\nI sure hope so!\n", @outputStream.buffer)
156
+
157
+ $, = "X"
158
+ @outputStream.buffer = ""
159
+ @outputStream.print("monkey", "duck", "zebra")
160
+ assert_equal("monkeyXduckXzebra\n", @outputStream.buffer)
161
+
162
+ $\ = nil
163
+ @outputStream.buffer = ""
164
+ @outputStream.print(20)
165
+ assert_equal("20", @outputStream.buffer)
166
+ end
167
+
168
+ def test_printf
169
+ @outputStream.printf("%d %04x", 123, 123)
170
+ assert_equal("123 007b", @outputStream.buffer)
171
+ end
172
+
173
+ def test_putc
174
+ @outputStream.putc("A")
175
+ assert_equal("A", @outputStream.buffer)
176
+ @outputStream.putc(65)
177
+ assert_equal("AA", @outputStream.buffer)
178
+ end
179
+
180
+ def test_puts
181
+ @outputStream.puts
182
+ assert_equal("\n", @outputStream.buffer)
183
+
184
+ @outputStream.puts("hello", "world")
185
+ assert_equal("\nhello\nworld\n", @outputStream.buffer)
186
+
187
+ @outputStream.buffer = ""
188
+ @outputStream.puts("hello\n", "world\n")
189
+ assert_equal("hello\nworld\n", @outputStream.buffer)
190
+
191
+ @outputStream.buffer = ""
192
+ @outputStream.puts(["hello\n", "world\n"])
193
+ assert_equal("hello\nworld\n", @outputStream.buffer)
194
+
195
+ @outputStream.buffer = ""
196
+ @outputStream.puts(["hello\n", "world\n"], "bingo")
197
+ assert_equal("hello\nworld\nbingo\n", @outputStream.buffer)
198
+
199
+ @outputStream.buffer = ""
200
+ @outputStream.puts(16, 20, 50, "hello")
201
+ assert_equal("16\n20\n50\nhello\n", @outputStream.buffer)
202
+ end
203
+ end
204
+
205
+
206
+ # Copyright (C) 2002-2004 Thomas Sondergaard
207
+ # rubyzip is free software; you can redistribute it and/or
208
+ # modify it under the terms of the ruby license.
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = true
4
+
5
+ $: << "../lib"
6
+
7
+ require 'test/unit'
8
+ require 'zip/stdrubyext'
9
+
10
+ class ModuleTest < Test::Unit::TestCase
11
+
12
+ def test_select_map
13
+ assert_equal([2, 4, 8, 10], [1, 2, 3, 4, 5].select_map { |e| e == 3 ? nil : 2*e })
14
+ end
15
+
16
+ end
17
+
18
+ class StringExtensionsTest < Test::Unit::TestCase
19
+
20
+ def test_starts_with
21
+ assert("hello".starts_with(""))
22
+ assert("hello".starts_with("h"))
23
+ assert("hello".starts_with("he"))
24
+ assert(! "hello".starts_with("hello there"))
25
+ assert(! "hello".starts_with(" he"))
26
+
27
+ assert_raise(TypeError, "type mismatch: NilClass given") {
28
+ "hello".starts_with(nil)
29
+ }
30
+ end
31
+
32
+ def test_ends_with
33
+ assert("hello".ends_with("o"))
34
+ assert("hello".ends_with("lo"))
35
+ assert("hello".ends_with("hello"))
36
+ assert(!"howdy".ends_with("o"))
37
+ assert(!"howdy".ends_with("oy"))
38
+ assert(!"howdy".ends_with("howdy doody"))
39
+ assert(!"howdy".ends_with("doody howdy"))
40
+ end
41
+
42
+ def test_ensure_end
43
+ assert_equal("hello!", "hello!".ensure_end("!"))
44
+ assert_equal("hello!", "hello!".ensure_end("o!"))
45
+ assert_equal("hello!", "hello".ensure_end("!"))
46
+ assert_equal("hello!", "hel".ensure_end("lo!"))
47
+ end
48
+ end
49
+
50
+ # Copyright (C) 2002, 2003 Thomas Sondergaard
51
+ # rubyzip is free software; you can redistribute it and/or
52
+ # modify it under the terms of the ruby license.