turkish_support 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21d687b0e2cd2035ebdc1b198340aaa12faa920bc458bc9920a7828215747baf
4
- data.tar.gz: c2d0ac7f7debf20bbcf50a28ac7758bd721d30ab5cfb68e3fbaac8d97f1c13de
3
+ metadata.gz: e26ab2401dd32844355047a8c1e1767011f07a6c9f767d01cf0595e2bdeff488
4
+ data.tar.gz: 6c115697dc9d568fab546290969236a60fe26a4b2a32ff3a151e9ee9d5233333
5
5
  SHA512:
6
- metadata.gz: 772a94afbe5c4d45333438ce3001fe210bd647b55fd0137cc644650da6acc214216fe31a2c074e087ba7791ae4b7e016167ac43d12bb6823f87d947a11aa6809
7
- data.tar.gz: 78e2537eddc53678bcf432df6fe24bd254c94f01fc04fa9203d599c75306bf9cca3ddc7e3b4f41ea7c837a3b2fca0bdde374118c8c625c3bd7d192f19335015c
6
+ metadata.gz: aa5710c1a5fd5b435d8c3a18dc61b8c883062f2b5b8064a315e415f7666d66c3218d56992ae73d4879a7f631773a4a3f6eeda5dd9d12e5ade2a9982a5e92d4d4
7
+ data.tar.gz: 0ba3f4573300ac8736f11ac13874f98354231dfbdd85604227e220455935e67289a2057374e89d7b29291418bcf3e95aab7f3c22c74264f36f900ea4b23a33ba
@@ -1,7 +1,7 @@
1
1
  module TurkishSupport
2
2
  refine Array do
3
3
  def sort
4
- if block_given? && any? { |item| !item.is_a? String }
4
+ if block_given?
5
5
  super
6
6
  else
7
7
  sort_by do |item|
@@ -1,3 +1,3 @@
1
1
  module TurkishSupport
2
- VERSION = '1.1.1'.freeze
2
+ VERSION = '1.1.2'.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
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,7 @@
1
1
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
2
  require 'turkish_support'
