Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Improve SequenceSet with Set, Range, Enumerable methods #239

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/net/imap/response_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,19 @@ class ThreadMember < Struct.new(:seqno, :children)
#
# An array of Net::IMAP::ThreadMember objects for mail items that are
# children of this in the thread.

# Returns a SequenceSet containing #seqno and all #children's seqno,
# recursively.
def to_sequence_set
SequenceSet.new all_seqnos
end

protected

def all_seqnos(node = self)
[node.seqno].concat node.children.flat_map { _1.all_seqnos }
end

end

# Net::IMAP::BodyStructure is included by all of the structs that can be
Expand Down
2 changes: 1 addition & 1 deletion lib/net/imap/response_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def unescape_quoted(quoted)
def sequence_set
str = combine_adjacent(*SEQUENCE_SET_TOKENS)
if Patterns::SEQUENCE_SET_STR.match?(str)
SequenceSet.new(str)
SequenceSet[str]
else
parse_error("unexpected atom %p, expected sequence-set", str)
end
Expand Down
1,354 changes: 1,318 additions & 36 deletions lib/net/imap/sequence_set.rb

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion test/net/imap/fixtures/response_parser/status_responses.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@
NUM: 1
SEQ: !ruby/struct:Net::IMAP::ExtensionData
data: !ruby/object:Net::IMAP::SequenceSet
atom: 1234:5,*:789654
string: 1234:5,*:789654
tuples:
- - 5
- 1234
- - 789654
- 4294967296
COMP-EMPTY: !ruby/struct:Net::IMAP::ExtensionData
data: []
COMP-QUOTED: !ruby/struct:Net::IMAP::ExtensionData
Expand Down
38 changes: 28 additions & 10 deletions test/net/imap/test_imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -591,42 +591,60 @@ def test_send_invalid_number
sock = server.accept
begin
sock.print("* OK test server\r\n")
sock.gets
sock.gets # Integer: 0
sock.print("RUBY0001 OK TEST completed\r\n")
sock.gets
sock.gets # Integer: 2**32 - 1
sock.print("RUBY0002 OK TEST completed\r\n")
sock.gets
sock.gets # MessageSet: 1
sock.print("RUBY0003 OK TEST completed\r\n")
sock.gets
sock.gets # MessageSet: 2**32 - 1
sock.print("RUBY0004 OK TEST completed\r\n")
sock.gets
sock.gets # SequenceSet: -1 => "*"
sock.print("RUBY0005 OK TEST completed\r\n")
sock.gets # SequenceSet: 1
sock.print("RUBY0006 OK TEST completed\r\n")
sock.gets # SequenceSet: 2**32 - 1
sock.print("RUBY0007 OK TEST completed\r\n")
sock.gets # LOGOUT
sock.print("* BYE terminating connection\r\n")
sock.print("RUBY0005 OK LOGOUT completed\r\n")
sock.print("RUBY0008 OK LOGOUT completed\r\n")
ensure
sock.close
server.close
end
end
begin
# regular numbers may be any uint32
imap = Net::IMAP.new(server_addr, :port => port)
assert_raise(Net::IMAP::DataFormatError) do
imap.__send__(:send_command, "TEST", -1)
end
imap.__send__(:send_command, "TEST", 0)
imap.__send__(:send_command, "TEST", 4294967295)
imap.__send__(:send_command, "TEST", 2**32 - 1)
assert_raise(Net::IMAP::DataFormatError) do
imap.__send__(:send_command, "TEST", 4294967296)
imap.__send__(:send_command, "TEST", 2**32)
end
# MessageSet numbers may be non-zero uint32
assert_raise(Net::IMAP::DataFormatError) do
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(-1))
end
assert_raise(Net::IMAP::DataFormatError) do
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(0))
end
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(1))
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(4294967295))
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(2**32 - 1))
assert_raise(Net::IMAP::DataFormatError) do
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(2**32))
end
# SequenceSet numbers may be non-zero uint3, and -1 is translated to *
imap.__send__(:send_command, "TEST", Net::IMAP::SequenceSet.new(-1))
assert_raise(Net::IMAP::DataFormatError) do
imap.__send__(:send_command, "TEST", Net::IMAP::SequenceSet.new(0))
end
imap.__send__(:send_command, "TEST", Net::IMAP::SequenceSet.new(1))
imap.__send__(:send_command, "TEST", Net::IMAP::SequenceSet.new(2**32-1))
assert_raise(Net::IMAP::DataFormatError) do
imap.__send__(:send_command, "TEST", Net::IMAP::MessageSet.new(4294967296))
imap.__send__(:send_command, "TEST", Net::IMAP::SequenceSet.new(2**32))
end
imap.logout
ensure
Expand Down
19 changes: 19 additions & 0 deletions test/net/imap/test_imap_response_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,23 @@ def test_uidplus_copyuid__uid_mapping
)
end

def test_thread_member_to_sequence_set
# copied from the fourth example in RFC5256: (3 6 (4 23)(44 7 96))
thmember = Net::IMAP::ThreadMember.method :new
thread = thmember.(3, [
thmember.(6, [
thmember.(4, [
thmember.(23, [])
]),
thmember.(44, [
thmember.(7, [
thmember.(96, [])
])
])
])
])
expected = Net::IMAP::SequenceSet.new("3:4,6:7,23,44,96")
assert_equal(expected, thread.to_sequence_set)
end

end
Loading