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/array/sort_by_spec.rb | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) | |
download | ruby-1d15d5f08032acf1b7bceacbb450d617ff6e0931.tar.gz |
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/array/sort_by_spec.rb')
-rw-r--r-- | spec/ruby/core/array/sort_by_spec.rb | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/spec/ruby/core/array/sort_by_spec.rb b/spec/ruby/core/array/sort_by_spec.rb new file mode 100644 index 0000000000..9f45f3ef4d --- /dev/null +++ b/spec/ruby/core/array/sort_by_spec.rb @@ -0,0 +1,52 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__) + +describe "Array#sort_by!" do + it "sorts array in place by passing each element to the given block" do + a = [-100, -2, 1, 200, 30000] + a.sort_by!{ |e| e.to_s.size } + a.should == [1, -2, 200, -100, 30000] + end + + it "returns an Enumerator if not given a block" do + (1..10).to_a.sort_by!.should be_an_instance_of(Enumerator) + end + + it "completes when supplied a block that always returns the same result" do + a = [2, 3, 5, 1, 4] + a.sort_by!{ 1 } + a.should be_an_instance_of(Array) + a.sort_by!{ 0 } + a.should be_an_instance_of(Array) + a.sort_by!{ -1 } + a.should be_an_instance_of(Array) + end + + it "raises a RuntimeError on a frozen array" do + lambda { ArraySpecs.frozen_array.sort_by! {}}.should raise_error(RuntimeError) + end + + it "raises a RuntimeError on an empty frozen array" do + lambda { ArraySpecs.empty_frozen_array.sort_by! {}}.should raise_error(RuntimeError) + end + + it "returns the specified value when it would break in the given block" do + [1, 2, 3].sort_by!{ break :a }.should == :a + end + + it "makes some modification even if finished sorting when it would break in the given block" do + partially_sorted = (1..5).map{|i| + ary = [5, 4, 3, 2, 1] + ary.sort_by!{|x,y| break if x==i; x<=>y} + ary + } + partially_sorted.any?{|ary| ary != [1, 2, 3, 4, 5]}.should be_true + end + + it "changes nothing when called on a single element array" do + [1].sort_by!(&:to_s).should == [1] + end + + it_behaves_like :enumeratorized_with_origin_size, :sort_by!, [1,2,3] +end |