1 require File.dirname(__FILE__) + '/../../spec_helper'
2 require File.dirname(__FILE__) + '/fixtures/classes'
3 require File.dirname(__FILE__) + '/shared/iteration'
5 describe "Hash#reject" do
6 it "is equivalent to hsh.dup.delete_if" do
7 h = { :a => 'a', :b => 'b', :c => 'd' }
8 h.reject { |k,v| k == 'd' }.should == (h.dup.delete_if { |k, v| k == 'd' })
11 all_args_delete_if = []
13 h.reject { |*args| all_args_reject << args }
14 h.delete_if { |*args| all_args_delete_if << args }
15 all_args_reject.should == all_args_delete_if
18 # dup doesn't copy singleton methods
20 h.reject { false }.to_a.should == [[1, 2]]
23 it "returns subclass instance for subclasses" do
24 MyHash[1 => 2, 3 => 4].reject { false }.class.should == MyHash
25 MyHash[1 => 2, 3 => 4].reject { true }.class.should == MyHash
28 it "processes entries with the same order as reject!" do
29 h = {:a => 1, :b => 2, :c => 3, :d => 4}
32 reject_bang_pairs = []
33 h.dup.reject { |*pair| reject_pairs << pair }
34 h.reject! { |*pair| reject_bang_pairs << pair }
36 reject_pairs.should == reject_bang_pairs
39 it_behaves_like(:hash_iteration_no_block, :reject)
42 describe "Hash#reject!" do
44 @hsh = {1 => 2, 3 => 4, 5 => 6}
48 it "is equivalent to delete_if if changes are made" do
49 {:a => 2}.reject! { |k,v| v > 1 }.should == ({:a => 2}.delete_if { |k, v| v > 1 })
53 all_args_delete_if = []
54 h.dup.reject! { |*args| all_args_reject << args }
55 h.dup.delete_if { |*args| all_args_delete_if << args }
56 all_args_reject.should == all_args_delete_if
59 it "returns nil if no changes were made" do
60 { :a => 1 }.reject! { |k,v| v > 1 }.should == nil
63 it "processes entries with the same order as delete_if" do
64 h = {:a => 1, :b => 2, :c => 3, :d => 4}
66 reject_bang_pairs = []
68 h.dup.reject! { |*pair| reject_bang_pairs << pair }
69 h.dup.delete_if { |*pair| delete_if_pairs << pair }
71 reject_bang_pairs.should == delete_if_pairs
74 compliant_on :ruby, :jruby do
75 it "raises a TypeError if called on a frozen instance" do
76 lambda { HashSpecs.frozen_hash.reject! { false } }.should raise_error(TypeError)
77 lambda { HashSpecs.empty_frozen_hash.reject! { true } }.should raise_error(TypeError)
81 ruby_version_is "" ... "1.8.7" do
82 it "raises a LocalJumpError when called on a non-empty hash without a block" do
83 lambda { @hsh.reject! }.should raise_error(LocalJumpError)
86 it "does not raise a LocalJumpError when called on an empty hash without a block" do
87 @empty.reject!.should == nil
91 ruby_version_is "1.8.7" do
92 it "returns an Enumerator when called on a non-empty hash without a block" do
93 @hsh.reject!.should be_kind_of(Enumerable::Enumerator)
96 it "returns an Enumerator when called on an empty hash without a block" do
97 @empty.reject!.should be_kind_of(Enumerable::Enumerator)
101 it_behaves_like(:hash_iteration_method, :reject!)