diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
commit | 1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch) | |
tree | a3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/core/hash/flatten_spec.rb | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) |
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory.
[Misc #13792] [ruby-core:82287]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/core/hash/flatten_spec.rb')
-rw-r--r-- | spec/ruby/core/hash/flatten_spec.rb | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/spec/ruby/core/hash/flatten_spec.rb b/spec/ruby/core/hash/flatten_spec.rb new file mode 100644 index 0000000000..60762f864d --- /dev/null +++ b/spec/ruby/core/hash/flatten_spec.rb @@ -0,0 +1,62 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Hash#flatten" do + + before :each do + @h = {plato: :greek, + witgenstein: [:austrian, :british], + russell: :welsh} + end + + it "returns an Array" do + {}.flatten.should be_an_instance_of(Array) + end + + it "returns an empty Array for an empty Hash" do + {}.flatten.should == [] + end + + it "sets each even index of the Array to a key of the Hash" do + a = @h.flatten + a[0].should == :plato + a[2].should == :witgenstein + a[4].should == :russell + end + + it "sets each odd index of the Array to the value corresponding to the previous element" do + a = @h.flatten + a[1].should == :greek + a[3].should == [:austrian, :british] + a[5].should == :welsh + end + + it "does not recursively flatten Array values when called without arguments" do + a = @h.flatten + a[3].should == [:austrian, :british] + end + + it "does not recursively flatten Hash values when called without arguments" do + @h[:russell] = {born: :wales, influenced_by: :mill } + a = @h.flatten + a[5].should_not == {born: :wales, influenced_by: :mill }.flatten + end + + it "recursively flattens Array values when called with an argument >= 2" do + a = @h.flatten(2) + a[3].should == :austrian + a[4].should == :british + end + + it "recursively flattens Array values to the given depth" do + @h[:russell] = [[:born, :wales], [:influenced_by, :mill]] + a = @h.flatten(2) + a[6].should == [:born, :wales] + a[7].should == [:influenced_by, :mill] + end + + it "raises a TypeError if given a non-Integer argument" do + lambda do + @h.flatten(Object.new) + end.should raise_error(TypeError) + end +end |