2017-05-07 08:04:49 -04:00
|
|
|
# -*- encoding: utf-8 -*-
|
2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'fixtures/classes'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
|
|
|
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???"
|
|
|
|
end
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
describe "full Unicode case mapping" do
|
|
|
|
it "works for all of Unicode with no option" do
|
|
|
|
"äÖü".swapcase.should == "ÄöÜ"
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "updates string metadata" do
|
|
|
|
swapcased = "Aßet".swapcase
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
swapcased.should == "aSSET"
|
|
|
|
swapcased.size.should == 5
|
|
|
|
swapcased.bytesize.should == 5
|
|
|
|
swapcased.ascii_only?.should be_true
|
2018-06-13 17:58:54 -04:00
|
|
|
end
|
2019-04-27 12:53:23 -04:00
|
|
|
end
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
describe "ASCII-only case mapping" do
|
|
|
|
it "does not swapcase non-ASCII characters" do
|
|
|
|
"aßet".swapcase(:ascii).should == "AßET"
|
2018-06-13 17:58:54 -04:00
|
|
|
end
|
2022-07-27 11:18:25 -04:00
|
|
|
|
|
|
|
it "works with substrings" do
|
|
|
|
"prefix aTé"[-3..-1].swapcase(:ascii).should == "Até"
|
|
|
|
end
|
2019-04-27 12:53:23 -04:00
|
|
|
end
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
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"
|
2018-06-13 17:58:54 -04:00
|
|
|
end
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "allows Lithuanian as an extra option" do
|
|
|
|
"aiS".swapcase(:turkic, :lithuanian).should == "Aİs"
|
|
|
|
end
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "does not allow any other additional option" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { "aiS".swapcase(:turkic, :ascii) }.should raise_error(ArgumentError)
|
2019-04-27 12:53:23 -04:00
|
|
|
end
|
|
|
|
end
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
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"
|
2018-06-13 17:58:54 -04:00
|
|
|
end
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "allows Turkic as an extra option (and applies Turkic semantics)" do
|
|
|
|
"iS".swapcase(:lithuanian, :turkic).should == "İs"
|
2018-06-13 17:58:54 -04:00
|
|
|
end
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "does not allow any other additional option" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { "aiS".swapcase(:lithuanian, :ascii) }.should raise_error(ArgumentError)
|
2017-05-19 04:19:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "does not allow the :fold option for upcasing" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { "abc".swapcase(:fold) }.should raise_error(ArgumentError)
|
2019-04-27 12:53:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not allow invalid options" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { "abc".swapcase(:invalid_option) }.should raise_error(ArgumentError)
|
2019-04-27 12:53:23 -04:00
|
|
|
end
|
|
|
|
|
2020-10-24 14:52:30 -04:00
|
|
|
ruby_version_is ''...'3.0' do
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ruby_version_is '3.0' do
|
|
|
|
it "returns String instances when called on a subclass" do
|
|
|
|
StringSpecs::MyString.new("").swapcase.should be_an_instance_of(String)
|
|
|
|
StringSpecs::MyString.new("hello").swapcase.should be_an_instance_of(String)
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "String#swapcase!" do
|
|
|
|
it "modifies self in place" do
|
|
|
|
a = "cYbEr_PuNk11"
|
|
|
|
a.swapcase!.should equal(a)
|
|
|
|
a.should == "CyBeR_pUnK11"
|
|
|
|
end
|
|
|
|
|
2020-01-28 14:47:48 -05:00
|
|
|
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
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
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
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2020-01-28 14:47:48 -05:00
|
|
|
it "works for non-ascii-compatible encodings" do
|
|
|
|
a = "äÖü".encode("utf-16le")
|
|
|
|
a.swapcase!
|
|
|
|
a.should == "ÄöÜ".encode("utf-16le")
|
|
|
|
end
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "updates string metadata" do
|
|
|
|
swapcased = "Aßet"
|
|
|
|
swapcased.swapcase!
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
swapcased.should == "aSSET"
|
|
|
|
swapcased.size.should == 5
|
|
|
|
swapcased.bytesize.should == 5
|
|
|
|
swapcased.ascii_only?.should be_true
|
2018-06-13 17:58:54 -04:00
|
|
|
end
|
2019-04-27 12:53:23 -04:00
|
|
|
end
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
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"
|
2018-06-13 17:58:54 -04:00
|
|
|
end
|
2020-01-28 14:47:48 -05:00
|
|
|
|
|
|
|
it "works for non-ascii-compatible encodings" do
|
|
|
|
a = "aBc".encode("utf-16le")
|
|
|
|
a.swapcase!(:ascii)
|
|
|
|
a.should == "AbC".encode("utf-16le")
|
|
|
|
end
|
2019-04-27 12:53:23 -04:00
|
|
|
end
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
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"
|
2018-06-13 17:58:54 -04:00
|
|
|
end
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "allows Lithuanian as an extra option" do
|
|
|
|
a = "aiS"
|
|
|
|
a.swapcase!(:turkic, :lithuanian)
|
|
|
|
a.should == "Aİs"
|
|
|
|
end
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "does not allow any other additional option" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { a = "aiS"; a.swapcase!(:turkic, :ascii) }.should raise_error(ArgumentError)
|
2019-04-27 12:53:23 -04:00
|
|
|
end
|
|
|
|
end
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
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"
|
2018-06-13 17:58:54 -04:00
|
|
|
end
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "allows Turkic as an extra option (and applies Turkic semantics)" do
|
|
|
|
a = "iS"
|
|
|
|
a.swapcase!(:lithuanian, :turkic)
|
|
|
|
a.should == "İs"
|
2018-06-13 17:58:54 -04:00
|
|
|
end
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "does not allow any other additional option" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { a = "aiS"; a.swapcase!(:lithuanian, :ascii) }.should raise_error(ArgumentError)
|
2017-10-28 11:15:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "does not allow the :fold option for upcasing" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { a = "abc"; a.swapcase!(:fold) }.should raise_error(ArgumentError)
|
2019-04-27 12:53:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not allow invalid options" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { a = "abc"; a.swapcase!(:invalid_option) }.should raise_error(ArgumentError)
|
2019-04-27 12:53:23 -04:00
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
it "returns nil if no modifications were made" do
|
|
|
|
a = "+++---111222???"
|
|
|
|
a.swapcase!.should == nil
|
|
|
|
a.should == "+++---111222???"
|
|
|
|
|
|
|
|
"".swapcase!.should == nil
|
|
|
|
end
|
|
|
|
|
2020-02-08 21:07:01 -05:00
|
|
|
it "raises a FrozenError when self is frozen" do
|
2017-05-07 08:04:49 -04:00
|
|
|
["", "hello"].each do |a|
|
|
|
|
a.freeze
|
2020-02-08 21:07:01 -05:00
|
|
|
-> { a.swapcase! }.should raise_error(FrozenError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|