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

Add test for Hash#each consistently yields a 2-element array #885

Closed
Closed
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
6 changes: 6 additions & 0 deletions core/hash/each_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
it_behaves_like :hash_each, :each
it_behaves_like :hash_iteration_no_block, :each
it_behaves_like :enumeratorized_with_origin_size, :each, { 1 => 2, 3 => 4, 5 => 6 }

ruby_version_is "3.0" do
it 'consistently yields a 2-element array' do
-> { { a: 1 }.each(&->(k, v) { }) }.should raise_error(ArgumentError)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you assert the message too? I think it'd help clarity.
Also it'd be good to test that it works fine with a lambda taking a single argument.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW it seems the same change applies to Hash#each_pair too, so could you move it to the core/hash/shared/each.rb shared spec?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's already covered actually:

ruby_version_is "3.0" do
it "always yields an Array of 2 elements, even when given a callable of arity 2" do
obj = Object.new
def obj.foo(key, value)
end
-> {
{ "a" => 1 }.send(@method, &obj.method(:foo))
}.should raise_error(ArgumentError)
-> {
{ "a" => 1 }.send(@method, &-> key, value { })
}.should raise_error(ArgumentError)
end
end

end
end
end