diff options
Diffstat (limited to 'spec/ruby/core/symbol')
31 files changed, 607 insertions, 558 deletions
diff --git a/spec/ruby/core/symbol/all_symbols_spec.rb b/spec/ruby/core/symbol/all_symbols_spec.rb index ef2b4f85e6..689f6211de 100644 --- a/spec/ruby/core/symbol/all_symbols_spec.rb +++ b/spec/ruby/core/symbol/all_symbols_spec.rb @@ -1,14 +1,19 @@ require_relative '../../spec_helper' describe "Symbol.all_symbols" do - it "returns an array containing all the Symbols in the symbol table" do + it "returns an array of Symbols" do all_symbols = Symbol.all_symbols - all_symbols.should be_an_instance_of(Array) - all_symbols.all? { |s| s.is_a?(Symbol) ? true : (p s; false) }.should == true + all_symbols.should.instance_of?(Array) + all_symbols.each { |s| s.should.instance_of?(Symbol) } end - it "returns an Array containing Symbols that have been created" do + it "includes symbols that are strongly referenced" do symbol = "symbol_specs_#{rand(5_000_000)}".to_sym - Symbol.all_symbols.should include(symbol) + Symbol.all_symbols.should.include?(symbol) + end + + it "includes symbols that are referenced in source code but not yet executed" do + Symbol.all_symbols.any? { |s| s.to_s == 'symbol_specs_referenced_in_source_code' }.should == true + :symbol_specs_referenced_in_source_code end end diff --git a/spec/ruby/core/symbol/capitalize_spec.rb b/spec/ruby/core/symbol/capitalize_spec.rb index 54aebf21ca..a93d951e6a 100644 --- a/spec/ruby/core/symbol/capitalize_spec.rb +++ b/spec/ruby/core/symbol/capitalize_spec.rb @@ -3,7 +3,7 @@ require_relative '../../spec_helper' describe "Symbol#capitalize" do it "returns a Symbol" do - :glark.capitalize.should be_an_instance_of(Symbol) + :glark.capitalize.should.instance_of?(Symbol) end it "converts the first character to uppercase if it is ASCII" do @@ -14,18 +14,9 @@ describe "Symbol#capitalize" do :"£1.20".capitalize.should == :"£1.20" end - ruby_version_is ''...'2.4' do - it "leaves the first character alone if it is not an alphabetical ASCII character" do - "\u{00DE}c".to_sym.capitalize.should == :"Þc" - "\u{00DF}C".to_sym.capitalize.should == :"ßc" - end - end - - ruby_version_is '2.4' do - it "capitalizes the first character if it is Unicode" do - :"äöü".capitalize.should == :"Äöü" - :"aou".capitalize.should == :"Aou" - end + it "capitalizes the first character if it is Unicode" do + :"äöü".capitalize.should == :"Äöü" + :"aou".capitalize.should == :"Aou" end it "converts subsequent uppercase ASCII characters to their lowercase equivalents" do @@ -40,12 +31,6 @@ describe "Symbol#capitalize" do :mIxEd.capitalize.should == :Mixed end - ruby_version_is ''...'2.4' do - it "leaves uppercase Unicode characters as they were" do - "a\u{00DE}c".to_sym.capitalize.should == :"AÞc" - end - end - it "leaves lowercase Unicode characters (except in first position) as they were" do "a\u{00DF}C".to_sym.capitalize.should == :"Aßc" end diff --git a/spec/ruby/core/symbol/case_compare_spec.rb b/spec/ruby/core/symbol/case_compare_spec.rb index 0c6bc1eda5..a296132c04 100644 --- a/spec/ruby/core/symbol/case_compare_spec.rb +++ b/spec/ruby/core/symbol/case_compare_spec.rb @@ -1,11 +1,7 @@ require_relative '../../spec_helper' describe "Symbol#===" do - it "returns true when the argument is a Symbol" do - (Symbol === :ruby).should == true - end - - it "returns false when the argument is a String" do - (Symbol === 'ruby').should == false + it "is an alias of Symbol#==" do + Symbol.instance_method(:===).should == Symbol.instance_method(:==) end end diff --git a/spec/ruby/core/symbol/casecmp_spec.rb b/spec/ruby/core/symbol/casecmp_spec.rb index 232399b664..dcb77a8350 100644 --- a/spec/ruby/core/symbol/casecmp_spec.rb +++ b/spec/ruby/core/symbol/casecmp_spec.rb @@ -56,91 +56,97 @@ describe "Symbol#casecmp with Symbol" do lower_a_tilde.casecmp(upper_a_tilde).should == 1 lower_a_umlaut.casecmp(upper_a_umlaut).should == 1 end + + it "returns 0 for empty strings in different encodings" do + ''.to_sym.casecmp(''.encode("UTF-32LE").to_sym).should == 0 + end end describe "Symbol#casecmp" do it "returns nil if other is a String" do - :abc.casecmp("abc").should be_nil + :abc.casecmp("abc").should == nil end - it "returns nil if other is a Fixnum" do - :abc.casecmp(1).should be_nil + it "returns nil if other is an Integer" do + :abc.casecmp(1).should == nil end it "returns nil if other is an object" do obj = mock("string <=>") - :abc.casecmp(obj).should be_nil + :abc.casecmp(obj).should == nil end end -ruby_version_is "2.4" do - describe 'Symbol#casecmp?' do - it "compares symbols without regard to case" do - :abcdef.casecmp?(:abcde).should == false - :aBcDeF.casecmp?(:abcdef).should == true - :abcdef.casecmp?(:abcdefg).should == false - :abcdef.casecmp?(:ABCDEF).should == true - end - - it "doesn't consider non-ascii characters equal that aren't" do - # -- Latin-1 -- - upper_a_tilde = "\xC3".b.to_sym - upper_a_umlaut = "\xC4".b.to_sym - lower_a_tilde = "\xE3".b.to_sym - lower_a_umlaut = "\xE4".b.to_sym - - lower_a_tilde.casecmp?(lower_a_umlaut).should_not == true - lower_a_umlaut.casecmp?(lower_a_tilde).should_not == true - upper_a_tilde.casecmp?(upper_a_umlaut).should_not == true - upper_a_umlaut.casecmp?(upper_a_tilde).should_not == true - - # -- UTF-8 -- - upper_a_tilde = :"Ã" - lower_a_tilde = :"ã" - upper_a_umlaut = :"Ä" - lower_a_umlaut = :"ä" - - lower_a_tilde.casecmp?(lower_a_umlaut).should_not == true - lower_a_umlaut.casecmp?(lower_a_tilde).should_not == true - upper_a_tilde.casecmp?(upper_a_umlaut).should_not == true - upper_a_umlaut.casecmp?(upper_a_tilde).should_not == true - end - - it "doesn't do case mapping for non-ascii and non-unicode characters" do - # -- Latin-1 -- - upper_a_tilde = "\xC3".b.to_sym - upper_a_umlaut = "\xC4".b.to_sym - lower_a_tilde = "\xE3".b.to_sym - lower_a_umlaut = "\xE4".b.to_sym - - upper_a_tilde.casecmp?(lower_a_tilde).should == false - upper_a_umlaut.casecmp?(lower_a_umlaut).should == false - lower_a_tilde.casecmp?(upper_a_tilde).should == false - lower_a_umlaut.casecmp?(upper_a_umlaut).should == false - end - - it 'does case mapping for unicode characters' do - # -- UTF-8 -- - upper_a_tilde = :"Ã" - lower_a_tilde = :"ã" - upper_a_umlaut = :"Ä" - lower_a_umlaut = :"ä" - - upper_a_tilde.casecmp?(lower_a_tilde).should == true - upper_a_umlaut.casecmp?(lower_a_umlaut).should == true - lower_a_tilde.casecmp?(upper_a_tilde).should == true - lower_a_umlaut.casecmp?(upper_a_umlaut).should == true - end - - it 'returns nil when comparing characters with different encodings' do - # -- Latin-1 -- - upper_a_tilde = "\xC3".b.to_sym - - # -- UTF-8 -- - lower_a_tilde = :"ã" - - upper_a_tilde.casecmp?(lower_a_tilde).should == nil - lower_a_tilde.casecmp?(upper_a_tilde).should == nil - end +describe 'Symbol#casecmp?' do + it "compares symbols without regard to case" do + :abcdef.casecmp?(:abcde).should == false + :aBcDeF.casecmp?(:abcdef).should == true + :abcdef.casecmp?(:abcdefg).should == false + :abcdef.casecmp?(:ABCDEF).should == true + end + + it "doesn't consider non-ascii characters equal that aren't" do + # -- Latin-1 -- + upper_a_tilde = "\xC3".b.to_sym + upper_a_umlaut = "\xC4".b.to_sym + lower_a_tilde = "\xE3".b.to_sym + lower_a_umlaut = "\xE4".b.to_sym + + lower_a_tilde.casecmp?(lower_a_umlaut).should_not == true + lower_a_umlaut.casecmp?(lower_a_tilde).should_not == true + upper_a_tilde.casecmp?(upper_a_umlaut).should_not == true + upper_a_umlaut.casecmp?(upper_a_tilde).should_not == true + + # -- UTF-8 -- + upper_a_tilde = :"Ã" + lower_a_tilde = :"ã" + upper_a_umlaut = :"Ä" + lower_a_umlaut = :"ä" + + lower_a_tilde.casecmp?(lower_a_umlaut).should_not == true + lower_a_umlaut.casecmp?(lower_a_tilde).should_not == true + upper_a_tilde.casecmp?(upper_a_umlaut).should_not == true + upper_a_umlaut.casecmp?(upper_a_tilde).should_not == true + end + + it "doesn't do case mapping for non-ascii and non-unicode characters" do + # -- Latin-1 -- + upper_a_tilde = "\xC3".b.to_sym + upper_a_umlaut = "\xC4".b.to_sym + lower_a_tilde = "\xE3".b.to_sym + lower_a_umlaut = "\xE4".b.to_sym + + upper_a_tilde.casecmp?(lower_a_tilde).should == false + upper_a_umlaut.casecmp?(lower_a_umlaut).should == false + lower_a_tilde.casecmp?(upper_a_tilde).should == false + lower_a_umlaut.casecmp?(upper_a_umlaut).should == false + end + + it 'does case mapping for unicode characters' do + # -- UTF-8 -- + upper_a_tilde = :"Ã" + lower_a_tilde = :"ã" + upper_a_umlaut = :"Ä" + lower_a_umlaut = :"ä" + + upper_a_tilde.casecmp?(lower_a_tilde).should == true + upper_a_umlaut.casecmp?(lower_a_umlaut).should == true + lower_a_tilde.casecmp?(upper_a_tilde).should == true + lower_a_umlaut.casecmp?(upper_a_umlaut).should == true + end + + it 'returns nil when comparing characters with different encodings' do + # -- Latin-1 -- + upper_a_tilde = "\xC3".b.to_sym + + # -- UTF-8 -- + lower_a_tilde = :"ã" + + upper_a_tilde.casecmp?(lower_a_tilde).should == nil + lower_a_tilde.casecmp?(upper_a_tilde).should == nil + end + + it "returns true for empty symbols in different encodings" do + ''.to_sym.should.casecmp?(''.encode("UTF-32LE").to_sym) end end diff --git a/spec/ruby/core/symbol/comparison_spec.rb b/spec/ruby/core/symbol/comparison_spec.rb index 173a7da625..6d56176e97 100644 --- a/spec/ruby/core/symbol/comparison_spec.rb +++ b/spec/ruby/core/symbol/comparison_spec.rb @@ -37,15 +37,15 @@ end describe "Symbol#<=>" do it "returns nil if other is a String" do - (:abc <=> "abc").should be_nil + (:abc <=> "abc").should == nil end - it "returns nil if other is a Fixnum" do - (:abc <=> 1).should be_nil + it "returns nil if other is an Integer" do + (:abc <=> 1).should == nil end it "returns nil if other is an object" do obj = mock("string <=>") - (:abc <=> obj).should be_nil + (:abc <=> obj).should == nil end end diff --git a/spec/ruby/core/symbol/downcase_spec.rb b/spec/ruby/core/symbol/downcase_spec.rb index eb81c90d05..76418aa9da 100644 --- a/spec/ruby/core/symbol/downcase_spec.rb +++ b/spec/ruby/core/symbol/downcase_spec.rb @@ -3,7 +3,7 @@ require_relative '../../spec_helper' describe "Symbol#downcase" do it "returns a Symbol" do - :glark.downcase.should be_an_instance_of(Symbol) + :glark.downcase.should.instance_of?(Symbol) end it "converts uppercase ASCII characters to their lowercase equivalents" do @@ -14,17 +14,9 @@ describe "Symbol#downcase" do "\u{E0}Bc".to_sym.downcase.should == :"àbc" end - ruby_version_is ''...'2.4' do - it "leaves uppercase Unicode characters as they were" do - "\u{DE}Bc".to_sym.downcase.should == :"Þbc" - end - end - - ruby_version_is '2.4' do - it "uncapitalizes all Unicode characters" do - "ÄÖÜ".to_sym.downcase.should == :"äöü" - "AOU".to_sym.downcase.should == :"aou" - end + it "uncapitalizes all Unicode characters" do + "ÄÖÜ".to_sym.downcase.should == :"äöü" + "AOU".to_sym.downcase.should == :"aou" end it "leaves non-alphabetic ASCII characters as they were" do diff --git a/spec/ruby/core/symbol/dup_spec.rb b/spec/ruby/core/symbol/dup_spec.rb index 202720e8f5..eef3078030 100644 --- a/spec/ruby/core/symbol/dup_spec.rb +++ b/spec/ruby/core/symbol/dup_spec.rb @@ -1,9 +1,7 @@ require_relative '../../spec_helper' -ruby_version_is '2.4' do - describe "Symbol#dup" do - it "returns self" do - :a_symbol.dup.should equal(:a_symbol) - end +describe "Symbol#dup" do + it "returns self" do + :a_symbol.dup.should.equal?(:a_symbol) end end diff --git a/spec/ruby/core/symbol/element_reference_spec.rb b/spec/ruby/core/symbol/element_reference_spec.rb index df6bc15ddb..360a661891 100644 --- a/spec/ruby/core/symbol/element_reference_spec.rb +++ b/spec/ruby/core/symbol/element_reference_spec.rb @@ -1,6 +1,263 @@ require_relative '../../spec_helper' -require_relative 'shared/slice' +require_relative 'fixtures/classes' describe "Symbol#[]" do - it_behaves_like :symbol_slice, :[] + describe "with an Integer index" do + it "returns the character code of the element at the index" do + :symbol[1].should == ?y + end + + it "returns nil if the index starts from the end and is greater than the length" do + :symbol[-10].should == nil + end + + it "returns nil if the index is greater than the length" do + :symbol[42].should == nil + end + end + + describe "with an Integer index and length" do + describe "and a positive index and length" do + it "returns a slice" do + :symbol[1, 3].should == "ymb" + end + + it "returns a blank slice if the length is 0" do + :symbol[0, 0].should == "" + :symbol[1, 0].should == "" + end + + it "returns a slice of all remaining characters if the given length is greater than the actual length" do + :symbol[1, 100].should == "ymbol" + end + + it "returns nil if the index is greater than the length" do + :symbol[10, 1].should == nil + end + end + + describe "and a positive index and negative length" do + it "returns nil" do + :symbol[0, -1].should == nil + :symbol[1, -1].should == nil + end + end + + describe "and a negative index and positive length" do + it "returns a slice starting from the end upto the length" do + :symbol[-3, 2].should == "bo" + end + + it "returns a blank slice if the length is 0" do + :symbol[-1, 0].should == "" + end + + it "returns a slice of all remaining characters if the given length is larger than the actual length" do + :symbol[-4, 100].should == "mbol" + end + + it "returns nil if the index is past the start" do + :symbol[-10, 1].should == nil + end + end + + describe "and a negative index and negative length" do + it "returns nil" do + :symbol[-1, -1].should == nil + end + end + + describe "and a Float length" do + it "converts the length to an Integer" do + :symbol[2, 2.5].should == "mb" + end + end + + describe "and a nil length" do + it "raises a TypeError" do + -> { :symbol[1, nil] }.should.raise(TypeError) + end + end + + describe "and a length that cannot be converted into an Integer" do + it "raises a TypeError when given an Array" do + -> { :symbol[1, Array.new] }.should.raise(TypeError) + end + + it "raises a TypeError when given an Hash" do + -> { :symbol[1, Hash.new] }.should.raise(TypeError) + end + + it "raises a TypeError when given an Object" do + -> { :symbol[1, Object.new] }.should.raise(TypeError) + end + end + end + + describe "with a Float index" do + it "converts the index to an Integer" do + :symbol[1.5].should == ?y + end + end + + describe "with a nil index" do + it "raises a TypeError" do + -> { :symbol[nil] }.should.raise(TypeError) + end + end + + describe "with an index that cannot be converted into an Integer" do + it "raises a TypeError when given an Array" do + -> { :symbol[Array.new] }.should.raise(TypeError) + end + + it "raises a TypeError when given an Hash" do + -> { :symbol[Hash.new] }.should.raise(TypeError) + end + + it "raises a TypeError when given an Object" do + -> { :symbol[Object.new] }.should.raise(TypeError) + end + end + + describe "with a Range slice" do + describe "that is within bounds" do + it "returns a slice if both range values begin at the start and are within bounds" do + :symbol[1..4].should == "ymbo" + end + + it "returns a slice if the first range value begins at the start and the last begins at the end" do + :symbol[1..-1].should == "ymbol" + end + + it "returns a slice if the first range value begins at the end and the last begins at the end" do + :symbol[-4..-1].should == "mbol" + end + end + + describe "that is out of bounds" do + it "returns nil if the first range value begins past the end" do + :symbol[10..12].should == nil + end + + it "returns a blank string if the first range value is within bounds and the last range value is not" do + :symbol[-2..-10].should == "" + :symbol[2..-10].should == "" + end + + it "returns nil if the first range value starts from the end and is within bounds and the last value starts from the end and is greater than the length" do + :symbol[-10..-12].should == nil + end + + it "returns nil if the first range value starts from the end and is out of bounds and the last value starts from the end and is less than the length" do + :symbol[-10..-2].should == nil + end + end + + describe "with Float values" do + it "converts the first value to an Integer" do + :symbol[0.5..2].should == "sym" + end + + it "converts the last value to an Integer" do + :symbol[0..2.5].should == "sym" + end + end + end + + describe "with a Range subclass slice" do + it "returns a slice" do + range = SymbolSpecs::MyRange.new(1, 4) + :symbol[range].should == "ymbo" + end + end + + describe "with a Regex slice" do + describe "without a capture index" do + it "returns a string of the match" do + :symbol[/[^bol]+/].should == "sym" + end + + it "returns nil if the expression does not match" do + :symbol[/0-9/].should == nil + end + + it "sets $~ to the MatchData if there is a match" do + :symbol[/[^bol]+/] + $~[0].should == "sym" + end + + it "does not set $~ if there if there is not a match" do + :symbol[/[0-9]+/] + $~.should == nil + end + end + + describe "with a capture index" do + it "returns a string of the complete match if the capture index is 0" do + :symbol[/(sy)(mb)(ol)/, 0].should == "symbol" + end + + it "returns a string for the matched capture at the given index" do + :symbol[/(sy)(mb)(ol)/, 1].should == "sy" + :symbol[/(sy)(mb)(ol)/, -1].should == "ol" + end + + it "returns nil if there is no capture for the index" do + :symbol[/(sy)(mb)(ol)/, 4].should == nil + :symbol[/(sy)(mb)(ol)/, -4].should == nil + end + + it "converts the index to an Integer" do + :symbol[/(sy)(mb)(ol)/, 1.5].should == "sy" + end + + describe "and an index that cannot be converted to an Integer" do + it "raises a TypeError when given an Hash" do + -> { :symbol[/(sy)(mb)(ol)/, Hash.new] }.should.raise(TypeError) + end + + it "raises a TypeError when given an Array" do + -> { :symbol[/(sy)(mb)(ol)/, Array.new] }.should.raise(TypeError) + end + + it "raises a TypeError when given an Object" do + -> { :symbol[/(sy)(mb)(ol)/, Object.new] }.should.raise(TypeError) + end + end + + it "raises a TypeError if the index is nil" do + -> { :symbol[/(sy)(mb)(ol)/, nil] }.should.raise(TypeError) + end + + it "sets $~ to the MatchData if there is a match" do + :symbol[/(sy)(mb)(ol)/, 0] + $~[0].should == "symbol" + $~[1].should == "sy" + $~[2].should == "mb" + $~[3].should == "ol" + end + + it "does not set $~ to the MatchData if there is not a match" do + :symbol[/0-9/, 0] + $~.should == nil + end + end + end + + describe "with a String slice" do + it "does not set $~" do + $~ = nil + :symbol["sym"] + $~.should == nil + end + + it "returns a string if there is match" do + :symbol["ymb"].should == "ymb" + end + + it "returns nil if there is not a match" do + :symbol["foo"].should == nil + end + end end diff --git a/spec/ruby/core/symbol/empty_spec.rb b/spec/ruby/core/symbol/empty_spec.rb index 19c23cfe5f..1d90a59a5b 100644 --- a/spec/ruby/core/symbol/empty_spec.rb +++ b/spec/ruby/core/symbol/empty_spec.rb @@ -2,10 +2,10 @@ require_relative '../../spec_helper' describe "Symbol#empty?" do it "returns true if self is empty" do - :"".empty?.should be_true + :"".empty?.should == true end it "returns false if self is non-empty" do - :"a".empty?.should be_false + :"a".empty?.should == false end end diff --git a/spec/ruby/core/symbol/end_with_spec.rb b/spec/ruby/core/symbol/end_with_spec.rb new file mode 100644 index 0000000000..4b9f5a4996 --- /dev/null +++ b/spec/ruby/core/symbol/end_with_spec.rb @@ -0,0 +1,8 @@ +# -*- encoding: utf-8 -*- + +require_relative '../../spec_helper' +require_relative '../../shared/string/end_with' + +describe "Symbol#end_with?" do + it_behaves_like :end_with, :to_sym +end diff --git a/spec/ruby/core/symbol/id2name_spec.rb b/spec/ruby/core/symbol/id2name_spec.rb index 2caa89fc37..abcbff65f4 100644 --- a/spec/ruby/core/symbol/id2name_spec.rb +++ b/spec/ruby/core/symbol/id2name_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/id2name' describe "Symbol#id2name" do - it_behaves_like :symbol_id2name, :id2name + it "is an alias of Symbol#to_s" do + Symbol.instance_method(:id2name).should == Symbol.instance_method(:to_s) + end end diff --git a/spec/ruby/core/symbol/inspect_spec.rb b/spec/ruby/core/symbol/inspect_spec.rb index 58402ab261..f2269996af 100644 --- a/spec/ruby/core/symbol/inspect_spec.rb +++ b/spec/ruby/core/symbol/inspect_spec.rb @@ -5,6 +5,8 @@ describe "Symbol#inspect" do fred: ":fred", :fred? => ":fred?", :fred! => ":fred!", + :BAD! => ":BAD!", + :_BAD! => ":_BAD!", :$ruby => ":$ruby", :@ruby => ":@ruby", :@@ruby => ":@@ruby", @@ -64,9 +66,9 @@ describe "Symbol#inspect" do :~ => ":~", :| => ":|", - :"!" => [":\"!\"", ":!" ], - :"!=" => [":\"!=\"", ":!="], - :"!~" => [":\"!~\"", ":!~"], + :"!" => ":!", + :"!=" => ":!=", + :"!~" => ":!~", :"\$" => ":\"$\"", # for justice! :"&&" => ":\"&&\"", :"'" => ":\"\'\"", @@ -94,12 +96,36 @@ describe "Symbol#inspect" do :"foo " => ":\"foo \"", :" foo" => ":\" foo\"", :" " => ":\" \"", + + :"ê" => [":ê", ":\"\\u00EA\""], + :"测" => [":测", ":\"\\u6D4B\""], + :"🦊" => [":🦊", ":\"\\u{1F98A}\""], } + expected_by_encoding = Encoding::default_external == Encoding::UTF_8 ? 0 : 1 symbols.each do |input, expected| - expected = expected[1] if expected.is_a?(Array) + expected = expected[expected_by_encoding] if expected.is_a?(Array) it "returns self as a symbol literal for #{expected}" do input.inspect.should == expected end end + + it "quotes BINARY symbols" do + sym = "foo\xA4".b.to_sym + sym.inspect.should == ':"foo\xA4"' + end + + it "quotes symbols in non-ASCII-compatible encodings" do + Encoding.list.reject(&:ascii_compatible?).reject(&:dummy?).each do |encoding| + sym = "foo".encode(encoding).to_sym + sym.inspect.should == ':"foo"' + end + end + + it "quotes and escapes symbols in dummy encodings" do + Encoding.list.select(&:dummy?).each do |encoding| + sym = "abcd".dup.force_encoding(encoding).to_sym + sym.inspect.should == ':"\x61\x62\x63\x64"' + end + end end diff --git a/spec/ruby/core/symbol/intern_spec.rb b/spec/ruby/core/symbol/intern_spec.rb index ea04b87e8a..746d313d40 100644 --- a/spec/ruby/core/symbol/intern_spec.rb +++ b/spec/ruby/core/symbol/intern_spec.rb @@ -1,11 +1,7 @@ require_relative '../../spec_helper' describe "Symbol#intern" do - it "returns self" do - :foo.intern.should == :foo - end - - it "returns a Symbol" do - :foo.intern.should be_kind_of(Symbol) + it "is an alias of Symbol#to_sym" do + Symbol.instance_method(:intern).should == Symbol.instance_method(:to_sym) end end diff --git a/spec/ruby/core/symbol/length_spec.rb b/spec/ruby/core/symbol/length_spec.rb index 27bee575ef..29dd4ad46b 100644 --- a/spec/ruby/core/symbol/length_spec.rb +++ b/spec/ruby/core/symbol/length_spec.rb @@ -1,6 +1,23 @@ require_relative '../../spec_helper' -require_relative 'shared/length' describe "Symbol#length" do - it_behaves_like :symbol_length, :length + it "returns 0 for empty name" do + :''.length.should == 0 + end + + it "returns 1 for name formed by a NUL character" do + :"\x00".length.should == 1 + end + + it "returns 3 for name formed by 3 ASCII characters" do + :one.length.should == 3 + end + + it "returns 4 for name formed by 4 ASCII characters" do + :four.length.should == 4 + end + + it "returns 4 for name formed by 1 multibyte and 3 ASCII characters" do + :"\xC3\x9Cber".length.should == 4 + end end diff --git a/spec/ruby/core/symbol/match_spec.rb b/spec/ruby/core/symbol/match_spec.rb index c26d0569ed..7b165218c6 100644 --- a/spec/ruby/core/symbol/match_spec.rb +++ b/spec/ruby/core/symbol/match_spec.rb @@ -6,7 +6,7 @@ describe :symbol_match, shared: true do end it "returns nil if there is no match" do - :a.send(@method, /b/).should be_nil + :a.send(@method, /b/).should == nil end it "sets the last match pseudo-variables" do @@ -19,52 +19,59 @@ describe "Symbol#=~" do it_behaves_like :symbol_match, :=~ end -ruby_version_is ""..."2.4" do - describe "Symbol#match" do - it_behaves_like :symbol_match, :match +describe "Symbol#match" do + it "returns the MatchData" do + result = :abc.match(/b/) + result.should.is_a?(MatchData) + result[0].should == 'b' + end + + it "returns nil if there is no match" do + :a.match(/b/).should == nil + end + + it "sets the last match pseudo-variables" do + :a.match(/(.)/)[0].should == 'a' + $1.should == "a" end -end -ruby_version_is "2.4" do - describe "Symbol#match" do - it "returns the MatchData" do - result = :abc.match(/b/) - result.should be_kind_of(MatchData) - result[0].should == 'b' + describe "when passed a block" do + it "yields the MatchData" do + :abc.match(/./) {|m| ScratchPad.record m } + ScratchPad.recorded.should.is_a?(MatchData) end - it "returns nil if there is no match" do - :a.match(/b/).should be_nil + it "returns the block result" do + :abc.match(/./) { :result }.should == :result end - it "sets the last match pseudo-variables" do - :a.match(/(.)/)[0].should == 'a' - $1.should == "a" + it "does not yield if there is no match" do + ScratchPad.record [] + :b.match(/a/) {|m| ScratchPad << m } + ScratchPad.recorded.should == [] end end end -ruby_version_is "2.4" do - describe "Symbol#match?" do - before :each do - # Resetting Regexp.last_match - /DONTMATCH/.match '' - end +describe "Symbol#match?" do + before :each do + # Resetting Regexp.last_match + /DONTMATCH/.match '' + end - context "when matches the given regex" do - it "returns true but does not set Regexp.last_match" do - :string.match?(/string/i).should be_true - Regexp.last_match.should be_nil - end + context "when matches the given regex" do + it "returns true but does not set Regexp.last_match" do + :string.match?(/string/i).should == true + Regexp.last_match.should == nil end + end - it "returns false when does not match the given regex" do - :string.match?(/STRING/).should be_false - end + it "returns false when does not match the given regex" do + :string.match?(/STRING/).should == false + end - it "takes matching position as the 2nd argument" do - :string.match?(/str/i, 0).should be_true - :string.match?(/str/i, 1).should be_false - end + it "takes matching position as the 2nd argument" do + :string.match?(/str/i, 0).should == true + :string.match?(/str/i, 1).should == false end end diff --git a/spec/ruby/core/symbol/name_spec.rb b/spec/ruby/core/symbol/name_spec.rb new file mode 100644 index 0000000000..f9b631266c --- /dev/null +++ b/spec/ruby/core/symbol/name_spec.rb @@ -0,0 +1,17 @@ +require_relative '../../spec_helper' + +describe "Symbol#name" do + it "returns string" do + :ruby.name.should == "ruby" + :ルビー.name.should == "ルビー" + end + + it "returns same string instance" do + :"ruby_3".name.should.equal?(:ruby_3.name) + :"ruby_#{1+2}".name.should.equal?(:ruby_3.name) + end + + it "returns frozen string" do + :symbol.name.should.frozen? + end +end diff --git a/spec/ruby/core/symbol/next_spec.rb b/spec/ruby/core/symbol/next_spec.rb index 97fe913739..e80bbaa508 100644 --- a/spec/ruby/core/symbol/next_spec.rb +++ b/spec/ruby/core/symbol/next_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/succ' describe "Symbol#next" do - it_behaves_like :symbol_succ, :next + it "is an alias of Symbol#succ" do + Symbol.instance_method(:next).should == Symbol.instance_method(:succ) + end end diff --git a/spec/ruby/core/symbol/shared/id2name.rb b/spec/ruby/core/symbol/shared/id2name.rb deleted file mode 100644 index 47f97bd332..0000000000 --- a/spec/ruby/core/symbol/shared/id2name.rb +++ /dev/null @@ -1,9 +0,0 @@ -describe :symbol_id2name, shared: true do - it "returns the string corresponding to self" do - :rubinius.send(@method).should == "rubinius" - :squash.send(@method).should == "squash" - :[].send(@method).should == "[]" - :@ruby.send(@method).should == "@ruby" - :@@ruby.send(@method).should == "@@ruby" - end -end diff --git a/spec/ruby/core/symbol/shared/length.rb b/spec/ruby/core/symbol/shared/length.rb deleted file mode 100644 index 692e8c57e3..0000000000 --- a/spec/ruby/core/symbol/shared/length.rb +++ /dev/null @@ -1,23 +0,0 @@ -# -*- encoding: utf-8 -*- - -describe :symbol_length, shared: true do - it "returns 0 for empty name" do - :''.send(@method).should == 0 - end - - it "returns 1 for name formed by a NUL character" do - :"\x00".send(@method).should == 1 - end - - it "returns 3 for name formed by 3 ASCII characters" do - :one.send(@method).should == 3 - end - - it "returns 4 for name formed by 4 ASCII characters" do - :four.send(@method).should == 4 - end - - it "returns 4 for name formed by 1 multibyte and 3 ASCII characters" do - :"\xC3\x9Cber".send(@method).should == 4 - end -end diff --git a/spec/ruby/core/symbol/shared/slice.rb b/spec/ruby/core/symbol/shared/slice.rb deleted file mode 100644 index 80e1fd3ddc..0000000000 --- a/spec/ruby/core/symbol/shared/slice.rb +++ /dev/null @@ -1,278 +0,0 @@ -require_relative '../fixtures/classes' - -describe :symbol_slice, shared: true do - describe "with an Integer index" do - it "returns the character code of the element at the index" do - :symbol.send(@method, 1).should == ?y - end - - it "returns nil if the index starts from the end and is greater than the length" do - :symbol.send(@method, -10).should be_nil - end - - it "returns nil if the index is greater than the length" do - :symbol.send(@method, 42).should be_nil - end - end - - describe "with an Integer index and length" do - describe "and a positive index and length" do - it "returns a slice" do - :symbol.send(@method, 1,3).should == "ymb" - end - - it "returns a blank slice if the length is 0" do - :symbol.send(@method, 0,0).should == "" - :symbol.send(@method, 1,0).should == "" - end - - it "returns a slice of all remaining characters if the given length is greater than the actual length" do - :symbol.send(@method, 1,100).should == "ymbol" - end - - it "returns nil if the index is greater than the length" do - :symbol.send(@method, 10,1).should be_nil - end - end - - describe "and a positive index and negative length" do - it "returns nil" do - :symbol.send(@method, 0,-1).should be_nil - :symbol.send(@method, 1,-1).should be_nil - end - end - - describe "and a negative index and positive length" do - it "returns a slice starting from the end upto the length" do - :symbol.send(@method, -3,2).should == "bo" - end - - it "returns a blank slice if the length is 0" do - :symbol.send(@method, -1,0).should == "" - end - - it "returns a slice of all remaining characters if the given length is larger than the actual length" do - :symbol.send(@method, -4,100).should == "mbol" - end - - it "returns nil if the index is past the start" do - :symbol.send(@method, -10,1).should be_nil - end - end - - describe "and a negative index and negative length" do - it "returns nil" do - :symbol.send(@method, -1,-1).should be_nil - end - end - - describe "and a Float length" do - it "converts the length to an Integer" do - :symbol.send(@method, 2,2.5).should == "mb" - end - end - - describe "and a nil length" do - it "raises a TypeError" do - lambda { :symbol.send(@method, 1,nil) }.should raise_error(TypeError) - end - end - - describe "and a length that cannot be converted into an Integer" do - it "raises a TypeError when given an Array" do - lambda { :symbol.send(@method, 1,Array.new) }.should raise_error(TypeError) - end - - it "raises a TypeError when given an Hash" do - lambda { :symbol.send(@method, 1,Hash.new) }.should raise_error(TypeError) - end - - it "raises a TypeError when given an Object" do - lambda { :symbol.send(@method, 1,Object.new) }.should raise_error(TypeError) - end - end - end - - describe "with a Float index" do - it "converts the index to an Integer" do - :symbol.send(@method, 1.5).should == ?y - end - end - - describe "with a nil index" do - it "raises a TypeError" do - lambda { :symbol.send(@method, nil) }.should raise_error(TypeError) - end - end - - describe "with an index that cannot be converted into an Integer" do - it "raises a TypeError when given an Array" do - lambda { :symbol.send(@method, Array.new) }.should raise_error(TypeError) - end - - it "raises a TypeError when given an Hash" do - lambda { :symbol.send(@method, Hash.new) }.should raise_error(TypeError) - end - - it "raises a TypeError when given an Object" do - lambda { :symbol.send(@method, Object.new) }.should raise_error(TypeError) - end - end - - describe "with a Range slice" do - describe "that is within bounds" do - it "returns a slice if both range values begin at the start and are within bounds" do - :symbol.send(@method, 1..4).should == "ymbo" - end - - it "returns a slice if the first range value begins at the start and the last begins at the end" do - :symbol.send(@method, 1..-1).should == "ymbol" - end - - it "returns a slice if the first range value begins at the end and the last begins at the end" do - :symbol.send(@method, -4..-1).should == "mbol" - end - end - - describe "that is out of bounds" do - it "returns nil if the first range value begins past the end" do - :symbol.send(@method, 10..12).should be_nil - end - - it "returns a blank string if the first range value is within bounds and the last range value is not" do - :symbol.send(@method, -2..-10).should == "" - :symbol.send(@method, 2..-10).should == "" - end - - it "returns nil if the first range value starts from the end and is within bounds and the last value starts from the end and is greater than the length" do - :symbol.send(@method, -10..-12).should be_nil - end - - it "returns nil if the first range value starts from the end and is out of bounds and the last value starts from the end and is less than the length" do - :symbol.send(@method, -10..-2).should be_nil - end - end - - describe "with Float values" do - it "converts the first value to an Integer" do - :symbol.send(@method, 0.5..2).should == "sym" - end - - it "converts the last value to an Integer" do - :symbol.send(@method, 0..2.5).should == "sym" - end - end - end - - describe "with a Range subclass slice" do - it "returns a slice" do - range = SymbolSpecs::MyRange.new(1, 4) - :symbol.send(@method, range).should == "ymbo" - end - end - - describe "with a Regex slice" do - describe "without a capture index" do - it "returns a string of the match" do - :symbol.send(@method, /[^bol]+/).should == "sym" - end - - it "returns nil if the expression does not match" do - :symbol.send(@method, /0-9/).should be_nil - end - - it "sets $~ to the MatchData if there is a match" do - :symbol.send(@method, /[^bol]+/) - $~[0].should == "sym" - end - - it "does not set $~ if there if there is not a match" do - :symbol.send(@method, /[0-9]+/) - $~.should be_nil - end - - it "returns a tainted string if the regexp is tainted" do - :symbol.send(@method, /./.taint).tainted?.should be_true - end - - it "returns an untrusted string if the regexp is untrusted" do - :symbol.send(@method, /./.untrust).untrusted?.should be_true - end - end - - describe "with a capture index" do - it "returns a string of the complete match if the capture index is 0" do - :symbol.send(@method, /(sy)(mb)(ol)/, 0).should == "symbol" - end - - it "returns a string for the matched capture at the given index" do - :symbol.send(@method, /(sy)(mb)(ol)/, 1).should == "sy" - :symbol.send(@method, /(sy)(mb)(ol)/, -1).should == "ol" - end - - it "returns nil if there is no capture for the index" do - :symbol.send(@method, /(sy)(mb)(ol)/, 4).should be_nil - :symbol.send(@method, /(sy)(mb)(ol)/, -4).should be_nil - end - - it "converts the index to an Integer" do - :symbol.send(@method, /(sy)(mb)(ol)/, 1.5).should == "sy" - end - - it "returns a tainted string if the regexp is tainted" do - :symbol.send(@method, /(.)/.taint, 1).tainted?.should be_true - end - - it "returns an untrusted string if the regexp is untrusted" do - :symbol.send(@method, /(.)/.untrust, 1).untrusted?.should be_true - end - - describe "and an index that cannot be converted to an Integer" do - it "raises a TypeError when given an Hash" do - lambda { :symbol.send(@method, /(sy)(mb)(ol)/, Hash.new) }.should raise_error(TypeError) - end - - it "raises a TypeError when given an Array" do - lambda { :symbol.send(@method, /(sy)(mb)(ol)/, Array.new) }.should raise_error(TypeError) - end - - it "raises a TypeError when given an Object" do - lambda { :symbol.send(@method, /(sy)(mb)(ol)/, Object.new) }.should raise_error(TypeError) - end - end - - it "raises a TypeError if the index is nil" do - lambda { :symbol.send(@method, /(sy)(mb)(ol)/, nil) }.should raise_error(TypeError) - end - - it "sets $~ to the MatchData if there is a match" do - :symbol.send(@method, /(sy)(mb)(ol)/, 0) - $~[0].should == "symbol" - $~[1].should == "sy" - $~[2].should == "mb" - $~[3].should == "ol" - end - - it "does not set $~ to the MatchData if there is not a match" do - :symbol.send(@method, /0-9/, 0) - $~.should be_nil - end - end - end - - describe "with a String slice" do - it "does not set $~" do - $~ = nil - :symbol.send(@method, "sym") - $~.should be_nil - end - - it "returns a string if there is match" do - :symbol.send(@method, "ymb").should == "ymb" - end - - it "returns nil if there is not a match" do - :symbol.send(@method, "foo").should be_nil - end - end -end diff --git a/spec/ruby/core/symbol/shared/succ.rb b/spec/ruby/core/symbol/shared/succ.rb deleted file mode 100644 index dde298207e..0000000000 --- a/spec/ruby/core/symbol/shared/succ.rb +++ /dev/null @@ -1,18 +0,0 @@ -require_relative '../../../spec_helper' - -describe :symbol_succ, shared: true do - it "returns a successor" do - :abcd.send(@method).should == :abce - :THX1138.send(@method).should == :THX1139 - end - - it "propagates a 'carry'" do - :"1999zzz".send(@method).should == :"2000aaa" - :ZZZ9999.send(@method).should == :AAAA0000 - end - - it "increments non-alphanumeric characters when no alphanumeric characters are present" do - :"<<koala>>".send(@method).should == :"<<koalb>>" - :"***".send(@method).should == :"**+" - end -end diff --git a/spec/ruby/core/symbol/size_spec.rb b/spec/ruby/core/symbol/size_spec.rb index 5e2aa8d4d2..b5d375b3aa 100644 --- a/spec/ruby/core/symbol/size_spec.rb +++ b/spec/ruby/core/symbol/size_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/length' describe "Symbol#size" do - it_behaves_like :symbol_length, :size + it "is an alias of Symbol#length" do + Symbol.instance_method(:size).should == Symbol.instance_method(:length) + end end diff --git a/spec/ruby/core/symbol/slice_spec.rb b/spec/ruby/core/symbol/slice_spec.rb index d2421c474c..050a2cf38c 100644 --- a/spec/ruby/core/symbol/slice_spec.rb +++ b/spec/ruby/core/symbol/slice_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/slice' describe "Symbol#slice" do - it_behaves_like :symbol_slice, :slice + it "is an alias of Symbol#[]" do + Symbol.instance_method(:slice).should == Symbol.instance_method(:[]) + end end diff --git a/spec/ruby/core/symbol/start_with_spec.rb b/spec/ruby/core/symbol/start_with_spec.rb new file mode 100644 index 0000000000..cd43279003 --- /dev/null +++ b/spec/ruby/core/symbol/start_with_spec.rb @@ -0,0 +1,8 @@ +# -*- encoding: utf-8 -*- +require_relative '../../spec_helper' +require_relative 'fixtures/classes' +require_relative '../../shared/string/start_with' + +describe "Symbol#start_with?" do + it_behaves_like :start_with, :to_sym +end diff --git a/spec/ruby/core/symbol/succ_spec.rb b/spec/ruby/core/symbol/succ_spec.rb index 694bfff862..893164a2a6 100644 --- a/spec/ruby/core/symbol/succ_spec.rb +++ b/spec/ruby/core/symbol/succ_spec.rb @@ -1,6 +1,18 @@ require_relative '../../spec_helper' -require_relative 'shared/succ' describe "Symbol#succ" do - it_behaves_like :symbol_succ, :succ + it "returns a successor" do + :abcd.succ.should == :abce + :THX1138.succ.should == :THX1139 + end + + it "propagates a 'carry'" do + :"1999zzz".succ.should == :"2000aaa" + :ZZZ9999.succ.should == :AAAA0000 + end + + it "increments non-alphanumeric characters when no alphanumeric characters are present" do + :"<<koala>>".succ.should == :"<<koalb>>" + :"***".succ.should == :"**+" + end end diff --git a/spec/ruby/core/symbol/swapcase_spec.rb b/spec/ruby/core/symbol/swapcase_spec.rb index 9aec87a6d2..95fc29e32b 100644 --- a/spec/ruby/core/symbol/swapcase_spec.rb +++ b/spec/ruby/core/symbol/swapcase_spec.rb @@ -3,7 +3,7 @@ require_relative '../../spec_helper' describe "Symbol#swapcase" do it "returns a Symbol" do - :glark.swapcase.should be_an_instance_of(Symbol) + :glark.swapcase.should.instance_of?(Symbol) end it "converts lowercase ASCII characters to their uppercase equivalents" do @@ -18,21 +18,9 @@ describe "Symbol#swapcase" do :mIxEd.swapcase.should == :MiXeD end - ruby_version_is ''...'2.4' do - it "leaves uppercase Unicode characters as they were" do - "\u{00DE}Bc".to_sym.swapcase.should == :"ÞbC" - end - - it "leaves lowercase Unicode characters as they were" do - "\u{00DF}Bc".to_sym.swapcase.should == :"ßbC" - end - end - - ruby_version_is '2.4' do - it "swaps the case for Unicode characters" do - "äÖü".to_sym.swapcase.should == :"ÄöÜ" - "aOu".to_sym.swapcase.should == :"AoU" - end + it "swaps the case for Unicode characters" do + "äÖü".to_sym.swapcase.should == :"ÄöÜ" + "aOu".to_sym.swapcase.should == :"AoU" end it "leaves non-alphabetic ASCII characters as they were" do diff --git a/spec/ruby/core/symbol/symbol_spec.rb b/spec/ruby/core/symbol/symbol_spec.rb index 56ce6f4a42..3534686a08 100644 --- a/spec/ruby/core/symbol/symbol_spec.rb +++ b/spec/ruby/core/symbol/symbol_spec.rb @@ -6,14 +6,14 @@ describe "Symbol" do end it ".allocate raises a TypeError" do - lambda do + -> do Symbol.allocate - end.should raise_error(TypeError) + end.should.raise(TypeError) end it ".new is undefined" do - lambda do + -> do Symbol.new - end.should raise_error(NoMethodError) + end.should.raise(NoMethodError) end end diff --git a/spec/ruby/core/symbol/to_proc_spec.rb b/spec/ruby/core/symbol/to_proc_spec.rb index 8cf00b085f..93ed1e9e9b 100644 --- a/spec/ruby/core/symbol/to_proc_spec.rb +++ b/spec/ruby/core/symbol/to_proc_spec.rb @@ -3,7 +3,7 @@ require_relative '../../spec_helper' describe "Symbol#to_proc" do it "returns a new Proc" do proc = :to_s.to_proc - proc.should be_kind_of(Proc) + proc.should.is_a?(Proc) end it "sends self to arguments passed when calling #call on the Proc" do @@ -12,19 +12,54 @@ describe "Symbol#to_proc" do :to_s.to_proc.call(obj).should == "Received #to_s" end - it "raises an ArgumentError when calling #call on the Proc without receiver" do - lambda { :object_id.to_proc.call }.should raise_error(ArgumentError) + it "returns a Proc with #lambda? true" do + pr = :to_s.to_proc + pr.should.lambda? end - it "produces a proc that always returns [[:rest]] for #parameters" do + it "produces a Proc with arity -2" do pr = :to_s.to_proc - pr.parameters.should == [[:rest]] + pr.arity.should == -2 end -end -describe "Symbol#to_proc" do - before :all do - @klass = Class.new do + it "produces a Proc that always returns [[:req], [:rest]] for #parameters" do + pr = :to_s.to_proc + pr.parameters.should == [[:req], [:rest]] + end + + it "only calls public methods" do + body = proc do + public def pub; @a << :pub end + protected def pro; @a << :pro end + private def pri; @a << :pri end + attr_reader :a + end + + @a = [] + singleton_class.class_eval(&body) + tap(&:pub) + proc{tap(&:pro)}.should.raise(NoMethodError, /protected method [`']pro' called/) + proc{tap(&:pri)}.should.raise(NoMethodError, /private method [`']pri' called/) + @a.should == [:pub] + + @a = [] + c = Class.new(&body) + o = c.new + o.instance_variable_set(:@a, []) + o.tap(&:pub) + proc{tap(&:pro)}.should.raise(NoMethodError, /protected method [`']pro' called/) + proc{o.tap(&:pri)}.should.raise(NoMethodError, /private method [`']pri' called/) + o.a.should == [:pub] + end + + it "raises an ArgumentError when calling #call on the Proc without receiver" do + -> { + :object_id.to_proc.call + }.should.raise(ArgumentError, /no receiver given|wrong number of arguments \(given 0, expected 1\+\)/) + end + + it "passes along the block passed to Proc#call" do + klass = Class.new do def m yield end @@ -33,9 +68,11 @@ describe "Symbol#to_proc" do :m.to_proc.call(self) { :value } end end + klass.new.to_proc.should == :value end - it "passes along the block passed to Proc#call" do - @klass.new.to_proc.should == :value + it "produces a proc with source location nil" do + pr = :to_s.to_proc + pr.source_location.should == nil end end diff --git a/spec/ruby/core/symbol/to_s_spec.rb b/spec/ruby/core/symbol/to_s_spec.rb index cd963faa28..2cb57c4cbc 100644 --- a/spec/ruby/core/symbol/to_s_spec.rb +++ b/spec/ruby/core/symbol/to_s_spec.rb @@ -1,6 +1,32 @@ require_relative '../../spec_helper' -require_relative 'shared/id2name' describe "Symbol#to_s" do - it_behaves_like :symbol_id2name, :to_s + it "returns the string corresponding to self" do + :rubinius.to_s.should == "rubinius" + :squash.to_s.should == "squash" + :[].to_s.should == "[]" + :@ruby.to_s.should == "@ruby" + :@@ruby.to_s.should == "@@ruby" + end + + it "returns a String in the same encoding as self" do + string = "ruby".encode("US-ASCII") + symbol = string.to_sym + + symbol.to_s.encoding.should == Encoding::US_ASCII + end + + ruby_version_is "3.4" do + it "warns about mutating returned string" do + -> { :bad!.to_s.upcase! }.should complain(/warning: string returned by :bad!.to_s will be frozen in the future/) + end + + it "does not warn about mutation when Warning[:deprecated] is false" do + deprecated = Warning[:deprecated] + Warning[:deprecated] = false + -> { :bad!.to_s.upcase! }.should_not complain + ensure + Warning[:deprecated] = deprecated + end + end end diff --git a/spec/ruby/core/symbol/to_sym_spec.rb b/spec/ruby/core/symbol/to_sym_spec.rb index e75f3d48a8..062daa3fc7 100644 --- a/spec/ruby/core/symbol/to_sym_spec.rb +++ b/spec/ruby/core/symbol/to_sym_spec.rb @@ -3,7 +3,7 @@ require_relative '../../spec_helper' describe "Symbol#to_sym" do it "returns self" do [:rubinius, :squash, :[], :@ruby, :@@ruby].each do |sym| - sym.to_sym.should == sym + sym.to_sym.should.equal?(sym) end end end diff --git a/spec/ruby/core/symbol/upcase_spec.rb b/spec/ruby/core/symbol/upcase_spec.rb index 6183f3b754..3895d95efb 100644 --- a/spec/ruby/core/symbol/upcase_spec.rb +++ b/spec/ruby/core/symbol/upcase_spec.rb @@ -3,24 +3,16 @@ require_relative '../../spec_helper' describe "Symbol#upcase" do it "returns a Symbol" do - :glark.upcase.should be_an_instance_of(Symbol) + :glark.upcase.should.instance_of?(Symbol) end it "converts lowercase ASCII characters to their uppercase equivalents" do :lOwEr.upcase.should == :LOWER end - ruby_version_is ''...'2.4' do - it "leaves lowercase Unicode characters as they were" do - "\u{E0}Bc".to_sym.upcase.should == :"àBC" - end - end - - ruby_version_is '2.4' do - it "capitalizes all Unicode characters" do - "äöü".to_sym.upcase.should == :"ÄÖÜ" - "aou".to_sym.upcase.should == :"AOU" - end + it "capitalizes all Unicode characters" do + "äöü".to_sym.upcase.should == :"ÄÖÜ" + "aou".to_sym.upcase.should == :"AOU" end it "leaves non-alphabetic ASCII characters as they were" do |
