diff options
Diffstat (limited to 'spec/ruby/core/string/swapcase_spec.rb')
| -rw-r--r-- | spec/ruby/core/string/swapcase_spec.rb | 236 |
1 files changed, 123 insertions, 113 deletions
diff --git a/spec/ruby/core/string/swapcase_spec.rb b/spec/ruby/core/string/swapcase_spec.rb index bb89dee48f..f0e6e0182c 100644 --- a/spec/ruby/core/string/swapcase_spec.rb +++ b/spec/ruby/core/string/swapcase_spec.rb @@ -1,171 +1,181 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' describe "String#swapcase" do it "returns a new string with all uppercase chars from self converted to lowercase and vice versa" do - "Hello".swapcase.should == "hELLO" - "cYbEr_PuNk11".swapcase.should == "CyBeR_pUnK11" - "+++---111222???".swapcase.should == "+++---111222???" + "Hello".swapcase.should == "hELLO" + "cYbEr_PuNk11".swapcase.should == "CyBeR_pUnK11" + "+++---111222???".swapcase.should == "+++---111222???" end - it "taints resulting string when self is tainted" do - "".taint.swapcase.tainted?.should == true - "hello".taint.swapcase.tainted?.should == true + it "returns a String in the same encoding as self" do + "Hello".encode("US-ASCII").swapcase.encoding.should == Encoding::US_ASCII end - ruby_version_is ''...'2.4' do - it "is locale insensitive (only upcases a-z and only downcases A-Z)" do - "ÄÖÜ".swapcase.should == "ÄÖÜ" - "ärger".swapcase.should == "äRGER" - "BÄR".swapcase.should == "bÄr" + describe "full Unicode case mapping" do + it "works for all of Unicode with no option" do + "äÖü".swapcase.should == "ÄöÜ" end - end - - ruby_version_is '2.4' do - describe "full Unicode case mapping" do - it "works for all of Unicode with no option" do - "äÖü".swapcase.should == "ÄöÜ" - end - it "updates string metadata" do - swapcased = "Aßet".swapcase + it "updates string metadata" do + swapcased = "Aßet".swapcase - swapcased.should == "aSSET" - swapcased.size.should == 5 - swapcased.bytesize.should == 5 - swapcased.ascii_only?.should be_true - end + swapcased.should == "aSSET" + swapcased.size.should == 5 + swapcased.bytesize.should == 5 + swapcased.ascii_only?.should == true end + end - describe "ASCII-only case mapping" do - it "does not swapcase non-ASCII characters" do - "aßet".swapcase(:ascii).should == "AßET" - end + describe "ASCII-only case mapping" do + it "does not swapcase non-ASCII characters" do + "aßet".swapcase(:ascii).should == "AßET" end - describe "full Unicode case mapping adapted for Turkic languages" do - it "swaps case of ASCII characters according to Turkic semantics" do - "aiS".swapcase(:turkic).should == "Aİs" - end - - it "allows Lithuanian as an extra option" do - "aiS".swapcase(:turkic, :lithuanian).should == "Aİs" - end + it "works with substrings" do + "prefix aTé"[-3..-1].swapcase(:ascii).should == "Até" + end + end - it "does not allow any other additional option" do - lambda { "aiS".swapcase(:turkic, :ascii) }.should raise_error(ArgumentError) - end + describe "full Unicode case mapping adapted for Turkic languages" do + it "swaps case of ASCII characters according to Turkic semantics" do + "aiS".swapcase(:turkic).should == "Aİs" end - describe "full Unicode case mapping adapted for Lithuanian" do - it "currently works the same as full Unicode case mapping" do - "Iß".swapcase(:lithuanian).should == "iSS" - end + it "allows Lithuanian as an extra option" do + "aiS".swapcase(:turkic, :lithuanian).should == "Aİs" + end - it "allows Turkic as an extra option (and applies Turkic semantics)" do - "iS".swapcase(:lithuanian, :turkic).should == "İs" - end + it "does not allow any other additional option" do + -> { "aiS".swapcase(:turkic, :ascii) }.should.raise(ArgumentError) + end + end - it "does not allow any other additional option" do - lambda { "aiS".swapcase(:lithuanian, :ascii) }.should raise_error(ArgumentError) - end + describe "full Unicode case mapping adapted for Lithuanian" do + it "currently works the same as full Unicode case mapping" do + "Iß".swapcase(:lithuanian).should == "iSS" end - it "does not allow the :fold option for upcasing" do - lambda { "abc".swapcase(:fold) }.should raise_error(ArgumentError) + it "allows Turkic as an extra option (and applies Turkic semantics)" do + "iS".swapcase(:lithuanian, :turkic).should == "İs" end - it "does not allow invalid options" do - lambda { "abc".swapcase(:invalid_option) }.should raise_error(ArgumentError) + it "does not allow any other additional option" do + -> { "aiS".swapcase(:lithuanian, :ascii) }.should.raise(ArgumentError) end end - it "returns subclass instances when called on a subclass" do - StringSpecs::MyString.new("").swapcase.should be_an_instance_of(StringSpecs::MyString) - StringSpecs::MyString.new("hello").swapcase.should be_an_instance_of(StringSpecs::MyString) + it "does not allow the :fold option for upcasing" do + -> { "abc".swapcase(:fold) }.should.raise(ArgumentError) + end + + it "does not allow invalid options" do + -> { "abc".swapcase(:invalid_option) }.should.raise(ArgumentError) + end + + it "returns String instances when called on a subclass" do + StringSpecs::MyString.new("").swapcase.should.instance_of?(String) + StringSpecs::MyString.new("hello").swapcase.should.instance_of?(String) end end describe "String#swapcase!" do it "modifies self in place" do a = "cYbEr_PuNk11" - a.swapcase!.should equal(a) + a.swapcase!.should.equal?(a) a.should == "CyBeR_pUnK11" end - ruby_version_is '2.4' do - describe "full Unicode case mapping" do - it "modifies self in place for all of Unicode with no option" do - a = "äÖü" - a.swapcase! - a.should == "ÄöÜ" - end + it "modifies self in place for non-ascii-compatible encodings" do + a = "cYbEr_PuNk11".encode("utf-16le") + a.swapcase! + a.should == "CyBeR_pUnK11".encode("utf-16le") + end - it "updates string metadata" do - swapcased = "Aßet" - swapcased.swapcase! + describe "full Unicode case mapping" do + it "modifies self in place for all of Unicode with no option" do + a = "äÖü" + a.swapcase! + a.should == "ÄöÜ" + end - swapcased.should == "aSSET" - swapcased.size.should == 5 - swapcased.bytesize.should == 5 - swapcased.ascii_only?.should be_true - end + it "works for non-ascii-compatible encodings" do + a = "äÖü".encode("utf-16le") + a.swapcase! + a.should == "ÄöÜ".encode("utf-16le") end - describe "modifies self in place for ASCII-only case mapping" do - it "does not swapcase non-ASCII characters" do - a = "aßet" - a.swapcase!(:ascii) - a.should == "AßET" - end + it "updates string metadata" do + swapcased = "Aßet" + swapcased.swapcase! + + swapcased.should == "aSSET" + swapcased.size.should == 5 + swapcased.bytesize.should == 5 + swapcased.ascii_only?.should == true end + end - describe "modifies self in place for full Unicode case mapping adapted for Turkic languages" do - it "swaps case of ASCII characters according to Turkic semantics" do - a = "aiS" - a.swapcase!(:turkic) - a.should == "Aİs" - end + describe "modifies self in place for ASCII-only case mapping" do + it "does not swapcase non-ASCII characters" do + a = "aßet" + a.swapcase!(:ascii) + a.should == "AßET" + end - it "allows Lithuanian as an extra option" do - a = "aiS" - a.swapcase!(:turkic, :lithuanian) - a.should == "Aİs" - end + it "works for non-ascii-compatible encodings" do + a = "aBc".encode("utf-16le") + a.swapcase!(:ascii) + a.should == "AbC".encode("utf-16le") + end + end - it "does not allow any other additional option" do - lambda { a = "aiS"; a.swapcase!(:turkic, :ascii) }.should raise_error(ArgumentError) - end + describe "modifies self in place for full Unicode case mapping adapted for Turkic languages" do + it "swaps case of ASCII characters according to Turkic semantics" do + a = "aiS" + a.swapcase!(:turkic) + a.should == "Aİs" end - describe "full Unicode case mapping adapted for Lithuanian" do - it "currently works the same as full Unicode case mapping" do - a = "Iß" - a.swapcase!(:lithuanian) - a.should == "iSS" - end + it "allows Lithuanian as an extra option" do + a = "aiS" + a.swapcase!(:turkic, :lithuanian) + a.should == "Aİs" + end - it "allows Turkic as an extra option (and applies Turkic semantics)" do - a = "iS" - a.swapcase!(:lithuanian, :turkic) - a.should == "İs" - end + it "does not allow any other additional option" do + -> { a = "aiS"; a.swapcase!(:turkic, :ascii) }.should.raise(ArgumentError) + end + end - it "does not allow any other additional option" do - lambda { a = "aiS"; a.swapcase!(:lithuanian, :ascii) }.should raise_error(ArgumentError) - end + describe "full Unicode case mapping adapted for Lithuanian" do + it "currently works the same as full Unicode case mapping" do + a = "Iß" + a.swapcase!(:lithuanian) + a.should == "iSS" end - it "does not allow the :fold option for upcasing" do - lambda { a = "abc"; a.swapcase!(:fold) }.should raise_error(ArgumentError) + it "allows Turkic as an extra option (and applies Turkic semantics)" do + a = "iS" + a.swapcase!(:lithuanian, :turkic) + a.should == "İs" end - it "does not allow invalid options" do - lambda { a = "abc"; a.swapcase!(:invalid_option) }.should raise_error(ArgumentError) + it "does not allow any other additional option" do + -> { a = "aiS"; a.swapcase!(:lithuanian, :ascii) }.should.raise(ArgumentError) end end + it "does not allow the :fold option for upcasing" do + -> { a = "abc"; a.swapcase!(:fold) }.should.raise(ArgumentError) + end + + it "does not allow invalid options" do + -> { a = "abc"; a.swapcase!(:invalid_option) }.should.raise(ArgumentError) + end + it "returns nil if no modifications were made" do a = "+++---111222???" a.swapcase!.should == nil @@ -174,10 +184,10 @@ describe "String#swapcase!" do "".swapcase!.should == nil end - it "raises a #{frozen_error_class} when self is frozen" do + it "raises a FrozenError when self is frozen" do ["", "hello"].each do |a| a.freeze - lambda { a.swapcase! }.should raise_error(frozen_error_class) + -> { a.swapcase! }.should.raise(FrozenError) end end end |
