bzip2-ffi 1.0.0 → 1.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.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data/CHANGES.md +33 -2
- data/Gemfile +43 -2
- data/LICENSE +13 -13
- data/README.md +92 -62
- data/Rakefile +24 -0
- data/bzip2-ffi.gemspec +10 -1
- data/lib/bzip2/ffi/error.rb +5 -2
- data/lib/bzip2/ffi/io.rb +59 -47
- data/lib/bzip2/ffi/libbz2.rb +7 -3
- data/lib/bzip2/ffi/reader.rb +210 -104
- data/lib/bzip2/ffi/version.rb +4 -1
- data/lib/bzip2/ffi/writer.rb +81 -62
- data/lib/bzip2/ffi.rb +9 -6
- data/test/error_test.rb +19 -20
- data/test/fixtures/lorem-4096-bytes-compressed.txt.bz2 +0 -0
- data/test/fixtures/lorem-first-structure-4096-bytes.txt.bz2 +0 -0
- data/test/fixtures/two_structures.bz2 +0 -0
- data/test/io_test.rb +34 -32
- data/test/reader_test.rb +339 -111
- data/test/test_helper.rb +45 -8
- data/test/version_test.rb +4 -1
- data/test/writer_test.rb +99 -73
- data.tar.gz.sig +0 -0
- metadata +33 -26
- metadata.gz.sig +2 -1
- /data/test/fixtures/{bzipped → compressed.bz2} +0 -0
data/lib/bzip2/ffi/writer.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
require 'pathname'
|
2
5
|
|
3
6
|
module Bzip2
|
4
7
|
module FFI
|
5
|
-
#
|
6
|
-
# public instance methods of
|
8
|
+
# {Writer} compresses and writes a bzip2 compressed stream or file. The
|
9
|
+
# public instance methods of {Writer} are intended to be equivalent to those
|
7
10
|
# of a standard `IO` object.
|
8
11
|
#
|
9
12
|
# Data can be written as a stream using {open} and {#write}. For example,
|
@@ -15,7 +18,7 @@ module Bzip2
|
|
15
18
|
# end
|
16
19
|
# end
|
17
20
|
#
|
18
|
-
# Alternatively, without passing a block to
|
21
|
+
# Alternatively, without passing a block to {open}:
|
19
22
|
#
|
20
23
|
# writer = Bzip2::FFI::Writer.open(io_or_path)
|
21
24
|
# begin
|
@@ -30,41 +33,45 @@ module Bzip2
|
|
30
33
|
#
|
31
34
|
# Bzip2::FFI::Writer.write(io_or_path, 'Hello, World!')
|
32
35
|
#
|
33
|
-
# The {open} and {write} methods accept either an
|
34
|
-
# path.
|
36
|
+
# The {open} and {write} methods accept either an IO-like object or a file
|
37
|
+
# path. IO-like objects must have a `#write` method. Paths can be given as
|
35
38
|
# either a `String` or `Pathname`.
|
36
39
|
#
|
37
40
|
# No character conversion is performed when writing and compressing. The
|
38
41
|
# {write} and {#write} methods compress the raw bytes from the given
|
39
42
|
# `String` (using the encoding of the `String`).
|
43
|
+
#
|
44
|
+
# {Writer} does not support seeking (it's not supported by the underlying
|
45
|
+
# libbz2 library). There are no `#seek` or `#pos=` methods.
|
40
46
|
class Writer < IO
|
41
47
|
# Size of the buffer passed to libbz2 for it to write compressed data to.
|
42
48
|
#
|
43
49
|
# @private
|
44
50
|
OUT_BUFFER_SIZE = 4096 #:nodoc:
|
51
|
+
private_constant :OUT_BUFFER_SIZE
|
45
52
|
|
46
53
|
class << self
|
47
54
|
# Use send to keep this hidden from YARD (visibility tag does not work).
|
48
55
|
send(:public, :new)
|
49
56
|
|
50
|
-
# Opens a {Writer} to compress and write bzip2 compressed data to
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
57
|
+
# Opens a {Writer} to compress and write bzip2 compressed data to either
|
58
|
+
# an IO-like object or a file. IO-like objects must have a `#write`
|
59
|
+
# method. Files can be specified using either a `String` containing the
|
60
|
+
# file path or a `Pathname`.
|
54
61
|
#
|
55
|
-
# If no block is given, the opened
|
62
|
+
# If no block is given, the opened {Writer} instance is returned. After
|
56
63
|
# writing data, the instance must be closed using the {#close} method in
|
57
64
|
# order to complete the compression process.
|
58
65
|
#
|
59
|
-
# If a block is given, it will be passed the opened
|
60
|
-
# as an argument. After the block terminates, the
|
66
|
+
# If a block is given, it will be passed the opened {Writer} instance
|
67
|
+
# as an argument. After the block terminates, the {Writer} instance will
|
61
68
|
# automatically be closed. `open` will then return the result of the
|
62
69
|
# block.
|
63
70
|
#
|
64
71
|
# The following options can be specified using the `options` `Hash`:
|
65
72
|
#
|
66
|
-
# * `:autoclose` - When passing an
|
67
|
-
# close
|
73
|
+
# * `:autoclose` - When passing an IO-like object, set to `true` to
|
74
|
+
# close it when the {Writer} instance is closed.
|
68
75
|
# * `:block_size` - Specifies the block size used for compression. It
|
69
76
|
# should be set to an integer between 1 and 9
|
70
77
|
# inclusive. The actual block size used is 100 kB
|
@@ -86,25 +93,25 @@ module Bzip2
|
|
86
93
|
# the fallback. Allowable values range from 0 to 250
|
87
94
|
# inclusive. 0 is a special case, equivalent to using
|
88
95
|
# the default libbz2 work factor value (30 as of
|
89
|
-
# bzip2 v1.0.
|
96
|
+
# bzip2 v1.0.8). If not specified, `:work_factor`
|
90
97
|
# defaults to 0.
|
91
98
|
#
|
92
|
-
# If an
|
93
|
-
#
|
94
|
-
#
|
99
|
+
# If an IO-like object that has a `#binmode` method is passed to {open},
|
100
|
+
# `#binmode` will be called on `io_or_path` before yielding to the block
|
101
|
+
# or returning.
|
95
102
|
#
|
96
103
|
# If a path to a file that already exists is passed to `open`, the file
|
97
104
|
# will be truncated before writing.
|
98
105
|
#
|
99
|
-
# @param io_or_path [Object] Either an
|
106
|
+
# @param io_or_path [Object] Either an IO-like object with a `#write`
|
100
107
|
# method or a file path as a `String` or
|
101
108
|
# `Pathname`.
|
102
109
|
# @param options [Hash] Optional parameters (`:autoclose`, `:block_size`
|
103
110
|
# and `:small`).
|
104
|
-
# @return [Object] The opened
|
111
|
+
# @return [Object] The opened {Writer} instance if no block is given, or
|
105
112
|
# the result of the block if a block is given.
|
106
113
|
# @raise [ArgumentError] If `io_or_path` is _not_ a `String`, `Pathname`
|
107
|
-
# or an
|
114
|
+
# or an IO-like object with a `#write` method.
|
108
115
|
# @raise [Errno::ENOENT] If the parent directory of the specified file
|
109
116
|
# does not exist.
|
110
117
|
# @raise [Error::Bzip2Error] If an error occurs when initializing
|
@@ -122,14 +129,14 @@ module Bzip2
|
|
122
129
|
end
|
123
130
|
|
124
131
|
# Compresses data from a `String` and writes an entire bzip2 compressed
|
125
|
-
# structure to either an
|
126
|
-
# must have a
|
132
|
+
# structure to either an IO-like object or a file. IO-like objects
|
133
|
+
# must have a `#write` method. Files can be specified using either a
|
127
134
|
# `String` containing the file path or a `Pathname`.
|
128
135
|
#
|
129
136
|
# The following options can be specified using the `options` `Hash`:
|
130
137
|
#
|
131
|
-
# * `:autoclose` - When passing an
|
132
|
-
# close
|
138
|
+
# * `:autoclose` - When passing an IO-like object, set to `true` to
|
139
|
+
# close it when the {Writer} instance is closed.
|
133
140
|
# * `:block_size` - Specifies the block size used for compression. It
|
134
141
|
# should be set to an integer between 1 and 9
|
135
142
|
# inclusive. The actual block size used is 100 kB
|
@@ -151,28 +158,28 @@ module Bzip2
|
|
151
158
|
# the fallback. Allowable values range from 0 to 250
|
152
159
|
# inclusive. 0 is a special case, equivalent to using
|
153
160
|
# the default libbz2 work factor value (30 as of
|
154
|
-
# bzip2 v1.0.
|
161
|
+
# bzip2 v1.0.8). If not specified, `:work_factor`
|
155
162
|
# defaults to 0.
|
156
163
|
#
|
157
164
|
# No character conversion is performed. The raw bytes from `string` are
|
158
165
|
# compressed (using the encoding of `string`).
|
159
166
|
#
|
160
|
-
# If an
|
161
|
-
#
|
167
|
+
# If an IO-like object that has a `#binmode` method is passed to
|
168
|
+
# {write}, `#binmode` will be called on `io_or_path` before any
|
162
169
|
# compressed data is written.
|
163
170
|
#
|
164
171
|
# The number of uncompressed bytes written is returned.
|
165
172
|
#
|
166
|
-
# @param io_or_path [Object] Either an
|
173
|
+
# @param io_or_path [Object] Either an IO-like object with a `#write`
|
167
174
|
# method or a file path as a `String` or
|
168
175
|
# `Pathname`.
|
169
|
-
# @param string [Object] The string to write (
|
170
|
-
#
|
176
|
+
# @param string [Object] The string to write (the result of calling
|
177
|
+
# `#to_s` on `string` will be written).
|
171
178
|
# @param options [Hash] Optional parameters (`:autoclose`, `:block_size`
|
172
179
|
# and `:small`).
|
173
180
|
# @return [Integer] The number of uncompressed bytes written.
|
174
181
|
# @raise [ArgumentError] If `io_or_path` is _not_ a `String`, `Pathname`
|
175
|
-
# or an
|
182
|
+
# or an IO-like object with a `#write` method.
|
176
183
|
# @raise [Errno::ENOENT] If the parent directory of the specified file
|
177
184
|
# does not exist.
|
178
185
|
# @raise [Error::Bzip2Error] If an error occurs when initializing
|
@@ -186,10 +193,10 @@ module Bzip2
|
|
186
193
|
private
|
187
194
|
|
188
195
|
# Returns a Proc that can be used as a finalizer to call
|
189
|
-
#
|
196
|
+
# {Libbz2::BZ2_bzCompressEnd} with the given `stream`.
|
190
197
|
#
|
191
198
|
# @param stream [Libbz2::BzStream] The stream that should be passed to
|
192
|
-
#
|
199
|
+
# {Libbz2::BZ2_bzCompressEnd}.
|
193
200
|
def finalize(stream)
|
194
201
|
->(id) do
|
195
202
|
Libbz2::BZ2_bzCompressEnd(stream)
|
@@ -197,13 +204,13 @@ module Bzip2
|
|
197
204
|
end
|
198
205
|
end
|
199
206
|
|
200
|
-
# Initializes a {Writer} to write compressed data to an
|
201
|
-
# (`io`). `io` must have a
|
207
|
+
# Initializes a {Writer} to write compressed data to an IO-like object
|
208
|
+
# (`io`). `io` must have a `#write` method.
|
202
209
|
#
|
203
210
|
# The following options can be specified using the `options` `Hash`:
|
204
211
|
#
|
205
|
-
# * `:autoclose` - When passing an
|
206
|
-
# close
|
212
|
+
# * `:autoclose` - When passing an IO-like object, set to `true` to
|
213
|
+
# close it when the {Writer} instance is closed.
|
207
214
|
# * `:block_size` - Specifies the block size used for compression. It
|
208
215
|
# should be set to an integer between 1 and 9
|
209
216
|
# inclusive. The actual block size used is 100 kB
|
@@ -225,32 +232,32 @@ module Bzip2
|
|
225
232
|
# the fallback. Allowable values range from 0 to 250
|
226
233
|
# inclusive. 0 is a special case, equivalent to using
|
227
234
|
# the default libbz2 work factor value (30 as of
|
228
|
-
# bzip2 v1.0.
|
235
|
+
# bzip2 v1.0.8). If not specified, `:work_factor`
|
229
236
|
# defaults to 0.
|
230
237
|
#
|
231
|
-
#
|
238
|
+
# `#binmode` is called on `io` if `io` responds to `#binmode`.
|
232
239
|
#
|
233
|
-
# After use, the
|
240
|
+
# After use, the {Writer} instance must be closed using the {#close}
|
234
241
|
# method in order to complete the compression process.
|
235
242
|
#
|
236
|
-
# @param io [Object] An
|
243
|
+
# @param io [Object] An IO-like object that has a `#write` method.
|
237
244
|
# @param options [Hash] Optional parameters (`:autoclose`, `:block_size`
|
238
245
|
# and `:small`).
|
239
|
-
# @raise [ArgumentError] If `io` is `nil` or does not respond to
|
246
|
+
# @raise [ArgumentError] If `io` is `nil` or does not respond to `#write`.
|
240
247
|
# @raise [RangeError] If `options[:block_size]` is less than 1 or greater
|
241
248
|
# than 9, or `options[:work_factor]` is less than 0 or
|
242
249
|
# greater than 250.
|
243
250
|
# @raise [Error::Bzip2Error] If an error occurs when initializing libbz2.
|
244
|
-
def initialize(io, options = {})
|
251
|
+
def initialize(io, options = {})
|
245
252
|
super
|
246
253
|
raise ArgumentError, 'io must respond to write' unless io.respond_to?(:write)
|
247
|
-
|
254
|
+
|
248
255
|
block_size = options[:block_size] || 9
|
249
256
|
work_factor = options[:work_factor] || 0
|
250
|
-
|
257
|
+
|
251
258
|
raise RangeError, 'block_size must be >= 1 and <= 9' if block_size < 1 || block_size > 9
|
252
259
|
raise RangeError, 'work_factor must be >= 0 and <= 250' if work_factor < 0 || work_factor > 250
|
253
|
-
|
260
|
+
|
254
261
|
check_error(Libbz2::BZ2_bzCompressInit(stream, block_size, 0, work_factor))
|
255
262
|
|
256
263
|
ObjectSpace.define_finalizer(self, self.class.send(:finalize, stream))
|
@@ -261,11 +268,11 @@ module Bzip2
|
|
261
268
|
# {Writer}.
|
262
269
|
#
|
263
270
|
# If the {open} method is used with a block, it is not necessary to call
|
264
|
-
#
|
271
|
+
# {close}. Otherwise, {close} must be called once the all the data to be
|
265
272
|
# compressed has been passed to `#write`.
|
266
273
|
#
|
267
274
|
# @return [NilType] `nil`.
|
268
|
-
# @raise [IOError] If the
|
275
|
+
# @raise [IOError] If the {Writer} has already been closed.
|
269
276
|
def close
|
270
277
|
s = stream
|
271
278
|
flush_buffers(s, Libbz2::BZ_FINISH, Libbz2::BZ_STREAM_END)
|
@@ -283,11 +290,11 @@ module Bzip2
|
|
283
290
|
#
|
284
291
|
# The number of uncompressed bytes written is returned.
|
285
292
|
#
|
286
|
-
# @param string [Object] The string to write (
|
287
|
-
#
|
293
|
+
# @param string [Object] The string to write (the result of calling
|
294
|
+
# `#to_s` on `string` will be written).
|
288
295
|
# @return [Integer] The number of uncompressed bytes written.
|
289
296
|
# @raise [Error::Bzip2Error] If an error occurs during compression.
|
290
|
-
# @raise [IOError] If the
|
297
|
+
# @raise [IOError] If the {Writer} has been closed.
|
291
298
|
def write(string)
|
292
299
|
string = string.to_s
|
293
300
|
|
@@ -296,7 +303,7 @@ module Bzip2
|
|
296
303
|
buffer = ::FFI::MemoryPointer.new(1, OUT_BUFFER_SIZE)
|
297
304
|
begin
|
298
305
|
next_in.write_bytes(string)
|
299
|
-
s[:next_in] = next_in
|
306
|
+
s[:next_in] = next_in
|
300
307
|
s[:avail_in] = next_in.size
|
301
308
|
|
302
309
|
while s[:avail_in] > 0
|
@@ -322,29 +329,41 @@ module Bzip2
|
|
322
329
|
# writes out the current bzip2 compressed block to the underlying
|
323
330
|
# compressed stream or file.
|
324
331
|
#
|
325
|
-
# It is not usually necessary to call
|
332
|
+
# It is not usually necessary to call {flush}.
|
326
333
|
#
|
327
|
-
# Calling
|
334
|
+
# Calling {flush} may result in a larger compressed output.
|
328
335
|
#
|
329
336
|
# @return [Writer] `self`.
|
330
337
|
# @raise [Error::Bzip2Error] If an error occurs during the flush
|
331
338
|
# operation.
|
332
|
-
# @raise [IOError] If the
|
339
|
+
# @raise [IOError] If the {Writer} has been closed.
|
333
340
|
def flush
|
334
341
|
flush_buffers(stream, Libbz2::BZ_FLUSH, Libbz2::BZ_RUN_OK)
|
335
342
|
self
|
336
343
|
end
|
337
344
|
|
345
|
+
# Returns the number of uncompressed bytes that have been written.
|
346
|
+
#
|
347
|
+
# @return [Integer] The number of uncompressed bytes that have been
|
348
|
+
# written.
|
349
|
+
# @raise [IOError] If the {Writer} has been closed.
|
350
|
+
def tell
|
351
|
+
s = stream
|
352
|
+
(s[:total_in_hi32] << 32) | s[:total_in_lo32]
|
353
|
+
end
|
354
|
+
alias pos tell
|
355
|
+
|
338
356
|
private
|
339
357
|
|
340
|
-
# Calls
|
341
|
-
# of data that has been provided in prior calls.
|
358
|
+
# Calls {Libbz2::BZ2_bzCompress} repeatedly without input to complete
|
359
|
+
# compression of data that has been provided in prior calls.
|
342
360
|
#
|
343
|
-
# @param s [Libbz2::BzStream] The stream to pass to
|
344
|
-
#
|
361
|
+
# @param s [Libbz2::BzStream] The stream to pass to
|
362
|
+
# {Libbz2::BZ2_bzCompress}.
|
363
|
+
# @param action [Integer] The action to pass to {Libbz2::BZ2_bzCompress}.
|
345
364
|
# @param terminate_result [Integer] The result code that indicates when
|
346
365
|
# the action has been completed.
|
347
|
-
# @raise [Error::Bzip2Error] If
|
366
|
+
# @raise [Error::Bzip2Error] If {Libbz2::BZ2_bzCompress} reports an error.
|
348
367
|
def flush_buffers(s, action, terminate_result)
|
349
368
|
s[:next_in] = nil
|
350
369
|
s[:avail_in] = 0
|
@@ -367,7 +386,7 @@ module Bzip2
|
|
367
386
|
buffer.free
|
368
387
|
s[:next_out] = nil
|
369
388
|
end
|
370
|
-
end
|
389
|
+
end
|
371
390
|
end
|
372
391
|
end
|
373
392
|
end
|
data/lib/bzip2/ffi.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
module Bzip2
|
2
5
|
# Bzip2::FFI is a wrapper for libbz2 using FFI bindings. Bzip2 compressed data
|
3
6
|
# can be read and written as a stream using the Reader and Writer classes.
|
@@ -5,10 +8,10 @@ module Bzip2
|
|
5
8
|
end
|
6
9
|
end
|
7
10
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
require_relative 'ffi/libbz2'
|
12
|
+
require_relative 'ffi/error'
|
13
|
+
require_relative 'ffi/io'
|
14
|
+
require_relative 'ffi/reader'
|
15
|
+
require_relative 'ffi/writer'
|
16
|
+
require_relative 'ffi/version'
|
14
17
|
|
data/test/error_test.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative 'test_helper'
|
2
5
|
|
3
6
|
class ErrorTest < Minitest::Test
|
4
7
|
def test_initialize_base_class
|
@@ -7,30 +10,26 @@ class ErrorTest < Minitest::Test
|
|
7
10
|
assert_same(message, error.message)
|
8
11
|
end
|
9
12
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
]
|
21
|
-
|
22
|
-
classes.each do |c|
|
13
|
+
[
|
14
|
+
Bzip2::FFI::Error::SequenceError,
|
15
|
+
Bzip2::FFI::Error::ParamError,
|
16
|
+
Bzip2::FFI::Error::MemoryError,
|
17
|
+
Bzip2::FFI::Error::DataError,
|
18
|
+
Bzip2::FFI::Error::MagicDataError,
|
19
|
+
Bzip2::FFI::Error::ConfigError,
|
20
|
+
Bzip2::FFI::Error::UnexpectedEofError
|
21
|
+
].each do |c|
|
22
|
+
define_method("test_initialize_sub_classes_#{c.name.split('::').last.gsub(/([a-z])([A-Z])/, '\1_\2').downcase}") do
|
23
23
|
error = c.new
|
24
24
|
refute_nil(error.message)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
[-6, -7, -8, -10].each do |code|
|
28
|
+
# -6, -7 and -8 are errors that are only raised by the libbz2 high-level
|
29
|
+
# interface. Only the low-level interface is used by Bzip2::FFI. -10 is not
|
30
|
+
# defined by libbz2.
|
31
|
+
[-6, -7, -8, -10].each do |code|
|
32
|
+
define_method("test_initialize_unexpected_#{code}") do
|
34
33
|
error = Bzip2::FFI::Error::UnexpectedError.new(code)
|
35
34
|
assert_includes(error.message, code.to_s)
|
36
35
|
end
|
Binary file
|
Binary file
|
Binary file
|
data/test/io_test.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative 'test_helper'
|
2
5
|
|
3
6
|
class IOTest < Minitest::Test
|
4
7
|
class DummyIO
|
5
8
|
def initialize
|
6
9
|
@closed = false
|
7
10
|
end
|
8
|
-
|
11
|
+
|
9
12
|
def close
|
10
13
|
@closed = true
|
11
14
|
end
|
@@ -36,6 +39,9 @@ class IOTest < Minitest::Test
|
|
36
39
|
end
|
37
40
|
end
|
38
41
|
|
42
|
+
# Private constant.
|
43
|
+
LIBBZ2 = Bzip2::FFI.const_get(:Libbz2)
|
44
|
+
|
39
45
|
def test_autoclose_set_true
|
40
46
|
io = TestIO.new(DummyIO.new, autoclose: false)
|
41
47
|
assert_equal(false, io.autoclose?)
|
@@ -138,7 +144,7 @@ class IOTest < Minitest::Test
|
|
138
144
|
io.close
|
139
145
|
assert_equal(true, io.closed?)
|
140
146
|
end
|
141
|
-
|
147
|
+
|
142
148
|
def test_external_encoding
|
143
149
|
io = TestIO.new(DummyIO.new)
|
144
150
|
assert_equal(Encoding::ASCII_8BIT, io.external_encoding)
|
@@ -192,7 +198,7 @@ class IOTest < Minitest::Test
|
|
192
198
|
io = TestIO.new(DummyIO.new)
|
193
199
|
s = io.stream
|
194
200
|
refute_nil(s)
|
195
|
-
assert_kind_of(
|
201
|
+
assert_kind_of(LIBBZ2.const_get(:BzStream), s)
|
196
202
|
end
|
197
203
|
|
198
204
|
def test_stream_when_closed
|
@@ -213,38 +219,34 @@ class IOTest < Minitest::Test
|
|
213
219
|
assert_equal('closed stream', e.message)
|
214
220
|
end
|
215
221
|
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
(0..4).each do |i|
|
222
|
+
(0..4).each do |i|
|
223
|
+
define_method("test_check_error_not_error_#{i}") do
|
224
|
+
io = TestIO.new(DummyIO.new)
|
220
225
|
assert_equal(i, io.check_error(i))
|
221
226
|
end
|
222
227
|
end
|
223
228
|
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
}
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
codes.each do |code, error_class|
|
229
|
+
{
|
230
|
+
:BZ_SEQUENCE_ERROR => Bzip2::FFI::Error::SequenceError,
|
231
|
+
:BZ_PARAM_ERROR => Bzip2::FFI::Error::ParamError,
|
232
|
+
:BZ_MEM_ERROR => Bzip2::FFI::Error::MemoryError,
|
233
|
+
:BZ_DATA_ERROR => Bzip2::FFI::Error::DataError,
|
234
|
+
:BZ_DATA_ERROR_MAGIC => Bzip2::FFI::Error::MagicDataError,
|
235
|
+
:BZ_CONFIG_ERROR => Bzip2::FFI::Error::ConfigError
|
236
|
+
}.each do |type, error_class|
|
237
|
+
define_method("test_check_error_error_#{type}") do
|
238
|
+
io = TestIO.new(DummyIO.new)
|
239
|
+
code = LIBBZ2.const_get(type)
|
237
240
|
assert_raises(error_class) { io.check_error(code) }
|
238
241
|
end
|
239
242
|
end
|
240
243
|
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
#
|
246
|
-
|
247
|
-
[-6, -7, -8, -10].each do |code|
|
244
|
+
# -6, -7 and -8 are codes that are only raised by the libbz2 high-level
|
245
|
+
# interface. Only the low-level interface is used by Bzip2::FFI. -10 is not
|
246
|
+
# defined by libbz2.
|
247
|
+
[-6, -7, -8, -10].each do |code|
|
248
|
+
define_method("test_check_error_unexpected_#{code}") do
|
249
|
+
io = TestIO.new(DummyIO.new)
|
248
250
|
error = assert_raises(Bzip2::FFI::Error::UnexpectedError) { io.check_error(code) }
|
249
251
|
assert_includes(error.message, code.to_s)
|
250
252
|
end
|
@@ -304,14 +306,14 @@ class IOTest < Minitest::Test
|
|
304
306
|
res = TestIO.open(dummy_io) do |io|
|
305
307
|
assert_kind_of(TestIO, io)
|
306
308
|
refute(io.closed?)
|
307
|
-
assert_equal(false, io.autoclose?)
|
309
|
+
assert_equal(false, io.autoclose?)
|
308
310
|
assert_same(dummy_io, io.io)
|
309
311
|
copy_io = io
|
310
312
|
42
|
311
313
|
end
|
312
314
|
|
313
315
|
assert(copy_io.closed?)
|
314
|
-
assert_equal(42, res)
|
316
|
+
assert_equal(42, res)
|
315
317
|
end
|
316
318
|
|
317
319
|
def test_open_block_options
|
@@ -321,14 +323,14 @@ class IOTest < Minitest::Test
|
|
321
323
|
res = TestIO.open(dummy_io, autoclose: true) do |io|
|
322
324
|
assert_kind_of(TestIO, io)
|
323
325
|
refute(io.closed?)
|
324
|
-
assert_equal(true, io.autoclose?)
|
326
|
+
assert_equal(true, io.autoclose?)
|
325
327
|
assert_same(dummy_io, io.io)
|
326
328
|
copy_io = io
|
327
329
|
42
|
328
330
|
end
|
329
331
|
|
330
332
|
assert(copy_io.closed?)
|
331
|
-
assert_equal(42, res)
|
333
|
+
assert_equal(42, res)
|
332
334
|
end
|
333
335
|
|
334
336
|
def test_open_block_closes
|