summaryrefslogtreecommitdiff
path: root/spec/ruby
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2019-09-29 18:01:32 +0200
committerBenoit Daloze <eregontp@gmail.com>2019-09-29 18:01:32 +0200
commita17bc04d159ec9839cc8cfb02dc0cdd2802110f4 (patch)
treea10d4121aeb1517c198ab5b1577bca93912dc1f9 /spec/ruby
parentf9a9f3c7c6cf3fe534d4934dd3b502d721151b81 (diff)
Update to ruby/spec@e69a14c
Diffstat (limited to 'spec/ruby')
-rw-r--r--spec/ruby/core/enumerator/lazy/flat_map_spec.rb8
-rw-r--r--spec/ruby/core/gc/stat_spec.rb16
-rw-r--r--spec/ruby/core/kernel/warn_spec.rb16
-rw-r--r--spec/ruby/language/optional_assignments_spec.rb55
-rw-r--r--spec/ruby/library/rubygems/gem/bin_path_spec.rb30
-rw-r--r--spec/ruby/optional/capi/ext/string_spec.c16
-rw-r--r--spec/ruby/optional/capi/module_spec.rb5
-rw-r--r--spec/ruby/optional/capi/string_spec.rb20
8 files changed, 158 insertions, 8 deletions
diff --git a/spec/ruby/core/enumerator/lazy/flat_map_spec.rb b/spec/ruby/core/enumerator/lazy/flat_map_spec.rb
index 6a3391a503..5dcaa8bfa1 100644
--- a/spec/ruby/core/enumerator/lazy/flat_map_spec.rb
+++ b/spec/ruby/core/enumerator/lazy/flat_map_spec.rb
@@ -5,4 +5,12 @@ require_relative 'shared/collect_concat'
describe "Enumerator::Lazy#flat_map" do
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
diff --git a/spec/ruby/core/gc/stat_spec.rb b/spec/ruby/core/gc/stat_spec.rb
new file mode 100644
index 0000000000..51bd00c7c7
--- /dev/null
+++ b/spec/ruby/core/gc/stat_spec.rb
@@ -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
diff --git a/spec/ruby/core/kernel/warn_spec.rb b/spec/ruby/core/kernel/warn_spec.rb
index d05a37958d..2a404a2a35 100644
--- a/spec/ruby/core/kernel/warn_spec.rb
+++ b/spec/ruby/core/kernel/warn_spec.rb
@@ -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|)
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
w = KernelSpecs::WarnInNestedCall.new
-> { 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)
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
diff --git a/spec/ruby/language/optional_assignments_spec.rb b/spec/ruby/language/optional_assignments_spec.rb
index 04abc2496b..3d9e0dbb65 100644
--- a/spec/ruby/language/optional_assignments_spec.rb
+++ b/spec/ruby/language/optional_assignments_spec.rb
@@ -42,6 +42,18 @@ describe 'Optional variable assignments' do
a.should == 10
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
describe 'using a accessor' do
@@ -89,6 +101,49 @@ describe 'Optional variable assignments' do
@a.b.should == 10
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
diff --git a/spec/ruby/library/rubygems/gem/bin_path_spec.rb b/spec/ruby/library/rubygems/gem/bin_path_spec.rb
new file mode 100644
index 0000000000..cdf9507bfd
--- /dev/null
+++ b/spec/ruby/library/rubygems/gem/bin_path_spec.rb
@@ -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
diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c
index 6ad0b46ae4..4976dc92c4 100644
--- a/spec/ruby/optional/capi/ext/string_spec.c
+++ b/spec/ruby/optional/capi/ext/string_spec.c
@@ -14,14 +14,9 @@ extern "C" {
* On TruffleRuby RSTRING_PTR and the bytes remain in managed memory
* until they must be written to native memory.
* In some specs we want to test using the native memory. */
-char* NATIVE_RSTRING_PTR(VALUE str) {
- char* ptr = RSTRING_PTR(str);
- char** native = malloc(sizeof(char*));
- *native = ptr;
- ptr = *native;
- free(native);
- return ptr;
-}
+#ifndef NATIVE_RSTRING_PTR
+#define NATIVE_RSTRING_PTR(str) RSTRING_PTR(str)
+#endif
VALUE string_spec_rb_cstr2inum(VALUE self, VALUE str, VALUE 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);
}
+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) {
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_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_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_locale", string_spec_rb_str_export_locale, 1);
rb_define_method(cls, "rb_str_dup", string_spec_rb_str_dup, 1);
diff --git a/spec/ruby/optional/capi/module_spec.rb b/spec/ruby/optional/capi/module_spec.rb
index 34e6db82d8..bf09e9d8a5 100644
--- a/spec/ruby/optional/capi/module_spec.rb
+++ b/spec/ruby/optional/capi/module_spec.rb
@@ -322,6 +322,11 @@ describe "CApiModule" do
@class.should_not have_instance_method(:ruby_test_method)
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
-> { @m.rb_undef_method @class, "not_exist" }.should_not raise_error
end
diff --git a/spec/ruby/optional/capi/string_spec.rb b/spec/ruby/optional/capi/string_spec.rb
index 469a190abd..1cfd590ca4 100644
--- a/spec/ruby/optional/capi/string_spec.rb
+++ b/spec/ruby/optional/capi/string_spec.rb
@@ -977,4 +977,24 @@ 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