turkish_support 1.0.4 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  module TurkishSupport
2
- VERSION = '1.0.4'.freeze
2
+ VERSION = '2.0.1'.freeze
3
3
  end
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+ using TurkishSupport
3
+
4
+ module TurkishSupport
5
+ describe Array do
6
+ describe '#sort' do
7
+
8
+ let(:samples) {
9
+ [
10
+ {
11
+ mixed: %w[ bağcılar bahçelievler şimdi çüNKÜ olmalı üç\ kere düş ılık duy ],
12
+ sorted: %w[ bağcılar bahçelievler çüNKÜ duy düş ılık olmalı şimdi üç\ kere ]
13
+ },
14
+ {
15
+ mixed: %w[ iki Üç dört ılık İğne iyne Ul ],
16
+ sorted: %w[ İğne Ul Üç dört ılık iki iyne ]
17
+ },
18
+ {
19
+ mixed: %w[ Sıtkı1\ Bağdat Sıtkı\ Bağdat a 3s 2\ b ab\ ],
20
+ sorted: %w[ 2\ b 3s Sıtkı\ Bağdat Sıtkı1\ Bağdat a ab\ ]
21
+ }
22
+ ]
23
+ }
24
+
25
+ let(:block_samples) {
26
+ [
27
+ {
28
+ mixed: %w[ ağa aça aşa aöa aüa aua afa aba ],
29
+ sorted: {
30
+ "a[1]<=>b[1]": %w[ aba aça afa ağa aöa aşa aua aüa ],
31
+ "b[1]<=>a[1]": %w[ aüa aua aşa aöa ağa afa aça aba ]
32
+ }
33
+ },
34
+ {
35
+ mixed: %w[ iki Üç dört ılık İğne iyne Ul ],
36
+ sorted: {
37
+ "a.length<=>b.length": %w[ Üç Ul iki dört ılık İğne iyne ],
38
+ "b.length<=>a.length": %w[dört ılık İğne iyne iki Üç Ul ]
39
+ }
40
+ },
41
+ {
42
+ mixed: [["Şakir", 2], ["İsmet", 0], ["Zeliha", 1]],
43
+ sorted: {
44
+ "a[1]<=>b[1]": [["İsmet", 0], ["Zeliha", 1], ["Şakir", 2]],
45
+ "b[0]<=>a[0]": [["Zeliha", 1], ["Şakir", 2], ["İsmet", 0]]
46
+ }
47
+ }
48
+ ]
49
+ }
50
+
51
+ context 'nondestructive version' do
52
+ context 'no block given' do
53
+ it 'does not change the original value of the array' do
54
+ samples.each do |sample|
55
+ expect { sample[:mixed].sort }.to_not change { sample[:mixed] }
56
+ end
57
+ end
58
+
59
+ it 'sorts array in alphabetical order' do
60
+ samples.each do |sample|
61
+ expect(sample[:mixed].sort).to eq(sample[:sorted])
62
+ end
63
+ end
64
+ end
65
+
66
+ context 'block given' do
67
+ it 'sorts array for random conditions' do
68
+ sample = block_samples.first
69
+ expect(sample[:mixed].sort {|a, b| a[1] <=> b[1]})
70
+ .to eq(sample[:sorted][:"a[1]<=>b[1]"])
71
+
72
+ expect(sample[:mixed].sort {|a, b| b[1] <=> a[1]})
73
+ .to eq(sample[:sorted][:"b[1]<=>a[1]"])
74
+
75
+ sample = block_samples[1]
76
+ expect(sample[:mixed].sort {|a, b| a.length <=> b.length})
77
+ .to eq(sample[:sorted][:"a.length<=>b.length"])
78
+
79
+ expect(sample[:mixed].sort {|a, b| b.length <=> a.length})
80
+ .to eq(sample[:sorted][:"b.length<=>a.length"])
81
+ end
82
+
83
+ it 'sorts nested arrays' do
84
+ sample = block_samples[2]
85
+ expect(sample[:mixed].sort {|a, b| a[1] <=> b[1]})
86
+ .to eq(sample[:sorted][:"a[1]<=>b[1]"])
87
+ expect(sample[:mixed].sort {|a, b| b[0] <=> a[0]})
88
+ .to eq(sample[:sorted][:"b[0]<=>a[0]"])
89
+ end
90
+ end
91
+ end
92
+
93
+ context 'with destructive version' do
94
+ it 'changes the original value of the array' do
95
+ samples.each do |sample|
96
+ expect { sample[:mixed].sort! }.to change { sample[:mixed] }
97
+ end
98
+ end
99
+ end
100
+
101
+ end
102
+ end
103
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'turkish_support/helpers'
3
5
 
