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 2019-09-29 18:01:32 +02:00
parent f9a9f3c7c6
commit a17bc04d15
8 changed files with 158 additions and 8 deletions

View file

@ -5,4 +5,12 @@ require_relative 'shared/collect_concat'
describe "Enumerator::Lazy#flat_map" do describe "Enumerator::Lazy#flat_map" do
it_behaves_like :enumerator_lazy_collect_concat, :flat_map it_behaves_like :enumerator_lazy_collect_concat, :flat_map
it "properly unwraps nested yields" do
s = Enumerator.new do |y| loop do y << [1, 2] end end
expected = s.take(3).flat_map { |x| x }.to_a
actual = s.lazy.take(3).flat_map{ |x| x }.force
actual.should == expected
end
end end

View file

@ -0,0 +1,16 @@
require_relative '../../spec_helper'
describe "GC.stat" do
it "supports access by key" do
keys = [:heap_free_slots, :total_allocated_objects, :count]
keys.each do |key|
GC.stat(key).should be_kind_of(Integer)
end
end
it "returns hash of values" do
stat = GC.stat
stat.should be_kind_of(Hash)
stat.keys.should include(:count)
end
end

View file

@ -101,6 +101,16 @@ describe "Kernel#warn" do
-> { w.f4("foo", 3) }.should output(nil, %r|core/kernel/fixtures/classes.rb:#{w.f3_call_lineno}: warning: foo|) -> { w.f4("foo", 3) }.should output(nil, %r|core/kernel/fixtures/classes.rb:#{w.f3_call_lineno}: warning: foo|)
end end
it "converts first arg using to_s" do
w = KernelSpecs::WarnInNestedCall.new
-> { w.f4(false, 0) }.should output(nil, %r|core/kernel/fixtures/classes.rb:#{w.warn_call_lineno}: warning: false|)
-> { w.f4(nil, 1) }.should output(nil, %r|core/kernel/fixtures/classes.rb:#{w.f1_call_lineno}: warning: |)
obj = mock("obj")
obj.should_receive(:to_s).and_return("to_s called")
-> { w.f4(obj, 2) }.should output(nil, %r|core/kernel/fixtures/classes.rb:#{w.f2_call_lineno}: warning: to_s called|)
end
it "does not prepend caller information if line number is too big" do it "does not prepend caller information if line number is too big" do
w = KernelSpecs::WarnInNestedCall.new w = KernelSpecs::WarnInNestedCall.new
-> { w.f4("foo", 100) }.should output(nil, "warning: foo\n") -> { w.f4("foo", 100) }.should output(nil, "warning: foo\n")
@ -136,5 +146,11 @@ describe "Kernel#warn" do
-> { warn "", uplevel: Object.new }.should raise_error(TypeError) -> { warn "", uplevel: Object.new }.should raise_error(TypeError)
end end
end end
it "treats empty hash as no keyword argument" do
h = {}
-> { warn(**h) }.should_not complain(verbose: true)
-> { warn('foo', **h) }.should complain("foo\n")
end
end end
end end

View file

@ -42,6 +42,18 @@ describe 'Optional variable assignments' do
a.should == 10 a.should == 10
end end
it 'returns the new value if set to false' do
a = false
(a ||= 20).should == 20
end
it 'returns the original value if truthy' do
a = 10
(a ||= 20).should == 10
end
end end
describe 'using a accessor' do describe 'using a accessor' do
@ -89,6 +101,49 @@ describe 'Optional variable assignments' do
@a.b.should == 10 @a.b.should == 10
end end
it 'returns the new value if set to false' do
def @a.b=(x)
:v
end
@a.b = false
(@a.b ||= 20).should == 20
end
it 'returns the original value if truthy' do
def @a.b=(x)
@b = x
:v
end
@a.b = 10
(@a.b ||= 20).should == 10
end
it 'works when writer is private' do
klass = Class.new do
def t
self.b = false
(self.b ||= 10).should == 10
(self.b ||= 20).should == 10
end
def b
@b
end
def b=(x)
@b = x
:v
end
private :b=
end
klass.new.t
end
end end
end end

View file

@ -0,0 +1,30 @@
require_relative '../../../spec_helper'
require 'rubygems'
describe "Gem.bin_path" do
before :each do
@bundle_gemfile = ENV['BUNDLE_GEMFILE']
ENV['BUNDLE_GEMFILE'] = tmp("no-gemfile")
end
after :each do
ENV['BUNDLE_GEMFILE'] = @bundle_gemfile
end
it "finds executables of default gems, which are the only files shipped for default gems" do
# For instance, Gem.bin_path("bundler", "bundle") is used by rails new
if Gem.respond_to? :default_specifications_dir
default_specifications_dir = Gem.default_specifications_dir
else
default_specifications_dir = Gem::Specification.default_specifications_dir
end
Gem::Specification.each_spec([default_specifications_dir]) do |spec|
spec.executables.each do |exe|
path = Gem.bin_path(spec.name, exe)
File.exist?(path).should == true
end
end
end
end

View file

@ -14,14 +14,9 @@ extern "C" {
* On TruffleRuby RSTRING_PTR and the bytes remain in managed memory * On TruffleRuby RSTRING_PTR and the bytes remain in managed memory
* until they must be written to native memory. * until they must be written to native memory.
* In some specs we want to test using the native memory. */ * In some specs we want to test using the native memory. */
char* NATIVE_RSTRING_PTR(VALUE str) { #ifndef NATIVE_RSTRING_PTR
char* ptr = RSTRING_PTR(str); #define NATIVE_RSTRING_PTR(str) RSTRING_PTR(str)
char** native = malloc(sizeof(char*)); #endif
*native = ptr;
ptr = *native;
free(native);
return ptr;
}
VALUE string_spec_rb_cstr2inum(VALUE self, VALUE str, VALUE inum) { VALUE string_spec_rb_cstr2inum(VALUE self, VALUE str, VALUE inum) {
int num = FIX2INT(inum); int num = FIX2INT(inum);
@ -124,6 +119,10 @@ VALUE string_spec_rb_str_conv_enc_opts(VALUE self, VALUE str, VALUE from, VALUE
return rb_str_conv_enc_opts(str, from_enc, to_enc, FIX2INT(ecflags), ecopts); return rb_str_conv_enc_opts(str, from_enc, to_enc, FIX2INT(ecflags), ecopts);
} }
VALUE string_spec_rb_str_drop_bytes(VALUE self, VALUE str, VALUE len) {
return rb_str_drop_bytes(str, NUM2LONG(len));
}
VALUE string_spec_rb_str_export(VALUE self, VALUE str) { VALUE string_spec_rb_str_export(VALUE self, VALUE str) {
return rb_str_export(str); return rb_str_export(str);
} }
@ -427,6 +426,7 @@ void Init_string_spec(void) {
rb_define_method(cls, "rb_str_cmp", string_spec_rb_str_cmp, 2); rb_define_method(cls, "rb_str_cmp", string_spec_rb_str_cmp, 2);
rb_define_method(cls, "rb_str_conv_enc", string_spec_rb_str_conv_enc, 3); rb_define_method(cls, "rb_str_conv_enc", string_spec_rb_str_conv_enc, 3);
rb_define_method(cls, "rb_str_conv_enc_opts", string_spec_rb_str_conv_enc_opts, 5); rb_define_method(cls, "rb_str_conv_enc_opts", string_spec_rb_str_conv_enc_opts, 5);
rb_define_method(cls, "rb_str_drop_bytes", string_spec_rb_str_drop_bytes, 2);
rb_define_method(cls, "rb_str_export", string_spec_rb_str_export, 1); rb_define_method(cls, "rb_str_export", string_spec_rb_str_export, 1);
rb_define_method(cls, "rb_str_export_locale", string_spec_rb_str_export_locale, 1); rb_define_method(cls, "rb_str_export_locale", string_spec_rb_str_export_locale, 1);
rb_define_method(cls, "rb_str_dup", string_spec_rb_str_dup, 1); rb_define_method(cls, "rb_str_dup", string_spec_rb_str_dup, 1);

View file

@ -322,6 +322,11 @@ describe "CApiModule" do
@class.should_not have_instance_method(:ruby_test_method) @class.should_not have_instance_method(:ruby_test_method)
end end
it "undefines private methods also" do
@m.rb_undef_method @class, "initialize_copy"
-> { @class.new.dup }.should raise_error(NoMethodError)
end
it "does not raise exceptions when passed a missing name" do it "does not raise exceptions when passed a missing name" do
-> { @m.rb_undef_method @class, "not_exist" }.should_not raise_error -> { @m.rb_undef_method @class, "not_exist" }.should_not raise_error
end end

View file

@ -977,4 +977,24 @@ end
end end
end end
describe "rb_str_drop_bytes" do
it "drops N characters for an ASCII string" do
str = "12345678".encode("US-ASCII")
@s.rb_str_drop_bytes(str, 4)
str.should == "5678".encode("US-ASCII")
end
it "drop N/2 characters for a UTF-16 string" do
str = "12345678".encode("UTF-16LE")
@s.rb_str_drop_bytes(str, 4)
str.should == "345678".encode("UTF-16LE")
end
it "drop N/4 characters for a UTF-32 string" do
str = "12345678".encode("UTF-32LE")
@s.rb_str_drop_bytes(str, 4)
str.should == "2345678".encode("UTF-32LE")
end
end
end end