Skip to content

Commit

Permalink
🐛 Fix SequenceSet.try_convert
Browse files Browse the repository at this point in the history
It's a silly mistake, but it had no test coverage.

I'm sure I did have this working at one point, but no test coverage!
  • Loading branch information
nevans committed Nov 8, 2024
1 parent 8591ec0 commit 950a992
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/net/imap/sequence_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def [](first, *rest)
# raised.
def try_convert(obj)
return obj if obj.is_a?(SequenceSet)
return nil unless respond_to?(:to_sequence_set)
return nil unless obj.respond_to?(:to_sequence_set)
obj = obj.to_sequence_set
return obj if obj.is_a?(SequenceSet)
raise DataFormatError, "invalid object returned from to_sequence_set"
Expand Down
16 changes: 16 additions & 0 deletions test/net/imap/test_sequence_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ def compare_to_reference_set(nums, set, seqset)
assert_raise DataFormatError do SequenceSet[Set.new] end
end

test ".try_convert" do
assert_nil SequenceSet.try_convert(nil)
assert_nil SequenceSet.try_convert(123)
assert_nil SequenceSet.try_convert(12..34)
assert_nil SequenceSet.try_convert("12:34")
assert_nil SequenceSet.try_convert(Object.new)

obj = Object.new
def obj.to_sequence_set; SequenceSet[192, 168, 1, 255] end
assert_equal SequenceSet[192, 168, 1, 255], SequenceSet.try_convert(obj)

obj = Object.new
def obj.to_sequence_set; 192_168.001_255 end
assert_raise DataFormatError do SequenceSet.try_convert(obj) end
end

test "#[non-negative index]" do
assert_nil SequenceSet.empty[0]
assert_equal 1, SequenceSet[1..][0]
Expand Down

0 comments on commit 950a992

Please sign in to comment.