summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/casecmp_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/string/casecmp_spec.rb')
-rw-r--r--spec/ruby/core/string/casecmp_spec.rb114
1 files changed, 99 insertions, 15 deletions
diff --git a/spec/ruby/core/string/casecmp_spec.rb b/spec/ruby/core/string/casecmp_spec.rb
index fdb888ff2b..90577aaac0 100644
--- a/spec/ruby/core/string/casecmp_spec.rb
+++ b/spec/ruby/core/string/casecmp_spec.rb
@@ -1,6 +1,6 @@
-# -*- encoding: ascii-8bit -*-
-require File.expand_path('../../../spec_helper', __FILE__)
-require File.expand_path('../fixtures/classes.rb', __FILE__)
+# -*- encoding: utf-8 -*-
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
describe "String#casecmp independent of case" do
it "returns -1 when less than other" do
@@ -25,25 +25,21 @@ describe "String#casecmp independent of case" do
"abc".casecmp(other).should == 0
end
- ruby_version_is ""..."2.5" do
- it "raises a TypeError if other can't be converted to a string" do
- lambda { "abc".casecmp(mock('abc')) }.should raise_error(TypeError)
- end
+ it "returns nil if other can't be converted to a string" do
+ "abc".casecmp(mock('abc')).should == nil
end
- ruby_version_is "2.5" do
- it "returns nil if other can't be converted to a string" do
- "abc".casecmp(mock('abc')).should be_nil
- end
+ it "returns nil if incompatible encodings" do
+ "あれ".casecmp("れ".encode(Encoding::EUC_JP)).should == nil
end
describe "in UTF-8 mode" do
describe "for non-ASCII characters" do
before :each do
- @upper_a_tilde = "\xc3\x83"
- @lower_a_tilde = "\xc3\xa3"
- @upper_a_umlaut = "\xc3\x84"
- @lower_a_umlaut = "\xc3\xa4"
+ @upper_a_tilde = "Ã"
+ @lower_a_tilde = "ã"
+ @upper_a_umlaut = "Ä"
+ @lower_a_umlaut = "ä"
end
it "returns -1 when numerically less than other" do
@@ -96,6 +92,10 @@ describe "String#casecmp independent of case" do
it "returns 1 when numerically greater than other" do
@lower_a_tilde.casecmp(@upper_a_tilde).should == 1
end
+
+ it "does not case fold" do
+ "ß".casecmp("ss").should == 1
+ end
end
describe "when comparing a subclass instance" do
@@ -117,4 +117,88 @@ describe "String#casecmp independent of case" do
"B".casecmp(a).should == 1
end
end
+
+ it "returns 0 for empty strings in different encodings" do
+ ''.b.casecmp('').should == 0
+ ''.b.casecmp(''.encode("UTF-32LE")).should == 0
+ end
+end
+
+describe 'String#casecmp? independent of case' do
+ it 'returns true when equal to other' do
+ 'abc'.casecmp?('abc').should == true
+ 'abc'.casecmp?('ABC').should == true
+ end
+
+ it 'returns false when not equal to other' do
+ 'abc'.casecmp?('DEF').should == false
+ 'abc'.casecmp?('def').should == false
+ end
+
+ it "tries to convert other to string using to_str" do
+ other = mock('x')
+ other.should_receive(:to_str).and_return("abc")
+
+ "abc".casecmp?(other).should == true
+ end
+
+ it "returns nil if incompatible encodings" do
+ "あれ".casecmp?("れ".encode(Encoding::EUC_JP)).should == nil
+ end
+
+ describe 'for UNICODE characters' do
+ it 'returns true when downcase(:fold) on unicode' do
+ 'äöü'.casecmp?('ÄÖÜ').should == true
+ end
+ end
+
+ describe "when comparing a subclass instance" do
+ it 'returns true when equal to other' do
+ a = StringSpecs::MyString.new "a"
+ 'a'.casecmp?(a).should == true
+ 'A'.casecmp?(a).should == true
+ end
+
+ it 'returns false when not equal to other' do
+ b = StringSpecs::MyString.new "a"
+ 'b'.casecmp?(b).should == false
+ 'B'.casecmp?(b).should == false
+ end
+ end
+
+ describe "in UTF-8 mode" do
+ describe "for non-ASCII characters" do
+ before :each do
+ @upper_a_tilde = "Ã"
+ @lower_a_tilde = "ã"
+ @upper_a_umlaut = "Ä"
+ @lower_a_umlaut = "ä"
+ end
+
+ it "returns true when they are the same with normalized case" do
+ @upper_a_tilde.casecmp?(@lower_a_tilde).should == true
+ end
+
+ it "returns false when they are unrelated" do
+ @upper_a_tilde.casecmp?(@upper_a_umlaut).should == false
+ end
+
+ it "returns true when they have the same bytes" do
+ @upper_a_tilde.casecmp?(@upper_a_tilde).should == true
+ end
+ end
+ end
+
+ it "case folds" do
+ "ß".casecmp?("ss").should == true
+ end
+
+ it "returns nil if other can't be converted to a string" do
+ "abc".casecmp?(mock('abc')).should == nil
+ end
+
+ it "returns true for empty strings in different encodings" do
+ ''.b.should.casecmp?('')
+ ''.b.should.casecmp?(''.encode("UTF-32LE"))
+ end
end