From 0a17f97e54661fb85c6dabdf38c37def28affcb9 Mon Sep 17 00:00:00 2001 From: nicholas evans Date: Sat, 10 Feb 2024 14:12:38 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20SequenceSet#deconstruct?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Returns an array with `#normalized_string` when the sequence set is valid and an empty array otherwise. --- lib/net/imap/sequence_set.rb | 4 ++++ test/net/imap/test_sequence_set.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/net/imap/sequence_set.rb b/lib/net/imap/sequence_set.rb index 7e5206a1..f5da7b24 100644 --- a/lib/net/imap/sequence_set.rb +++ b/lib/net/imap/sequence_set.rb @@ -384,6 +384,10 @@ def valid_string # Related: #valid_string, #normalized_string, #to_s def string; @string ||= normalized_string if valid? end + # Returns an array with #normalized_string when valid and an empty array + # otherwise. + def deconstruct; valid? ? [normalized_string] : [] end + # Assigns a new string to #string and resets #elements to match. It # cannot be set to an empty string—assign +nil+ or use #clear instead. # The string is validated but not normalized. diff --git a/test/net/imap/test_sequence_set.rb b/test/net/imap/test_sequence_set.rb index e8460139..3949c7f3 100644 --- a/test/net/imap/test_sequence_set.rb +++ b/test/net/imap/test_sequence_set.rb @@ -779,6 +779,18 @@ def test_inspect((expected, input, freeze)) assert_equal str, set.string end + test "#deconstruct" do |data| + set = SequenceSet.new(data[:input]) + str = data[:normalize] + if str + assert_equal [str], set.deconstruct + set => SequenceSet[str] + else + assert_equal [], set.deconstruct + set => SequenceSet[] + end + end + test "#normalized_string" do |data| set = SequenceSet.new(data[:input]) assert_equal data[:normalize], set.normalized_string