3
+
4
+ arr = %w[ ağa aça aşa aöa aüa aua afa aba ]
5
+ arr.sort # => ["aba", "afa", "aua", "aça", "aöa", "aüa", "ağa", "aşa"]
6
+ arr.sort { |a, b| a[0] <=> b[0] } # => ["aba", "aça", "aşa", "aöa", "aüa", "aua", "afa", "ağa"]
7
+ arr.sort { |a, b| b[0] <=> a[0] } # => ["aba", "aça", "aşa", "aöa", "aüa", "aua", "afa", "ağa"]
@@ -0,0 +1,596 @@
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'].join }
9
+ let(:upcased_english_alphabet) { [*'A'..'Z'].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
+
17
+ expect { word[/\w+/] }
18
+ .to_not change { word }
19
+ end
20
+
21
+ it 'is able to capture Turkish characters' do
22
+ expect(turkish_words
23
+ .all? { |w| w[/\w+/] == w }
24
+ ).to eq(true)
25
+
26
+ expect(turkish_words
27
+ .all? { |w| w[/[a-z]+/] == w }
28
+ ).to eq(true)
29
+
30
+ expect(turkish_words
31
+ .map(&:upcase)
32
+ .all? { |w| w[/[a-z]+/i] == w }
33
+ ).to eq(true)
34
+ end
35
+ end
36
+
37
+ context 'with destructive version' do
38
+ it 'changes the original value of the string' do
39
+ word = turkish_words.sample
40
+
41
+ expect { word[/\w+/] = 'new value' }
42
+ .to change { word }
43
+
44
+ expect(word)
45
+ .to eq('new value')
46
+ end
47
+ end
48
+ end
49
+
50
+ describe '#index' do
51
+ it 'does not change the original value of the string' do
52
+ word = turkish_words.sample
53
+
54
+ expect { word.index(/\w+/) }
55
+ .to_not change { word }
56
+ end
57
+
58
+ it 'is able to capture Turkish characters' do
59
+ expect(turkish_words
60
+ .all? { |w| w.index(/\w+/).zero? }
61
+ ).to eq(true)
62
+
63
+ expect(turkish_words
64
+ .all? { |w| w.index(/[a-z]+/).zero? }
65
+ ).to eq(true)
66
+
67
+ expect(turkish_words
68
+ .map(&:upcase)
69
+ .all? { |w| w.index(/[a-z]+/i).zero? }
70
+ ).to eq(true)
71
+ end
72
+
73
+ it 'begins to search from the right position' do
74
+ expect('şç-!+*/-ğüı'.index(/\w+/, 2)).to eq(8)
75
+ end
76
+ end
77
+
78
+ describe '#rindex' do
79
+ it 'does not change the original value of the string' do
80
+ word = turkish_words.sample
81
+ expect { word.rindex(/\w+/) }.to_not change { word }
82
+ end
83
+
84
+ it 'is able to capture Turkish characters' do
85
+ expect(turkish_words
86
+ .map(&:reverse)
87
+ .all? { |w| w.rindex(/\w+/) == w.size - 1 }
88
+ ).to eq(true)
89
+
90
+ expect(turkish_words
91
+ .map(&:reverse)
92
+ .all? { |w| w.rindex(/[a-z]+/) == w.size - 1 }
93
+ ).to eq(true)
94
+
95
+ expect(turkish_words
96
+ .map(&:upcase)
97
+ .map(&:reverse)
98
+ .all? { |w| w.rindex(/[a-z]+/i) == w.size - 1 }
99
+ ).to eq(true)
100
+ end
101
+
102
+ it 'finishes the searching to the right position' do
103
+ expect('şç-!+*/-ğüı'.rindex(/\w+/, 7)).to eq(1)
104
+ end
105
+ end
106
+
107
+ describe '#partition' do
108
+ let(:word1) { turkish_words.sample }
109
+ let(:word2) { turkish_words.sample }
110
+ let(:two_words) { "#{word1} #{word2}" }
111
+
112
+ it 'does not change the original value of the string' do
113
+ expect { two_words.partition(/\W+/) }.to_not change { two_words }
114
+ end
115
+
116
+ it 'is able to capture Turkish characters' do
117
+ expect(two_words.partition(/\W+/)).to eq([word1, ' ', word2])
118
+ end
119
+ end
120
+
121
+ describe '#rpartition' do
122
+ let(:word1) { turkish_words.sample }
123
+ let(:word2) { turkish_words.sample }
124
+ let(:word3) { turkish_words.sample }
125
+ let(:three_words) { "#{word1} #{word2} #{word3}" }
126
+
127
+ it 'does not change the original value of the string' do
128
+ expect { three_words.rpartition(/\W+/) }.to_not change { three_words }
129
+ end
130
+
131
+ it 'is able to capture Turkish characters' do
132
+ expect(three_words.rpartition(/\W+/))
133
+ .to eq(["#{word1} #{word2}", ' ', word3])
134
+ end
135
+ end
136
+
137
+ describe '#slice' do
138
+ context 'with non-destructive version' do
139
+ it 'does not change the original value of the string' do
140
+ sentence = turkish_words * ' '
141
+ expect { sentence.slice(/\w+/) }.to_not change { sentence }
142
+ end
143
+
144
+ it 'is able to capture Turkish characters' do
145
+ expect(turkish_words
146
+ .all? { |w| w.slice(/\w+/) == w }
147
+ ).to eq(true)
148
+
149
+ expect(turkish_words
150
+ .all? { |w| w.slice(/[a-z]+/) == w }
151
+ ).to eq(true)
152
+
153
+ expect(turkish_words
154
+ .map(&:upcase)
155
+ .all? { |w| w.slice(/[a-z]+/i) == w }
156
+ ).to eq(true)
157
+ end
158
+ end
159
+
160
+ context 'with destructive version' do
161
+ it 'changes the original value of the string' do
162
+ sentence = turkish_words * ' '
163
+
164
+ expect { sentence.slice!(/\w+/) }.to change { sentence }
165
+ expect(sentence).to eq(' ' + turkish_words[1..-1] * ' ')
166
+ end
167
+ end
168
+ end
169
+
170
+ describe '#split' do
171
+ it 'is able to capture Turkish characters' do
172
+ expect(turkish_words
173
+ .join(' ')
174
+ .split(/\w+/)
175
+ .join
176
+ .strip
177
+ .empty?
178
+ ).to eq(true)
179
+
180
+ expect(turkish_words
181
+ .join(' ')
182
+ .split(/[a-z]+/)
183
+ .join
184
+ .strip
185
+ .empty?
186
+ ).to eq(true)
187
+
188
+ expect(turkish_words
189
+ .join(' ')
190
+ .upcase
191
+ .split(/[a-z]+/i)
192
+ .join
193
+ .strip
194
+ .empty?
195
+ ).to eq(true)
196
+ end
197
+ end
198
+
199
+ describe '#upcase' do
200
+ context 'with non-destructive version' do
201
+ it 'does not change the original value of the string' do
202
+ expect { downcased_turkish_alphabet.upcase }
203
+ .to_not change { downcased_turkish_alphabet }
204
+ end
205
+
206
+ it 'upcases all of Turkish characters' do
207
+ upcased_string = downcased_turkish_alphabet.upcase
208
+
209
+ expect(upcased_string).to eq(upcased_turkish_alphabet)
210
+ end
211
+
212
+ it 'upcases English characters except i as I' do
213
+ upcased_string = downcased_english_alphabet.upcase
214
+
215
+ expect(upcased_string).to eq(upcased_english_alphabet.tr('I', 'İ'))
216
+ end
217
+ end
218
+
219
+ context 'with destructive version' do
220
+ it 'changes the original value of the string' do
221
+ expect { downcased_turkish_alphabet.upcase! }
222
+ .to change { downcased_turkish_alphabet }
223
+
224
+ expect(downcased_turkish_alphabet).to eq(upcased_turkish_alphabet)
225
+ end
226
+ end
227
+ end
228
+
229
+ describe '#downcase' do
230
+ context 'with non-destructive version' do
231
+ it 'does not change the original value of the string' do
232
+ expect { upcased_turkish_alphabet.downcase }
233
+ .to_not change { upcased_turkish_alphabet }
234
+ end
235
+
236
+ it 'downcases all of Turkish characters' do
237
+ downcased_string = upcased_turkish_alphabet.downcase
238
+
239
+ expect(downcased_string)
240
+ .to eq(downcased_turkish_alphabet)
241
+ end
242
+
243
+ it 'downcases English characters except I as ı' do
244
+ downcased_string = upcased_english_alphabet.downcase
245
+ expect(downcased_string)
246
+ .to eq(downcased_english_alphabet.tr('i', 'ı'))
247
+ end
248
+ end
249
+
250
+ context 'with destructive version' do
251
+ it 'changes the original value of the string' do
252
+ expect { upcased_turkish_alphabet.downcase! }
253
+ .to change { upcased_turkish_alphabet }
254
+ expect(upcased_turkish_alphabet)
255
+ .to eq(downcased_turkish_alphabet)
256
+ end
257
+ end
258
+ end
259
+
260
+ describe '#capitalize' do
261
+ context 'with non-destructive version' do
262
+ it 'does not change the original value of the string' do
263
+ turkish_word = turkish_words.sample
264
+
265
+ expect { turkish_word.capitalize }.to_not change { turkish_word }
266
+ end
267
+
268
+ it 'capitalizes the leading first Turkish character' do
269
+ # rubocop:disable Style/SymbolProc
270
+ capitalized_words = turkish_words.map { |w| w.capitalize }
271
+
272
+ expect(capitalized_words)
273
+ .to eq(%w[Çamur Ihlamur İnsan Ördek Şahika Ümraniye])
274
+ end
275
+
276
+ it 'capitalizes the first character of a string and downcase others' do
277
+ expect('türkÇE desteĞİ'.capitalize)
278
+ .to eq('Türkçe desteği')
279
+ end
280
+
281
+ it 'capitalizes the first character of an English string' do
282
+ english_word = 'spy'
283
+ capitalized_string = english_word.capitalize
284
+
285
+ expect(capitalized_string)
286
+ .to eq('Spy')
287
+ end
288
+ end
289
+
290
+ context 'with destructive version' do
291
+ it 'changes the original value of the string' do
292
+ turkish_word = 'çamur'
293
+
294
+ expect { turkish_word.capitalize! }
295
+ .to change { turkish_word }
296
+
297
+ expect(turkish_word)
298
+ .to eq('Çamur')
299
+ end
300
+ end
301
+ end
302
+
303
+ describe '#casecmp' do
304
+ it 'compares Turkish characters correctly' do
305
+ result = downcased_turkish_alphabet.casecmp(upcased_turkish_alphabet)
306
+
307
+ expect(result.zero?)
308
+ .to eq(true)
309
+ end
310
+
311
+ it 'compares Turkish characters correctly' do
312
+ expect('sıtkı'.casecmp('SıTKI').zero?)
313
+ .to eq(true)
314
+ end
315
+ end
316
+
317
+ describe '#titleize' do
318
+ context 'with non-destructive version' do
319
+ it 'does not change the original value of the string' do
320
+ word = 'mERHABA çAMUR iSMETOĞULLARI'
321
+
322
+ expect { word.titleize }
323
+ .to_not change { word }
324
+ end
325
+
326
+ it 'upcases first character of all words' do
327
+ titleized = 'merhaba çamur ismet'.titleize
328
+
329
+ expect(titleized)
330
+ .to eq('Merhaba Çamur İsmet')
331
+ end
332
+
333
+ it 'no problem with words that consist of special chars only' do
334
+ titleized = '(( merhaba çamur ismet'.titleize
335
+
336
+ expect(titleized)
337
+ .to eq('(( Merhaba Çamur İsmet')
338
+ end
339
+
340
+ it 'downcases characters other than first characters of all words' do
341
+ titleized = 'mERHABA çAMUR iSMETOĞULLARI'.titleize
342
+
343
+ expect(titleized)
344
+ .to eq('Merhaba Çamur İsmetoğulları')
345
+ end
346
+
347
+ it 'support strings that include paranthesis, quotes, etc.' do
348
+ titleized = "rUBY roCkS... (really! 'tRUSt' ME)".titleize
349
+
350
+ expect(titleized)
351
+ .to eq("Ruby Rocks... (Really! 'Trust' Me)")
352
+ end
353
+
354
+ it 'does not capitalize conjuctions when false passed' do
355
+ titleized = 'kerem VE aslı VeYa leyla İlE mecnun'.titleize(false)
356
+
357
+ expect(titleized)
358
+ .to eq('Kerem ve Aslı veya Leyla ile Mecnun')
359
+ end
360
+ end
361
+
362
+ context 'with destructive version' do
363
+ it 'changes the original value of the string' do
364
+ word = 'mERHABA çAMUR iSMETOĞULLARI'
365
+
366
+ expect { word.titleize! }
367
+ .to change { word }
368
+
369
+ expect(word)
370
+ .to eq('Merhaba Çamur İsmetoğulları')
371
+ end
372
+ end
373
+ end
374
+
375
+ describe '#swapcase' do
376
+ context 'with non-destructive version' do
377
+ it 'does not change the original value of the string' do
378
+ word = 'mErHaba çamur ismetoğullarI'
379
+
380
+ expect { word.swapcase }
381
+ .to_not change { word }
382
+ end
383
+
384
+ it 'swaps characters correctly' do
385
+ word = 'mErHaba çamur ismetoğullarI'.swapcase
386
+
387
+ expect(word)
388
+ .to eq('MeRhABA ÇAMUR İSMETOĞULLARı')
389
+ end
390
+ end
391
+
392
+ context 'with destructive version' do
393
+ it 'changes the original value of the string' do
394
+ word = 'mErHaba çamur ismetoğullarI'
395
+
396
+ expect { word.swapcase! }
397
+ .to change { word }
398
+ expect(word)
399
+ .to eq('MeRhABA ÇAMUR İSMETOĞULLARı')
400
+ end
401
+ end
402
+ end
403
+
404
+ describe '#match' do
405
+ it "matches Turkish characters when regex include '\\w'" do
406
+ expect('Aşağı'.match(/\w+/).to_s)
407
+ .to eq('Aşağı')
408
+
409
+ expect('Aşağı Ayrancı'.match(/^\w+\s\w+$/).to_s)
410
+ .to eq('Aşağı Ayrancı')
411
+ end
412
+
413
+ it 'matches Turkish characters when regex include lowercase range' do
414
+ expect('aüvvağğ öövvaağ'.match(/^[a-z]+\s[a-z]+$/).to_s)
415
+ .to eq('aüvvağğ öövvaağ')
416
+ end
417
+
418
+ it 'matches Turkish characters when regex include uppercase range' do
419
+ expect('BAĞCIlar'.match(/[A-Z]+/).to_s)
420
+ .to eq('BAĞCI')
421
+ end
422
+
423
+ it "doesn't match Turkish characters when regex include '\\W'" do
424
+ expect('Aşağı Ayrancı'.match(/\W+/).to_s).to eq(' ')
425
+ end
426
+ end
427
+
428
+ describe '#scan' do
429
+ it "matches Turkish characters when regex include '\\w'" do
430
+ expect(turkish_words.join(' ').scan(/\w+/)).to eq(turkish_words)
431
+ end
432
+
433
+ it 'matches Turkish characters when regex include lowercase range' do
434
+ expect(turkish_words
435
+ .join(' ')
436
+ .scan(/^[a-z\s]+$/)
437
+ ).to eq([turkish_words.join(' ')])
438
+ end
439
+
440
+ it 'matches Turkish characters when regex include uppercase range' do
441
+ expect(turkish_words
442
+ .join(' ')
443
+ .upcase
444
+ .scan(/^[A-Z\s]+$/)
445
+ ).to eq([turkish_words.join(' ').upcase])
446
+ end
447
+
448
+ it "matches Turkish characters when regex include '\\w'" do
449
+ expect(turkish_words
450
+ .join(' ')
451
+ .scan(/\W+/)
452
+ .map(&:strip)
453
+ .all?(&:empty?)
454
+ ).to eq(true)
455
+ end
456
+ end
457
+
458
+ describe '#=~' do
459
+ tr_chars = ALPHA[:tr_lower] + ALPHA[:tr_upper]
460
+
461
+ it "matches Turkish characters when regex include '\\w'" do
462
+ expect(tr_chars
463
+ .split(//)
464
+ .all? { |ch| (ch =~ /\w/).zero? }
465
+ ).to eq(true)
466
+ end
467
+
468
+ it 'matches Turkish characters when regex include lowercase range' do
469
+ expect(ALPHA[:tr_lower]
470
+ .split(//)
471
+ .all? { |ch| (ch =~ /[a-z]/).zero? }
472
+ ).to eq(true)
473
+ end
474
+
475
+ it 'matches Turkish characters when regex include uppercase range' do
476
+ expect(ALPHA[:tr_upper]
477
+ .split(//)
478
+ .all? { |ch| (ch =~ /[A-Z]/).zero? }
479
+ ).to eq(true)
480
+ end
481
+
482
+ it "doesn't match Turkish characters when regex include '\\W'" do
483
+ expect(tr_chars
484
+ .split(//)
485
+ .all? { |ch| (ch =~ /\W/).nil? }
486
+ ).to eq(true)
487
+ end
488
+ end
489
+
490
+ describe '#sub' do
491
+ context 'non-destructive version' do
492
+ it 'does not affect the object' do
493
+ word = 'ağapaşa ağa'
494
+
495
+ expect { word.sub(/\w+/, 'bey') }.to_not change { word }
496
+ end
497
+
498
+ it 'matches Turkish characters, and replaces them' do
499
+ expect('ağapaşa ağa'.sub(/\w+/, 'bey')).to eq('bey ağa')
500
+
501
+ expect('ağapaşa ağa şapka'.sub(/\W+/, 'bey'))
502
+ .to eq('ağapaşabeyağa şapka')
503
+
504
+ expect('ağapaşaağa'.sub(/[a-h]+/, 'bey')).to eq('beypaşaağa')
505
+ end
506
+ end
507
+
508
+ context 'destructive version' do
509
+ it 'affects the object' do
510
+ word = 'ağapaşa ağa'
511
+
512
+ expect { word.sub!(/\w+/, 'bey') }
513
+ .to change { word }
514
+ .from('ağapaşa ağa')
515
+ .to('bey ağa')
516
+ end
517
+ end
518
+ end
519
+
520
+ describe '#gsub' do
521
+ context 'non-destructive version' do
522
+ it 'does not affect the object' do
523
+ word = 'ağapaşa ağa'
524
+
525
+ expect { word.gsub(/\w+/, 'bey') }.to_not change { word }
526
+ end
527
+
528
+ it 'matches Turkish characters, and replaces them' do
529
+ expect('ağapaşa ağa'.gsub(/\w+/, 'bey')).to eq('bey bey')
530
+
531
+ expect('ağapaşa ağa şapka'.gsub(/\W+/, 'bey'))
532
+ .to eq('ağapaşabeyağabeyşapka')
533
+
534
+ expect('ağapaşaağa'.gsub(/[a-h]+/, 'bey')).to eq('beypbeyşbey')
535
+ end
536
+ end
537
+
538
+ context 'destructive version' do
539
+ it 'affects the object' do
540
+ word = 'ağapaşa ağa'
541
+ expect { word.gsub!(/\w+/, 'bey') }
542
+ .to change { word }
543
+ .from('ağapaşa ağa')
544
+ .to('bey bey')
545
+ end
546
+ end
547
+ end
548
+
549
+ describe "#<=>" do
550
+ let(:sorted_equal_length_strings) { %w[Cahit Çağla Ömer Sıtkı Şakir] }
551
+ let(:sorted_different_length_strings) { %w[c ca falan om saki sıt] }
552
+ context "with equal length strings" do
553
+ it "works for smaller test" do
554
+ 0.upto(sorted_equal_length_strings.length - 2) do |i|
555
+ expect(sorted_equal_length_strings[i] <=> sorted_equal_length_strings[i+1]).to eq(-1)
556
+ end
557
+ end
558
+
559
+ it "works for bigger test" do
560
+ (sorted_equal_length_strings.length-1).downto(1) do |i|
561
+ expect(sorted_equal_length_strings[i] <=> sorted_equal_length_strings[i-1]).to eq(1)
562
+ end
563
+ end
564
+
565
+ it "works for equals test" do
566
+ sorted_equal_length_strings.each do |str|
567
+ expect(str <=> str).to eq(0)
568
+ end
569
+ end
570
+ end
571
+
572
+ context "with different length strings" do
573
+ it "works for smaller test" do
574
+ 0.upto(sorted_different_length_strings.length - 2) do |i|
575
+ expect(sorted_different_length_strings[i] <=> sorted_different_length_strings[i+1]).to eq(-1)
576
+ end
577
+ end
578
+
579
+ it "works for bigger test" do
580
+ (sorted_different_length_strings.length-1).downto(1) do |i|
581
+ expect(sorted_different_length_strings[i] <=> sorted_different_length_strings[i-1]).to eq(1)
582
+ end
583
+ end
584
+ end
585
+
586
+ context "invalid comparisons" do
587
+ it "returns nil" do
588
+ expect("a" <=> 3.5).to eq(nil)
589
+ expect("a" <=> true).to eq(nil)
590
+ expect("a" <=> Object.new).to eq(nil)
591
+ expect("a" <=> 1).to eq(nil)
592
+ end
593
+ end
594
+ end
595
+ end
596
+ end