summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/casecmp_spec.rb
blob: 986fbc87181d6622358ef7de1917962cd406fb6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- 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
    "a".casecmp("b").should == -1
    "A".casecmp("b").should == -1
  end

  it "returns 0 when equal to other" do
    "a".casecmp("a").should == 0
    "A".casecmp("a").should == 0
  end

  it "returns 1 when greater than other" do
    "b".casecmp("a").should == 1
    "B".casecmp("a").should == 1
  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 == 0
  end

  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 be_nil
  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 -1 when numerically less than other" do
        @upper_a_tilde.casecmp(@lower_a_tilde).should == -1
        @upper_a_tilde.casecmp(@upper_a_umlaut).should == -1
      end

      it "returns 0 when numerically equal to other" do
        @upper_a_tilde.casecmp(@upper_a_tilde).should == 0
      end

      it "returns 1 when numerically greater than other" do
        @lower_a_umlaut.casecmp(@upper_a_umlaut).should == 1
        @lower_a_umlaut.casecmp(@lower_a_tilde).should == 1
      end
    end

    describe "for ASCII characters" do
      it "returns -1 when less than other" do
        "a".casecmp("b").should == -1
        "A".casecmp("b").should == -1
      end

      it "returns 0 when equal to other" do
        "a".casecmp("a").should == 0
        "A".casecmp("a").should == 0
      end

      it "returns 1 when greater than other" do
        "b".casecmp("a").should == 1
        "B".casecmp("a").should == 1
      end
    end
  end

  describe "for non-ASCII characters" do
    before :each do
      @upper_a_tilde = "\xc3"
      @lower_a_tilde = "\xe3"
    end

    it "returns -1 when numerically less than other" do
      @upper_a_tilde.casecmp(@lower_a_tilde).should == -1
    end

    it "returns 0 when equal to other" do
      @upper_a_tilde.casecmp("\xc3").should == 0
    end

    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
    it "returns -1 when less than other" do
      b = StringSpecs::MyString.new "b"
      "a".casecmp(b).should == -1
      "A".casecmp(b).should == -1
    end

    it "returns 0 when equal to other" do
      a = StringSpecs::MyString.new "a"
      "a".casecmp(a).should == 0
      "A".casecmp(a).should == 0
    end

    it "returns 1 when greater than other" do
      a = StringSpecs::MyString.new "a"
      "b".casecmp(a).should == 1
      "B".casecmp(a).should == 1
    end
  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 be_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 be_true
  end

  it "returns nil if other can't be converted to a string" do
    "abc".casecmp?(mock('abc')).should be_nil
  end
end