Skip to content

Commit

Permalink
✨ Add SequenceSet#deconstruct
Browse files Browse the repository at this point in the history
Returns an array with `#string` when valid and an empty array otherwise.
  • Loading branch information
nevans committed Oct 24, 2024
1 parent 43e6a66 commit 07869ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/net/imap/sequence_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ def valid_string
# Related: #valid_string, #normalized_string, #to_s
def string; @string ||= normalized_string if valid? end

# Returns an array with #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.
Expand Down
12 changes: 12 additions & 0 deletions test/net/imap/test_sequence_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 07869ee

Please sign in to comment.