summaryrefslogtreecommitdiff
path: root/spec/ruby/core/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/kernel')
-rw-r--r--spec/ruby/core/kernel/clone_spec.rb6
-rw-r--r--spec/ruby/core/kernel/dup_spec.rb6
-rw-r--r--spec/ruby/core/kernel/enum_for_spec.rb4
-rw-r--r--spec/ruby/core/kernel/fail_spec.rb39
-rw-r--r--spec/ruby/core/kernel/format_spec.rb42
-rw-r--r--spec/ruby/core/kernel/is_a_spec.rb54
-rw-r--r--spec/ruby/core/kernel/kind_of_spec.rb5
-rw-r--r--spec/ruby/core/kernel/require_relative_spec.rb8
-rw-r--r--spec/ruby/core/kernel/shared/kind_of.rb55
-rw-r--r--spec/ruby/core/kernel/shared/sprintf.rb23
-rw-r--r--spec/ruby/core/kernel/then_spec.rb10
-rw-r--r--spec/ruby/core/kernel/to_enum_spec.rb56
12 files changed, 158 insertions, 150 deletions
diff --git a/spec/ruby/core/kernel/clone_spec.rb b/spec/ruby/core/kernel/clone_spec.rb
index 4ddb23d6e6..80e7a78abb 100644
--- a/spec/ruby/core/kernel/clone_spec.rb
+++ b/spec/ruby/core/kernel/clone_spec.rb
@@ -10,12 +10,6 @@ describe "Kernel#clone" do
@obj = KernelSpecs::Duplicate.new 1, :a
end
- it "calls #initialize_copy on the new instance" do
- clone = @obj.clone
- ScratchPad.recorded.should_not == @obj.object_id
- ScratchPad.recorded.should == clone.object_id
- end
-
it "uses the internal allocator and does not call #allocate" do
klass = Class.new
instance = klass.new
diff --git a/spec/ruby/core/kernel/dup_spec.rb b/spec/ruby/core/kernel/dup_spec.rb
index 99de5e3732..fa4ca71476 100644
--- a/spec/ruby/core/kernel/dup_spec.rb
+++ b/spec/ruby/core/kernel/dup_spec.rb
@@ -10,12 +10,6 @@ describe "Kernel#dup" do
@obj = KernelSpecs::Duplicate.new 1, :a
end
- it "calls #initialize_copy on the new instance" do
- dup = @obj.dup
- ScratchPad.recorded.should_not == @obj.object_id
- ScratchPad.recorded.should == dup.object_id
- end
-
it "uses the internal allocator and does not call #allocate" do
klass = Class.new
instance = klass.new
diff --git a/spec/ruby/core/kernel/enum_for_spec.rb b/spec/ruby/core/kernel/enum_for_spec.rb
index 0092e20468..ef0fb64e63 100644
--- a/spec/ruby/core/kernel/enum_for_spec.rb
+++ b/spec/ruby/core/kernel/enum_for_spec.rb
@@ -1,5 +1,7 @@
require_relative '../../spec_helper'
describe "Kernel#enum_for" do
- it "needs to be reviewed for spec completeness"
+ it "is an alias of Kernel#to_enum" do
+ Kernel.instance_method(:enum_for).should == Kernel.instance_method(:to_enum)
+ end
end
diff --git a/spec/ruby/core/kernel/fail_spec.rb b/spec/ruby/core/kernel/fail_spec.rb
index ac379b67d5..2d117d26cd 100644
--- a/spec/ruby/core/kernel/fail_spec.rb
+++ b/spec/ruby/core/kernel/fail_spec.rb
@@ -1,42 +1,13 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/classes'
describe "Kernel#fail" do
- it "is a private method" do
- Kernel.private_instance_methods(false).should.include?(:fail)
- end
-
- it "raises a RuntimeError" do
- -> { fail }.should.raise(RuntimeError)
- end
-
- it "accepts an Object with an exception method returning an Exception" do
- obj = Object.new
- def obj.exception(msg)
- StandardError.new msg
- end
- -> { fail obj, "..." }.should.raise(StandardError, "...")
- end
-
- it "instantiates the specified exception class" do
- error_class = Class.new(RuntimeError)
- -> { fail error_class }.should.raise(error_class)
- end
-
- it "uses the specified message" do
- -> {
- begin
- fail "the duck is not irish."
- rescue => e
- e.message.should == "the duck is not irish."
- raise
- else
- raise Exception
- end
- }.should.raise(RuntimeError)
+ it "is an alias of Kernel#raise" do
+ Kernel.instance_method(:fail).should == Kernel.instance_method(:raise)
end
end
describe "Kernel.fail" do
- it "needs to be reviewed for spec completeness"
+ it "is an alias of Kernel.raise" do
+ Kernel.method(:fail).should == Kernel.method(:raise)
+ end
end
diff --git a/spec/ruby/core/kernel/format_spec.rb b/spec/ruby/core/kernel/format_spec.rb
index 35c752b1ab..a311f3c3d7 100644
--- a/spec/ruby/core/kernel/format_spec.rb
+++ b/spec/ruby/core/kernel/format_spec.rb
@@ -1,47 +1,13 @@
require_relative '../../spec_helper'
-require_relative 'fixtures/classes'
-# NOTE: most specs are in sprintf_spec.rb, this is just an alias
describe "Kernel#format" do
- it "is a private method" do
- Kernel.private_instance_methods(false).should.include?(:format)
+ it "is an alias of Kernel#sprintf" do
+ Kernel.instance_method(:format).should == Kernel.instance_method(:sprintf)
end
end
describe "Kernel.format" do
- it "is accessible as a module function" do
- Kernel.format("%s", "hello").should == "hello"
- end
-
- describe "when $VERBOSE is true" do
- it "warns if too many arguments are passed" do
- code = <<~RUBY
- $VERBOSE = true
- format("test", 1)
- RUBY
-
- ruby_exe(code, args: "2>&1").should.include?("warning: too many arguments for format string")
- end
-
- it "does not warns if too many keyword arguments are passed" do
- code = <<~RUBY
- $VERBOSE = true
- format("test %{test}", test: 1, unused: 2)
- RUBY
-
- ruby_exe(code, args: "2>&1").should_not.include?("warning")
- end
-
- ruby_bug "#20593", ""..."3.4" do
- it "doesn't warns if keyword arguments are passed and none are used" do
- code = <<~RUBY
- $VERBOSE = true
- format("test", test: 1)
- format("test", {})
- RUBY
-
- ruby_exe(code, args: "2>&1").should_not.include?("warning")
- end
- end
+ it "is an alias of Kernel.sprintf" do
+ Kernel.method(:format).should == Kernel.method(:sprintf)
end
end
diff --git a/spec/ruby/core/kernel/is_a_spec.rb b/spec/ruby/core/kernel/is_a_spec.rb
index bd8c96529a..ff36a769c7 100644
--- a/spec/ruby/core/kernel/is_a_spec.rb
+++ b/spec/ruby/core/kernel/is_a_spec.rb
@@ -1,6 +1,56 @@
require_relative '../../spec_helper'
-require_relative 'shared/kind_of'
+require_relative 'fixtures/classes'
describe "Kernel#is_a?" do
- it_behaves_like :kernel_kind_of, :is_a?
+ before :each do
+ @o = KernelSpecs::KindaClass.new
+ end
+
+ it "returns true if given class is the object's class" do
+ @o.is_a?(KernelSpecs::KindaClass).should == true
+ end
+
+ it "returns true if given class is an ancestor of the object's class" do
+ @o.is_a?(KernelSpecs::AncestorClass).should == true
+ @o.is_a?(String).should == true
+ @o.is_a?(Object).should == true
+ end
+
+ it "returns false if the given class is not object's class nor an ancestor" do
+ @o.is_a?(Array).should == false
+ end
+
+ it "returns true if given a Module that is included in object's class" do
+ @o.is_a?(KernelSpecs::MyModule).should == true
+ end
+
+ it "returns true if given a Module that is included one of object's ancestors only" do
+ @o.is_a?(KernelSpecs::AncestorModule).should == true
+ end
+
+ it "returns true if given a Module that object has been extended with" do
+ @o.is_a?(KernelSpecs::MyExtensionModule).should == true
+ end
+
+ it "returns true if given a Module that object has been prepended with" do
+ @o.is_a?(KernelSpecs::MyPrependedModule).should == true
+ end
+
+ it "returns false if given a Module not included nor prepended in object's class nor ancestors" do
+ @o.is_a?(KernelSpecs::SomeOtherModule).should == false
+ end
+
+ it "raises a TypeError if given an object that is not a Class nor a Module" do
+ -> { @o.is_a?(1) }.should.raise(TypeError)
+ -> { @o.is_a?('KindaClass') }.should.raise(TypeError)
+ -> { @o.is_a?(:KindaClass) }.should.raise(TypeError)
+ -> { @o.is_a?(Object.new) }.should.raise(TypeError)
+ end
+
+ it "does not take into account `class` method overriding" do
+ def @o.class; Integer; end
+
+ @o.is_a?(Integer).should == false
+ @o.is_a?(KernelSpecs::KindaClass).should == true
+ end
end
diff --git a/spec/ruby/core/kernel/kind_of_spec.rb b/spec/ruby/core/kernel/kind_of_spec.rb
index c988edccb5..7fcc72543d 100644
--- a/spec/ruby/core/kernel/kind_of_spec.rb
+++ b/spec/ruby/core/kernel/kind_of_spec.rb
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/kind_of'
describe "Kernel#kind_of?" do
- it_behaves_like :kernel_kind_of, :kind_of?
+ it "is an alias of Kernel#is_a?" do
+ Kernel.instance_method(:kind_of?).should == Kernel.instance_method(:is_a?)
+ end
end
diff --git a/spec/ruby/core/kernel/require_relative_spec.rb b/spec/ruby/core/kernel/require_relative_spec.rb
index 332045b200..b3ac1fc64c 100644
--- a/spec/ruby/core/kernel/require_relative_spec.rb
+++ b/spec/ruby/core/kernel/require_relative_spec.rb
@@ -109,9 +109,9 @@ describe "Kernel#require_relative with a relative path" do
-> {
require_relative(path)
- }.should(raise_error(LoadError) { |e|
+ }.should.raise(LoadError) { |e|
e.path.should == File.expand_path(path, @abs_dir)
- })
+ }
end
it "calls #to_str on non-String objects" do
@@ -311,9 +311,9 @@ describe "Kernel#require_relative with an absolute path" do
-> {
require_relative(path)
- }.should(raise_error(LoadError) { |e|
+ }.should.raise(LoadError) { |e|
e.path.should == File.expand_path(path, @abs_dir)
- })
+ }
end
it "calls #to_str on non-String objects" do
diff --git a/spec/ruby/core/kernel/shared/kind_of.rb b/spec/ruby/core/kernel/shared/kind_of.rb
deleted file mode 100644
index a5e0eab08f..0000000000
--- a/spec/ruby/core/kernel/shared/kind_of.rb
+++ /dev/null
@@ -1,55 +0,0 @@
-require_relative '../fixtures/classes'
-
-describe :kernel_kind_of, shared: true do
- before :each do
- @o = KernelSpecs::KindaClass.new
- end
-
- it "returns true if given class is the object's class" do
- @o.send(@method, KernelSpecs::KindaClass).should == true
- end
-
- it "returns true if given class is an ancestor of the object's class" do
- @o.send(@method, KernelSpecs::AncestorClass).should == true
- @o.send(@method, String).should == true
- @o.send(@method, Object).should == true
- end
-
- it "returns false if the given class is not object's class nor an ancestor" do
- @o.send(@method, Array).should == false
- end
-
- it "returns true if given a Module that is included in object's class" do
- @o.send(@method, KernelSpecs::MyModule).should == true
- end
-
- it "returns true if given a Module that is included one of object's ancestors only" do
- @o.send(@method, KernelSpecs::AncestorModule).should == true
- end
-
- it "returns true if given a Module that object has been extended with" do
- @o.send(@method, KernelSpecs::MyExtensionModule).should == true
- end
-
- it "returns true if given a Module that object has been prepended with" do
- @o.send(@method, KernelSpecs::MyPrependedModule).should == true
- end
-
- it "returns false if given a Module not included nor prepended in object's class nor ancestors" do
- @o.send(@method, KernelSpecs::SomeOtherModule).should == false
- end
-
- it "raises a TypeError if given an object that is not a Class nor a Module" do
- -> { @o.send(@method, 1) }.should.raise(TypeError)
- -> { @o.send(@method, 'KindaClass') }.should.raise(TypeError)
- -> { @o.send(@method, :KindaClass) }.should.raise(TypeError)
- -> { @o.send(@method, Object.new) }.should.raise(TypeError)
- end
-
- it "does not take into account `class` method overriding" do
- def @o.class; Integer; end
-
- @o.send(@method, Integer).should == false
- @o.send(@method, KernelSpecs::KindaClass).should == true
- end
-end
diff --git a/spec/ruby/core/kernel/shared/sprintf.rb b/spec/ruby/core/kernel/shared/sprintf.rb
index b40bd95f59..e57334c5ab 100644
--- a/spec/ruby/core/kernel/shared/sprintf.rb
+++ b/spec/ruby/core/kernel/shared/sprintf.rb
@@ -1031,4 +1031,27 @@ describe :kernel_sprintf, shared: true do
it "does not raise error when passed more arguments than needed" do
sprintf("%s %d %c", "string", 2, "c", []).should == "string 2 c"
end
+
+ describe "when $VERBOSE is true" do
+ it "warns if too many arguments are passed" do
+ -> {
+ format("test", 1)
+ }.should complain(/too many arguments for format string/, verbose: true)
+ end
+
+ it "does not warns if too many keyword arguments are passed" do
+ -> {
+ format("test %{test}", test: 1, unused: 2)
+ }.should_not complain(verbose: true)
+ end
+
+ ruby_bug "#20593", ""..."3.4" do
+ it "doesn't warns if keyword arguments are passed and none are used" do
+ -> {
+ format("test", test: 1)
+ format("test", {})
+ }.should_not complain(verbose: true)
+ end
+ end
+ end
end
diff --git a/spec/ruby/core/kernel/then_spec.rb b/spec/ruby/core/kernel/then_spec.rb
index 8109a2960a..bda5a69662 100644
--- a/spec/ruby/core/kernel/then_spec.rb
+++ b/spec/ruby/core/kernel/then_spec.rb
@@ -2,5 +2,13 @@ require_relative '../../spec_helper'
require_relative 'shared/then'
describe "Kernel#then" do
- it_behaves_like :kernel_then, :then
+ ruby_version_is ""..."3.4" do
+ it_behaves_like :kernel_then, :then
+ end
+
+ ruby_version_is "3.4" do
+ it "is an alias of Kernel#yield_self" do
+ Kernel.instance_method(:then).should == Kernel.instance_method(:yield_self)
+ end
+ end
end
diff --git a/spec/ruby/core/kernel/to_enum_spec.rb b/spec/ruby/core/kernel/to_enum_spec.rb
index 9d9945450f..1cee26b694 100644
--- a/spec/ruby/core/kernel/to_enum_spec.rb
+++ b/spec/ruby/core/kernel/to_enum_spec.rb
@@ -1,5 +1,59 @@
require_relative '../../spec_helper'
describe "Kernel#to_enum" do
- it "needs to be reviewed for spec completeness"
+ it "is defined in Kernel" do
+ Kernel.method_defined?(:to_enum).should == true
+ end
+
+ it "returns a new enumerator" do
+ "abc".to_enum.should.instance_of?(Enumerator)
+ end
+
+ it "defaults the first argument to :each" do
+ enum = [1,2].to_enum
+ enum.map { |v| v }.should == [1,2].each { |v| v }
+ end
+
+ it "sets regexp matches in the caller" do
+ "wawa".to_enum(:scan, /./).map {|o| $& }.should == ["w", "a", "w", "a"]
+ a = []
+ "wawa".to_enum(:scan, /./).each {|o| a << $& }
+ a.should == ["w", "a", "w", "a"]
+ end
+
+ it "exposes multi-arg yields as an array" do
+ o = Object.new
+ def o.each
+ yield :a
+ yield :b1, :b2
+ yield [:c]
+ yield :d1, :d2
+ yield :e1, :e2, :e3
+ end
+
+ enum = o.to_enum
+ enum.next.should == :a
+ enum.next.should == [:b1, :b2]
+ enum.next.should == [:c]
+ enum.next.should == [:d1, :d2]
+ enum.next.should == [:e1, :e2, :e3]
+ end
+
+ it "uses the passed block's value to calculate the size of the enumerator" do
+ Object.new.to_enum { 100 }.size.should == 100
+ end
+
+ it "defers the evaluation of the passed block until #size is called" do
+ ScratchPad.record []
+
+ enum = Object.new.to_enum do
+ ScratchPad << :called
+ 100
+ end
+
+ ScratchPad.recorded.should.empty?
+
+ enum.size
+ ScratchPad.recorded.should == [:called]
+ end
end