summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/downcase_spec.rb
blob: 9fb93902b11d385116b34afab022b72935e44b66 (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
195
196
197
198
199
200
# -*- encoding: utf-8 -*-
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

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

  ruby_version_is ''...'2.4' do
    it "is locale insensitive (only replaces A-Z)" do
      "ÄÖÜ".downcase.should == "ÄÖÜ"

      str = Array.new(256) { |c| c.chr }.join
      expected = Array.new(256) do |i|
        c = i.chr
        c.between?("A", "Z") ? c.downcase : c
      end.join

      str.downcase.should == expected
    end
  end

  ruby_version_is '2.4' do
    describe "full Unicode case mapping" do
      it "works for all of Unicode with no option" do
        "ÄÖÜ".downcase.should == "äöü"
      end

      it "updates string metadata" do
        downcased = "\u{212A}ING".downcase

        downcased.should == "king"
        downcased.size.should == 4
        downcased.bytesize.should == 4
        downcased.ascii_only?.should be_true
      end
    end

    describe "ASCII-only case mapping" do
      it "does not downcase non-ASCII characters" do
        "CÅR".downcase(:ascii).should == "cÅr"
      end
    end

    describe "full Unicode case mapping adapted for Turkic languages" do
      it "downcases characters according to Turkic semantics" do
        "İ".downcase(:turkic).should == "i"
      end

      it "allows Lithuanian as an extra option" do
        "İ".downcase(:turkic, :lithuanian).should == "i"
      end

      it "does not allow any other additional option" do
        lambda { "İ".downcase(:turkic, :ascii) }.should raise_error(ArgumentError)
      end
    end

    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

      it "allows Turkic as an extra option (and applies Turkic semantics)" do
        "İS".downcase(:lithuanian, :turkic).should == "is"
      end

      it "does not allow any other additional option" do
        lambda { "İS".downcase(:lithuanian, :ascii) }.should raise_error(ArgumentError)
      end
    end

    describe "case folding" do
      it "case folds special characters" do
        "ß".downcase.should == "ß"
        "ß".downcase(:fold).should == "ss"
      end
    end

    it "does not allow invalid options" do
      lambda { "ABC".downcase(:invalid_option) }.should raise_error(ArgumentError)
    end
  end

  it "taints result when self is tainted" do
    "".taint.downcase.tainted?.should == true
    "x".taint.downcase.tainted?.should == true
    "X".taint.downcase.tainted?.should == true
  end

  it "returns a subclass instance for subclasses" do
    StringSpecs::MyString.new("FOObar").downcase.should be_an_instance_of(StringSpecs::MyString)
  end
end

describe "String#downcase!" do
  it "modifies self in place" do
    a = "HeLlO"
    a.downcase!.should equal(a)
    a.should == "hello"
  end

  ruby_version_is '2.4' do
    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

      it "updates string metadata" do
        downcased = "\u{212A}ING"
        downcased.downcase!

        downcased.should == "king"
        downcased.size.should == 4
        downcased.bytesize.should == 4
        downcased.ascii_only?.should be_true
      end
    end

    describe "ASCII-only case mapping" do
      it "does not downcase non-ASCII characters" do
        a = "CÅR"
        a.downcase!(:ascii)
        a.should == "cÅr"
      end
    end

    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

      it "allows Lithuanian as an extra option" do
        a = "İ"
        a.downcase!(:turkic, :lithuanian)
        a.should == "i"
      end

      it "does not allow any other additional option" do
        lambda { a = "İ"; a.downcase!(:turkic, :ascii) }.should raise_error(ArgumentError)
      end
    end

    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

      it "allows Turkic as an extra option (and applies Turkic semantics)" do
        a = "İS"
        a.downcase!(:lithuanian, :turkic)
        a.should == "is"
      end

      it "does not allow any other additional option" do
        lambda { a = "İS"; a.downcase!(:lithuanian, :ascii) }.should raise_error(ArgumentError)
      end
    end

    describe "case folding" do
      it "case folds special characters" do
        a = "ß"
        a.downcase!
        a.should == "ß"

        a.downcase!(:fold)
        a.should == "ss"
      end
    end

    it "does not allow invalid options" do
      lambda { a = "ABC"; a.downcase!(:invalid_option) }.should raise_error(ArgumentError)
    end
  end

  it "returns nil if no modifications were made" do
    a = "hello"
    a.downcase!.should == nil
    a.should == "hello"
  end

  it "raises a #{frozen_error_class} when self is frozen" do
    lambda { "HeLlo".freeze.downcase! }.should raise_error(frozen_error_class)
    lambda { "hello".freeze.downcase! }.should raise_error(frozen_error_class)
  end

  with_feature :encoding do
    it "sets the result String encoding to the source String encoding" do
      "ABC".downcase.encoding.should equal(Encoding::UTF_8)
    end
  end
end