Skip to content

Commit 37daa7f

Browse files
committed
✅ Split up IMAP#responses test by arguments
Two args (counting the block arg) => four tests. Only the original (no args) form is affected by `config.responses_without_block`, so the others make their assertions for every possible config setting.
1 parent 69f7fc2 commit 37daa7f

File tree

1 file changed

+107
-24
lines changed

1 file changed

+107
-24
lines changed

test/net/imap/test_imap_responses.rb

Lines changed: 107 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
class IMAPResponsesTest < Test::Unit::TestCase
88
include Net::IMAP::FakeServer::TestHelper
99

10+
CONFIG_OPTIONS = %i[
11+
silence_deprecation_warning
12+
warn
13+
raise
14+
].freeze
15+
1016
def setup
1117
Net::IMAP.config.reset
1218
@do_not_reverse_lookup = Socket.do_not_reverse_lookup
@@ -22,36 +28,113 @@ def teardown
2228
Socket.do_not_reverse_lookup = @do_not_reverse_lookup
2329
end
2430

25-
test "#responses" do
31+
def for_each_config_option(imap)
32+
original = imap.config.responses_without_block
33+
CONFIG_OPTIONS.each do |option|
34+
imap.config.responses_without_block = option
35+
yield option
36+
end
37+
ensure
38+
imap.config.responses_without_block = original
39+
end
40+
41+
# with a block: returns the block result
42+
test "#responses(&block)" do
2643
with_fake_server do |server, imap|
27-
# responses available before SELECT/EXAMINE
28-
assert_equal(%w[IMAP4REV1 NAMESPACE MOVE IDLE UTF8=ACCEPT],
29-
imap.responses("CAPABILITY", &:last))
30-
resp = imap.select "INBOX"
31-
# responses are cleared after SELECT/EXAMINE
32-
assert_equal(nil, imap.responses("CAPABILITY", &:last))
33-
assert_equal([Net::IMAP::TaggedResponse, "RUBY0001", "OK"],
34-
[resp.class, resp.tag, resp.name])
35-
assert_equal([172], imap.responses { _1["EXISTS"] })
36-
assert_equal([3857529045], imap.responses("UIDVALIDITY") { _1 })
37-
assert_equal(1, imap.responses("RECENT", &:last))
38-
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
39-
# Deprecated style, without a block:
44+
stderr = EnvUtil.verbose_warning do
45+
# Config options make no difference to responses(&block)
46+
for_each_config_option(imap) do
47+
# responses available before SELECT/EXAMINE
48+
assert_equal(%w[IMAP4REV1 NAMESPACE MOVE IDLE UTF8=ACCEPT],
49+
imap.responses { _1["CAPABILITY"].last })
50+
end
51+
# responses are cleared after SELECT/EXAMINE
52+
imap.select "INBOX"
53+
for_each_config_option(imap) do
54+
assert_equal nil, imap.responses { _1["CAPABILITY"].last }
55+
assert_equal [172], imap.responses { _1["EXISTS"].dup }
56+
assert_equal [3857529045], imap.responses { _1["UIDVALIDITY"].dup }
57+
assert_equal 1, imap.responses { _1["RECENT"].last }
58+
assert_equal(%i[Answered Flagged Deleted Seen Draft],
59+
imap.responses { _1["FLAGS"].last })
60+
end
61+
end
62+
assert_empty stderr # never warn when a block is given
63+
end
64+
end
65+
66+
# with a type and a block: returns the block result
67+
test "#responses(type, &block)" do
68+
with_fake_server do |server, imap|
69+
stderr = EnvUtil.verbose_warning do
70+
# Config options make no difference to responses(type, &block)
71+
for_each_config_option(imap) do
72+
# responses available before SELECT/EXAMINE
73+
assert_equal(%w[IMAP4REV1 NAMESPACE MOVE IDLE UTF8=ACCEPT],
74+
imap.responses("CAPABILITY", &:last))
75+
end
76+
# responses are cleared after SELECT/EXAMINE
77+
imap.select "INBOX"
78+
for_each_config_option(imap) do
79+
assert_equal nil, imap.responses("CAPABILITY", &:last)
80+
assert_equal [172], imap.responses("EXISTS", &:dup)
81+
assert_equal [3857529045], imap.responses("UIDVALIDITY", &:dup)
82+
assert_equal 1, imap.responses("RECENT", &:last)
83+
assert_equal [4392], imap.responses("UIDNEXT", &:dup)
84+
assert_equal(%i[Answered Flagged Deleted Seen Draft],
85+
imap.responses("FLAGS", &:last))
86+
end
87+
end
88+
assert_empty stderr # never warn when type or block are given
89+
end
90+
end
91+
92+
# with with a type and no block: always raise an exception
93+
test "#responses(type, &nil)" do
94+
with_fake_server do |server, imap|
95+
for_each_config_option(imap) do
96+
assert_raise(ArgumentError) do imap.responses("CAPABILITY") end
97+
end
98+
end
99+
end
100+
101+
def assert_responses_warn
102+
assert_warn(/Pass a block.*or.*clear_responses/i) do
103+
yield
104+
end
105+
end
106+
107+
# without type or block: relies on config.responses_without_block
108+
test "#responses without type or block" do
109+
with_fake_server do |server, imap|
110+
# can be configured to raise
40111
imap.config.responses_without_block = :raise
41112
assert_raise(ArgumentError) do imap.responses end
113+
# with warnings (default for v0.5)
42114
imap.config.responses_without_block = :warn
43-
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
44-
assert_warn(/Pass a block.*or.*clear_responses/i) do
45-
assert_equal(%i[Answered Flagged Deleted Seen Draft],
46-
imap.responses["FLAGS"]&.last)
115+
assert_responses_warn do assert_kind_of Hash, imap.responses end
116+
assert_responses_warn do refute imap.responses.frozen? end
117+
assert_responses_warn do refute imap.responses["CAPABILITY"].frozen? end
118+
assert_responses_warn do
119+
assert_equal(%w[IMAP4REV1 NAMESPACE MOVE IDLE UTF8=ACCEPT],
120+
imap.responses["CAPABILITY"].last)
47121
end
48-
# TODO: assert_no_warn?
122+
assert_responses_warn do imap.responses["FAKE"] = :uh_oh! end
123+
assert_responses_warn do assert_equal :uh_oh!, imap.responses["FAKE"] end
124+
assert_responses_warn do imap.responses.delete("FAKE") end
125+
assert_responses_warn do assert_equal [], imap.responses["FAKE"] end
126+
# warnings can be silenced
49127
imap.config.responses_without_block = :silence_deprecation_warning
50-
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
51-
stderr = EnvUtil.verbose_warning {
52-
assert_equal(%i[Answered Flagged Deleted Seen Draft],
53-
imap.responses["FLAGS"]&.last)
54-
}
128+
stderr = EnvUtil.verbose_warning do
129+
refute imap.responses.frozen?
130+
refute imap.responses["CAPABILITY"].frozen?
131+
assert_equal(%w[IMAP4REV1 NAMESPACE MOVE IDLE UTF8=ACCEPT],
132+
imap.responses["CAPABILITY"].last)
133+
imap.responses["FAKE"] = :uh_oh!
134+
assert_equal :uh_oh!, imap.responses["FAKE"]
135+
imap.responses.delete("FAKE")
136+
assert_equal [], imap.responses["FAKE"]
137+
end
55138
assert_empty stderr
56139
end
57140
end

0 commit comments

Comments
 (0)