Skip to content

Commit

Permalink
Updated to latest minitest
Browse files Browse the repository at this point in the history
  • Loading branch information
dmendel committed Feb 4, 2024
1 parent 75d889e commit 2c8588a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 26 deletions.
1 change: 1 addition & 0 deletions ChangeLog.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Skip :until_valid is now fast for :asserted_value.
* Added Section - a way to transform the data stream.
* Added transforms for brotli, lz4, xor, zlib, zstd.
* Updated to current minitest

== Version 2.4.15 (2023-02-07)

Expand Down
2 changes: 1 addition & 1 deletion bindata.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = ">= 2.5.0"

s.add_development_dependency('rake')
s.add_development_dependency('minitest', "> 5.0.0", "< 5.12.0")
s.add_development_dependency('minitest', "> 5.0.0")
s.add_development_dependency('simplecov')
s.description = <<-END.gsub(/^ +/, "")
BinData is a declarative way to read and write binary file formats.
Expand Down
2 changes: 1 addition & 1 deletion lib/bindata/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def register_dynamic_class(name)
def warn_if_name_is_already_registered(name, class_to_register)
prev_class = @registry[name]
if prev_class && prev_class != class_to_register
warn "warning: replacing registered class #{prev_class} " \
Kernel.warn "warning: replacing registered class #{prev_class} " \
"with #{class_to_register}"
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bindata/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def sensible_default
# Warns when reading if :value && no :read_length
module WarnNoReadLengthPlugin
def read_and_return_value(io)
warn "#{debug_name} does not have a :read_length parameter - returning empty string"
Kernel.warn "#{debug_name} does not have a :read_length parameter - returning empty string"
""
end
end
Expand Down
14 changes: 6 additions & 8 deletions test/array_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@
end

it "warns about :length" do
Kernel.must_warn ":length is not used with BinData::Array. You probably want to change this to :initial_length" do
obj = BinData::Array.new(type: :uint8, length: 3)
obj.read "123"
end
_ {
obj = BinData::Array.new(type: :uint8, length: 3)
}.must_warn ":length is not used with BinData::Array. You probably want to change this to :initial_length"
end

it "warns about :read_length" do
Kernel.must_warn ":read_length is not used with BinData::Array. You probably want to change this to :initial_length" do
obj = BinData::Array.new(type: :uint8, read_length: 3)
obj.read "123"
end
_ {
obj = BinData::Array.new(type: :uint8, read_length: 3)
}.must_warn ":read_length is not used with BinData::Array. You probably want to change this to :initial_length"
end

it "fails if a given type is unknown" do
Expand Down
8 changes: 4 additions & 4 deletions test/string_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,15 @@ def snapshot
describe BinData::String, "warnings" do
it "warns if has :asserted_value but no :length" do
obj = BinData::String.new(asserted_value: "ABC")
obj.must_warn "obj does not have a :read_length parameter - returning empty string" do
_ {
_ { obj.read("abcde") }.must_raise BinData::ValidityError
end
}.must_warn "obj does not have a :read_length parameter - returning empty string"
end

it "warns if has :value but no :read_length" do
obj = BinData::String.new(value: "ABC")
obj.must_warn "obj does not have a :read_length parameter - returning empty string" do
_ {
obj.read("abcde")
end
}.must_warn "obj does not have a :read_length parameter - returning empty string"
end
end
31 changes: 20 additions & 11 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,41 @@ def expose_methods_for_testing
def value_read_from_written
self.class.read(self.to_binary_s)
end
end

def must_equal_binary(expected)
must_equal expected.dup.force_encoding(Encoding::BINARY)
module Minitest::Assertions
def assert_equals_binary(expected, actual)
assert_equal expected.dup.force_encoding(Encoding::BINARY), actual
end

def must_raise_on_line(exp, line, msg = nil)
ex = self.must_raise exp
(ex.message).must_equal msg if msg
def assert_raises_on_line(exp, line, msg = nil, &block)
ex = assert_raises(exp, &block)
assert_equal(msg, ex.message) if msg

idx = ex.backtrace.find_index { |bt| /:in `must_raise_on_line'$/ =~ bt }
idx = ex.backtrace.find_index { |bt| /:in `assert_raises_on_line'$/ =~ bt }

line_num_regex = /.*:(\d+)(:.*|$)/
err_line = line_num_regex.match(ex.backtrace[0])[1].to_i
ref_line = line_num_regex.match(ex.backtrace[idx + 1])[1].to_i
ref_line = line_num_regex.match(ex.backtrace[idx + 2])[1].to_i

(err_line - ref_line).must_equal line
assert_equal((err_line - ref_line), line)
end

def must_warn(msg, &block)
def assert_warns(msg, &block)
result = ""
callable = proc { |str|
result = str
}
self.stub(:warn, callable) do
Kernel.stub(:warn, callable) do
block.call
end
(result).must_equal msg

assert_equal msg, result
end
end

module Minitest::Expectations
infect_an_assertion :assert_equals_binary, :must_equal_binary
infect_an_assertion :assert_raises_on_line, :must_raise_on_line, :block
infect_an_assertion :assert_warns, :must_warn, :block
end

0 comments on commit 2c8588a

Please sign in to comment.