1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/spec/ruby/core/enumerable/one_spec.rb
eregon 1d15d5f080 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
2017-09-20 20:18:52 +00:00

49 lines
1.5 KiB
Ruby

require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Enumerable#one?" do
describe "when passed a block" do
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
it "gathers initial args as elements when each yields multiple" do
multi = EnumerableSpecs::YieldsMulti.new
multi.one? {|e| e == 1 }.should be_true
end
it "yields multiple arguments when each yields multiple" do
multi = EnumerableSpecs::YieldsMulti.new
yielded = []
multi.one? {|e, i| yielded << [e, i] }
yielded.should == [[1, 2], [3, 4]]
end
end
describe "when not passed a 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
end