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#capitalize" do
|
|
|
|
it "returns a copy of self with the first character converted to uppercase and the remainder to lowercase" do
|
|
|
|
"".capitalize.should == ""
|
|
|
|
"h".capitalize.should == "H"
|
|
|
|
"H".capitalize.should == "H"
|
|
|
|
"hello".capitalize.should == "Hello"
|
|
|
|
"HELLO".capitalize.should == "Hello"
|
|
|
|
"123ABC".capitalize.should == "123abc"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "taints resulting string when self is tainted" do
|
|
|
|
"".taint.capitalize.tainted?.should == true
|
|
|
|
"hello".taint.capitalize.tainted?.should == true
|
|
|
|
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
|
|
|
|
"äöÜ".capitalize.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 "only capitalizes the first resulting character when upcasing a character produces a multi-character sequence" do
|
|
|
|
"ß".capitalize.should == "Ss"
|
|
|
|
end
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "updates string metadata" do
|
|
|
|
capitalized = "ßeT".capitalize
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
capitalized.should == "Sset"
|
|
|
|
capitalized.size.should == 4
|
|
|
|
capitalized.bytesize.should == 4
|
|
|
|
capitalized.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 capitalize non-ASCII characters" do
|
|
|
|
"ßet".capitalize(:ascii).should == "ßet"
|
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 "full Unicode case mapping adapted for Turkic languages" do
|
|
|
|
it "capitalizes ASCII characters according to Turkic semantics" do
|
|
|
|
"iSa".capitalize(:turkic).should == "İsa"
|
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
|
|
|
|
"iSa".capitalize(:turkic, :lithuanian).should == "İsa"
|
|
|
|
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
|
|
|
|
lambda { "iSa".capitalize(:turkic, :ascii) }.should raise_error(ArgumentError)
|
|
|
|
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ß".capitalize(:lithuanian).should == "Iß"
|
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
|
|
|
|
"iß".capitalize(:lithuanian, :turkic).should == "İß"
|
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
|
|
|
|
lambda { "iß".capitalize(: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
|
|
|
|
lambda { "abc".capitalize(:fold) }.should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not allow invalid options" do
|
|
|
|
lambda { "abc".capitalize(:invalid_option) }.should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
it "returns subclass instances when called on a subclass" do
|
|
|
|
StringSpecs::MyString.new("hello").capitalize.should be_an_instance_of(StringSpecs::MyString)
|
|
|
|
StringSpecs::MyString.new("Hello").capitalize.should be_an_instance_of(StringSpecs::MyString)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "String#capitalize!" do
|
|
|
|
it "capitalizes self in place" do
|
|
|
|
a = "hello"
|
|
|
|
a.capitalize!.should equal(a)
|
|
|
|
a.should == "Hello"
|
|
|
|
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.capitalize!
|
|
|
|
a.should == "Äöü"
|
|
|
|
end
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "only capitalizes the first resulting character when upcasing a character produces a multi-character sequence" do
|
|
|
|
a = "ß"
|
|
|
|
a.capitalize!
|
|
|
|
a.should == "Ss"
|
|
|
|
end
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
it "updates string metadata" do
|
|
|
|
capitalized = "ßeT"
|
|
|
|
capitalized.capitalize!
|
2018-06-13 17:58:54 -04:00
|
|
|
|
2019-04-27 12:53:23 -04:00
|
|
|
capitalized.should == "Sset"
|
|
|
|
capitalized.size.should == 4
|
|
|
|
capitalized.bytesize.should == 4
|
|
|
|
capitalized.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 capitalize non-ASCII characters" do
|
|
|
|
a = "ßet"
|
|
|
|
a.capitalize!(:ascii)
|
|
|
|
a.should == "ßet"
|
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 full Unicode case mapping adapted for Turkic languages" do
|
|
|
|
it "capitalizes ASCII characters according to Turkic semantics" do
|
|
|
|
a = "iSa"
|
|
|
|
a.capitalize!(:turkic)
|
|
|
|
a.should == "İsa"
|
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 = "iSa"
|
|
|
|
a.capitalize!(:turkic, :lithuanian)
|
|
|
|
a.should == "İsa"
|
|
|
|
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
|
|
|
|
lambda { a = "iSa"; a.capitalize!(:turkic, :ascii) }.should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
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 Lithuanian" do
|
|
|
|
it "currently works the same as full Unicode case mapping" do
|
|
|
|
a = "iß"
|
|
|
|
a.capitalize!(:lithuanian)
|
|
|
|
a.should == "Iß"
|
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 = "iß"
|
|
|
|
a.capitalize!(:lithuanian, :turkic)
|
|
|
|
a.should == "İß"
|
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
|
|
|
|
lambda { a = "iß"; a.capitalize!(: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
|
|
|
|
lambda { a = "abc"; a.capitalize!(:fold) }.should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not allow invalid options" do
|
|
|
|
lambda { a = "abc"; a.capitalize!(:invalid_option) }.should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
it "returns nil when no changes are made" do
|
|
|
|
a = "Hello"
|
|
|
|
a.capitalize!.should == nil
|
|
|
|
a.should == "Hello"
|
|
|
|
|
|
|
|
"".capitalize!.should == nil
|
|
|
|
"H".capitalize!.should == nil
|
|
|
|
end
|
|
|
|
|
2017-12-27 11:12:47 -05:00
|
|
|
it "raises a #{frozen_error_class} when self is frozen" do
|
2017-05-07 08:04:49 -04:00
|
|
|
["", "Hello", "hello"].each do |a|
|
|
|
|
a.freeze
|
2017-12-27 11:12:47 -05:00
|
|
|
lambda { a.capitalize! }.should raise_error(frozen_error_class)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|