2017-05-07 12:04:49 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
2018-03-04 15:09:32 +00:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'fixtures/classes'
|
2017-05-07 12:04:49 +00:00
|
|
|
|
|
|
|
describe "String#downcase" do
|
|
|
|
it "returns a copy of self with all uppercase letters downcased" do
|
|
|
|
"hELLO".downcase.should == "hello"
|
|
|
|
"hello".downcase.should == "hello"
|
|
|
|
end
|
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
describe "full Unicode case mapping" do
|
|
|
|
it "works for all of Unicode with no option" do
|
|
|
|
"ÄÖÜ".downcase.should == "äöü"
|
|
|
|
end
|
2017-05-07 12:04:49 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
it "updates string metadata" do
|
|
|
|
downcased = "\u{212A}ING".downcase
|
2017-05-07 12:04:49 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
downcased.should == "king"
|
|
|
|
downcased.size.should == 4
|
|
|
|
downcased.bytesize.should == 4
|
|
|
|
downcased.ascii_only?.should be_true
|
2017-05-07 12:04:49 +00:00
|
|
|
end
|
2017-05-19 08:19:02 +00:00
|
|
|
end
|
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
describe "ASCII-only case mapping" do
|
|
|
|
it "does not downcase non-ASCII characters" do
|
|
|
|
"CÅR".downcase(:ascii).should == "cÅr"
|
2018-06-13 21:58:54 +00:00
|
|
|
end
|
2019-04-27 18:53:23 +02:00
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
describe "full Unicode case mapping adapted for Turkic languages" do
|
|
|
|
it "downcases characters according to Turkic semantics" do
|
|
|
|
"İ".downcase(:turkic).should == "i"
|
2018-06-13 21:58:54 +00:00
|
|
|
end
|
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
it "allows Lithuanian as an extra option" do
|
|
|
|
"İ".downcase(:turkic, :lithuanian).should == "i"
|
2018-06-13 21:58:54 +00:00
|
|
|
end
|
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
it "does not allow any other additional option" do
|
2019-07-27 12:40:09 +02:00
|
|
|
-> { "İ".downcase(:turkic, :ascii) }.should raise_error(ArgumentError)
|
2019-04-27 18:53:23 +02:00
|
|
|
end
|
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
describe "full Unicode case mapping adapted for Lithuanian" do
|
|
|
|
it "currently works the same as full Unicode case mapping" do
|
|
|
|
"İS".downcase(:lithuanian).should == "i\u{307}s"
|
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
it "allows Turkic as an extra option (and applies Turkic semantics)" do
|
|
|
|
"İS".downcase(:lithuanian, :turkic).should == "is"
|
2018-06-13 21:58:54 +00:00
|
|
|
end
|
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
it "does not allow any other additional option" do
|
2019-07-27 12:40:09 +02:00
|
|
|
-> { "İS".downcase(:lithuanian, :ascii) }.should raise_error(ArgumentError)
|
2018-06-13 21:58:54 +00:00
|
|
|
end
|
2019-04-27 18:53:23 +02:00
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
describe "case folding" do
|
|
|
|
it "case folds special characters" do
|
|
|
|
"ß".downcase.should == "ß"
|
|
|
|
"ß".downcase(:fold).should == "ss"
|
2017-05-19 08:19:02 +00:00
|
|
|
end
|
2017-05-07 12:04:49 +00:00
|
|
|
end
|
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
it "does not allow invalid options" do
|
2019-07-27 12:40:09 +02:00
|
|
|
-> { "ABC".downcase(:invalid_option) }.should raise_error(ArgumentError)
|
2019-04-27 18:53:23 +02:00
|
|
|
end
|
|
|
|
|
2019-09-24 20:59:12 -07:00
|
|
|
ruby_version_is ''...'2.7' do
|
|
|
|
it "taints result when self is tainted" do
|
2020-05-03 12:28:29 +02:00
|
|
|
"".taint.downcase.should.tainted?
|
|
|
|
"x".taint.downcase.should.tainted?
|
|
|
|
"X".taint.downcase.should.tainted?
|
2019-09-24 20:59:12 -07:00
|
|
|
end
|
2017-05-07 12:04:49 +00:00
|
|
|
end
|
|
|
|
|
2020-10-24 11:52:30 -07:00
|
|
|
ruby_version_is ''...'3.0' do
|
|
|
|
it "returns a subclass instance for subclasses" do
|
|
|
|
StringSpecs::MyString.new("FOObar").downcase.should be_an_instance_of(StringSpecs::MyString)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ruby_version_is '3.0' do
|
|
|
|
it "returns a String instance for subclasses" do
|
|
|
|
StringSpecs::MyString.new("FOObar").downcase.should be_an_instance_of(String)
|
|
|
|
end
|
2017-05-07 12:04:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "String#downcase!" do
|
|
|
|
it "modifies self in place" do
|
|
|
|
a = "HeLlO"
|
|
|
|
a.downcase!.should equal(a)
|
|
|
|
a.should == "hello"
|
|
|
|
end
|
|
|
|
|
2020-01-28 20:47:48 +01:00
|
|
|
it "modifies self in place for non-ascii-compatible encodings" do
|
|
|
|
a = "HeLlO".encode("utf-16le")
|
|
|
|
a.downcase!
|
|
|
|
a.should == "hello".encode("utf-16le")
|
|
|
|
end
|
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
describe "full Unicode case mapping" do
|
|
|
|
it "modifies self in place for all of Unicode with no option" do
|
|
|
|
a = "ÄÖÜ"
|
|
|
|
a.downcase!
|
|
|
|
a.should == "äöü"
|
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
it "updates string metadata" do
|
|
|
|
downcased = "\u{212A}ING"
|
|
|
|
downcased.downcase!
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
downcased.should == "king"
|
|
|
|
downcased.size.should == 4
|
|
|
|
downcased.bytesize.should == 4
|
|
|
|
downcased.ascii_only?.should be_true
|
2018-06-13 21:58:54 +00:00
|
|
|
end
|
2019-04-27 18:53:23 +02:00
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
describe "ASCII-only case mapping" do
|
|
|
|
it "does not downcase non-ASCII characters" do
|
|
|
|
a = "CÅR"
|
|
|
|
a.downcase!(:ascii)
|
|
|
|
a.should == "cÅr"
|
2018-06-13 21:58:54 +00:00
|
|
|
end
|
2020-01-28 20:47:48 +01:00
|
|
|
|
|
|
|
it "works for non-ascii-compatible encodings" do
|
|
|
|
a = "ABC".encode("utf-16le")
|
|
|
|
a.downcase!(:ascii)
|
|
|
|
a.should == "abc".encode("utf-16le")
|
|
|
|
end
|
2019-04-27 18:53:23 +02:00
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
describe "full Unicode case mapping adapted for Turkic languages" do
|
|
|
|
it "downcases characters according to Turkic semantics" do
|
|
|
|
a = "İ"
|
|
|
|
a.downcase!(:turkic)
|
|
|
|
a.should == "i"
|
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
it "allows Lithuanian as an extra option" do
|
|
|
|
a = "İ"
|
|
|
|
a.downcase!(:turkic, :lithuanian)
|
|
|
|
a.should == "i"
|
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
it "does not allow any other additional option" do
|
2019-07-27 12:40:09 +02:00
|
|
|
-> { a = "İ"; a.downcase!(:turkic, :ascii) }.should raise_error(ArgumentError)
|
2018-06-13 21:58:54 +00:00
|
|
|
end
|
2019-04-27 18:53:23 +02:00
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
describe "full Unicode case mapping adapted for Lithuanian" do
|
|
|
|
it "currently works the same as full Unicode case mapping" do
|
|
|
|
a = "İS"
|
|
|
|
a.downcase!(:lithuanian)
|
|
|
|
a.should == "i\u{307}s"
|
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
it "allows Turkic as an extra option (and applies Turkic semantics)" do
|
|
|
|
a = "İS"
|
|
|
|
a.downcase!(:lithuanian, :turkic)
|
|
|
|
a.should == "is"
|
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
it "does not allow any other additional option" do
|
2019-07-27 12:40:09 +02:00
|
|
|
-> { a = "İS"; a.downcase!(:lithuanian, :ascii) }.should raise_error(ArgumentError)
|
2018-06-13 21:58:54 +00:00
|
|
|
end
|
2019-04-27 18:53:23 +02:00
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
describe "case folding" do
|
|
|
|
it "case folds special characters" do
|
|
|
|
a = "ß"
|
|
|
|
a.downcase!
|
|
|
|
a.should == "ß"
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
a.downcase!(:fold)
|
|
|
|
a.should == "ss"
|
2018-06-13 21:58:54 +00:00
|
|
|
end
|
2019-04-27 18:53:23 +02:00
|
|
|
end
|
2018-06-13 21:58:54 +00:00
|
|
|
|
2019-04-27 18:53:23 +02:00
|
|
|
it "does not allow invalid options" do
|
2019-07-27 12:40:09 +02:00
|
|
|
-> { a = "ABC"; a.downcase!(:invalid_option) }.should raise_error(ArgumentError)
|
2017-10-28 15:15:48 +00:00
|
|
|
end
|
|
|
|
|
2017-05-07 12:04:49 +00:00
|
|
|
it "returns nil if no modifications were made" do
|
|
|
|
a = "hello"
|
|
|
|
a.downcase!.should == nil
|
|
|
|
a.should == "hello"
|
|
|
|
end
|
|
|
|
|
2020-02-09 11:07:01 +09:00
|
|
|
it "raises a FrozenError when self is frozen" do
|
|
|
|
-> { "HeLlo".freeze.downcase! }.should raise_error(FrozenError)
|
|
|
|
-> { "hello".freeze.downcase! }.should raise_error(FrozenError)
|
2017-05-07 12:04:49 +00:00
|
|
|
end
|
|
|
|
|
2019-04-28 23:20:11 +02:00
|
|
|
it "sets the result String encoding to the source String encoding" do
|
|
|
|
"ABC".downcase.encoding.should equal(Encoding::UTF_8)
|
2017-05-07 12:04:49 +00:00
|
|
|
end
|
|
|
|
end
|