diff --git a/lib/net/imap/sequence_set.rb b/lib/net/imap/sequence_set.rb index f5da7b24..74a1d9ea 100644 --- a/lib/net/imap/sequence_set.rb +++ b/lib/net/imap/sequence_set.rb @@ -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" diff --git a/test/net/imap/test_sequence_set.rb b/test/net/imap/test_sequence_set.rb index 3949c7f3..85204b24 100644 --- a/test/net/imap/test_sequence_set.rb +++ b/test/net/imap/test_sequence_set.rb @@ -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]