Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 39 additions & 41 deletions core/builtin_constants/builtin_constants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,65 +87,63 @@
end

ruby_version_is "4.0" do
context "The constant" do
describe "Ruby" do
it "is a Module" do
Ruby.should.instance_of?(Module)
end
describe "Ruby" do
it "is a Module" do
Ruby.should.instance_of?(Module)
end
end

describe "Ruby::VERSION" do
it "is equal to RUBY_VERSION" do
Ruby::VERSION.should equal(RUBY_VERSION)
end
describe "Ruby::VERSION" do
it "is equal to RUBY_VERSION" do
Ruby::VERSION.should equal(RUBY_VERSION)
end
end

describe "RUBY::PATCHLEVEL" do
it "is equal to RUBY_PATCHLEVEL" do
Ruby::PATCHLEVEL.should equal(RUBY_PATCHLEVEL)
end
describe "RUBY::PATCHLEVEL" do
it "is equal to RUBY_PATCHLEVEL" do
Ruby::PATCHLEVEL.should equal(RUBY_PATCHLEVEL)
end
end

describe "Ruby::COPYRIGHT" do
it "is equal to RUBY_COPYRIGHT" do
Ruby::COPYRIGHT.should equal(RUBY_COPYRIGHT)
end
describe "Ruby::COPYRIGHT" do
it "is equal to RUBY_COPYRIGHT" do
Ruby::COPYRIGHT.should equal(RUBY_COPYRIGHT)
end
end

describe "Ruby::DESCRIPTION" do
it "is equal to RUBY_DESCRIPTION" do
Ruby::DESCRIPTION.should equal(RUBY_DESCRIPTION)
end
describe "Ruby::DESCRIPTION" do
it "is equal to RUBY_DESCRIPTION" do
Ruby::DESCRIPTION.should equal(RUBY_DESCRIPTION)
end
end

describe "Ruby::ENGINE" do
it "is equal to RUBY_ENGINE" do
Ruby::ENGINE.should equal(RUBY_ENGINE)
end
describe "Ruby::ENGINE" do
it "is equal to RUBY_ENGINE" do
Ruby::ENGINE.should equal(RUBY_ENGINE)
end
end

describe "Ruby::ENGINE_VERSION" do
it "is equal to RUBY_ENGINE_VERSION" do
Ruby::ENGINE_VERSION.should equal(RUBY_ENGINE_VERSION)
end
describe "Ruby::ENGINE_VERSION" do
it "is equal to RUBY_ENGINE_VERSION" do
Ruby::ENGINE_VERSION.should equal(RUBY_ENGINE_VERSION)
end
end

describe "Ruby::PLATFORM" do
it "is equal to RUBY_PLATFORM" do
Ruby::PLATFORM.should equal(RUBY_PLATFORM)
end
describe "Ruby::PLATFORM" do
it "is equal to RUBY_PLATFORM" do
Ruby::PLATFORM.should equal(RUBY_PLATFORM)
end
end

describe "Ruby::RELEASE_DATE" do
it "is equal to RUBY_RELEASE_DATE" do
Ruby::RELEASE_DATE.should equal(RUBY_RELEASE_DATE)
end
describe "Ruby::RELEASE_DATE" do
it "is equal to RUBY_RELEASE_DATE" do
Ruby::RELEASE_DATE.should equal(RUBY_RELEASE_DATE)
end
end

describe "Ruby::REVISION" do
it "is equal to RUBY_REVISION" do
Ruby::REVISION.should equal(RUBY_REVISION)
end
describe "Ruby::REVISION" do
it "is equal to RUBY_REVISION" do
Ruby::REVISION.should equal(RUBY_REVISION)
end
end
end
51 changes: 29 additions & 22 deletions core/range/to_set_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require_relative '../../spec_helper'
require_relative '../enumerable/fixtures/classes'

describe "Enumerable#to_set" do
describe "Range#to_set" do
it "returns a new Set created from self" do
(1..4).to_set.should == Set[1, 2, 3, 4]
(1...4).to_set.should == Set[1, 2, 3]
Expand All @@ -11,37 +11,44 @@
(1..3).to_set { |x| x * x }.should == Set[1, 4, 9]
end

it "raises a TypeError for a beginningless range" do
-> {
(..0).to_set
}.should raise_error(TypeError, "can't iterate from NilClass")
end

ruby_version_is "4.0" do
it "raises a RangeError if the range is infinite" do
it "raises a RangeError if the range is endless" do
-> { (1..).to_set }.should raise_error(RangeError, "cannot convert endless range to a set")
-> { (1...).to_set }.should raise_error(RangeError, "cannot convert endless range to a set")
end
end

ruby_version_is ""..."4.0" do
it "instantiates an object of provided as the first argument set class" do
set = (1..3).to_set(EnumerableSpecs::SetSubclass)
set.should be_kind_of(EnumerableSpecs::SetSubclass)
set.to_a.sort.should == [1, 2, 3]
context "given positional arguments" do
ruby_version_is ""..."4.0" do
it "instantiates an object of provided as the first argument set class" do
set = (1..3).to_set(EnumerableSpecs::SetSubclass)
set.should be_kind_of(EnumerableSpecs::SetSubclass)
set.to_a.sort.should == [1, 2, 3]
end
end
end

