diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/ruby/core/exception/exception_spec.rb | 16 | ||||
-rw-r--r-- | spec/ruby/core/proc/shared/to_s.rb | 14 | ||||
-rw-r--r-- | spec/ruby/core/string/chomp_spec.rb | 4 | ||||
-rw-r--r-- | spec/ruby/core/string/shared/each_line.rb | 13 | ||||
-rw-r--r-- | spec/ruby/core/symbol/to_proc_spec.rb | 18 | ||||
-rw-r--r-- | spec/ruby/language/alias_spec.rb | 8 | ||||
-rw-r--r-- | spec/ruby/library/stringio/initialize_spec.rb | 24 | ||||
-rw-r--r-- | spec/ruby/optional/capi/bignum_spec.rb | 10 | ||||
-rw-r--r-- | spec/ruby/optional/capi/ext/bignum_spec.c | 5 | ||||
-rw-r--r-- | spec/ruby/optional/capi/ext/kernel_spec.c | 4 | ||||
-rw-r--r-- | spec/ruby/optional/capi/kernel_spec.rb | 26 |
11 files changed, 127 insertions, 15 deletions
diff --git a/spec/ruby/core/exception/exception_spec.rb b/spec/ruby/core/exception/exception_spec.rb index 750c0ae452..3a01366920 100644 --- a/spec/ruby/core/exception/exception_spec.rb +++ b/spec/ruby/core/exception/exception_spec.rb @@ -66,6 +66,22 @@ describe "Exception#exception" do e2.message.should == "message" end + it "when raised will be rescued as the new exception" do + begin + begin + raised_first = StandardError.new('first') + raise raised_first + rescue => caught_first + raised_second = raised_first.exception('second') + raise raised_second + end + rescue => caught_second + end + + raised_first.should == caught_first + raised_second.should == caught_second + end + class CustomArgumentError < StandardError attr_reader :val def initialize(val) diff --git a/spec/ruby/core/proc/shared/to_s.rb b/spec/ruby/core/proc/shared/to_s.rb index 530eaff3a0..46b21dd083 100644 --- a/spec/ruby/core/proc/shared/to_s.rb +++ b/spec/ruby/core/proc/shared/to_s.rb @@ -32,14 +32,24 @@ describe :proc_to_s, shared: true do describe "for a proc created with UnboundMethod#to_proc" do it "returns a description including '(lambda)' and optionally including file and line number" do def hello; end - method("hello").to_proc.send(@method).should =~ /^#<Proc:([^ ]*?)(@([^ ]*)\/to_s\.rb:22)? \(lambda\)>$/ end it "has an ASCII-8BIT encoding" do def hello; end - method("hello").to_proc.send(@method).encoding.should == Encoding::ASCII_8BIT end end + + describe "for a proc created with Symbol#to_proc" do + it "returns a description including '(&:symbol)'" do + proc = :foobar.to_proc + proc.send(@method).should =~ /^#<Proc:0x\h+\(&:foobar\)>$/ + end + + it "has an ASCII-8BIT encoding" do + proc = :foobar.to_proc + proc.send(@method).encoding.should == Encoding::ASCII_8BIT + end + end end diff --git a/spec/ruby/core/string/chomp_spec.rb b/spec/ruby/core/string/chomp_spec.rb index 6fa8d7c6c5..3c20141ee7 100644 --- a/spec/ruby/core/string/chomp_spec.rb +++ b/spec/ruby/core/string/chomp_spec.rb @@ -158,6 +158,10 @@ describe "String#chomp" do it "does not taint the result when the argument is tainted" do "abc".chomp("abc".taint).tainted?.should be_false end + + it "returns an empty String when the argument equals self" do + "abc".chomp("abc").should == "" + end end end diff --git a/spec/ruby/core/string/shared/each_line.rb b/spec/ruby/core/string/shared/each_line.rb index 19cf5e6d78..066b0c1040 100644 --- a/spec/ruby/core/string/shared/each_line.rb +++ b/spec/ruby/core/string/shared/each_line.rb @@ -27,6 +27,19 @@ describe :string_each_line, shared: true do c.should == ["hello\n", "\n", "\n", "world"] end + it "splits strings containing multibyte characters" do + s = <<~EOS + foo + 🤡🤡🤡🤡🤡🤡🤡 + bar + baz + EOS + + b = [] + s.send(@method) { |part| b << part } + b.should == ["foo\n", "🤡🤡🤡🤡🤡🤡🤡\n", "bar\n", "baz\n"] + end + it "taints substrings that are passed to the block if self is tainted" do "one\ntwo\r\nthree".taint.send(@method) { |s| s.tainted?.should == true } diff --git a/spec/ruby/core/symbol/to_proc_spec.rb b/spec/ruby/core/symbol/to_proc_spec.rb index 8cf00b085f..65f6e27be4 100644 --- a/spec/ruby/core/symbol/to_proc_spec.rb +++ b/spec/ruby/core/symbol/to_proc_spec.rb @@ -12,19 +12,22 @@ describe "Symbol#to_proc" do :to_s.to_proc.call(obj).should == "Received #to_s" end + it "produces a proc with arity -1" do + pr = :to_s.to_proc + pr.arity.should == -1 + end + it "raises an ArgumentError when calling #call on the Proc without receiver" do - lambda { :object_id.to_proc.call }.should raise_error(ArgumentError) + lambda { :object_id.to_proc.call }.should raise_error(ArgumentError, "no receiver given") end it "produces a proc that always returns [[:rest]] for #parameters" do pr = :to_s.to_proc pr.parameters.should == [[:rest]] end -end -describe "Symbol#to_proc" do - before :all do - @klass = Class.new do + it "passes along the block passed to Proc#call" do + klass = Class.new do def m yield end @@ -33,9 +36,6 @@ describe "Symbol#to_proc" do :m.to_proc.call(self) { :value } end end - end - - it "passes along the block passed to Proc#call" do - @klass.new.to_proc.should == :value + klass.new.to_proc.should == :value end end diff --git a/spec/ruby/language/alias_spec.rb b/spec/ruby/language/alias_spec.rb index 04ee09f61c..0aa238b273 100644 --- a/spec/ruby/language/alias_spec.rb +++ b/spec/ruby/language/alias_spec.rb @@ -38,14 +38,14 @@ describe "The alias keyword" do @obj.a.should == 5 end - it "works with a doubule quoted symbol on the left-hand side" do + it "works with a double quoted symbol on the left-hand side" do @meta.class_eval do alias :"a" value end @obj.a.should == 5 end - it "works with an interoplated symbol on the left-hand side" do + it "works with an interpolated symbol on the left-hand side" do @meta.class_eval do alias :"#{'a'}" value end @@ -66,14 +66,14 @@ describe "The alias keyword" do @obj.a.should == 5 end - it "works with a doubule quoted symbol on the right-hand side" do + it "works with a double quoted symbol on the right-hand side" do @meta.class_eval do alias a :"value" end @obj.a.should == 5 end - it "works with an interoplated symbol on the right-hand side" do + it "works with an interpolated symbol on the right-hand side" do @meta.class_eval do alias a :"#{'value'}" end diff --git a/spec/ruby/library/stringio/initialize_spec.rb b/spec/ruby/library/stringio/initialize_spec.rb index bbb11b8f31..b503ed2206 100644 --- a/spec/ruby/library/stringio/initialize_spec.rb +++ b/spec/ruby/library/stringio/initialize_spec.rb @@ -183,3 +183,27 @@ describe "StringIO#initialize when passed no arguments" do @io.string.should == "" end end + +describe "StringIO#initialize sets the encoding to" do + before :each do + @external = Encoding.default_external + Encoding.default_external = Encoding::ISO_8859_2 + end + + after :each do + Encoding.default_external = @external + end + + it "Encoding.default_external when passed no arguments" do + io = StringIO.new + io.external_encoding.should == Encoding::ISO_8859_2 + io.string.encoding.should == Encoding::ISO_8859_2 + end + + it "the same as the encoding of the String when passed a String" do + s = ''.force_encoding(Encoding::EUC_JP) + io = StringIO.new(s) + io.external_encoding.should == Encoding::EUC_JP + io.string.encoding.should == Encoding::EUC_JP + end +end diff --git a/spec/ruby/optional/capi/bignum_spec.rb b/spec/ruby/optional/capi/bignum_spec.rb index ee95550680..a5863519b6 100644 --- a/spec/ruby/optional/capi/bignum_spec.rb +++ b/spec/ruby/optional/capi/bignum_spec.rb @@ -97,6 +97,16 @@ describe "CApiBignumSpecs" do end end + describe "RBIGNUM_SIGN" do + it "returns 1 for a positive Bignum" do + @s.RBIGNUM_SIGN(bignum_value(1)).should == 1 + end + + it "returns 0 for a negative Bignum" do + @s.RBIGNUM_SIGN(-bignum_value(1)).should == 0 + end + end + describe "rb_big_cmp" do it "compares a Bignum with a Bignum" do @s.rb_big_cmp(bignum_value, bignum_value(1)).should == -1 diff --git a/spec/ruby/optional/capi/ext/bignum_spec.c b/spec/ruby/optional/capi/ext/bignum_spec.c index a773e06436..14a51f5099 100644 --- a/spec/ruby/optional/capi/ext/bignum_spec.c +++ b/spec/ruby/optional/capi/ext/bignum_spec.c @@ -31,6 +31,10 @@ static VALUE bignum_spec_rb_big2ulong(VALUE self, VALUE num) { return ULONG2NUM(rb_big2ulong(num)); } +static VALUE bignum_spec_RBIGNUM_SIGN(VALUE self, VALUE val) { + return INT2FIX(RBIGNUM_SIGN(val)); +} + static VALUE bignum_spec_rb_big_cmp(VALUE self, VALUE x, VALUE y) { return rb_big_cmp(x, y); } @@ -90,6 +94,7 @@ void Init_bignum_spec(void) { rb_define_method(cls, "rb_big2long", bignum_spec_rb_big2long, 1); rb_define_method(cls, "rb_big2str", bignum_spec_rb_big2str, 2); rb_define_method(cls, "rb_big2ulong", bignum_spec_rb_big2ulong, 1); + rb_define_method(cls, "RBIGNUM_SIGN", bignum_spec_RBIGNUM_SIGN, 1); rb_define_method(cls, "rb_big_cmp", bignum_spec_rb_big_cmp, 2); rb_define_method(cls, "rb_big_pack", bignum_spec_rb_big_pack, 1); rb_define_method(cls, "rb_big_pack_array", bignum_spec_rb_big_pack_array, 2); diff --git a/spec/ruby/optional/capi/ext/kernel_spec.c b/spec/ruby/optional/capi/ext/kernel_spec.c index a4879b0523..5ec45f542c 100644 --- a/spec/ruby/optional/capi/ext/kernel_spec.c +++ b/spec/ruby/optional/capi/ext/kernel_spec.c @@ -26,6 +26,9 @@ VALUE kernel_spec_rb_block_proc(VALUE self) { return rb_block_proc(); } +VALUE kernel_spec_rb_block_lambda(VALUE self) { + return rb_block_lambda(); +} VALUE block_call_inject(VALUE yield_value, VALUE data2) { /* yield_value yields the first block argument */ @@ -286,6 +289,7 @@ void Init_kernel_spec(void) { rb_define_method(cls, "rb_block_call_multi_arg", kernel_spec_rb_block_call_multi_arg, 1); rb_define_method(cls, "rb_block_call_no_func", kernel_spec_rb_block_call_no_func, 1); rb_define_method(cls, "rb_block_proc", kernel_spec_rb_block_proc, 0); + rb_define_method(cls, "rb_block_lambda", kernel_spec_rb_block_lambda, 0); rb_define_method(cls, "rb_frame_this_func_test", kernel_spec_rb_frame_this_func, 0); rb_define_method(cls, "rb_frame_this_func_test_again", kernel_spec_rb_frame_this_func, 0); rb_define_method(cls, "rb_ensure", kernel_spec_rb_ensure, 4); diff --git a/spec/ruby/optional/capi/kernel_spec.rb b/spec/ruby/optional/capi/kernel_spec.rb index e50ae9c4e4..627442104f 100644 --- a/spec/ruby/optional/capi/kernel_spec.rb +++ b/spec/ruby/optional/capi/kernel_spec.rb @@ -440,6 +440,32 @@ describe "C-API Kernel function" do proc = @s.rb_block_proc() { 1+1 } proc.should be_kind_of(Proc) proc.call.should == 2 + proc.lambda?.should == false + end + + it "passes through an existing lambda and does not convert to a proc" do + b = -> { 1+1 } + proc = @s.rb_block_proc(&b) + proc.should equal(b) + proc.call.should == 2 + proc.lambda?.should == true + end + end + + describe "rb_block_lambda" do + it "converts the implicit block into a Proc but does not convert it to a lambda" do + proc = @s.rb_block_proc { 1+1 } + proc.should be_kind_of(Proc) + proc.call.should == 2 + proc.lambda?.should == false + end + + it "passes through an existing Proc and does not convert to a lambda" do + b = proc { 1+1 } + proc = @s.rb_block_proc(&b) + proc.should equal(b) + proc.call.should == 2 + proc.lambda?.should == false end end |