@@ -13,176 +15,90 @@ describe 'TurkishSupportHelpers' do
13
15
  let(:tr_specific_lower) { 'çğıiöşü' }
14
16
  let(:tr_specific_upper) { 'ÇĞIİÖŞÜ' }
15
17
  let(:tr_all) { tr_specific_upper + tr_specific_lower }
16
- let(:conjuctions) { %w(ve ile veya) }
17
- let(:turkish_words) { %w(çamur ıhlamur insan ördek şahika ümraniye) }
18
- let(:special_chars) { %w{ ( " ' } }
19
-
20
- describe '#prepare_for' do
21
- context ':upcase' do
22
- it "replaces unsupported lower case letters with their upper cases'" do
23
- expect(prepare_for(:upcase, 'çğıiöşü'))
24
- .to eq('ÇĞIİÖŞÜ')
25
- end
26
- end
27
-
28
- context ':downcase' do
29
- it "replaces unsupported upper case letters with their lower cases'" do
30
- expect(prepare_for(:downcase, 'ÇĞIİÖŞÜ'))
31
- .to eq('çğıiöşü')
32
- end
33
- end
34
-
35
- context ':capitalize' do
36
- it 'replaces unsupported upper case and lower case letters' do
37
- dbl = double
38
- allow(dbl)
39
- .to receive(:prepare_for)
40
- .with(:capitalize, 'çaĞLAyan')
41
- .and_return('Çağlayan')
42
-
43
- expect(dbl.prepare_for(:capitalize, 'çaĞLAyan'))
44
- .to eq('Çağlayan')
45
- end
46
- end
47
-
48
- context 'with invalid parameters' do
49
- it 'raises an error if any parameter is not valid' do
50
- expect { prepare_for(:not_valid, :not_valid) }
51
- .to raise_error ArgumentError,
52
- 'Invalid arguments for method `prepare_for`!'
53
- expect { prepare_for(:swapcase, 'ÇĞ') }
54
- .to raise_error ArgumentError,
55
- 'Invalid arguments for method `prepare_for`!'
56
- expect { prepare_for(:upcase, 123) }
57
- .to raise_error ArgumentError,
58
- 'Invalid arguments for method `prepare_for`!'
59
- end
60
- end
61
- end
18
+ let(:conjuctions) { %w[ve ile veya] }
19
+ let(:turkish_words) { %w[çamur ıhlamur insan ördek şahika ümraniye] }
20
+ let(:special_chars) { %w[( " '] }
62
21
 
63
22
  describe '#tr_lower?' do
64
23
  it 'returns true for any Turkish specific lower case character' do
65
- expect(tr_specific_lower
66
- .chars
67
- .all? { |ch| tr_lower?(ch) })
68
- .to eq(true)
24
+ expect(tr_specific_lower.chars.all? { |ch| tr_lower?(ch) }).to(eq(true))
69
25
  end
70
26
 
71
27
  context 'returns false' do
72
28
  it 'for non-Turkish specific lower case characters' do
73
- expect(downcased_alphabet
74
- .delete(tr_specific_lower)
75
- .chars
76
- .any? { |ch| tr_lower?(ch) }
77
- ).to eq(false)
29
+ expect(downcased_alphabet.delete(tr_specific_lower).chars.any? { |ch| tr_lower?(ch) }).to(eq(false))
78
30
  end
79
31
 
80
32
  it 'for any upper case characters' do
81
- expect(upcased_alphabet
82
- .chars
83
- .any? { |ch| tr_lower?(ch) }
84
- ).to eq(false)
33
+ expect(upcased_alphabet.chars.any? { |ch| tr_lower?(ch) }).to(eq(false))
85
34
  end
86
35
 
87
36
  it 'for non-letter characters' do
88
- expect("\"é1!2'3^+4.,-_"
89
- .chars
90
- .any? { |ch| tr_lower?(ch) }
91
- ).to eq(false)
37
+ expect("\"é1!2'3^+4.,-_".chars.any? { |ch| tr_lower?(ch) }).to(eq(false))
92
38
  end
93
39
  end
94
40
  end
95
41
 
96
42
  describe '#tr_upper?' do
97
43
  it 'returns true for any Turkish specific upper case character' do
98
- expect(tr_specific_upper
99
- .chars
100
- .all? { |ch| tr_upper?(ch) }
101
- ).to eq(true)
44
+ expect(tr_specific_upper.chars.all? { |ch| tr_upper?(ch) }).to(eq(true))
102
45
  end
103
46
 
104
47
  context 'returns false' do
105
48
  it 'for non-Turkish specific upper case characters' do
106
- expect(upcased_alphabet
107
- .delete(tr_specific_upper)
108
- .chars
109
- .any? { |ch| tr_upper?(ch) }
110
- ).to eq(false)
49
+ expect(upcased_alphabet.delete(tr_specific_upper).chars.any? { |ch| tr_upper?(ch) }).to(eq(false))
111
50
  end
112
51
 
113
52
  it 'for any lower case characters' do
114
- expect(downcased_alphabet
115
- .chars
116
- .any? { |ch| tr_upper?(ch) }
117
- ).to eq(false)
53
+ expect(downcased_alphabet.chars.any? { |ch| tr_upper?(ch) }).to(eq(false))
118
54
  end
119
55
 
120
56
  it 'for non-letter characters' do
121
- expect("\"é1!2'3^+4.,-_"
122
- .chars
123
- .any? { |ch| tr_upper?(ch) }
124
- ).to eq(false)
57
+ expect("\"é1!2'3^+4.,-_".chars.any? { |ch| tr_upper?(ch) }).to(eq(false))
125
58
  end
126
59
  end
127
60
  end
128
61
 
129
62
  describe '#tr_char?' do
130
63
  it 'returns true for any Turkish specific character' do
131
- expect(tr_all
132
- .chars
133
- .all? { |ch| tr_char?(ch) }
134
- ).to eq(true)
64
+ expect(tr_all.chars.all? { |ch| tr_char?(ch) }).to(eq(true))
135
65
  end
136
66
 
137
67
  context 'returns false' do
138
68
  it 'for non-Turkish specific characters' do
139
- expect(alphabet.delete(tr_all)
140
- .chars
141
- .any? { |ch| tr_char?(ch) }
142
- ).to eq(false)
69
+ expect(alphabet.delete(tr_all).chars.any? { |ch| tr_char?(ch) }).to(eq(false))
143
70
  end
144
71
 
145
72
  it 'for non-letter characters' do
146
- expect("\"é1!2'3^+4.,-_"
147
- .chars
148
- .any? { |ch| tr_char?(ch) }
149
- ).to eq(false)
73
+ expect("\"é1!2'3^+4.,-_".chars.any? { |ch| tr_char?(ch) }).to(eq(false))
150
74
  end
151
75
  end
152
76
  end
153
77
 
154
78
  describe '#conjuction?' do
155
79
  it 'returns true for any conjuction' do
156
- expect(conjuctions
157
- .all? { |c| conjuction?(c) }
158
- ).to eq(true)
80
+ expect(conjuctions.all? { |c| conjuction?(c) }).to(eq(true))
159
81
  end
160
82
 
161
83
  it 'returns false for any word contains conjuction' do
162
- expect(%w(veda aile veyahut)
163
- .any? { |c| conjuction?(c) }
164
- ).to eq(false)
84
+ expect(%w[veda aile veyahut].any? { |c| conjuction?(c) }).to(eq(false))
165
85
  end
166
86
  end
167
87
 
168
88
  describe '#start_with_a_special_char?' do
169
89
  it 'returns true for all words starts with a special char' do
170
90
  special_words = turkish_words.map { |w| special_chars.sample + w }
171
- expect(special_words
172
- .all? { |word| start_with_a_special_char?(word) }
173
- ).to eq(true)
91
+ expect(special_words.all? { |word| start_with_a_special_char?(word) }).to(eq(true))
174
92
  end
175
93
 
176
94
  it 'returns false any words not starts with a special char' do
177
- expect(turkish_words
178
- .any? { |word| start_with_a_special_char?(word) }
179
- ).to eq(false)
95
+ expect(turkish_words.any? { |word| start_with_a_special_char?(word) }).to(eq(false))
180
96
  end
181
97
  end
182
98
 
183
99
  describe '#translate_range' do
184
100
  it 'translates a complete lower-case range correctly' do
185
- expect(translate_range('a-z')).to eq(downcased_alphabet)
101
+ expect(translate_range('a-z')).to(eq(downcased_alphabet))
186
102
  end
187
103
 
188
104
  it 'translates a complete upper-case range correctly' do
@@ -193,65 +109,52 @@ describe 'TurkishSupportHelpers' do
193
109
  invalid_arguments = %w(1-Z A-9 1-9 a-])
194
110
  expect(invalid_arguments
195
111
  .all? do |arg|
196
- expect { translate_range(arg) }
197
- .to raise_error ArgumentError,
198
- 'Invalid regexp range arguments!'
199
- end
200
- ).to eq(true)
112
+ expect { translate_range(arg) }
113
+ .to raise_error ArgumentError,
114
+ 'Invalid regexp range arguments!'
115
+ end).to eq(true)
201
116
  end
202
117
 
203
118
  it 'raises an error if arguments are not in same case' do
204
119
  expect { translate_range('a-Z') }
205
- .to raise_error ArgumentError,
206
- 'Invalid regexp range arguments!'
120
+ .to(raise_error(ArgumentError, 'Invalid regexp range arguments!'))
207
121
  end
208
122
 
209
123
  it 'translates a partial range correctly' do
210
- expect(translate_range('d-t'))
211
- .to eq('defgğhıijklmnoöpqrsşt')
212
- expect(translate_range('Ç-Ü'))
213
- .to eq('ÇDEFGĞHIİJKLMNOÖPQRSŞTUÜ')
124
+ expect(translate_range('d-t')).to(eq('defgğhıijklmnoöpqrsşt'))
125
+ expect(translate_range('Ç-Ü')).to(eq('ÇDEFGĞHIİJKLMNOÖPQRSŞTUÜ'))
214
126
  end
215
127
 
216
128
  it 'translates a range correctly when casefold option is true' do
217
- expect(translate_range('d-t', true))
218
- .to eq('defgğhıijklmnoöpqrsştĞIİÖŞ')
219
- expect(translate_range('C-U', true))
220
- .to eq('CÇDEFGĞHIİJKLMNOÖPQRSŞTUçğıiöş')
129
+ expect(translate_range('d-t', casefold: true)).to(eq('defgğhıijklmnoöpqrsştĞIİÖŞ'))
130
+ expect(translate_range('C-U', casefold: true)).to(eq('CÇDEFGĞHIİJKLMNOÖPQRSŞTUçğıiöş'))
221
131
  end
222
132
 
223
133
  it 'translates ranges created using Turkish characters' do
224
- expect(translate_range('Ç-Ü', true))
225
- .to eq('ÇDEFGĞHIİJKLMNOÖPQRSŞTUÜçğıiöşü')
226
- expect(translate_range('ç-ü', true))
227
- .to eq('çdefgğhıijklmnoöpqrsştuüÇĞIİÖŞÜ')
134
+ expect(translate_range('Ç-Ü', casefold: true)).to(eq('ÇDEFGĞHIİJKLMNOÖPQRSŞTUÜçğıiöşü'))
135
+ expect(translate_range('ç-ü', casefold: true)).to(eq('çdefgğhıijklmnoöpqrsştuüÇĞIİÖŞÜ'))
228
136
  end
229
137
  end
230
138
 
231
139
  describe '#translate_regexp' do
232
140
  it 'translates simple regexp to include Turkish characters' do
233
- expect(translate_regexp(/\w+\s[a-h]/))
234
- .to eq(/[\p{Latin}\d_]+\s[abcçdefgğh]/)
141
+ expect(translate_regexp(/\w+\s[a-h]/)).to(eq(/[\p{Latin}\d_]+\s[abcçdefgğh]/))
235
142
  end
236
143
 
237
144
  it 'translates simple regexp range created with Turkish characters' do
238
- expect(translate_regexp(/[ç-ş]/))
239
- .to eq(/[çdefgğhıijklmnoöpqrsş]/)
145
+ expect(translate_regexp(/[ç-ş]/)).to(eq(/[çdefgğhıijklmnoöpqrsş]/))
240
146
  end
241
147
 
242
148
  it 'preserves flags' do
243
- expect(translate_regexp(/\w+/mixu))
244
- .to eq(/[\p{Latin}\d_]+/mixu)
149
+ expect(translate_regexp(/\w+/mixu)).to(eq(/[\p{Latin}\d_]+/mixu))
245
150
  end
246
151
 
247
152
  it 'translates many range inside a character class' do
248
- expect(translate_regexp(/[a-ht-üy-z]/))
249
- .to eq(/[abcçdefgğhtuüyz]/)
153
+ expect(translate_regexp(/[a-ht-üy-z]/)).to(eq(/[abcçdefgğhtuüyz]/))
250
154
  end
251
155
 
252
156
  it 'translates many range inside different character class' do
253
- expect(translate_regexp(/[a-ht-üy-z]\s[a-dr-z]/))
254
- .to eq(/[abcçdefgğhtuüyz]\s[abcçdrsştuüvwxyz]/)
157
+ expect(translate_regexp(/[a-ht-üy-z]\s[a-dr-z]/)).to(eq(/[abcçdefgğhtuüyz]\s[abcçdrsştuüvwxyz]/))
255
158
  end
256
159
 
257
160
  it 'does not translate ranges outside character class' do
@@ -0,0 +1,415 @@
1
+ require 'spec_helper'
2
+ using TurkishSupport
3
+
4
+ module TurkishSupport # rubocop:disable Metrics/ModuleLength
5
+ describe String do
6
+ let(:downcased_turkish_alphabet) { 'abcçdefgğhıijklmnoöprsştuüvyz' }
7
+ let(:upcased_turkish_alphabet) { 'ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ' }
8
+ let(:downcased_english_alphabet) { ('a'..'z').to_a.join }
9
+ let(:upcased_english_alphabet) { ('A'..'Z').to_a.join }
10
+ let(:turkish_words) { %w[çamur ıhlamur insan ördek şahika ümraniye] }
11
+
12
+ describe '#[]' do
13
+ context 'with non-destructive version' do
14
+ it 'does not change the original value of the string' do
15
+ word = turkish_words.sample
16
+ expect { word[/\w+/] }.to_not(change { word })
17
+ end
18
+
19
+ it 'is able to capture Turkish characters' do
20
+ expect(turkish_words.all? { |w| w[/\w+/] == w }).to eq(true)
21
+ expect(turkish_words.all? { |w| w[/[a-z]+/] == w }).to eq(true)
22
+ expect(turkish_words.map(&:upcase).all? { |w| w[/[a-z]+/i] == w }).to eq(true)
23
+ end
24
+ end
25
+
26
+ context 'with destructive version' do
27
+ it 'changes the original value of the string' do
28
+ word = turkish_words.sample
29
+ expect { word[/\w+/] = 'new value' }.to(change { word })
30
+ expect(word).to eq('new value')
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '#index' do
36
+ it 'does not change the original value of the string' do
37
+ word = turkish_words.sample
38
+ expect { word.index(/\w+/) }.to_not(change { word })
39
+ end
40
+
41
+ it 'is able to capture Turkish characters' do
42
+ expect(turkish_words.all? { |w| w.index(/\w+/).zero? }).to(eq(true))
43
+ expect(turkish_words.all? { |w| w.index(/[a-z]+/).zero? }).to eq(true)
44
+ expect(turkish_words.map(&:upcase).all? { |w| w.index(/[a-z]+/i).zero? }).to eq(true)
45
+ end
46
+
47
+ it 'begins to search from the right position' do
48
+ expect('şç-!+*/-ğüı'.index(/\w+/, 2)).to eq(8)
49
+ end
50
+ end
51
+
52
+ describe '#rindex' do
53
+ it 'does not change the original value of the string' do
54
+ word = turkish_words.sample
55
+ expect { word.rindex(/\w+/) }.to_not(change { word })
56
+ end
57
+
58
+ it 'is able to capture Turkish characters' do
59
+ expect(turkish_words.map(&:reverse).all? { |w| w.rindex(/\w+/) == w.size - 1 }).to(eq(true))
60
+ expect(turkish_words.map(&:reverse).all? { |w| w.rindex(/[a-z]+/) == w.size - 1 }).to eq(true)
61
+ expect(turkish_words.map { |w| w.upcase.reverse }.all? { |w| w.rindex(/[a-z]+/i) == w.size - 1 }).to(eq(true))
62
+ end
63
+
64
+ it 'finishes the searching to the right position' do
65
+ expect('şç-!+*/-ğüı'.rindex(/\w+/, 7)).to eq(1)
66
+ end
67
+ end
68
+
69
+ describe '#partition' do
70
+ let(:word1) { turkish_words.sample }
71
+ let(:word2) { turkish_words.sample }
72
+ let(:two_words) { "#{word1} #{word2}" }
73
+
74
+ it 'does not change the original value of the string' do
75
+ expect { two_words.partition(/\W+/) }.to_not(change { two_words })
76
+ end
77
+
78
+ it 'is able to capture Turkish characters' do
79
+ expect(two_words.partition(/\W+/)).to(eq([word1, ' ', word2]))
80
+ end
81
+ end
82
+
83
+ describe '#rpartition' do
84
+ let(:word1) { turkish_words.sample }
85
+ let(:word2) { turkish_words.sample }
86
+ let(:word3) { turkish_words.sample }
87
+ let(:three_words) { "#{word1} #{word2} #{word3}" }
88
+
89
+ it 'does not change the original value of the string' do
90
+ expect { three_words.rpartition(/\W+/) }.to_not(change { three_words })
91
+ end
92
+
93
+ it 'is able to capture Turkish characters' do
94
+ expect(three_words.rpartition(/\W+/)).to(eq(["#{word1} #{word2}", ' ', word3]))
95
+ end
96
+ end
97
+
98
+ describe '#slice' do
99
+ context 'with non-destructive version' do
100
+ it 'does not change the original value of the string' do
101
+ sentence = turkish_words * ' '
102
+ expect { sentence.slice(/\w+/) }.to_not(change { sentence })
103
+ end
104
+
105
+ it 'is able to capture Turkish characters' do
106
+ expect(turkish_words.all? { |w| w.slice(/\w+/) == w }).to(eq(true))
107
+ expect(turkish_words.all? { |w| w.slice(/[a-z]+/) == w }).to eq(true)
108
+ expect(turkish_words.map(&:upcase).all? { |w| w.slice(/[a-z]+/i) == w }).to eq(true)
109
+ end
110
+ end
111
+
112
+ context 'with destructive version' do
113
+ it 'changes the original value of the string' do
114
+ sentence = turkish_words * ' '
115
+ expect { sentence.slice!(/\w+/) }.to(change { sentence })
116
+ expect(sentence).to eq(" #{turkish_words[1..] * ' '}")
117
+ end
118
+ end
119
+ end
120
+
121
+ describe '#split' do
122
+ it 'is able to capture Turkish characters' do
123
+ expect(turkish_words.join(' ').split(/\w+/).join.strip.empty?).to(eq(true))
124
+ expect(turkish_words.join(' ').split(/[a-z]+/).join.strip.empty?).to(eq(true))
125
+ expect(turkish_words.join(' ').upcase.split(/[a-z]+/i).join.strip.empty?).to(eq(true))
126
+ end
127
+ end
128
+
129
+ describe '#upcase' do
130
+ context 'with non-destructive version' do
131
+ it 'does not change the original value of the string' do
132
+ expect { downcased_turkish_alphabet.upcase }.to_not(change { downcased_turkish_alphabet })
133
+ end
134
+
135
+ it 'upcases all of Turkish characters' do
136
+ upcased_string = downcased_turkish_alphabet.upcase
137
+ expect(upcased_string).to(eq(upcased_turkish_alphabet))
138
+ end
139
+
140
+ it 'upcases English characters except i as I' do
141
+ upcased_string = downcased_english_alphabet.upcase
142
+ expect(upcased_string).to(eq(upcased_english_alphabet.tr('I', 'İ')))
143
+ end
144
+ end
145
+
146
+ context 'with destructive version' do
147
+ it 'changes the original value of the string' do
148
+ expect { downcased_turkish_alphabet.upcase! }.to(change { downcased_turkish_alphabet })
149
+ expect(downcased_turkish_alphabet).to(eq(upcased_turkish_alphabet))
150
+ end
151
+ end
152
+ end
153
+
154
+ describe '#downcase' do
155
+ context 'with non-destructive version' do
156
+ it 'does not change the original value of the string' do
157
+ expect { upcased_turkish_alphabet.downcase }.to_not(change { upcased_turkish_alphabet })
158
+ end
159
+
160
+ it 'downcases all of Turkish characters' do
161
+ downcased_string = upcased_turkish_alphabet.downcase
162
+ expect(downcased_string).to(eq(downcased_turkish_alphabet))
163
+ end
164
+
165
+ it 'downcases English characters except I as ı' do
166
+ downcased_string = upcased_english_alphabet.downcase
167
+ expect(downcased_string).to(eq(downcased_english_alphabet.tr('i', 'ı')))
168
+ end
169
+ end
170
+
171
+ context 'with destructive version' do
172
+ it 'changes the original value of the string' do
173
+ expect { upcased_turkish_alphabet.downcase! }.to(change { upcased_turkish_alphabet })
174
+ expect(upcased_turkish_alphabet).to(eq(downcased_turkish_alphabet))
175
+ end
176
+ end
177
+ end
178
+
179
+ describe '#capitalize' do
180
+ context 'with non-destructive version' do
181
+ it 'does not change the original value of the string' do
182
+ turkish_word = turkish_words.sample
183
+ expect { turkish_word.capitalize }.to_not(change { turkish_word })
184
+ end
185
+
186
+ it 'capitalizes the leading first Turkish character' do
187
+ capitalized_words = turkish_words.map(&:capitalize)
188
+ expect(capitalized_words).to(eq(%w[Çamur Ihlamur İnsan Ördek Şahika Ümraniye]))
189
+ end
190
+
191
+ it 'capitalizes the first character of a string and downcase others' do
192
+ expect('türkÇE desteĞİ'.capitalize).to(eq('Türkçe desteği'))
193
+ end
194
+
195
+ it 'capitalizes the first character of an English string' do
196
+ english_word = 'spy'
197
+ expect(english_word.capitalize).to(eq('Spy'))
198
+ end
199
+ end
200
+
201
+ context 'with destructive version' do
202
+ it 'changes the original value of the string' do
203
+ turkish_word = 'çamur'
204
+ expect { turkish_word.capitalize! }.to(change { turkish_word })
205
+ expect(turkish_word).to eq('Çamur')
206
+ end
207
+ end
208
+ end
209
+
210
+ describe '#casecmp' do
211
+ it 'compares Turkish characters correctly' do
212
+ result = downcased_turkish_alphabet.casecmp(upcased_turkish_alphabet)
213
+
214
+ expect(result.zero?).to(eq(true))
215
+ end
216
+
217
+ it 'compares Turkish characters correctly' do
218
+ expect('sıtkı'.casecmp('SıTKI')&.zero?).to(eq(true))
219
+ end
220
+ end
221
+
222
+ describe '#titleize' do
223
+ context 'with non-destructive version' do
224
+ it 'does not change the original value of the string' do
225
+ word = 'mERHABA çAMUR iSMETOĞULLARI'
226
+ expect { word.titleize }.to_not(change { word })
227
+ end
228
+
229
+ it 'upcases first character of all words' do
230
+ titleized = 'merhaba çamur ismet'.titleize
231
+ expect(titleized).to(eq('Merhaba Çamur İsmet'))
232
+ end
233
+
234
+ it 'no problem with words that consist of special chars only' do
235
+ titleized = '(( merhaba çamur ismet'.titleize
236
+ expect(titleized).to(eq('(( Merhaba Çamur İsmet'))
237
+ end
238
+
239
+ it 'downcases characters other than first characters of all words' do
240
+ titleized = 'mERHABA çAMUR iSMETOĞULLARI'.titleize
241
+ expect(titleized).to(eq('Merhaba Çamur İsmetoğulları'))
242
+ end
243
+
244
+ it 'support strings that include paranthesis, quotes, etc.' do
245
+ titleized = "rUBY roCkS... (really! 'tRUSt' ME)".titleize
246
+ expect(titleized).to(eq("Ruby Rocks... (Really! 'Trust' Me)"))
247
+ end
248
+
249
+ it 'does not capitalize conjuctions when false passed' do
250
+ titleized = 'kerem VE aslı VeYa leyla İlE mecnun'.titleize(conjuction: false)
251
+ expect(titleized).to eq('Kerem ve Aslı veya Leyla ile Mecnun')
252
+ end
253
+ end
254
+
255
+ context 'with destructive version' do
256
+ it 'changes the original value of the string' do
257
+ word = 'mERHABA çAMUR iSMETOĞULLARI'
258
+ expect { word.titleize! }.to(change { word })
259
+
260
+ expect(word).to(eq('Merhaba Çamur İsmetoğulları'))
261
+ end
262
+ end
263
+ end
264
+
265
+ describe '#swapcase' do
266
+ context 'with non-destructive version' do
267
+ it 'does not change the original value of the string' do
268
+ word = 'mErHaba çamur ismetoğullarI'
269
+ expect { word.swapcase }.to_not(change { word })
270
+ end
271
+
272
+ it 'swaps characters correctly' do
273
+ word = 'mErHaba çamur ismetoğullarI'.swapcase
274
+ expect(word).to(eq('MeRhABA ÇAMUR İSMETOĞULLARı'))
275
+ end
276
+ end
277
+
278
+ context 'with destructive version' do
279
+ it 'changes the original value of the string' do
280
+ word = 'mErHaba çamur ismetoğullarI'
281
+ expect { word.swapcase! }.to(change { word })
282
+ expect(word).to(eq('MeRhABA ÇAMUR İSMETOĞULLARı'))
283
+ end
284
+ end
285
+ end
286
+
287
+ describe '#match' do
288
+ it "matches Turkish characters when regex include '\\w'" do
289
+ expect('Aşağı'.match(/\w+/).to_s).to(eq('Aşağı'))
290
+ expect('Aşağı Ayrancı'.match(/^\w+\s\w+$/).to_s).to(eq('Aşağı Ayrancı'))
291
+ end
292
+
293
+ it 'matches Turkish characters when regex include lowercase range' do
294
+ expect('aüvvağğ öövvaağ'.match(/^[a-z]+\s[a-z]+$/).to_s).to(eq('aüvvağğ öövvaağ'))
295
+ end
296
+
297
+ it 'matches Turkish characters when regex include uppercase range' do
298
+ expect('BAĞCIlar'.match(/[A-Z]+/).to_s).to(eq('BAĞCI'))
299
+ end
300
+
301
+ it "doesn't match Turkish characters when regex include '\\W'" do
302
+ expect('Aşağı Ayrancı'.match(/\W+/).to_s).to(eq(' '))
303
+ end
304
+ end
305
+
306
+ describe '#scan' do
307
+ it "matches Turkish characters when regex include '\\w'" do
308
+ expect(turkish_words.join(' ').scan(/\w+/)).to eq(turkish_words)
309
+ end
310
+
311
+ it 'matches Turkish characters when regex include lowercase range' do
312
+ expect(turkish_words.join(' ').scan(/^[a-z\s]+$/)).to(eq([turkish_words.join(' ')]))
313
+ end
314
+
315
+ it 'matches Turkish characters when regex include uppercase range' do
316
+ expect(turkish_words.join(' ').upcase.scan(/^[A-Z\s]+$/)).to(eq([turkish_words.join(' ').upcase]))
317
+ end
318
+
319
+ it "matches Turkish characters when regex include '\\w'" do
320
+ expect(turkish_words.join(' ').scan(/\W+/).map(&:strip).all?(&:empty?)).to(eq(true))
321
+ end
322
+ end
323
+
324
+ describe '#sub' do
325
+ context 'non-destructive version' do
326
+ it 'does not affect the object' do
327
+ word = 'ağapaşa ağa'
328
+ expect { word.sub(/\w+/, 'bey') }.to_not(change { word })
329
+ end
330
+
331
+ it 'matches Turkish characters, and replaces them' do
332
+ expect('ağapaşa ağa'.sub(/\w+/, 'bey')).to(eq('bey ağa'))
333
+ expect('ağapaşa ağa şapka'.sub(/\W+/, 'bey')).to(eq('ağapaşabeyağa şapka'))
334
+ expect('ağapaşaağa'.sub(/[a-h]+/, 'bey')).to(eq('beypaşaağa'))
335
+ end
336
+ end
337
+
338
+ context 'destructive version' do
339
+ it 'affects the object' do
340
+ word = 'ağapaşa ağa'
341
+ expect { word.sub!(/\w+/, 'bey') }.to(change { word }.from('ağapaşa ağa').to('bey ağa'))
342
+ end
343
+ end
344
+ end
345
+
346
+ describe '#gsub' do
347
+ context 'non-destructive version' do
348
+ it 'does not affect the object' do
349
+ word = 'ağapaşa ağa'
350
+ expect { word.gsub(/\w+/, 'bey') }.to_not(change { word })
351
+ end
352
+
353
+ it 'matches Turkish characters, and replaces them' do
354
+ expect('ağapaşa ağa'.gsub(/\w+/, 'bey')).to(eq('bey bey'))
355
+ expect('ağapaşa ağa şapka'.gsub(/\W+/, 'bey')).to(eq('ağapaşabeyağabeyşapka'))
356
+ expect('ağapaşaağa'.gsub(/[a-h]+/, 'bey')).to(eq('beypbeyşbey'))
357
+ end
358
+ end
359
+
360
+ context 'destructive version' do
361
+ it 'affects the object' do
362
+ word = 'ağapaşa ağa'
363
+ expect { word.gsub!(/\w+/, 'bey') }.to(change { word }.from('ağapaşa ağa').to('bey bey'))
364
+ end
365
+ end
366
+ end
367
+
368
+ describe '#<=>' do
369
+ let(:sorted_equal_length_strings) { %w[Cahit Çağla Ömer Sıtkı Şakir] }
370
+ let(:sorted_different_length_strings) { %w[c ca falan om saki sıt] }
371
+ context 'with equal length strings' do
372
+ it 'works for smaller test' do
373
+ 0.upto(sorted_equal_length_strings.length - 2) do |i|
374
+ expect(sorted_equal_length_strings[i] <=> sorted_equal_length_strings[i + 1]).to(eq(-1))
375
+ end
376
+ end
377
+
378
+ it 'works for bigger test' do
379
+ (sorted_equal_length_strings.length - 1).downto(1) do |i|
380
+ expect(sorted_equal_length_strings[i] <=> sorted_equal_length_strings[i - 1]).to(eq(1))
381
+ end
382
+ end
383
+
384
+ it 'works for equals test' do
385
+ sorted_equal_length_strings.each do |str|
386
+ expect(str <=> str).to eq(0) # rubocop:disable Lint/BinaryOperatorWithIdenticalOperands
387
+ end
388
+ end
389
+ end
390
+
391
+ context 'with different length strings' do
392
+ it 'works for smaller test' do
393
+ 0.upto(sorted_different_length_strings.length - 2) do |i|
394
+ expect(sorted_different_length_strings[i] <=> sorted_different_length_strings[i + 1]).to eq(-1)
395
+ end
396
+ end
397
+
398
+ it 'works for bigger test' do
399
+ (sorted_different_length_strings.length - 1).downto(1) do |i|
400
+ expect(sorted_different_length_strings[i] <=> sorted_different_length_strings[i - 1]).to eq(1)
401
+ end
402
+ end
403
+ end
404
+
405
+ context 'invalid comparisons' do
406
+ it 'returns nil' do
407
+ expect('a' <=> 3.5).to eq(nil)
408
+ expect('a' <=> true).to eq(nil)
409
+ expect('a' <=> Object.new).to eq(nil)
410
+ expect('a' <=> 1).to eq(nil)
411
+ end
412
+ end
413
+ end
414
+ end
415
+ end