2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'fixtures/classes'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
|
|
|
describe "Enumerable#one?" do
|
2018-06-13 17:41:45 -04:00
|
|
|
before :each do
|
|
|
|
@empty = EnumerableSpecs::Empty.new
|
|
|
|
@enum = EnumerableSpecs::Numerous.new
|
|
|
|
@enum1 = EnumerableSpecs::Numerous.new(0, 1, 2, -1)
|
|
|
|
@enum2 = EnumerableSpecs::Numerous.new(nil, false, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "always returns false on empty enumeration" do
|
|
|
|
@empty.one?.should == false
|
|
|
|
@empty.one? { true }.should == false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an ArgumentError when more than 1 argument is provided" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { @enum.one?(1, 2, 3) }.should raise_error(ArgumentError)
|
|
|
|
-> { [].one?(1, 2, 3) }.should raise_error(ArgumentError)
|
|
|
|
-> { {}.one?(1, 2, 3) }.should raise_error(ArgumentError)
|
2018-06-13 17:41:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not hide exceptions out of #each" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> {
|
2018-06-13 17:41:45 -04:00
|
|
|
EnumerableSpecs::ThrowingEach.new.one?
|
|
|
|
}.should raise_error(RuntimeError)
|
|
|
|
|
2019-07-27 06:40:09 -04:00
|
|
|
-> {
|
2018-06-13 17:41:45 -04:00
|
|
|
EnumerableSpecs::ThrowingEach.new.one? { false }
|
|
|
|
}.should raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with no block" do
|
|
|
|
it "returns true if only one element evaluates to true" do
|
|
|
|
[false, nil, true].one?.should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if two elements evaluate to true" do
|
|
|
|
[false, :value, nil, true].one?.should be_false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if all elements evaluate to false" do
|
|
|
|
[false, nil, false].one?.should be_false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "gathers whole arrays as elements when each yields multiple" do
|
|
|
|
multi = EnumerableSpecs::YieldsMultiWithSingleTrue.new
|
|
|
|
multi.one?.should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with a block" do
|
2017-05-07 08:04:49 -04:00
|
|
|
it "returns true if block returns true once" do
|
|
|
|
[:a, :b, :c].one? { |s| s == :a }.should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if the block returns true more than once" do
|
|
|
|
[:a, :b, :c].one? { |s| s == :a || s == :b }.should be_false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if the block only returns false" do
|
|
|
|
[:a, :b, :c].one? { |s| s == :d }.should be_false
|
|
|
|
end
|
|
|
|
|
2018-06-13 17:41:45 -04:00
|
|
|
it "does not hide exceptions out of the block" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> {
|
2018-06-13 17:41:45 -04:00
|
|
|
@enum.one? { raise "from block" }
|
|
|
|
}.should raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
it "gathers initial args as elements when each yields multiple" do
|
|
|
|
multi = EnumerableSpecs::YieldsMulti.new
|
2018-06-13 17:41:45 -04:00
|
|
|
yielded = []
|
|
|
|
multi.one? { |e| yielded << e; false }.should == false
|
|
|
|
yielded.should == [1, 3, 6]
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "yields multiple arguments when each yields multiple" do
|
|
|
|
multi = EnumerableSpecs::YieldsMulti.new
|
|
|
|
yielded = []
|
2018-06-13 17:41:45 -04:00
|
|
|
multi.one? { |*args| yielded << args; false }.should == false
|
|
|
|
yielded.should == [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
2018-06-13 17:41:45 -04:00
|
|
|
end
|
|
|
|
|
2017-12-15 12:44:37 -05:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
describe 'when given a pattern argument' do
|
|
|
|
it "calls `===` on the pattern the return value " do
|
|
|
|
pattern = EnumerableSpecs::Pattern.new { |x| x == 1 }
|
|
|
|
@enum1.one?(pattern).should == true
|
|
|
|
pattern.yielded.should == [[0], [1], [2], [-1]]
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
# may raise an exception in future versions
|
|
|
|
ruby_version_is ""..."2.6" do
|
|
|
|
it "ignores block" do
|
|
|
|
@enum2.one?(NilClass) { raise }.should == true
|
|
|
|
[1, 2, nil].one?(NilClass) { raise }.should == true
|
|
|
|
{a: 1}.one?(Array) { raise }.should == true
|
2018-06-13 17:41:45 -04:00
|
|
|
end
|
2020-02-08 05:43:27 -05:00
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "always returns false on empty enumeration" do
|
|
|
|
@empty.one?(Integer).should == false
|
|
|
|
[].one?(Integer).should == false
|
|
|
|
{}.one?(NilClass).should == false
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "does not hide exceptions out of #each" do
|
|
|
|
-> {
|
|
|
|
EnumerableSpecs::ThrowingEach.new.one?(Integer)
|
|
|
|
}.should raise_error(RuntimeError)
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "returns true if the pattern returns a truthy value only once" do
|
|
|
|
@enum2.one?(NilClass).should == true
|
|
|
|
pattern = EnumerableSpecs::Pattern.new { |x| x == 2 }
|
|
|
|
@enum1.one?(pattern).should == true
|
2018-06-13 17:41:45 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
[1, 2, 42, 3].one?(pattern).should == true
|
2018-06-13 17:41:45 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
pattern = EnumerableSpecs::Pattern.new { |x| x == [:b, 2] }
|
|
|
|
{a: 1, b: 2}.one?(pattern).should == true
|
|
|
|
end
|
2018-06-13 17:41:45 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "returns false if the pattern returns a truthy value more than once" do
|
|
|
|
pattern = EnumerableSpecs::Pattern.new { |x| !x }
|
|
|
|
@enum2.one?(pattern).should == false
|
|
|
|
pattern.yielded.should == [[nil], [false]]
|
2018-06-13 17:41:45 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
[1, 2, 3].one?(Integer).should == false
|
|
|
|
{a: 1, b: 2}.one?(Array).should == false
|
|
|
|
end
|
2018-06-13 17:41:45 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "returns false if the pattern never returns a truthy value" do
|
|
|
|
pattern = EnumerableSpecs::Pattern.new { |x| nil }
|
|
|
|
@enum1.one?(pattern).should == false
|
|
|
|
pattern.yielded.should == [[0], [1], [2], [-1]]
|
2018-06-13 17:41:45 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
[1, 2, 3].one?(pattern).should == false
|
|
|
|
{a: 1}.one?(pattern).should == false
|
|
|
|
end
|
2018-06-13 17:41:45 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "does not hide exceptions out of pattern#===" do
|
|
|
|
pattern = EnumerableSpecs::Pattern.new { raise "from pattern" }
|
|
|
|
-> {
|
|
|
|
@enum.one?(pattern)
|
|
|
|
}.should raise_error(RuntimeError)
|
|
|
|
end
|
2018-06-13 17:41:45 -04:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "calls the pattern with gathered array when yielded with multiple arguments" do
|
|
|
|
multi = EnumerableSpecs::YieldsMulti.new
|
|
|
|
pattern = EnumerableSpecs::Pattern.new { false }
|
|
|
|
multi.one?(pattern).should == false
|
|
|
|
pattern.yielded.should == [[[1, 2]], [[3, 4, 5]], [[6, 7, 8, 9]]]
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|