ruby_version_is "4.0"..."4.1" do
it "instantiates an object of provided as the first argument set class and warns" do
set = nil
proc {
set = (1..3).to_set(EnumerableSpecs::SetSubclass)
}.should complain(/Enumerable#to_set/)
set.should be_kind_of(EnumerableSpecs::SetSubclass)
set.to_a.sort.should == [1, 2, 3]
ruby_version_is "4.0"..."4.1" do
it "instantiates an object of provided as the first argument set class and warns" do
-> {
set = (1..3).to_set(EnumerableSpecs::SetSubclass)
set.should be_kind_of(EnumerableSpecs::SetSubclass)
set.to_a.sort.should == [1, 2, 3]
}.should complain(/warning: passing arguments to Enumerable#to_set is deprecated/)
end
end
end

ruby_version_is "4.1" do
it "does not accept any positional argument" do
-> {
(1..3).to_set(EnumerableSpecs::SetSubclass)
}.should raise_error(ArgumentError, 'wrong number of arguments (given 1, expected 0)')
ruby_version_is "4.1" do
it "does not accept any positional argument" do
-> {
(1..3).to_set(EnumerableSpecs::SetSubclass)
}.should raise_error(ArgumentError, "wrong number of arguments (given 1, expected 0)")
end
end
end
end
35 changes: 4 additions & 31 deletions language/block_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ def all_kwrest(arg1, arg2, *rest, post1, post2, kw1: 1, kw2: 2, okw1:, okw2:, **
end
end

describe "`it` calls without arguments in a block with no ordinary parameters" do
describe "`it` calls without arguments in a block" do
ruby_version_is "3.3"..."3.4" do
it "emits a deprecation warning" do
-> {
Expand Down Expand Up @@ -1094,38 +1094,11 @@ def o.it
end
end
end

ruby_version_is "3.4" do
it "does not emit a deprecation warning" do
-> {
eval "proc { it }"
}.should_not complain
end

it "acts as the first argument if no local variables exist" do
eval("proc { it * 2 }").call(5).should == 10
end

it "can be reassigned to act as a local variable" do
eval("proc { tmp = it; it = tmp * 2; it }").call(21).should == 42
end

it "can be used in nested calls" do
eval("proc { it.map { it * 2 } }").call([1, 2, 3]).should == [2, 4, 6]
end

it "cannot be mixed with numbered parameters" do
-> {
eval "proc { it + _1 }"
}.should raise_error(SyntaxError, /numbered parameters are not allowed when 'it' is already used|'it' is already used in/)

-> {
eval "proc { _1 + it }"
}.should raise_error(SyntaxError, /numbered parameter is already used in|'it' is not allowed when a numbered parameter is already used/)
end
end
end

# Duplicates specs in language/it_parameter_spec.rb
# Need them here to run on Ruby versions prior 3.4
# TODO: remove when the minimal supported Ruby version is 3.4
describe "if `it` is defined as a variable" do
it "treats `it` as a captured variable if defined outside of a block" do
it = 5
Expand Down
44 changes: 42 additions & 2 deletions language/it_parameter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,28 @@
-> { it + -> { it * it }.call(2) }.call(3).should == 7
end

it "can be reassigned to act as a local variable" do
proc { tmp = it; it = tmp * 2; it }.call(21).should == 42
end

it "is a regular local variable if there is already a 'it' local variable" do
it = 0
proc { it }.call("a").should == 0
it = 0
proc { it }.call("a").should == 0
end

it "is a regular local variable if there is a method `it` defined" do
o = Object.new
def o.it
21
end

o.instance_eval("proc { it * 2 }").call(1).should == 2
end

it "is not shadowed by an reassignment in a block" do
a = nil
proc { a = it; it = 42 }.call(0)
a.should == 0 # if `it` were shadowed its value would be nil
end

it "raises SyntaxError when block parameters are specified explicitly" do
Expand All @@ -36,6 +55,16 @@
-> { eval("['a'].map { |x| it }") }.should raise_error(SyntaxError, /ordinary parameter is defined/)
end

it "cannot be mixed with numbered parameters" do
-> {
eval("proc { it + _1 }")
}.should raise_error(SyntaxError, /numbered parameters are not allowed when 'it' is already used|'it' is already used in/)

-> {
eval("proc { _1 + it }")
}.should raise_error(SyntaxError, /numbered parameter is already used in|'it' is not allowed when a numbered parameter is already used/)
end

it "affects block arity" do
-> {}.arity.should == 0
-> { it }.arity.should == 1
Expand All @@ -62,5 +91,16 @@ def obj.foo; it; end

-> { obj.foo("a") }.should raise_error(ArgumentError, /wrong number of arguments/)
end

context "given multiple arguments" do
it "provides it in a block and assigns the first argument for a block" do
proc { it }.call("a", "b").should == "a"
end

it "raises ArgumentError for a proc" do
-> { -> { it }.call("a", "b") }.should raise_error(ArgumentError, "wrong number of arguments (given 2, expected 1)")
-> { lambda { it }.call("a", "b") }.should raise_error(ArgumentError, "wrong number of arguments (given 2, expected 1)")
end
end
end
end
Loading