mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
75bfc6440d
commit
1d15d5f080
4370 changed files with 0 additions and 0 deletions
6
spec/ruby/core/range/begin_spec.rb
Normal file
6
spec/ruby/core/range/begin_spec.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/begin', __FILE__)
|
||||
|
||||
describe "Range#begin" do
|
||||
it_behaves_like(:range_begin, :begin)
|
||||
end
|
137
spec/ruby/core/range/bsearch_spec.rb
Normal file
137
spec/ruby/core/range/bsearch_spec.rb
Normal file
|
@ -0,0 +1,137 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__)
|
||||
|
||||
describe "Range#bsearch" do
|
||||
it "returns an Enumerator when not passed a block" do
|
||||
(0..1).bsearch.should be_an_instance_of(Enumerator)
|
||||
end
|
||||
|
||||
it_behaves_like :enumeratorized_with_unknown_size, :bsearch, (1..3)
|
||||
|
||||
it "raises a TypeError if the block returns an Object" do
|
||||
lambda { (0..1).bsearch { Object.new } }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises a TypeError if the block returns a String" do
|
||||
lambda { (0..1).bsearch { "1" } }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises a TypeError if the Range has Object values" do
|
||||
value = mock("range bsearch")
|
||||
r = Range.new value, value
|
||||
|
||||
lambda { r.bsearch { true } }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises a TypeError if the Range has String values" do
|
||||
lambda { ("a".."e").bsearch { true } }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
context "with Integer values" do
|
||||
context "with a block returning true or false" do
|
||||
it "returns nil if the block returns false for every element" do
|
||||
(0...3).bsearch { |x| x > 3 }.should be_nil
|
||||
end
|
||||
|
||||
it "returns nil if the block returns nil for every element" do
|
||||
(0..3).bsearch { |x| nil }.should be_nil
|
||||
end
|
||||
|
||||
it "returns minimum element if the block returns true for every element" do
|
||||
(-2..4).bsearch { |x| x < 4 }.should == -2
|
||||
end
|
||||
|
||||
it "returns the smallest element for which block returns true" do
|
||||
(0..4).bsearch { |x| x >= 2 }.should == 2
|
||||
(-1..4).bsearch { |x| x >= 1 }.should == 1
|
||||
end
|
||||
|
||||
it "returns the last element if the block returns true for the last element" do
|
||||
(0..4).bsearch { |x| x >= 4 }.should == 4
|
||||
(0...4).bsearch { |x| x >= 3 }.should == 3
|
||||
end
|
||||
end
|
||||
|
||||
context "with a block returning negative, zero, positive numbers" do
|
||||
it "returns nil if the block returns less than zero for every element" do
|
||||
(0..3).bsearch { |x| x <=> 5 }.should be_nil
|
||||
end
|
||||
|
||||
it "returns nil if the block returns greater than zero for every element" do
|
||||
(0..3).bsearch { |x| x <=> -1 }.should be_nil
|
||||
|
||||
end
|
||||
|
||||
it "returns nil if the block never returns zero" do
|
||||
(0..3).bsearch { |x| x < 2 ? 1 : -1 }.should be_nil
|
||||
end
|
||||
|
||||
it "accepts (+/-)Float::INFINITY from the block" do
|
||||
(0..4).bsearch { |x| Float::INFINITY }.should be_nil
|
||||
(0..4).bsearch { |x| -Float::INFINITY }.should be_nil
|
||||
end
|
||||
|
||||
it "returns an element at an index for which block returns 0.0" do
|
||||
result = (0..4).bsearch { |x| x < 2 ? 1.0 : x > 2 ? -1.0 : 0.0 }
|
||||
result.should == 2
|
||||
end
|
||||
|
||||
it "returns an element at an index for which block returns 0" do
|
||||
result = (0..4).bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 }
|
||||
[1, 2].should include(result)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with Float values" do
|
||||
context "with a block returning true or false" do
|
||||
it "returns nil if the block returns false for every element" do
|
||||
(0.1...2.3).bsearch { |x| x > 3 }.should be_nil
|
||||
end
|
||||
|
||||
it "returns nil if the block returns nil for every element" do
|
||||
(-0.0..2.3).bsearch { |x| nil }.should be_nil
|
||||
end
|
||||
|
||||
it "returns minimum element if the block returns true for every element" do
|
||||
(-0.2..4.8).bsearch { |x| x < 4 }.should == -0.2
|
||||
end
|
||||
|
||||
it "returns the smallest element for which block returns true" do
|
||||
(0..4.2).bsearch { |x| x >= 2 }.should == 2
|
||||
(-1.2..4.3).bsearch { |x| x >= 1 }.should == 1
|
||||
end
|
||||
end
|
||||
|
||||
context "with a block returning negative, zero, positive numbers" do
|
||||
it "returns nil if the block returns less than zero for every element" do
|
||||
(-2.0..3.2).bsearch { |x| x <=> 5 }.should be_nil
|
||||
end
|
||||
|
||||
it "returns nil if the block returns greater than zero for every element" do
|
||||
(0.3..3.0).bsearch { |x| x <=> -1 }.should be_nil
|
||||
|
||||
end
|
||||
|
||||
it "returns nil if the block never returns zero" do
|
||||
(0.2..2.3).bsearch { |x| x < 2 ? 1 : -1 }.should be_nil
|
||||
end
|
||||
|
||||
it "accepts (+/-)Float::INFINITY from the block" do
|
||||
(0.1..4.5).bsearch { |x| Float::INFINITY }.should be_nil
|
||||
(-5.0..4.0).bsearch { |x| -Float::INFINITY }.should be_nil
|
||||
end
|
||||
|
||||
it "returns an element at an index for which block returns 0.0" do
|
||||
result = (0.0..4.0).bsearch { |x| x < 2 ? 1.0 : x > 2 ? -1.0 : 0.0 }
|
||||
result.should == 2
|
||||
end
|
||||
|
||||
it "returns an element at an index for which block returns 0" do
|
||||
result = (0.1..4.9).bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 }
|
||||
result.should >= 1
|
||||
result.should <= 2
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
11
spec/ruby/core/range/case_compare_spec.rb
Normal file
11
spec/ruby/core/range/case_compare_spec.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/cover_and_include', __FILE__)
|
||||
require File.expand_path('../shared/cover', __FILE__)
|
||||
|
||||
describe "Range#===" do
|
||||
it "returns the result of calling #include? on self" do
|
||||
range = 0...10
|
||||
range.should_receive(:include?).with(2).and_return(:true)
|
||||
(range === 2).should == :true
|
||||
end
|
||||
end
|
9
spec/ruby/core/range/cover_spec.rb
Normal file
9
spec/ruby/core/range/cover_spec.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
# -*- encoding: ascii-8bit -*-
|
||||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/cover_and_include', __FILE__)
|
||||
require File.expand_path('../shared/cover', __FILE__)
|
||||
|
||||
describe "Range#cover?" do
|
||||
it_behaves_like :range_cover_and_include, :cover?
|
||||
it_behaves_like :range_cover, :cover?
|
||||
end
|
15
spec/ruby/core/range/dup_spec.rb
Normal file
15
spec/ruby/core/range/dup_spec.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Range#dup" do
|
||||
it "duplicates the range" do
|
||||
copy = (1..3).dup
|
||||
copy.begin.should == 1
|
||||
copy.end.should == 3
|
||||
copy.exclude_end?.should == false
|
||||
|
||||
copy = ("a"..."z").dup
|
||||
copy.begin.should == "a"
|
||||
copy.end.should == "z"
|
||||
copy.exclude_end?.should == true
|
||||
end
|
||||
end
|
66
spec/ruby/core/range/each_spec.rb
Normal file
66
spec/ruby/core/range/each_spec.rb
Normal file
|
@ -0,0 +1,66 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../enumerable/shared/enumeratorized', __FILE__)
|
||||
|
||||
describe "Range#each" do
|
||||
it "passes each element to the given block by using #succ" do
|
||||
a = []
|
||||
(-5..5).each { |i| a << i }
|
||||
a.should == [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
|
||||
|
||||
a = []
|
||||
('A'..'D').each { |i| a << i }
|
||||
a.should == ['A','B','C','D']
|
||||
|
||||
a = []
|
||||
('A'...'D').each { |i| a << i }
|
||||
a.should == ['A','B','C']
|
||||
|
||||
a = []
|
||||
(0xfffd...0xffff).each { |i| a << i }
|
||||
a.should == [0xfffd, 0xfffe]
|
||||
|
||||
y = mock('y')
|
||||
x = mock('x')
|
||||
x.should_receive(:<=>).with(y).any_number_of_times.and_return(-1)
|
||||
x.should_receive(:<=>).with(x).any_number_of_times.and_return(0)
|
||||
x.should_receive(:succ).any_number_of_times.and_return(y)
|
||||
y.should_receive(:<=>).with(x).any_number_of_times.and_return(1)
|
||||
y.should_receive(:<=>).with(y).any_number_of_times.and_return(0)
|
||||
|
||||
a = []
|
||||
(x..y).each { |i| a << i }
|
||||
a.should == [x, y]
|
||||
end
|
||||
|
||||
it "raises a TypeError if the first element does not respond to #succ" do
|
||||
lambda { (0.5..2.4).each { |i| i } }.should raise_error(TypeError)
|
||||
|
||||
b = mock('x')
|
||||
(a = mock('1')).should_receive(:<=>).with(b).and_return(1)
|
||||
|
||||
lambda { (a..b).each { |i| i } }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "returns self" do
|
||||
range = 1..10
|
||||
range.each{}.should equal(range)
|
||||
end
|
||||
|
||||
it "returns an enumerator when no block given" do
|
||||
enum = (1..3).each
|
||||
enum.should be_an_instance_of(Enumerator)
|
||||
enum.to_a.should == [1, 2, 3]
|
||||
end
|
||||
|
||||
it "raises a TypeError if the first element is a Time object" do
|
||||
t = Time.now
|
||||
lambda { (t..t+1).each { |i| i } }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "passes each Symbol element by using #succ" do
|
||||
(:aa..:ac).each.to_a.should == [:aa, :ab, :ac]
|
||||
(:aa...:ac).each.to_a.should == [:aa, :ab]
|
||||
end
|
||||
|
||||
it_behaves_like :enumeratorized_with_origin_size, :each, (1..3)
|
||||
end
|
6
spec/ruby/core/range/end_spec.rb
Normal file
6
spec/ruby/core/range/end_spec.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/end', __FILE__)
|
||||
|
||||
describe "Range#end" do
|
||||
it_behaves_like(:range_end, :end)
|
||||
end
|
10
spec/ruby/core/range/eql_spec.rb
Normal file
10
spec/ruby/core/range/eql_spec.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/equal_value', __FILE__)
|
||||
|
||||
describe "Range#eql?" do
|
||||
it_behaves_like(:range_eql, :eql?)
|
||||
|
||||
it "returns false if the endpoints are not eql?" do
|
||||
(0..1).send(@method, 0..1.0).should == false
|
||||
end
|
||||
end
|
10
spec/ruby/core/range/equal_value_spec.rb
Normal file
10
spec/ruby/core/range/equal_value_spec.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/equal_value', __FILE__)
|
||||
|
||||
describe "Range#==" do
|
||||
it_behaves_like(:range_eql, :==)
|
||||
|
||||
it "returns true if the endpoints are ==" do
|
||||
(0..1).send(@method, 0..1.0).should == true
|
||||
end
|
||||
end
|
19
spec/ruby/core/range/exclude_end_spec.rb
Normal file
19
spec/ruby/core/range/exclude_end_spec.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Range#exclude_end?" do
|
||||
it "returns false if the range does not exclude the end value" do
|
||||
(-2..2).exclude_end?.should == false
|
||||
('A'..'B').exclude_end?.should == false
|
||||
(0.5..2.4).exclude_end?.should == false
|
||||
(0xfffd..0xffff).exclude_end?.should == false
|
||||
Range.new(0, 1).exclude_end?.should == false
|
||||
end
|
||||
|
||||
it "returns true if the range excludes the end value" do
|
||||
(0...5).exclude_end?.should == true
|
||||
('A'...'B').exclude_end?.should == true
|
||||
(0.5...2.4).exclude_end?.should == true
|
||||
(0xfffd...0xffff).exclude_end?.should == true
|
||||
Range.new(0, 1, true).exclude_end?.should == true
|
||||
end
|
||||
end
|
49
spec/ruby/core/range/first_spec.rb
Normal file
49
spec/ruby/core/range/first_spec.rb
Normal file
|
@ -0,0 +1,49 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/begin', __FILE__)
|
||||
|
||||
describe "Range#first" do
|
||||
it_behaves_like(:range_begin, :first)
|
||||
|
||||
it "returns the specified number of elements from the beginning" do
|
||||
(0..2).first(2).should == [0, 1]
|
||||
end
|
||||
|
||||
it "returns an empty array for an empty Range" do
|
||||
(0...0).first(2).should == []
|
||||
end
|
||||
|
||||
it "returns an empty array when passed zero" do
|
||||
(0..2).first(0).should == []
|
||||
end
|
||||
|
||||
it "returns all elements in the range when count exceeds the number of elements" do
|
||||
(0..2).first(4).should == [0, 1, 2]
|
||||
end
|
||||
|
||||
it "raises an ArgumentError when count is negative" do
|
||||
lambda { (0..2).first(-1) }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "calls #to_int to convert the argument" do
|
||||
obj = mock_int(2)
|
||||
(3..7).first(obj).should == [3, 4]
|
||||
end
|
||||
|
||||
it "raises a TypeError if #to_int does not return an Integer" do
|
||||
obj = mock("to_int")
|
||||
obj.should_receive(:to_int).and_return("1")
|
||||
lambda { (2..3).first(obj) }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "truncates the value when passed a Float" do
|
||||
(2..9).first(2.8).should == [2, 3]
|
||||
end
|
||||
|
||||
it "raises a TypeError when passed nil" do
|
||||
lambda { (2..3).first(nil) }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises a TypeError when passed a String" do
|
||||
lambda { (2..3).first("1") }.should raise_error(TypeError)
|
||||
end
|
||||
end
|
65
spec/ruby/core/range/fixtures/classes.rb
Normal file
65
spec/ruby/core/range/fixtures/classes.rb
Normal file
|
@ -0,0 +1,65 @@
|
|||
module RangeSpecs
|
||||
class TenfoldSucc
|
||||
include Comparable
|
||||
|
||||
attr_reader :n
|
||||
|
||||
def initialize(n)
|
||||
@n = n
|
||||
end
|
||||
|
||||
def <=>(other)
|
||||
@n <=> other.n
|
||||
end
|
||||
|
||||
def succ
|
||||
self.class.new(@n * 10)
|
||||
end
|
||||
end
|
||||
|
||||
# Custom Range classes Xs and Ys
|
||||
class Custom
|
||||
include Comparable
|
||||
attr_reader :length
|
||||
|
||||
def initialize(n)
|
||||
@length = n
|
||||
end
|
||||
|
||||
def eql?(other)
|
||||
inspect.eql? other.inspect
|
||||
end
|
||||
alias :== :eql?
|
||||
|
||||
def inspect
|
||||
'custom'
|
||||
end
|
||||
|
||||
def <=>(other)
|
||||
@length <=> other.length
|
||||
end
|
||||
end
|
||||
|
||||
class Xs < Custom # represent a string of 'x's
|
||||
def succ
|
||||
Xs.new(@length + 1)
|
||||
end
|
||||
|
||||
def inspect
|
||||
'x' * @length
|
||||
end
|
||||
end
|
||||
|
||||
class Ys < Custom # represent a string of 'y's
|
||||
def succ
|
||||
Ys.new(@length + 1)
|
||||
end
|
||||
|
||||
def inspect
|
||||
'y' * @length
|
||||
end
|
||||
end
|
||||
|
||||
class MyRange < Range
|
||||
end
|
||||
end
|
24
spec/ruby/core/range/hash_spec.rb
Normal file
24
spec/ruby/core/range/hash_spec.rb
Normal file
|
@ -0,0 +1,24 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Range#hash" do
|
||||
it "is provided" do
|
||||
(0..1).respond_to?(:hash).should == true
|
||||
('A'..'Z').respond_to?(:hash).should == true
|
||||
(0xfffd..0xffff).respond_to?(:hash).should == true
|
||||
(0.5..2.4).respond_to?(:hash).should == true
|
||||
end
|
||||
|
||||
it "generates the same hash values for Ranges with the same start, end and exclude_end? values" do
|
||||
(0..1).hash.should == (0..1).hash
|
||||
(0...10).hash.should == (0...10).hash
|
||||
(0..10).hash.should_not == (0...10).hash
|
||||
end
|
||||
|
||||
it "generates a Fixnum for the hash value" do
|
||||
(0..0).hash.should be_an_instance_of(Fixnum)
|
||||
(0..1).hash.should be_an_instance_of(Fixnum)
|
||||
(0...10).hash.should be_an_instance_of(Fixnum)
|
||||
(0..10).hash.should be_an_instance_of(Fixnum)
|
||||
end
|
||||
|
||||
end
|
10
spec/ruby/core/range/include_spec.rb
Normal file
10
spec/ruby/core/range/include_spec.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
# -*- encoding: ascii-8bit -*-
|
||||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/cover_and_include', __FILE__)
|
||||
require File.expand_path('../shared/include', __FILE__)
|
||||
require File.expand_path('../shared/cover', __FILE__)
|
||||
|
||||
describe "Range#include?" do
|
||||
it_behaves_like :range_cover_and_include, :include?
|
||||
it_behaves_like :range_include, :include?
|
||||
end
|
41
spec/ruby/core/range/initialize_spec.rb
Normal file
41
spec/ruby/core/range/initialize_spec.rb
Normal file
|
@ -0,0 +1,41 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Range#initialize" do
|
||||
before do
|
||||
@range = Range.allocate
|
||||
end
|
||||
|
||||
it "is private" do
|
||||
Range.should have_private_instance_method("initialize")
|
||||
end
|
||||
|
||||
it "initializes correctly the Range object when given 2 arguments" do
|
||||
lambda { @range.send(:initialize, 0, 1) }.should_not raise_error
|
||||
end
|
||||
|
||||
it "initializes correctly the Range object when given 3 arguments" do
|
||||
lambda { @range.send(:initialize, 0, 1, true) }.should_not raise_error
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if passed without or with only one argument" do
|
||||
lambda { @range.send(:initialize) }.should raise_error(ArgumentError)
|
||||
lambda { @range.send(:initialize, 1) }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if passed with four or more arguments" do
|
||||
lambda { @range.send(:initialize, 1, 3, 5, 7) }.should raise_error(ArgumentError)
|
||||
lambda { @range.send(:initialize, 1, 3, 5, 7, 9) }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "raises a NameError if called on an already initialized Range" do
|
||||
lambda { (0..1).send(:initialize, 1, 3) }.should raise_error(NameError)
|
||||
lambda { (0..1).send(:initialize, 1, 3, true) }.should raise_error(NameError)
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if arguments don't respond to <=>" do
|
||||
o1 = Object.new
|
||||
o2 = Object.new
|
||||
|
||||
lambda { @range.send(:initialize, o1, o2) }.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
26
spec/ruby/core/range/inspect_spec.rb
Normal file
26
spec/ruby/core/range/inspect_spec.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Range#inspect" do
|
||||
it "provides a printable form, using #inspect to convert the start and end objects" do
|
||||
('A'..'Z').inspect.should == '"A".."Z"'
|
||||
('A'...'Z').inspect.should == '"A"..."Z"'
|
||||
|
||||
(0..21).inspect.should == "0..21"
|
||||
(-8..0).inspect.should == "-8..0"
|
||||
(-411..959).inspect.should == "-411..959"
|
||||
(0xfff..0xfffff).inspect.should == "4095..1048575"
|
||||
(0.5..2.4).inspect.should == "0.5..2.4"
|
||||
end
|
||||
|
||||
it "returns a tainted string if either end is tainted" do
|
||||
(("a".taint)..."c").inspect.tainted?.should be_true
|
||||
("a"...("c".taint)).inspect.tainted?.should be_true
|
||||
("a"..."c").taint.inspect.tainted?.should be_true
|
||||
end
|
||||
|
||||
it "returns a untrusted string if either end is untrusted" do
|
||||
(("a".untrust)..."c").inspect.untrusted?.should be_true
|
||||
("a"...("c".untrust)).inspect.untrusted?.should be_true
|
||||
("a"..."c").untrust.inspect.untrusted?.should be_true
|
||||
end
|
||||
end
|
49
spec/ruby/core/range/last_spec.rb
Normal file
49
spec/ruby/core/range/last_spec.rb
Normal file
|
@ -0,0 +1,49 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/end', __FILE__)
|
||||
|
||||
describe "Range#last" do
|
||||
it_behaves_like(:range_end, :last)
|
||||
|
||||
it "returns the specified number of elements from the end" do
|
||||
(1..5).last(3).should == [3, 4, 5]
|
||||
end
|
||||
|
||||
it "returns an empty array for an empty Range" do
|
||||
(0...0).last(2).should == []
|
||||
end
|
||||
|
||||
it "returns an empty array when passed zero" do
|
||||
(0..2).last(0).should == []
|
||||
end
|
||||
|
||||
it "returns all elements in the range when count exceeds the number of elements" do
|
||||
(2..4).last(5).should == [2, 3, 4]
|
||||
end
|
||||
|
||||
it "raises an ArgumentError when count is negative" do
|
||||
lambda { (0..2).last(-1) }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "calls #to_int to convert the argument" do
|
||||
obj = mock_int(2)
|
||||
(3..7).last(obj).should == [6, 7]
|
||||
end
|
||||
|
||||
it "raises a TypeError if #to_int does not return an Integer" do
|
||||
obj = mock("to_int")
|
||||
obj.should_receive(:to_int).and_return("1")
|
||||
lambda { (2..3).last(obj) }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "truncates the value when passed a Float" do
|
||||
(2..9).last(2.8).should == [8, 9]
|
||||
end
|
||||
|
||||
it "raises a TypeError when passed nil" do
|
||||
lambda { (2..3).last(nil) }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises a TypeError when passed a String" do
|
||||
lambda { (2..3).last("1") }.should raise_error(TypeError)
|
||||
end
|
||||
end
|
82
spec/ruby/core/range/max_spec.rb
Normal file
82
spec/ruby/core/range/max_spec.rb
Normal file
|
@ -0,0 +1,82 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Range#max" do
|
||||
it "returns the maximum value in the range when called with no arguments" do
|
||||
(1..10).max.should == 10
|
||||
(1...10).max.should == 9
|
||||
(0...2**64).max.should == 18446744073709551615
|
||||
('f'..'l').max.should == 'l'
|
||||
('a'...'f').max.should == 'e'
|
||||
end
|
||||
|
||||
it "returns the maximum value in the Float range when called with no arguments" do
|
||||
(303.20..908.1111).max.should == 908.1111
|
||||
end
|
||||
|
||||
it "raises TypeError when called on an exclusive range and a non Integer value" do
|
||||
lambda { (303.20...908.1111).max }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "returns nil when the endpoint is less than the start point" do
|
||||
(100..10).max.should be_nil
|
||||
('z'..'l').max.should be_nil
|
||||
end
|
||||
|
||||
it "returns nil when the endpoint equals the start point and the range is exclusive" do
|
||||
(5...5).max.should be_nil
|
||||
end
|
||||
|
||||
it "returns the endpoint when the endpoint equals the start point and the range is inclusive" do
|
||||
(5..5).max.should equal(5)
|
||||
end
|
||||
|
||||
it "returns nil when the endpoint is less than the start point in a Float range" do
|
||||
(3003.20..908.1111).max.should be_nil
|
||||
end
|
||||
|
||||
it "returns end point when the range is Time..Time(included end point)" do
|
||||
time_start = Time.now
|
||||
time_end = Time.now + 1.0
|
||||
(time_start..time_end).max.should equal(time_end)
|
||||
end
|
||||
|
||||
it "raises TypeError when called on a Time...Time(excluded end point)" do
|
||||
time_start = Time.now
|
||||
time_end = Time.now + 1.0
|
||||
lambda { (time_start...time_end).max }.should raise_error(TypeError)
|
||||
end
|
||||
end
|
||||
|
||||
describe "Range#max given a block" do
|
||||
it "passes each pair of values in the range to the block" do
|
||||
acc = []
|
||||
(1..10).max {|a,b| acc << [a,b]; a }
|
||||
acc.flatten!
|
||||
(1..10).each do |value|
|
||||
acc.include?(value).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
it "passes each pair of elements to the block in reversed order" do
|
||||
acc = []
|
||||
(1..5).max {|a,b| acc << [a,b]; a }
|
||||
acc.should == [[2,1],[3,2], [4,3], [5, 4]]
|
||||
end
|
||||
|
||||
it "calls #> and #< on the return value of the block" do
|
||||
obj = mock('obj')
|
||||
obj.should_receive(:>).exactly(2).times
|
||||
obj.should_receive(:<).exactly(2).times
|
||||
(1..3).max {|a,b| obj }
|
||||
end
|
||||
|
||||
it "returns the element the block determines to be the maximum" do
|
||||
(1..3).max {|a,b| -3 }.should == 1
|
||||
end
|
||||
|
||||
it "returns nil when the endpoint is less than the start point" do
|
||||
(100..10).max {|x,y| x <=> y}.should be_nil
|
||||
('z'..'l').max {|x,y| x <=> y}.should be_nil
|
||||
(5...5).max {|x,y| x <=> y}.should be_nil
|
||||
end
|
||||
end
|
10
spec/ruby/core/range/member_spec.rb
Normal file
10
spec/ruby/core/range/member_spec.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
# -*- encoding: ascii-8bit -*-
|
||||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../shared/cover_and_include', __FILE__)
|
||||
require File.expand_path('../shared/include', __FILE__)
|
||||
require File.expand_path('../shared/cover', __FILE__)
|
||||
|
||||
describe "Range#member?" do
|
||||
it_behaves_like :range_cover_and_include, :member?
|
||||
it_behaves_like :range_include, :member?
|
||||
end
|
75
spec/ruby/core/range/min_spec.rb
Normal file
75
spec/ruby/core/range/min_spec.rb
Normal file
|
@ -0,0 +1,75 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Range#min" do
|
||||
it "returns the minimum value in the range when called with no arguments" do
|
||||
(1..10).min.should == 1
|
||||
('f'..'l').min.should == 'f'
|
||||
end
|
||||
|
||||
it "returns the minimum value in the Float range when called with no arguments" do
|
||||
(303.20..908.1111).min.should == 303.20
|
||||
end
|
||||
|
||||
it "returns nil when the start point is greater than the endpoint" do
|
||||
(100..10).min.should be_nil
|
||||
('z'..'l').min.should be_nil
|
||||
end
|
||||
|
||||
it "returns nil when the endpoint equals the start point and the range is exclusive" do
|
||||
(7...7).min.should be_nil
|
||||
end
|
||||
|
||||
it "returns the start point when the endpoint equals the start point and the range is inclusive" do
|
||||
(7..7).min.should equal(7)
|
||||
end
|
||||
|
||||
it "returns nil when the start point is greater than the endpoint in a Float range" do
|
||||
(3003.20..908.1111).min.should be_nil
|
||||
end
|
||||
|
||||
it "returns start point when the range is Time..Time(included end point)" do
|
||||
time_start = Time.now
|
||||
time_end = Time.now + 1.0
|
||||
(time_start..time_end).min.should equal(time_start)
|
||||
end
|
||||
|
||||
it "returns start point when the range is Time...Time(excluded end point)" do
|
||||
time_start = Time.now
|
||||
time_end = Time.now + 1.0
|
||||
(time_start...time_end).min.should equal(time_start)
|
||||
end
|
||||
end
|
||||
|
||||
describe "Range#min given a block" do
|
||||
it "passes each pair of values in the range to the block" do
|
||||
acc = []
|
||||
(1..10).min {|a,b| acc << [a,b]; a }
|
||||
acc.flatten!
|
||||
(1..10).each do |value|
|
||||
acc.include?(value).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
it "passes each pair of elements to the block where the first argument is the current element, and the last is the first element" do
|
||||
acc = []
|
||||
(1..5).min {|a,b| acc << [a,b]; a }
|
||||
acc.should == [[2, 1], [3, 1], [4, 1], [5, 1]]
|
||||
end
|
||||
|
||||
it "calls #> and #< on the return value of the block" do
|
||||
obj = mock('obj')
|
||||
obj.should_receive(:>).exactly(2).times
|
||||
obj.should_receive(:<).exactly(2).times
|
||||
(1..3).min {|a,b| obj }
|
||||
end
|
||||
|
||||
it "returns the element the block determines to be the minimum" do
|
||||
(1..3).min {|a,b| -3 }.should == 3
|
||||
end
|
||||
|
||||
it "returns nil when the start point is greater than the endpoint" do
|
||||
(100..10).min {|x,y| x <=> y}.should be_nil
|
||||
('z'..'l').min {|x,y| x <=> y}.should be_nil
|
||||
(7...7).min {|x,y| x <=> y}.should be_nil
|
||||
end
|
||||
end
|
34
spec/ruby/core/range/new_spec.rb
Normal file
34
spec/ruby/core/range/new_spec.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Range.new" do
|
||||
it "constructs a range using the given start and end" do
|
||||
range = Range.new('a', 'c')
|
||||
range.should == ('a'..'c')
|
||||
|
||||
range.first.should == 'a'
|
||||
range.last.should == 'c'
|
||||
end
|
||||
|
||||
it "includes the end object when the third parameter is omitted or false" do
|
||||
Range.new('a', 'c').to_a.should == ['a', 'b', 'c']
|
||||
Range.new(1, 3).to_a.should == [1, 2, 3]
|
||||
|
||||
Range.new('a', 'c', false).to_a.should == ['a', 'b', 'c']
|
||||
Range.new(1, 3, false).to_a.should == [1, 2, 3]
|
||||
|
||||
Range.new('a', 'c', true).to_a.should == ['a', 'b']
|
||||
Range.new(1, 3, 1).to_a.should == [1, 2]
|
||||
|
||||
Range.new(1, 3, mock('[1,2]')).to_a.should == [1, 2]
|
||||
Range.new(1, 3, :test).to_a.should == [1, 2]
|
||||
end
|
||||
|
||||
it "raises an ArgumentError when the given start and end can't be compared by using #<=>" do
|
||||
lambda { Range.new(1, mock('x')) }.should raise_error(ArgumentError)
|
||||
lambda { Range.new(mock('x'), mock('y')) }.should raise_error(ArgumentError)
|
||||
|
||||
b = mock('x')
|
||||
(a = mock('nil')).should_receive(:<=>).with(b).and_return(nil)
|
||||
lambda { Range.new(a, b) }.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
7
spec/ruby/core/range/range_spec.rb
Normal file
7
spec/ruby/core/range/range_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Range" do
|
||||
it "includes Enumerable" do
|
||||
Range.include?(Enumerable).should == true
|
||||
end
|
||||
end
|
10
spec/ruby/core/range/shared/begin.rb
Normal file
10
spec/ruby/core/range/shared/begin.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
describe :range_begin, shared: true do
|
||||
it "returns the first element of self" do
|
||||
(-1..1).send(@method).should == -1
|
||||
(0..1).send(@method).should == 0
|
||||
(0xffff...0xfffff).send(@method).should == 65535
|
||||
('Q'..'T').send(@method).should == 'Q'
|
||||
('Q'...'T').send(@method).should == 'Q'
|
||||
(0.5..2.4).send(@method).should == 0.5
|
||||
end
|
||||
end
|
93
spec/ruby/core/range/shared/cover.rb
Normal file
93
spec/ruby/core/range/shared/cover.rb
Normal file
|
@ -0,0 +1,93 @@
|
|||
# -*- encoding: ascii-8bit -*-
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../fixtures/classes', __FILE__)
|
||||
|
||||
describe :range_cover, shared: true do
|
||||
it "uses the range element's <=> to make the comparison" do
|
||||
a = mock('a')
|
||||
a.should_receive(:<=>).twice.and_return(-1,-1)
|
||||
(a..'z').send(@method, 'b').should be_true
|
||||
end
|
||||
|
||||
it "uses a continuous inclusion test" do
|
||||
('a'..'f').send(@method, 'aa').should be_true
|
||||
('a'..'f').send(@method, 'babe').should be_true
|
||||
('a'..'f').send(@method, 'baby').should be_true
|
||||
('a'..'f').send(@method, 'ga').should be_false
|
||||
(-10..-2).send(@method, -2.5).should be_true
|
||||
end
|
||||
|
||||
describe "on string elements" do
|
||||
it "returns true if other is matched by element.succ" do
|
||||
('a'..'c').send(@method, 'b').should be_true
|
||||
('a'...'c').send(@method, 'b').should be_true
|
||||
end
|
||||
|
||||
it "returns true if other is not matched by element.succ" do
|
||||
('a'..'c').send(@method, 'bc').should be_true
|
||||
('a'...'c').send(@method, 'bc').should be_true
|
||||
end
|
||||
end
|
||||
|
||||
describe "with weird succ" do
|
||||
describe "when included end value" do
|
||||
before :each do
|
||||
@range = RangeSpecs::TenfoldSucc.new(1)..RangeSpecs::TenfoldSucc.new(99)
|
||||
end
|
||||
|
||||
it "returns false if other is less than first element" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(0)).should be_false
|
||||
end
|
||||
|
||||
it "returns true if other is equal as first element" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(1)).should be_true
|
||||
end
|
||||
|
||||
it "returns true if other is matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(10)).should be_true
|
||||
end
|
||||
|
||||
it "returns true if other is not matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(2)).should be_true
|
||||
end
|
||||
|
||||
it "returns true if other is equal as last element but not matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(99)).should be_true
|
||||
end
|
||||
|
||||
it "returns false if other is greater than last element but matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(100)).should be_false
|
||||
end
|
||||
end
|
||||
|
||||
describe "when excluded end value" do
|
||||
before :each do
|
||||
@range = RangeSpecs::TenfoldSucc.new(1)...RangeSpecs::TenfoldSucc.new(99)
|
||||
end
|
||||
|
||||
it "returns false if other is less than first element" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(0)).should be_false
|
||||
end
|
||||
|
||||
it "returns true if other is equal as first element" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(1)).should be_true
|
||||
end
|
||||
|
||||
it "returns true if other is matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(10)).should be_true
|
||||
end
|
||||
|
||||
it "returns true if other is not matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(2)).should be_true
|
||||
end
|
||||
|
||||
it "returns false if other is equal as last element but not matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(99)).should be_false
|
||||
end
|
||||
|
||||
it "returns false if other is greater than last element but matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(100)).should be_false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
66
spec/ruby/core/range/shared/cover_and_include.rb
Normal file
66
spec/ruby/core/range/shared/cover_and_include.rb
Normal file
|
@ -0,0 +1,66 @@
|
|||
# -*- encoding: ascii-8bit -*-
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
|
||||
describe :range_cover_and_include, shared: true do
|
||||
it "returns true if other is an element of self" do
|
||||
(0..5).send(@method, 2).should == true
|
||||
(-5..5).send(@method, 0).should == true
|
||||
(-1...1).send(@method, 10.5).should == false
|
||||
(-10..-2).send(@method, -2.5).should == true
|
||||
('C'..'X').send(@method, 'M').should == true
|
||||
('C'..'X').send(@method, 'A').should == false
|
||||
('B'...'W').send(@method, 'W').should == false
|
||||
('B'...'W').send(@method, 'Q').should == true
|
||||
(0xffff..0xfffff).send(@method, 0xffffd).should == true
|
||||
(0xffff..0xfffff).send(@method, 0xfffd).should == false
|
||||
(0.5..2.4).send(@method, 2).should == true
|
||||
(0.5..2.4).send(@method, 2.5).should == false
|
||||
(0.5..2.4).send(@method, 2.4).should == true
|
||||
(0.5...2.4).send(@method, 2.4).should == false
|
||||
end
|
||||
|
||||
it "compares values using <=>" do
|
||||
rng = (1..5)
|
||||
m = mock("int")
|
||||
m.should_receive(:coerce).and_return([1, 2])
|
||||
m.should_receive(:<=>).and_return(1)
|
||||
|
||||
rng.send(@method, m).should be_false
|
||||
end
|
||||
|
||||
it "raises an ArgumentError without exactly one argument" do
|
||||
lambda{ (1..2).send(@method) }.should raise_error(ArgumentError)
|
||||
lambda{ (1..2).send(@method, 1, 2) }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "returns true if argument is equal to the first value of the range" do
|
||||
(0..5).send(@method, 0).should be_true
|
||||
('f'..'s').send(@method, 'f').should be_true
|
||||
end
|
||||
|
||||
it "returns true if argument is equal to the last value of the range" do
|
||||
(0..5).send(@method, 5).should be_true
|
||||
(0...5).send(@method, 4).should be_true
|
||||
('f'..'s').send(@method, 's').should be_true
|
||||
end
|
||||
|
||||
it "returns true if argument is less than the last value of the range and greater than the first value" do
|
||||
(20..30).send(@method, 28).should be_true
|
||||
('e'..'h').send(@method, 'g').should be_true
|
||||
("\u{999}".."\u{9999}").send @method, "\u{9995}"
|
||||
end
|
||||
|
||||
it "returns true if argument is sole element in the range" do
|
||||
(30..30).send(@method, 30).should be_true
|
||||
end
|
||||
|
||||
it "returns false if range is empty" do
|
||||
(30...30).send(@method, 30).should be_false
|
||||
(30...30).send(@method, nil).should be_false
|
||||
end
|
||||
|
||||
it "returns false if the range does not contain the argument" do
|
||||
('A'..'C').send(@method, 20.9).should be_false
|
||||
('A'...'C').send(@method, 'C').should be_false
|
||||
end
|
||||
end
|
10
spec/ruby/core/range/shared/end.rb
Normal file
10
spec/ruby/core/range/shared/end.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
describe :range_end, shared: true do
|
||||
it "end returns the last element of self" do
|
||||
(-1..1).send(@method).should == 1
|
||||
(0..1).send(@method).should == 1
|
||||
("A".."Q").send(@method).should == "Q"
|
||||
("A"..."Q").send(@method).should == "Q"
|
||||
(0xffff...0xfffff).send(@method).should == 1048575
|
||||
(0.5..2.4).send(@method).should == 2.4
|
||||
end
|
||||
end
|
45
spec/ruby/core/range/shared/equal_value.rb
Normal file
45
spec/ruby/core/range/shared/equal_value.rb
Normal file
|
@ -0,0 +1,45 @@
|
|||
require File.expand_path('../../fixtures/classes', __FILE__)
|
||||
|
||||
describe :range_eql, shared: true do
|
||||
it "returns true if other has same begin, end, and exclude_end? values" do
|
||||
(0..2).send(@method, 0..2).should == true
|
||||
('G'..'M').send(@method,'G'..'M').should == true
|
||||
(0.5..2.4).send(@method, 0.5..2.4).should == true
|
||||
(5..10).send(@method, Range.new(5,10)).should == true
|
||||
('D'..'V').send(@method, Range.new('D','V')).should == true
|
||||
(0.5..2.4).send(@method, Range.new(0.5, 2.4)).should == true
|
||||
(0xffff..0xfffff).send(@method, 0xffff..0xfffff).should == true
|
||||
(0xffff..0xfffff).send(@method, Range.new(0xffff,0xfffff)).should == true
|
||||
|
||||
a = RangeSpecs::Xs.new(3)..RangeSpecs::Xs.new(5)
|
||||
b = Range.new(RangeSpecs::Xs.new(3), RangeSpecs::Xs.new(5))
|
||||
a.send(@method, b).should == true
|
||||
end
|
||||
|
||||
it "returns false if one of the attributes differs" do
|
||||
('Q'..'X').send(@method, 'A'..'C').should == false
|
||||
('Q'...'X').send(@method, 'Q'..'W').should == false
|
||||
('Q'..'X').send(@method, 'Q'...'X').should == false
|
||||
(0.5..2.4).send(@method, 0.5...2.4).should == false
|
||||
(1482..1911).send(@method, 1482...1911).should == false
|
||||
(0xffff..0xfffff).send(@method, 0xffff...0xfffff).should == false
|
||||
|
||||
a = RangeSpecs::Xs.new(3)..RangeSpecs::Xs.new(5)
|
||||
b = Range.new(RangeSpecs::Ys.new(3), RangeSpecs::Ys.new(5))
|
||||
a.send(@method, b).should == false
|
||||
end
|
||||
|
||||
it "returns false if other is not a Range" do
|
||||
(1..10).send(@method, 1).should == false
|
||||
(1..10).send(@method, 'a').should == false
|
||||
(1..10).send(@method, mock('x')).should == false
|
||||
end
|
||||
|
||||
it "returns true for subclasses of Range" do
|
||||
Range.new(1, 2).send(@method, RangeSpecs::MyRange.new(1, 2)).should == true
|
||||
|
||||
a = Range.new(RangeSpecs::Xs.new(3), RangeSpecs::Xs.new(5))
|
||||
b = RangeSpecs::MyRange.new(RangeSpecs::Xs.new(3), RangeSpecs::Xs.new(5))
|
||||
a.send(@method, b).should == true
|
||||
end
|
||||
end
|
91
spec/ruby/core/range/shared/include.rb
Normal file
91
spec/ruby/core/range/shared/include.rb
Normal file
|
@ -0,0 +1,91 @@
|
|||
# -*- encoding: ascii-8bit -*-
|
||||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../fixtures/classes', __FILE__)
|
||||
|
||||
describe :range_include, shared: true do
|
||||
describe "on string elements" do
|
||||
it "returns true if other is matched by element.succ" do
|
||||
('a'..'c').send(@method, 'b').should be_true
|
||||
('a'...'c').send(@method, 'b').should be_true
|
||||
end
|
||||
|
||||
it "returns false if other is not matched by element.succ" do
|
||||
('a'..'c').send(@method, 'bc').should be_false
|
||||
('a'...'c').send(@method, 'bc').should be_false
|
||||
end
|
||||
end
|
||||
|
||||
describe "with weird succ" do
|
||||
describe "when included end value" do
|
||||
before :each do
|
||||
@range = RangeSpecs::TenfoldSucc.new(1)..RangeSpecs::TenfoldSucc.new(99)
|
||||
end
|
||||
|
||||
it "returns false if other is less than first element" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(0)).should be_false
|
||||
end
|
||||
|
||||
it "returns true if other is equal as first element" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(1)).should be_true
|
||||
end
|
||||
|
||||
it "returns true if other is matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(10)).should be_true
|
||||
end
|
||||
|
||||
it "returns false if other is not matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(2)).should be_false
|
||||
end
|
||||
|
||||
it "returns false if other is equal as last element but not matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(99)).should be_false
|
||||
end
|
||||
|
||||
it "returns false if other is greater than last element but matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(100)).should be_false
|
||||
end
|
||||
end
|
||||
|
||||
describe "when excluded end value" do
|
||||
before :each do
|
||||
@range = RangeSpecs::TenfoldSucc.new(1)...RangeSpecs::TenfoldSucc.new(99)
|
||||
end
|
||||
|
||||
it "returns false if other is less than first element" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(0)).should be_false
|
||||
end
|
||||
|
||||
it "returns true if other is equal as first element" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(1)).should be_true
|
||||
end
|
||||
|
||||
it "returns true if other is matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(10)).should be_true
|
||||
end
|
||||
|
||||
it "returns false if other is not matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(2)).should be_false
|
||||
end
|
||||
|
||||
it "returns false if other is equal as last element but not matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(99)).should be_false
|
||||
end
|
||||
|
||||
it "returns false if other is greater than last element but matched by element.succ" do
|
||||
@range.send(@method, RangeSpecs::TenfoldSucc.new(100)).should be_false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "with Time endpoints" do
|
||||
it "uses cover? logic" do
|
||||
now = Time.now
|
||||
range = (now..(now + 60))
|
||||
|
||||
range.include?(now).should == true
|
||||
range.include?(now - 1).should == false
|
||||
range.include?(now + 60).should == true
|
||||
range.include?(now + 61).should == false
|
||||
end
|
||||
end
|
||||
end
|
31
spec/ruby/core/range/size_spec.rb
Normal file
31
spec/ruby/core/range/size_spec.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Range#size" do
|
||||
it "returns the number of elements in the range" do
|
||||
(1..16).size.should == 16
|
||||
(1...16).size.should == 15
|
||||
|
||||
(1.0..16.0).size.should == 16
|
||||
(1.0...16.0).size.should == 15
|
||||
(1.0..15.9).size.should == 15
|
||||
(1.1..16.0).size.should == 15
|
||||
(1.1..15.9).size.should == 15
|
||||
end
|
||||
|
||||
it "returns 0 if last is less than first" do
|
||||
(16..0).size.should == 0
|
||||
(16.0..0.0).size.should == 0
|
||||
(Float::INFINITY..0).size.should == 0
|
||||
end
|
||||
|
||||
it 'returns Float::INFINITY for increasing, infinite ranges' do
|
||||
(0..Float::INFINITY).size.should == Float::INFINITY
|
||||
(-Float::INFINITY..0).size.should == Float::INFINITY
|
||||
(-Float::INFINITY..Float::INFINITY).size.should == Float::INFINITY
|
||||
end
|
||||
|
||||
it "returns nil if first and last are not Numeric" do
|
||||
(:a..:z).size.should be_nil
|
||||
('a'..'z').size.should be_nil
|
||||
end
|
||||
end
|
347
spec/ruby/core/range/step_spec.rb
Normal file
347
spec/ruby/core/range/step_spec.rb
Normal file
|
@ -0,0 +1,347 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Range#step" do
|
||||
before :each do
|
||||
ScratchPad.record []
|
||||
end
|
||||
|
||||
it "returns an enumerator when no block is given" do
|
||||
enum = (1..10).step(4)
|
||||
enum.should be_an_instance_of(Enumerator)
|
||||
enum.to_a.should eql([1, 5, 9])
|
||||
end
|
||||
|
||||
it "returns self" do
|
||||
r = 1..2
|
||||
r.step { }.should equal(r)
|
||||
end
|
||||
|
||||
it "raises TypeError if step" do
|
||||
obj = mock("mock")
|
||||
lambda { (1..10).step(obj) { } }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "calls #to_int to coerce step to an Integer" do
|
||||
obj = mock("Range#step")
|
||||
obj.should_receive(:to_int).and_return(1)
|
||||
|
||||
(1..2).step(obj) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([1, 2])
|
||||
end
|
||||
|
||||
it "raises a TypeError if step does not respond to #to_int" do
|
||||
obj = mock("Range#step non-integer")
|
||||
|
||||
lambda { (1..2).step(obj) { } }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises a TypeError if #to_int does not return an Integer" do
|
||||
obj = mock("Range#step non-integer")
|
||||
obj.should_receive(:to_int).and_return("1")
|
||||
|
||||
lambda { (1..2).step(obj) { } }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "coerces the argument to integer by invoking to_int" do
|
||||
(obj = mock("2")).should_receive(:to_int).and_return(2)
|
||||
res = []
|
||||
(1..10).step(obj) {|x| res << x}
|
||||
res.should == [1, 3, 5, 7, 9]
|
||||
end
|
||||
|
||||
it "raises a TypeError if the first element does not respond to #succ" do
|
||||
obj = mock("Range#step non-comparable")
|
||||
obj.should_receive(:<=>).with(obj).and_return(1)
|
||||
|
||||
lambda { (obj..obj).step { |x| x } }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if step is 0" do
|
||||
lambda { (-1..1).step(0) { |x| x } }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if step is 0.0" do
|
||||
lambda { (-1..1).step(0.0) { |x| x } }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if step is negative" do
|
||||
lambda { (-1..1).step(-2) { |x| x } }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
describe "with inclusive end" do
|
||||
describe "and Integer values" do
|
||||
it "yields Integer values incremented by 1 and less than or equal to end when not passed a step" do
|
||||
(-2..2).step { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-2, -1, 0, 1, 2])
|
||||
end
|
||||
|
||||
it "yields Integer values incremented by an Integer step" do
|
||||
(-5..5).step(2) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-5, -3, -1, 1, 3, 5])
|
||||
end
|
||||
|
||||
it "yields Float values incremented by a Float step" do
|
||||
(-2..2).step(1.5) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-2.0, -0.5, 1.0])
|
||||
end
|
||||
end
|
||||
|
||||
describe "and Float values" do
|
||||
it "yields Float values incremented by 1 and less than or equal to end when not passed a step" do
|
||||
(-2.0..2.0).step { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0, 2.0])
|
||||
end
|
||||
|
||||
it "yields Float values incremented by an Integer step" do
|
||||
(-5.0..5.0).step(2) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0, 5.0])
|
||||
end
|
||||
|
||||
it "yields Float values incremented by a Float step" do
|
||||
(-1.0..1.0).step(0.5) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5, 1.0])
|
||||
end
|
||||
|
||||
it "returns Float values of 'step * n + begin <= end'" do
|
||||
(1.0..6.4).step(1.8) { |x| ScratchPad << x }
|
||||
(1.0..12.7).step(1.3) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([1.0, 2.8, 4.6, 6.4, 1.0, 2.3, 3.6,
|
||||
4.9, 6.2, 7.5, 8.8, 10.1, 11.4, 12.7])
|
||||
end
|
||||
end
|
||||
|
||||
describe "and Integer, Float values" do
|
||||
it "yields Float values incremented by 1 and less than or equal to end when not passed a step" do
|
||||
(-2..2.0).step { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0, 2.0])
|
||||
end
|
||||
|
||||
it "yields Float values incremented by an Integer step" do
|
||||
(-5..5.0).step(2) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0, 5.0])
|
||||
end
|
||||
|
||||
it "yields Float values incremented by a Float step" do
|
||||
(-1..1.0).step(0.5) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5, 1.0])
|
||||
end
|
||||
end
|
||||
|
||||
describe "and Float, Integer values" do
|
||||
it "yields Float values incremented by 1 and less than or equal to end when not passed a step" do
|
||||
(-2.0..2).step { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0, 2.0])
|
||||
end
|
||||
|
||||
it "yields Float values incremented by an Integer step" do
|
||||
(-5.0..5).step(2) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0, 5.0])
|
||||
end
|
||||
|
||||
it "yields Float values incremented by a Float step" do
|
||||
(-1.0..1).step(0.5) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5, 1.0])
|
||||
end
|
||||
end
|
||||
|
||||
describe "and String values" do
|
||||
it "yields String values incremented by #succ and less than or equal to end when not passed a step" do
|
||||
("A".."E").step { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should == ["A", "B", "C", "D", "E"]
|
||||
end
|
||||
|
||||
it "yields String values incremented by #succ called Integer step times" do
|
||||
("A".."G").step(2) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should == ["A", "C", "E", "G"]
|
||||
end
|
||||
|
||||
it "raises a TypeError when passed a Float step" do
|
||||
lambda { ("A".."G").step(2.0) { } }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "calls #succ on begin and each element returned by #succ" do
|
||||
obj = mock("Range#step String start")
|
||||
obj.should_receive(:<=>).exactly(3).times.and_return(-1, -1, -1, 0)
|
||||
obj.should_receive(:succ).exactly(2).times.and_return(obj)
|
||||
|
||||
(obj..obj).step { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should == [obj, obj, obj]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "with exclusive end" do
|
||||
describe "and Integer values" do
|
||||
it "yields Integer values incremented by 1 and less than end when not passed a step" do
|
||||
(-2...2).step { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-2, -1, 0, 1])
|
||||
end
|
||||
|
||||
it "yields Integer values incremented by an Integer step" do
|
||||
(-5...5).step(2) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-5, -3, -1, 1, 3])
|
||||
end
|
||||
|
||||
it "yields Float values incremented by a Float step" do
|
||||
(-2...2).step(1.5) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-2.0, -0.5, 1.0])
|
||||
end
|
||||
end
|
||||
|
||||
describe "and Float values" do
|
||||
it "yields Float values incremented by 1 and less than end when not passed a step" do
|
||||
(-2.0...2.0).step { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
|
||||
end
|
||||
|
||||
it "yields Float values incremented by an Integer step" do
|
||||
(-5.0...5.0).step(2) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
|
||||
end
|
||||
|
||||
it "yields Float values incremented by a Float step" do
|
||||
(-1.0...1.0).step(0.5) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
|
||||
end
|
||||
|
||||
it "returns Float values of 'step * n + begin < end'" do
|
||||
(1.0...6.4).step(1.8) { |x| ScratchPad << x }
|
||||
(1.0...55.6).step(18.2) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([1.0, 2.8, 4.6, 1.0, 19.2, 37.4])
|
||||
end
|
||||
end
|
||||
|
||||
describe "and Integer, Float values" do
|
||||
it "yields Float values incremented by 1 and less than end when not passed a step" do
|
||||
(-2...2.0).step { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
|
||||
end
|
||||
|
||||
it "yields Float values incremented by an Integer step" do
|
||||
(-5...5.0).step(2) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
|
||||
end
|
||||
|
||||
it "yields an Float and then Float values incremented by a Float step" do
|
||||
(-1...1.0).step(0.5) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
|
||||
end
|
||||
end
|
||||
|
||||
describe "and Float, Integer values" do
|
||||
it "yields Float values incremented by 1 and less than end when not passed a step" do
|
||||
(-2.0...2).step { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
|
||||
end
|
||||
|
||||
it "yields Float values incremented by an Integer step" do
|
||||
(-5.0...5).step(2) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
|
||||
end
|
||||
|
||||
it "yields Float values incremented by a Float step" do
|
||||
(-1.0...1).step(0.5) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
|
||||
end
|
||||
end
|
||||
|
||||
describe "and String values" do
|
||||
it "yields String values incremented by #succ and less than or equal to end when not passed a step" do
|
||||
("A"..."E").step { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should == ["A", "B", "C", "D"]
|
||||
end
|
||||
|
||||
it "yields String values incremented by #succ called Integer step times" do
|
||||
("A"..."G").step(2) { |x| ScratchPad << x }
|
||||
ScratchPad.recorded.should == ["A", "C", "E"]
|
||||
end
|
||||
|
||||
it "raises a TypeError when passed a Float step" do
|
||||
lambda { ("A"..."G").step(2.0) { } }.should raise_error(TypeError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "when no block is given" do
|
||||
describe "returned Enumerator" do
|
||||
describe "size" do
|
||||
it "raises a TypeError if step does not respond to #to_int" do
|
||||
obj = mock("Range#step non-integer")
|
||||
enum = (1..2).step(obj)
|
||||
lambda { enum.size }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises a TypeError if #to_int does not return an Integer" do
|
||||
obj = mock("Range#step non-integer")
|
||||
obj.should_receive(:to_int).and_return("1")
|
||||
enum = (1..2).step(obj)
|
||||
|
||||
lambda { enum.size }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if step is 0" do
|
||||
enum = (-1..1).step(0)
|
||||
lambda { enum.size }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if step is 0.0" do
|
||||
enum = (-1..1).step(0.0)
|
||||
lambda { enum.size }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if step is negative" do
|
||||
enum = (-1..1).step(-2)
|
||||
lambda { enum.size }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "returns the ceil of range size divided by the number of steps" do
|
||||
(1..10).step(4).size.should == 3
|
||||
(1..10).step(3).size.should == 4
|
||||
(1..10).step(2).size.should == 5
|
||||
(1..10).step(1).size.should == 10
|
||||
(-5..5).step(2).size.should == 6
|
||||
(1...10).step(4).size.should == 3
|
||||
(1...10).step(3).size.should == 3
|
||||
(1...10).step(2).size.should == 5
|
||||
(1...10).step(1).size.should == 9
|
||||
(-5...5).step(2).size.should == 5
|
||||
end
|
||||
|
||||
it "returns the correct number of steps when one of the arguments is a float" do
|
||||
(-1..1.0).step(0.5).size.should == 5
|
||||
(-1.0...1.0).step(0.5).size.should == 4
|
||||
end
|
||||
|
||||
it "returns the range size when there's no step_size" do
|
||||
(-2..2).step.size.should == 5
|
||||
(-2.0..2.0).step.size.should == 5
|
||||
(-2..2.0).step.size.should == 5
|
||||
(-2.0..2).step.size.should == 5
|
||||
(1.0..6.4).step(1.8).size.should == 4
|
||||
(1.0..12.7).step(1.3).size.should == 10
|
||||
(-2...2).step.size.should == 4
|
||||
(-2.0...2.0).step.size.should == 4
|
||||
(-2...2.0).step.size.should == 4
|
||||
(-2.0...2).step.size.should == 4
|
||||
(1.0...6.4).step(1.8).size.should == 3
|
||||
(1.0...55.6).step(18.2).size.should == 3
|
||||
end
|
||||
|
||||
it "returns nil with begin and end are String" do
|
||||
("A".."E").step(2).size.should == nil
|
||||
("A"..."E").step(2).size.should == nil
|
||||
("A".."E").step.size.should == nil
|
||||
("A"..."E").step.size.should == nil
|
||||
end
|
||||
|
||||
it "return nil and not raises a TypeError if the first element does not respond to #succ" do
|
||||
obj = mock("Range#step non-comparable")
|
||||
obj.should_receive(:<=>).with(obj).and_return(1)
|
||||
enum = (obj..obj).step
|
||||
lambda { enum.size }.should_not raise_error
|
||||
enum.size.should == nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
22
spec/ruby/core/range/to_a_spec.rb
Normal file
22
spec/ruby/core/range/to_a_spec.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Range#to_a" do
|
||||
it "converts self to an array" do
|
||||
(-5..5).to_a.should == [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
|
||||
('A'..'D').to_a.should == ['A','B','C','D']
|
||||
('A'...'D').to_a.should == ['A','B','C']
|
||||
(0xfffd...0xffff).to_a.should == [0xfffd,0xfffe]
|
||||
lambda { (0.5..2.4).to_a }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "returns empty array for descending-ordered" do
|
||||
(5..-5).to_a.should == []
|
||||
('D'..'A').to_a.should == []
|
||||
('D'...'A').to_a.should == []
|
||||
(0xffff...0xfffd).to_a.should == []
|
||||
end
|
||||
|
||||
it "works with Ranges of Symbols" do
|
||||
(:A..:z).to_a.size.should == 58
|
||||
end
|
||||
end
|
25
spec/ruby/core/range/to_s_spec.rb
Normal file
25
spec/ruby/core/range/to_s_spec.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe "Range#to_s" do
|
||||
it "provides a printable form of self" do
|
||||
(0..21).to_s.should == "0..21"
|
||||
(-8..0).to_s.should == "-8..0"
|
||||
(-411..959).to_s.should == "-411..959"
|
||||
('A'..'Z').to_s.should == 'A..Z'
|
||||
('A'...'Z').to_s.should == 'A...Z'
|
||||
(0xfff..0xfffff).to_s.should == "4095..1048575"
|
||||
(0.5..2.4).to_s.should == "0.5..2.4"
|
||||
end
|
||||
|
||||
it "returns a tainted string if either end is tainted" do
|
||||
(("a".taint)..."c").to_s.tainted?.should be_true
|
||||
("a"...("c".taint)).to_s.tainted?.should be_true
|
||||
("a"..."c").taint.to_s.tainted?.should be_true
|
||||
end
|
||||
|
||||
it "returns a untrusted string if either end is untrusted" do
|
||||
(("a".untrust)..."c").to_s.untrusted?.should be_true
|
||||
("a"...("c".untrust)).to_s.untrusted?.should be_true
|
||||
("a"..."c").untrust.to_s.untrusted?.should be_true
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue