1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
This commit is contained in:
Benoit Daloze 2020-09-30 12:21:48 +02:00
parent ce986b41ca
commit 201d501640
17 changed files with 209 additions and 6 deletions

View file

@ -41,6 +41,7 @@ ComparisonTest
ConstantSpecsIncludedModule ConstantSpecsIncludedModule
ConstantVisibility ConstantVisibility
Coverage Coverage
CoverageSpecs
CustomArgumentError CustomArgumentError
DRb DRb
DRbIdConv DRbIdConv

View file

@ -5,6 +5,31 @@ describe "An Exception reaching the top level" do
ruby_exe('raise "foo"', args: "2>&1").should.include?("in `<main>': foo (RuntimeError)") ruby_exe('raise "foo"', args: "2>&1").should.include?("in `<main>': foo (RuntimeError)")
end end
ruby_version_is "2.6" do
it "the Exception#cause is printed to STDERR with backtraces" do
code = <<-RUBY
def raise_cause
raise "the cause"
end
def raise_wrapped
raise "wrapped"
end
begin
raise_cause
rescue
raise_wrapped
end
RUBY
lines = ruby_exe(code, args: "2>&1").lines
lines.reject! { |l| l.include?('rescue in') }
lines.map! { |l| l.split(':')[2..-1].join(':').chomp }
lines.should == ["in `raise_wrapped': wrapped (RuntimeError)",
"in `<main>'",
"in `raise_cause': the cause (RuntimeError)",
"in `<main>'"]
end
end
describe "with a custom backtrace" do describe "with a custom backtrace" do
it "is printed on STDERR" do it "is printed on STDERR" do
code = <<-RUBY code = <<-RUBY

View file

@ -80,4 +80,12 @@ describe "Kernel#freeze" do
o.freeze o.freeze
-> {o.instance_variable_set(:@foo, 1)}.should raise_error(RuntimeError) -> {o.instance_variable_set(:@foo, 1)}.should raise_error(RuntimeError)
end end
it "freezes an object's singleton class" do
o = Object.new
c = o.singleton_class
c.frozen?.should == false
o.freeze
c.frozen?.should == true
end
end end

View file

@ -4,7 +4,9 @@ require_relative 'fixtures/classes'
describe "Kernel#instance_variables" do describe "Kernel#instance_variables" do
describe "immediate values" do describe "immediate values" do
it "returns an empty array if no instance variables are defined" do it "returns an empty array if no instance variables are defined" do
0.instance_variables.should == [] [0, 0.5, true, false, nil].each do |value|
value.instance_variables.should == []
end
end end
it "returns the correct array if an instance variable is added" do it "returns the correct array if an instance variable is added" do

View file

@ -344,6 +344,21 @@ describe :kernel_require, shared: true do
loaded_feature = $LOADED_FEATURES.last loaded_feature = $LOADED_FEATURES.last
ScratchPad.recorded.should == [loaded_feature] ScratchPad.recorded.should == [loaded_feature]
end end
it "requires only once when a new matching file added to path" do
@object.require('load_fixture').should be_true
ScratchPad.recorded.should == [:loaded]
symlink_to_code_dir_two = tmp("codesymlinktwo")
File.symlink("#{CODE_LOADING_DIR}/b", symlink_to_code_dir_two)
begin
$LOAD_PATH.unshift(symlink_to_code_dir_two)
@object.require('load_fixture').should be_false
ensure
rm_r symlink_to_code_dir_two
end
end
end end
describe "with symlinks in the required feature and $LOAD_PATH" do describe "with symlinks in the required feature and $LOAD_PATH" do

View file

@ -536,6 +536,15 @@ describe "Process.spawn" do
File.read(@name).should == "glarkbang" File.read(@name).should == "glarkbang"
end end
it "closes STDERR in the child if :err => :close" do
File.open(@name, 'w') do |file|
-> do
code = "begin; STDOUT.puts 'out'; STDERR.puts 'hello'; rescue => e; puts 'rescued'; end"
Process.wait Process.spawn(ruby_cmd(code), :out => file, :err => :close)
end.should output_to_fd("out\nrescued\n", file)
end
end
# :close_others # :close_others
platform_is_not :windows do platform_is_not :windows do

View file

@ -19,13 +19,17 @@ describe "String#encode" do
it "returns a copy when Encoding.default_internal is nil" do it "returns a copy when Encoding.default_internal is nil" do
Encoding.default_internal = nil Encoding.default_internal = nil
str = "" str = ""
str.encode.should_not equal(str) encoded = str.encode
encoded.should_not equal(str)
encoded.should == str
end end
it "returns a copy for a ASCII-only String when Encoding.default_internal is nil" do it "returns a copy for a ASCII-only String when Encoding.default_internal is nil" do
Encoding.default_internal = nil Encoding.default_internal = nil
str = "abc" str = "abc"
str.encode.should_not equal(str) encoded = str.encode
encoded.should_not equal(str)
encoded.should == str
end end
it "encodes an ascii substring of a binary string to UTF-8" do it "encodes an ascii substring of a binary string to UTF-8" do
@ -39,7 +43,9 @@ describe "String#encode" do
describe "when passed to encoding" do describe "when passed to encoding" do
it "returns a copy when passed the same encoding as the String" do it "returns a copy when passed the same encoding as the String" do
str = "" str = ""
str.encode(Encoding::UTF_8).should_not equal(str) encoded = str.encode(Encoding::UTF_8)
encoded.should_not equal(str)
encoded.should == str
end end
it "round trips a String" do it "round trips a String" do
@ -75,6 +81,7 @@ describe "String#encode" do
encoded = str.encode("utf-8", "utf-8") encoded = str.encode("utf-8", "utf-8")
encoded.should_not equal(str) encoded.should_not equal(str)
encoded.should == str.force_encoding("utf-8")
encoded.encoding.should == Encoding::UTF_8 encoded.encoding.should == Encoding::UTF_8
end end
@ -87,14 +94,28 @@ describe "String#encode" do
describe "when passed to, options" do describe "when passed to, options" do
it "returns a copy when the destination encoding is the same as the String encoding" do it "returns a copy when the destination encoding is the same as the String encoding" do
str = "" str = ""
str.encode(Encoding::UTF_8, undef: :replace).should_not equal(str) encoded = str.encode(Encoding::UTF_8, undef: :replace)
encoded.should_not equal(str)
encoded.should == str
end end
end end
describe "when passed to, from, options" do describe "when passed to, from, options" do
it "returns a copy when both encodings are the same" do it "returns a copy when both encodings are the same" do
str = "" str = ""
str.encode("utf-8", "utf-8", invalid: :replace).should_not equal(str) encoded = str.encode("utf-8", "utf-8", invalid: :replace)
encoded.should_not equal(str)
encoded.should == str
end
it "returns a copy in the destination encoding when both encodings are the same" do
str = ""
str.force_encoding("binary")
encoded = str.encode("utf-8", "utf-8", invalid: :replace)
encoded.should_not equal(str)
encoded.should == str.force_encoding("utf-8")
encoded.encoding.should == Encoding::UTF_8
end end
end end
end end

View file

@ -89,6 +89,26 @@ describe "An instance method" do
def foo(a); end def foo(a); end
-> { foo 1, 2 }.should raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)') -> { foo 1, 2 }.should raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1)')
end end
it "raises FrozenError with the correct class name" do
-> {
Module.new do
self.freeze
def foo; end
end
}.should raise_error(FrozenError) { |e|
e.message.should.start_with? "can't modify frozen module"
}
-> {
Class.new do
self.freeze
def foo; end
end
}.should raise_error(FrozenError){ |e|
e.message.should.start_with? "can't modify frozen class"
}
end
end end
describe "An instance method definition with a splat" do describe "An instance method definition with a splat" do
@ -266,6 +286,25 @@ describe "A singleton method definition" do
obj.freeze obj.freeze
-> { def obj.foo; end }.should raise_error(FrozenError) -> { def obj.foo; end }.should raise_error(FrozenError)
end end
it "raises FrozenError with the correct class name" do
obj = Object.new
obj.freeze
-> { def obj.foo; end }.should raise_error(FrozenError){ |e|
e.message.should.start_with? "can't modify frozen object"
}
c = obj.singleton_class
-> { def c.foo; end }.should raise_error(FrozenError){ |e|
e.message.should.start_with? "can't modify frozen Class"
}
m = Module.new
m.freeze
-> { def m.foo; end }.should raise_error(FrozenError){ |e|
e.message.should.start_with? "can't modify frozen Module"
}
end
end end
describe "Redefining a singleton method" do describe "Redefining a singleton method" do

View file

@ -0,0 +1,11 @@
5 + 5
module CoverageSpecs
class_eval <<-RUBY, __FILE__, __LINE__ + 1
attr_reader :ok
RUBY
end
4 + 4

View file

@ -5,11 +5,13 @@ describe 'Coverage.result' do
before :all do before :all do
@class_file = fixture __FILE__, 'some_class.rb' @class_file = fixture __FILE__, 'some_class.rb'
@config_file = fixture __FILE__, 'start_coverage.rb' @config_file = fixture __FILE__, 'start_coverage.rb'
@eval_code_file = fixture __FILE__, 'eval_code.rb'
end end
after :each do after :each do
$LOADED_FEATURES.delete(@class_file) $LOADED_FEATURES.delete(@class_file)
$LOADED_FEATURES.delete(@config_file) $LOADED_FEATURES.delete(@config_file)
$LOADED_FEATURES.delete(@eval_code_file)
end end
it 'gives the covered files as a hash with arrays of count or nil' do it 'gives the covered files as a hash with arrays of count or nil' do
@ -75,4 +77,16 @@ describe 'Coverage.result' do
require @config_file.chomp('.rb') require @config_file.chomp('.rb')
Coverage.result.should_not include(@config_file) Coverage.result.should_not include(@config_file)
end end
it 'returns the correct results when eval is used' do
Coverage.start
require @eval_code_file.chomp('.rb')
result = Coverage.result
result.should == {
@eval_code_file => [
1, nil, 1, nil, 1, nil, nil, nil, nil, nil, 1
]
}
end
end end

View file

@ -0,0 +1,7 @@
require_relative '../../../spec_helper'
require 'digest'
require_relative 'shared/update'
describe "Digest::Instance#<<" do
it_behaves_like :digest_instance_update, :<<
end

View file

@ -0,0 +1,8 @@
describe :digest_instance_update, shared: true do
it "raises a RuntimeError if called" do
c = Class.new do
include Digest::Instance
end
-> { c.new.update("test") }.should raise_error(RuntimeError)
end
end

View file

@ -0,0 +1,7 @@
require_relative '../../../spec_helper'
require 'digest'
require_relative 'shared/update'
describe "Digest::Instance#update" do
it_behaves_like :digest_instance_update, :update
end

View file

@ -270,6 +270,15 @@ static VALUE kernel_spec_rb_yield_values(VALUE self, VALUE obj1, VALUE obj2) {
return rb_yield_values(2, obj1, obj2); return rb_yield_values(2, obj1, obj2);
} }
static VALUE kernel_spec_rb_yield_values2(VALUE self, VALUE ary) {
long len = RARRAY_LEN(ary);
VALUE *args = (VALUE*)alloca(sizeof(VALUE) * len);
for (int i = 0; i < len; i++) {
args[i] = rb_ary_entry(ary, i);
}
return rb_yield_values2((int)len, args);
}
static VALUE do_rec(VALUE obj, VALUE arg, int is_rec) { static VALUE do_rec(VALUE obj, VALUE arg, int is_rec) {
if(is_rec) { if(is_rec) {
return obj; return obj;
@ -351,6 +360,7 @@ void Init_kernel_spec(void) {
rb_define_method(cls, "rb_yield_indirected", kernel_spec_rb_yield_indirected, 1); rb_define_method(cls, "rb_yield_indirected", kernel_spec_rb_yield_indirected, 1);
rb_define_method(cls, "rb_yield_define_each", kernel_spec_rb_yield_define_each, 1); rb_define_method(cls, "rb_yield_define_each", kernel_spec_rb_yield_define_each, 1);
rb_define_method(cls, "rb_yield_values", kernel_spec_rb_yield_values, 2); rb_define_method(cls, "rb_yield_values", kernel_spec_rb_yield_values, 2);
rb_define_method(cls, "rb_yield_values2", kernel_spec_rb_yield_values2, 1);
rb_define_method(cls, "rb_yield_splat", kernel_spec_rb_yield_splat, 1); rb_define_method(cls, "rb_yield_splat", kernel_spec_rb_yield_splat, 1);
rb_define_method(cls, "rb_exec_recursive", kernel_spec_rb_exec_recursive, 1); rb_define_method(cls, "rb_exec_recursive", kernel_spec_rb_exec_recursive, 1);
rb_define_method(cls, "rb_set_end_proc", kernel_spec_rb_set_end_proc, 1); rb_define_method(cls, "rb_set_end_proc", kernel_spec_rb_set_end_proc, 1);

View file

@ -315,6 +315,10 @@ static VALUE object_spec_rb_iv_set(VALUE self, VALUE obj, VALUE name, VALUE valu
return rb_iv_set(obj, RSTRING_PTR(name), value); return rb_iv_set(obj, RSTRING_PTR(name), value);
} }
static VALUE object_spec_rb_ivar_count(VALUE self, VALUE obj) {
return ULONG2NUM(rb_ivar_count(obj));
}
static VALUE object_spec_rb_ivar_get(VALUE self, VALUE obj, VALUE sym_name) { static VALUE object_spec_rb_ivar_get(VALUE self, VALUE obj, VALUE sym_name) {
return rb_ivar_get(obj, SYM2ID(sym_name)); return rb_ivar_get(obj, SYM2ID(sym_name));
} }
@ -441,6 +445,7 @@ void Init_object_spec(void) {
rb_define_method(cls, "rb_obj_instance_eval", object_spec_rb_obj_instance_eval, 1); rb_define_method(cls, "rb_obj_instance_eval", object_spec_rb_obj_instance_eval, 1);
rb_define_method(cls, "rb_iv_get", object_spec_rb_iv_get, 2); rb_define_method(cls, "rb_iv_get", object_spec_rb_iv_get, 2);
rb_define_method(cls, "rb_iv_set", object_spec_rb_iv_set, 3); rb_define_method(cls, "rb_iv_set", object_spec_rb_iv_set, 3);
rb_define_method(cls, "rb_ivar_count", object_spec_rb_ivar_count, 1);
rb_define_method(cls, "rb_ivar_get", object_spec_rb_ivar_get, 2); rb_define_method(cls, "rb_ivar_get", object_spec_rb_ivar_get, 2);
rb_define_method(cls, "rb_ivar_set", object_spec_rb_ivar_set, 3); rb_define_method(cls, "rb_ivar_set", object_spec_rb_ivar_set, 3);
rb_define_method(cls, "rb_ivar_defined", object_spec_rb_ivar_defined, 2); rb_define_method(cls, "rb_ivar_defined", object_spec_rb_ivar_defined, 2);

View file

@ -238,6 +238,18 @@ describe "C-API Kernel function" do
end end
end end
describe "rb_yield_values2" do
it "yields passed arguments" do
ret = nil
@s.rb_yield_values2([1, 2]) { |x, y| ret = x + y }
ret.should == 3
end
it "returns the result from block evaluation" do
@s.rb_yield_values2([1, 2]) { |x, y| x + y }.should == 3
end
end
describe "rb_yield_splat" do describe "rb_yield_splat" do
it "yields with passed array's contents" do it "yields with passed array's contents" do
ret = nil ret = nil

View file

@ -820,6 +820,15 @@ describe "CApiObject" do
end end
end end
describe "rb_ivar_count" do
it "returns the number of instance variables" do
obj = Object.new
@o.rb_ivar_count(obj).should == 0
obj.instance_variable_set(:@foo, 42)
@o.rb_ivar_count(obj).should == 1
end
end
describe "rb_ivar_get" do describe "rb_ivar_get" do
it "returns the instance variable on an object" do it "returns the instance variable on an object" do
@o.rb_ivar_get(@test, :@foo).should == @test.instance_eval { @foo } @o.rb_ivar_get(@test, :@foo).should == @test.instance_eval { @foo }