From e8076ec1b26353085bfb0c42f6b17bfd03f1fb1e Mon Sep 17 00:00:00 2001 From: Herwin Date: Thu, 27 Jun 2024 11:32:26 +0200 Subject: [PATCH] Add specs for Range#reverse_each --- core/range/reverse_each_spec.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 core/range/reverse_each_spec.rb diff --git a/core/range/reverse_each_spec.rb b/core/range/reverse_each_spec.rb new file mode 100644 index 000000000..2f8fdd26b --- /dev/null +++ b/core/range/reverse_each_spec.rb @@ -0,0 +1,29 @@ +require_relative '../../spec_helper' + +ruby_version_is "3.3" do + describe "Range#reverse_each" do + it "works efficiently for very long Ranges of Integers" do + (1..2**100).reverse_each.take(3).size.should == 3 + end + + it "works for beginless Ranges of Integers" do + (..5).reverse_each.take(3).should == [5, 4, 3] + end + + it "works for Ranges of Strings by converting the Range to an Array first" do + ("a".."z").reverse_each.take(3).should == ["z", "y", "x"] + end + + it "raises a TypeError for endless Ranges of Integers" do + -> { + (1..).reverse_each.take(3) + }.should raise_error(TypeError, "can't iterate from NilClass") + end + + it "raises a TypeError for endless Ranges of other objects" do + -> { + ("a"..).reverse_each.take(3) + }.should raise_error(TypeError, "can't iterate from NilClass") + end + end +end