turkish_support 0.3.0 → 1.0.0.pre.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.hound.yml +3 -0
- data/.rubocop.yml +7 -0
- data/README.md +173 -41
- data/Rakefile +3 -3
- data/lib/turkish_support.rb +6 -6
- data/lib/turkish_support/array_methods.rb +5 -1
- data/lib/turkish_support/constants.rb +47 -14
- data/lib/turkish_support/helpers.rb +112 -0
- data/lib/turkish_support/string_methods.rb +33 -21
- data/lib/turkish_support/version.rb +1 -1
- data/spec/helpers_spec.rb +264 -0
- data/spec/turkish_support_spec.rb +482 -122
- data/turkish_support.gemspec +15 -14
- metadata +13 -9
- data/lib/turkish_support/destructives.rb +0 -17
- data/lib/turkish_support/string_helpers.rb +0 -42
@@ -1,20 +1,32 @@
|
|
1
1
|
module TurkishSupport
|
2
2
|
refine String do
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
(RE_RE_METHS + RE_OP_METHS).each do |meth|
|
4
|
+
define_method meth do |*args|
|
5
|
+
extend(TurkishSupportHelpers)
|
6
|
+
|
7
|
+
if RE_RE_METHS.include?(meth) || args[0].is_a?(Regexp)
|
8
|
+
args[0] = translate_regexp(args[0])
|
9
|
+
end
|
10
|
+
|
11
|
+
super(*args)
|
9
12
|
end
|
10
13
|
end
|
11
14
|
|
12
|
-
|
13
|
-
|
15
|
+
CASE_RELATED_METHS.each do |meth|
|
16
|
+
non_destructive = meth.to_s.chomp('!').to_sym
|
17
|
+
define_method(meth) do
|
18
|
+
extend(TurkishSupportHelpers)
|
19
|
+
str = prepare_for(non_destructive, self).public_send(non_destructive)
|
20
|
+
return meth.to_s.end_with?('!') ? public_send(:replace, str) : str
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def titleize(conjuctions = true)
|
25
|
+
split.map do |word|
|
14
26
|
word.downcase!
|
15
|
-
if
|
27
|
+
if conjuction?(word) && !conjuctions
|
16
28
|
word
|
17
|
-
elsif
|
29
|
+
elsif start_with_a_special_char?(word)
|
18
30
|
word.chr + word[1..-1].capitalize
|
19
31
|
else
|
20
32
|
word.capitalize
|
@@ -22,27 +34,27 @@ module TurkishSupport
|
|
22
34
|
end.join(' ')
|
23
35
|
end
|
24
36
|
|
37
|
+
def titleize!(conjuctions = true)
|
38
|
+
replace(titleize(conjuctions))
|
39
|
+
end
|
40
|
+
|
25
41
|
def swapcase
|
42
|
+
extend(TurkishSupportHelpers)
|
26
43
|
chars.map do |ch|
|
27
|
-
if ch
|
28
|
-
ch
|
44
|
+
if tr_char?(ch)
|
45
|
+
tr_lower?(ch) ? ch.upcase : ch.downcase
|
29
46
|
else
|
30
47
|
ch.public_send(:swapcase)
|
31
48
|
end
|
32
49
|
end.join
|
33
50
|
end
|
34
51
|
|
35
|
-
def
|
36
|
-
|
52
|
+
def swapcase!
|
53
|
+
replace(swapcase)
|
37
54
|
end
|
38
55
|
|
39
|
-
|
40
|
-
|
41
|
-
Regexp.new(pattern) unless pattern.kind_of? Regexp
|
42
|
-
pattern, options = pattern.source, pattern.options
|
43
|
-
pattern = Regexp.new(pattern.transform_regex, Regexp::FIXEDENCODING | options)
|
44
|
-
public_send(method_name, pattern)
|
45
|
-
end
|
56
|
+
def casecmp(other_string)
|
57
|
+
upcase.public_send(:casecmp, other_string.upcase)
|
46
58
|
end
|
47
59
|
end
|
48
60
|
end
|
@@ -0,0 +1,264 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'turkish_support/helpers'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.include TurkishSupportHelpers
|
6
|
+
using TurkishSupport
|
7
|
+
end
|
8
|
+
|
9
|
+
describe 'TurkishSupportHelpers' do
|
10
|
+
let(:downcased_alphabet) { 'abcçdefgğhıijklmnoöpqrsştuüvwxyz' }
|
11
|
+
let(:upcased_alphabet) { 'ABCÇDEFGĞHIİJKLMNOÖPQRSŞTUÜVWXYZ' }
|
12
|
+
let(:alphabet) { downcased_alphabet + upcased_alphabet }
|
13
|
+
let(:tr_specific_lower) { 'çğıiöşü' }
|
14
|
+
let(:tr_specific_upper) { 'ÇĞIİÖŞÜ' }
|
15
|
+
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
|
62
|
+
|
63
|
+
describe '#tr_lower?' do
|
64
|
+
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)
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'returns false' do
|
72
|
+
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)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'for any upper case characters' do
|
81
|
+
expect(upcased_alphabet
|
82
|
+
.chars
|
83
|
+
.any? { |ch| tr_lower?(ch) }
|
84
|
+
).to eq(false)
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'for non-letter characters' do
|
88
|
+
expect("\"é1!2'3^+4.,-_"
|
89
|
+
.chars
|
90
|
+
.any? { |ch| tr_lower?(ch) }
|
91
|
+
).to eq(false)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '#tr_upper?' do
|
97
|
+
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)
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'returns false' do
|
105
|
+
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)
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'for any lower case characters' do
|
114
|
+
expect(downcased_alphabet
|
115
|
+
.chars
|
116
|
+
.any? { |ch| tr_upper?(ch) }
|
117
|
+
).to eq(false)
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'for non-letter characters' do
|
121
|
+
expect("\"é1!2'3^+4.,-_"
|
122
|
+
.chars
|
123
|
+
.any? { |ch| tr_upper?(ch) }
|
124
|
+
).to eq(false)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe '#tr_char?' do
|
130
|
+
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)
|
135
|
+
end
|
136
|
+
|
137
|
+
context 'returns false' do
|
138
|
+
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)
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'for non-letter characters' do
|
146
|
+
expect("\"é1!2'3^+4.,-_"
|
147
|
+
.chars
|
148
|
+
.any? { |ch| tr_char?(ch) }
|
149
|
+
).to eq(false)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe '#conjuction?' do
|
155
|
+
it 'returns true for any conjuction' do
|
156
|
+
expect(conjuctions
|
157
|
+
.all? { |c| conjuction?(c) }
|
158
|
+
).to eq(true)
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'returns false for any word contains conjuction' do
|
162
|
+
expect(%w(veda aile veyahut)
|
163
|
+
.any? { |c| conjuction?(c) }
|
164
|
+
).to eq(false)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
describe '#start_with_a_special_char?' do
|
169
|
+
it 'returns true for all words starts with a special char' do
|
170
|
+
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)
|
174
|
+
end
|
175
|
+
|
176
|
+
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)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe '#translate_range' do
|
184
|
+
it 'translates a complete lower-case range correctly' do
|
185
|
+
expect(translate_range('a-z'))
|
186
|
+
.to eq(downcased_alphabet)
|
187
|
+
end
|
188
|
+
|
189
|
+
it 'translates a complete upper-case range correctly' do
|
190
|
+
expect(translate_range('A-Z'))
|
191
|
+
.to eq(upcased_alphabet)
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'raises an error if any arguments not a letter' do
|
195
|
+
invalid_arguments = %w(1-Z A-9 1-9 a-])
|
196
|
+
expect(invalid_arguments
|
197
|
+
.all? do |arg|
|
198
|
+
expect { translate_range(arg) }
|
199
|
+
.to raise_error ArgumentError,
|
200
|
+
'Invalid regexp range arguments!'
|
201
|
+
end
|
202
|
+
).to eq(true)
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'raises an error if arguments are not in same case' do
|
206
|
+
expect { translate_range('a-Z') }
|
207
|
+
.to raise_error ArgumentError,
|
208
|
+
'Invalid regexp range arguments!'
|
209
|
+
end
|
210
|
+
|
211
|
+
it 'translates a partial range correctly' do
|
212
|
+
expect(translate_range('d-t'))
|
213
|
+
.to eq('defgğhıijklmnoöpqrsşt')
|
214
|
+
expect(translate_range('Ç-Ü'))
|
215
|
+
.to eq('ÇDEFGĞHIİJKLMNOÖPQRSŞTUÜ')
|
216
|
+
end
|
217
|
+
|
218
|
+
it 'translates a range correctly when casefold option is true' do
|
219
|
+
expect(translate_range('d-t', true))
|
220
|
+
.to eq('defgğhıijklmnoöpqrsştĞIİÖŞ')
|
221
|
+
expect(translate_range('C-U', true))
|
222
|
+
.to eq('CÇDEFGĞHIİJKLMNOÖPQRSŞTUçğıiöş')
|
223
|
+
end
|
224
|
+
|
225
|
+
it 'translates ranges created using Turkish characters' do
|
226
|
+
expect(translate_range('Ç-Ü', true))
|
227
|
+
.to eq('ÇDEFGĞHIİJKLMNOÖPQRSŞTUÜçğıiöşü')
|
228
|
+
expect(translate_range('ç-ü', true))
|
229
|
+
.to eq('çdefgğhıijklmnoöpqrsştuüÇĞIİÖŞÜ')
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
describe '#translate_regexp' do
|
234
|
+
it 'translates simple regexp to include Turkish characters' do
|
235
|
+
expect(translate_regexp(/\w+\s[a-h]/))
|
236
|
+
.to eq(/[\p{Latin}\d_]+\s[abcçdefgğh]/)
|
237
|
+
end
|
238
|
+
|
239
|
+
it 'translates simple regexp range created with Turkish characters' do
|
240
|
+
expect(translate_regexp(/[ç-ş]/))
|
241
|
+
.to eq(/[çdefgğhıijklmnoöpqrsş]/)
|
242
|
+
end
|
243
|
+
|
244
|
+
it 'preserves flags' do
|
245
|
+
expect(translate_regexp(/\w+/mixu))
|
246
|
+
.to eq(/[\p{Latin}\d_]+/mixu)
|
247
|
+
end
|
248
|
+
|
249
|
+
it 'translates many range inside a character class' do
|
250
|
+
expect(translate_regexp(/[a-ht-üy-z]/))
|
251
|
+
.to eq(/[abcçdefgğhtuüyz]/)
|
252
|
+
end
|
253
|
+
|
254
|
+
it 'translates many range inside different character class' do
|
255
|
+
expect(translate_regexp(/[a-ht-üy-z]\s[a-dr-z]/))
|
256
|
+
.to eq(/[abcçdefgğhtuüyz]\s[abcçdrsştuüvwxyz]/)
|
257
|
+
end
|
258
|
+
|
259
|
+
it 'does not translate ranges outside character class' do
|
260
|
+
expect(translate_regexp(/[a-h]+\se-h[ç-ş]afse-h+/))
|
261
|
+
.to eq(/[abcçdefgğh]+\se-h[çdefgğhıijklmnoöpqrsş]afse-h+/)
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
@@ -9,249 +9,609 @@ module TurkishSupport
|
|
9
9
|
end
|
10
10
|
|
11
11
|
describe String do
|
12
|
-
let(:downcased_turkish_alphabet) {
|
13
|
-
let(:upcased_turkish_alphabet) {
|
12
|
+
let(:downcased_turkish_alphabet) { 'abcçdefgğhıijklmnoöprsştuüvyz' }
|
13
|
+
let(:upcased_turkish_alphabet) { 'ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ' }
|
14
14
|
let(:downcased_english_alphabet) { [*'a'..'z'].join }
|
15
15
|
let(:upcased_english_alphabet) { [*'A'..'Z'].join }
|
16
|
-
let(:turkish_words) { %w(çamur ıhlamur insan ördek şahika ümraniye) }
|
17
16
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
let(:turkish_words) do
|
18
|
+
%w(çamur ıhlamur insan ördek şahika ümraniye)
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#[]' do
|
22
|
+
context 'with non-destructive version' do
|
23
|
+
it 'does not change the original value of the string' do
|
24
|
+
word = turkish_words.sample
|
25
|
+
|
26
|
+
expect { word[/\w+/] }
|
27
|
+
.to_not change { word }
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'is able to capture Turkish characters' do
|
31
|
+
expect(turkish_words
|
32
|
+
.all? { |w| w[/\w+/] == w }
|
33
|
+
).to eq(true)
|
34
|
+
|
35
|
+
expect(turkish_words
|
36
|
+
.all? { |w| w[/[a-z]+/] == w }
|
37
|
+
).to eq(true)
|
38
|
+
|
39
|
+
expect(
|
40
|
+
turkish_words
|
41
|
+
.map(&:upcase)
|
42
|
+
.all? { |w| w[/[a-z]+/i] == w }
|
43
|
+
).to eq(true)
|
22
44
|
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'with destructive version' do
|
48
|
+
it 'changes the original value of the string' do
|
49
|
+
word = turkish_words.sample
|
50
|
+
|
51
|
+
expect { word[/\w+/] = 'new value' }
|
52
|
+
.to change { word }
|
53
|
+
|
54
|
+
expect(word)
|
55
|
+
.to eq('new value')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '#index' do
|
61
|
+
it 'does not change the original value of the string' do
|
62
|
+
word = turkish_words.sample
|
23
63
|
|
24
|
-
|
64
|
+
expect { word.index(/\w+/) }
|
65
|
+
.to_not change { word }
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'is able to capture Turkish characters' do
|
69
|
+
expect(turkish_words
|
70
|
+
.all? { |w| w.index(/\w+/).zero? }
|
71
|
+
).to eq(true)
|
72
|
+
|
73
|
+
expect(turkish_words
|
74
|
+
.all? { |w| w.index(/[a-z]+/).zero? }
|
75
|
+
).to eq(true)
|
76
|
+
|
77
|
+
expect(turkish_words
|
78
|
+
.map(&:upcase)
|
79
|
+
.all? { |w| w.index(/[a-z]+/i).zero? }
|
80
|
+
).to eq(true)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'begins to search from the right position' do
|
84
|
+
expect('şç-!+*/-ğüı'.index(/\w+/, 2))
|
85
|
+
.to eq(8)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe '#rindex' do
|
90
|
+
it 'does not change the original value of the string' do
|
91
|
+
word = turkish_words.sample
|
92
|
+
expect { word.rindex(/\w+/) }
|
93
|
+
.to_not change { word }
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'is able to capture Turkish characters' do
|
97
|
+
expect(turkish_words
|
98
|
+
.map(&:reverse)
|
99
|
+
.all? { |w| w.rindex(/\w+/) == w.size - 1 }
|
100
|
+
).to eq(true)
|
101
|
+
|
102
|
+
expect(turkish_words
|
103
|
+
.map(&:reverse)
|
104
|
+
.all? { |w| w.rindex(/[a-z]+/) == w.size - 1 }
|
105
|
+
).to eq(true)
|
106
|
+
|
107
|
+
expect(turkish_words
|
108
|
+
.map(&:upcase)
|
109
|
+
.map(&:reverse)
|
110
|
+
.all? { |w| w.rindex(/[a-z]+/i) == w.size - 1 }
|
111
|
+
).to eq(true)
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'finishes the searching to the right position' do
|
115
|
+
expect('şç-!+*/-ğüı'.rindex(/\w+/, 7))
|
116
|
+
.to eq(1)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe '#partition' do
|
121
|
+
let(:word1) { turkish_words.sample }
|
122
|
+
let(:word2) { turkish_words.sample }
|
123
|
+
let(:two_words) { "#{word1} #{word2}" }
|
124
|
+
|
125
|
+
it 'does not change the original value of the string' do
|
126
|
+
expect { two_words.partition(/\W+/) }
|
127
|
+
.to_not change { two_words }
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'is able to capture Turkish characters' do
|
131
|
+
expect(two_words.partition(/\W+/)
|
132
|
+
).to eq([word1, ' ', word2])
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe '#rpartition' do
|
137
|
+
let(:word1) { turkish_words.sample }
|
138
|
+
let(:word2) { turkish_words.sample }
|
139
|
+
let(:word3) { turkish_words.sample }
|
140
|
+
let(:three_words) { "#{word1} #{word2} #{word3}" }
|
141
|
+
|
142
|
+
it 'does not change the original value of the string' do
|
143
|
+
expect { three_words.rpartition(/\W+/) }
|
144
|
+
.to_not change { three_words }
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'is able to capture Turkish characters' do
|
148
|
+
expect(three_words.rpartition(/\W+/))
|
149
|
+
.to eq(["#{word1} #{word2}", ' ', word3])
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
describe '#slice' do
|
154
|
+
context 'with non-destructive version' do
|
155
|
+
it 'does not change the original value of the string' do
|
156
|
+
sentence = turkish_words * ' '
|
157
|
+
|
158
|
+
expect { sentence.slice(/\w+/) }
|
159
|
+
.to_not change { sentence }
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'is able to capture Turkish characters' do
|
163
|
+
expect(turkish_words
|
164
|
+
.all? { |w| w.slice(/\w+/) == w }
|
165
|
+
).to eq(true)
|
166
|
+
|
167
|
+
expect(turkish_words
|
168
|
+
.all? { |w| w.slice(/[a-z]+/) == w }
|
169
|
+
).to eq(true)
|
170
|
+
|
171
|
+
expect(turkish_words
|
172
|
+
.map(&:upcase)
|
173
|
+
.all? { |w| w.slice(/[a-z]+/i) == w }
|
174
|
+
).to eq(true)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
context 'with destructive version' do
|
179
|
+
it 'changes the original value of the string' do
|
180
|
+
sentence = turkish_words * ' '
|
181
|
+
|
182
|
+
expect { sentence.slice!(/\w+/) }
|
183
|
+
.to change { sentence }
|
184
|
+
expect(sentence)
|
185
|
+
.to eq(' ' + turkish_words[1..-1] * ' ')
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
describe '#split' do
|
191
|
+
it 'is able to capture Turkish characters' do
|
192
|
+
expect(turkish_words
|
193
|
+
.join(' ')
|
194
|
+
.split(/\w+/)
|
195
|
+
.join
|
196
|
+
.strip
|
197
|
+
.empty?
|
198
|
+
).to eq(true)
|
199
|
+
|
200
|
+
expect(turkish_words
|
201
|
+
.join(' ')
|
202
|
+
.split(/[a-z]+/)
|
203
|
+
.join
|
204
|
+
.strip
|
205
|
+
.empty?
|
206
|
+
).to eq(true)
|
207
|
+
|
208
|
+
expect(turkish_words
|
209
|
+
.join(' ')
|
210
|
+
.upcase
|
211
|
+
.split(/[a-z]+/i)
|
212
|
+
.join
|
213
|
+
.strip
|
214
|
+
.empty?
|
215
|
+
).to eq(true)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
describe '#upcase' do
|
220
|
+
context 'with non-destructive version' do
|
221
|
+
it 'does not change the original value of the string' do
|
222
|
+
expect { downcased_turkish_alphabet.upcase }
|
223
|
+
.to_not change { downcased_turkish_alphabet }
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'upcases all of Turkish characters' do
|
25
227
|
upcased_string = downcased_turkish_alphabet.upcase
|
26
|
-
|
228
|
+
|
229
|
+
expect(upcased_string)
|
230
|
+
.to eq(upcased_turkish_alphabet)
|
27
231
|
end
|
28
232
|
|
29
|
-
it
|
233
|
+
it 'upcases English characters except i as I' do
|
30
234
|
upcased_string = downcased_english_alphabet.upcase
|
31
|
-
|
235
|
+
|
236
|
+
expect(upcased_string)
|
237
|
+
.to eq(upcased_english_alphabet.tr('I', 'İ'))
|
32
238
|
end
|
33
239
|
end
|
34
240
|
|
35
|
-
context
|
36
|
-
it
|
37
|
-
expect{ downcased_turkish_alphabet.upcase! }
|
38
|
-
|
241
|
+
context 'with destructive version' do
|
242
|
+
it 'changes the original value of the string' do
|
243
|
+
expect { downcased_turkish_alphabet.upcase! }
|
244
|
+
.to change { downcased_turkish_alphabet }
|
245
|
+
|
246
|
+
expect(downcased_turkish_alphabet)
|
247
|
+
.to eq(upcased_turkish_alphabet)
|
39
248
|
end
|
40
249
|
end
|
41
250
|
end
|
42
251
|
|
43
|
-
describe
|
44
|
-
context
|
45
|
-
it
|
46
|
-
expect{ upcased_turkish_alphabet.downcase }
|
252
|
+
describe '#downcase' do
|
253
|
+
context 'with non-destructive version' do
|
254
|
+
it 'does not change the original value of the string' do
|
255
|
+
expect { upcased_turkish_alphabet.downcase }
|
256
|
+
.to_not change { upcased_turkish_alphabet }
|
47
257
|
end
|
48
258
|
|
49
|
-
it
|
259
|
+
it 'downcases all of Turkish characters' do
|
50
260
|
downcased_string = upcased_turkish_alphabet.downcase
|
51
|
-
|
261
|
+
|
262
|
+
expect(downcased_string)
|
263
|
+
.to eq(downcased_turkish_alphabet)
|
52
264
|
end
|
53
265
|
|
54
|
-
it
|
266
|
+
it 'downcases English characters except I as ı' do
|
55
267
|
downcased_string = upcased_english_alphabet.downcase
|
56
|
-
expect(downcased_string)
|
268
|
+
expect(downcased_string)
|
269
|
+
.to eq(downcased_english_alphabet.tr('i', 'ı'))
|
57
270
|
end
|
58
271
|
end
|
59
272
|
|
60
|
-
context
|
61
|
-
it
|
62
|
-
expect{ upcased_turkish_alphabet.downcase! }
|
63
|
-
|
273
|
+
context 'with destructive version' do
|
274
|
+
it 'changes the original value of the string' do
|
275
|
+
expect { upcased_turkish_alphabet.downcase! }
|
276
|
+
.to change { upcased_turkish_alphabet }
|
277
|
+
expect(upcased_turkish_alphabet)
|
278
|
+
.to eq(downcased_turkish_alphabet)
|
64
279
|
end
|
65
280
|
end
|
66
281
|
end
|
67
282
|
|
68
|
-
describe
|
69
|
-
context
|
70
|
-
it
|
283
|
+
describe '#capitalize' do
|
284
|
+
context 'with non-destructive version' do
|
285
|
+
it 'does not change the original value of the string' do
|
71
286
|
turkish_word = turkish_words.sample
|
72
|
-
|
287
|
+
|
288
|
+
expect { turkish_word.capitalize }
|
289
|
+
.to_not change { turkish_word }
|
73
290
|
end
|
74
291
|
|
75
|
-
it
|
76
|
-
|
77
|
-
|
292
|
+
it 'capitalizes the leading first Turkish character' do
|
293
|
+
# rubocop:disable Style/SymbolProc
|
294
|
+
capitalized_words = turkish_words.map { |w| w.capitalize }
|
295
|
+
|
296
|
+
expect(capitalized_words)
|
297
|
+
.to eq(%w(Çamur Ihlamur İnsan Ördek Şahika Ümraniye))
|
78
298
|
end
|
79
299
|
|
80
|
-
it
|
81
|
-
|
82
|
-
|
300
|
+
it 'capitalizes the first character of a string and downcase others' do
|
301
|
+
expect('türkÇE desteĞİ'.capitalize)
|
302
|
+
.to eq('Türkçe desteği')
|
83
303
|
end
|
84
304
|
|
85
|
-
it
|
86
|
-
english_word =
|
305
|
+
it 'capitalizes the first character of an English string' do
|
306
|
+
english_word = 'spy'
|
87
307
|
capitalized_string = english_word.capitalize
|
88
|
-
|
308
|
+
|
309
|
+
expect(capitalized_string)
|
310
|
+
.to eq('Spy')
|
89
311
|
end
|
90
312
|
end
|
91
313
|
|
92
|
-
context
|
93
|
-
it
|
314
|
+
context 'with destructive version' do
|
315
|
+
it 'changes the original value of the string' do
|
94
316
|
turkish_word = 'çamur'
|
95
|
-
|
96
|
-
expect
|
317
|
+
|
318
|
+
expect { turkish_word.capitalize! }
|
319
|
+
.to change { turkish_word }
|
320
|
+
|
321
|
+
expect(turkish_word)
|
322
|
+
.to eq('Çamur')
|
97
323
|
end
|
98
324
|
end
|
99
325
|
end
|
100
326
|
|
101
|
-
describe
|
102
|
-
it
|
327
|
+
describe '#casecmp' do
|
328
|
+
it 'compares Turkish characters correctly' do
|
103
329
|
result = downcased_turkish_alphabet.casecmp(upcased_turkish_alphabet)
|
104
|
-
|
330
|
+
|
331
|
+
expect(result.zero?)
|
332
|
+
.to eq(true)
|
105
333
|
end
|
106
334
|
|
107
|
-
it
|
108
|
-
|
109
|
-
|
335
|
+
it 'compares Turkish characters correctly' do
|
336
|
+
expect('sıtkı'.casecmp('SıTKI').zero?)
|
337
|
+
.to eq(true)
|
110
338
|
end
|
111
339
|
end
|
112
340
|
|
113
|
-
describe
|
114
|
-
context
|
115
|
-
it
|
116
|
-
word =
|
117
|
-
|
341
|
+
describe '#titleize' do
|
342
|
+
context 'with non-destructive version' do
|
343
|
+
it 'does not change the original value of the string' do
|
344
|
+
word = 'mERHABA çAMUR iSMETOĞULLARI'
|
345
|
+
|
346
|
+
expect { word.titleize }
|
347
|
+
.to_not change { word }
|
118
348
|
end
|
119
349
|
|
120
|
-
it
|
121
|
-
titleized =
|
122
|
-
|
350
|
+
it 'upcases first character of all words' do
|
351
|
+
titleized = 'merhaba çamur ismet'.titleize
|
352
|
+
|
353
|
+
expect(titleized)
|
354
|
+
.to eq('Merhaba Çamur İsmet')
|
123
355
|
end
|
124
356
|
|
125
|
-
it
|
126
|
-
titleized =
|
127
|
-
|
357
|
+
it 'downcases characters other than first characters of all words' do
|
358
|
+
titleized = 'mERHABA çAMUR iSMETOĞULLARI'.titleize
|
359
|
+
|
360
|
+
expect(titleized)
|
361
|
+
.to eq('Merhaba Çamur İsmetoğulları')
|
128
362
|
end
|
129
363
|
|
130
|
-
it
|
364
|
+
it 'support strings that include paranthesis, quotes, etc.' do
|
131
365
|
titleized = "rUBY roCkS... (really! 'tRUSt' ME)".titleize
|
132
|
-
|
366
|
+
|
367
|
+
expect(titleized)
|
368
|
+
.to eq("Ruby Rocks... (Really! 'Trust' Me)")
|
133
369
|
end
|
134
370
|
|
135
|
-
it
|
136
|
-
titleized =
|
137
|
-
|
371
|
+
it 'does not capitalize conjuctions when false passed' do
|
372
|
+
titleized = 'kerem VE aslı VeYa leyla İlE mecnun'.titleize(false)
|
373
|
+
|
374
|
+
expect(titleized)
|
375
|
+
.to eq('Kerem ve Aslı veya Leyla ile Mecnun')
|
138
376
|
end
|
139
377
|
end
|
140
378
|
|
141
|
-
context
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
379
|
+
context 'with destructive version' do
|
380
|
+
it 'changes the original value of the string' do
|
381
|
+
word = 'mERHABA çAMUR iSMETOĞULLARI'
|
382
|
+
|
383
|
+
expect { word.titleize! }
|
384
|
+
.to change { word }
|
385
|
+
|
386
|
+
expect(word)
|
387
|
+
.to eq('Merhaba Çamur İsmetoğulları')
|
147
388
|
end
|
389
|
+
end
|
148
390
|
end
|
149
391
|
|
150
|
-
describe
|
151
|
-
context
|
152
|
-
it
|
153
|
-
word =
|
154
|
-
|
392
|
+
describe '#swapcase' do
|
393
|
+
context 'with non-destructive version' do
|
394
|
+
it 'does not change the original value of the string' do
|
395
|
+
word = 'mErHaba çamur ismetoğullarI'
|
396
|
+
|
397
|
+
expect { word.swapcase }
|
398
|
+
.to_not change { word }
|
155
399
|
end
|
156
400
|
|
157
|
-
it
|
158
|
-
word =
|
159
|
-
|
401
|
+
it 'swaps characters correctly' do
|
402
|
+
word = 'mErHaba çamur ismetoğullarI'.swapcase
|
403
|
+
|
404
|
+
expect(word)
|
405
|
+
.to eq('MeRhABA ÇAMUR İSMETOĞULLARı')
|
160
406
|
end
|
161
407
|
end
|
162
408
|
|
163
|
-
context
|
164
|
-
it
|
165
|
-
word =
|
166
|
-
|
167
|
-
expect
|
409
|
+
context 'with destructive version' do
|
410
|
+
it 'changes the original value of the string' do
|
411
|
+
word = 'mErHaba çamur ismetoğullarI'
|
412
|
+
|
413
|
+
expect { word.swapcase! }
|
414
|
+
.to change { word }
|
415
|
+
expect(word)
|
416
|
+
.to eq('MeRhABA ÇAMUR İSMETOĞULLARı')
|
168
417
|
end
|
169
418
|
end
|
170
419
|
end
|
171
420
|
|
172
|
-
describe
|
421
|
+
describe '#match' do
|
173
422
|
it "matches Turkish characters when regex include '\\w'" do
|
174
|
-
expect('Aşağı'.match(/\w+/).to_s)
|
175
|
-
|
423
|
+
expect('Aşağı'.match(/\w+/).to_s)
|
424
|
+
.to eq('Aşağı')
|
425
|
+
|
426
|
+
expect('Aşağı Ayrancı'.match(/^\w+\s\w+$/).to_s)
|
427
|
+
.to eq('Aşağı Ayrancı')
|
176
428
|
end
|
177
429
|
|
178
|
-
it
|
179
|
-
expect('aüvvağğ öövvaağ'.match(/^[a-z]+\s[a-z]+$/).to_s)
|
430
|
+
it 'matches Turkish characters when regex include lowercase range' do
|
431
|
+
expect('aüvvağğ öövvaağ'.match(/^[a-z]+\s[a-z]+$/).to_s)
|
432
|
+
.to eq('aüvvağğ öövvaağ')
|
180
433
|
end
|
181
434
|
|
182
|
-
it
|
183
|
-
expect('BAĞCIlar'.match(/[A-Z]+/).to_s)
|
435
|
+
it 'matches Turkish characters when regex include uppercase range' do
|
436
|
+
expect('BAĞCIlar'.match(/[A-Z]+/).to_s)
|
437
|
+
.to eq('BAĞCI')
|
184
438
|
end
|
185
439
|
|
186
440
|
it "doesn't match Turkish characters when regex include '\\W'" do
|
187
|
-
expect('Aşağı Ayrancı'
|
441
|
+
expect('Aşağı Ayrancı'
|
442
|
+
.match(/\W+/)
|
443
|
+
.to_s
|
444
|
+
).to eq(' ')
|
188
445
|
end
|
189
446
|
end
|
190
447
|
|
191
|
-
describe
|
448
|
+
describe '#scan' do
|
192
449
|
it "matches Turkish characters when regex include '\\w'" do
|
193
|
-
expect(turkish_words
|
450
|
+
expect(turkish_words
|
451
|
+
.join(' ')
|
452
|
+
.scan(/\w+/)
|
453
|
+
).to eq(turkish_words)
|
194
454
|
end
|
195
455
|
|
196
|
-
it
|
197
|
-
expect(turkish_words
|
456
|
+
it 'matches Turkish characters when regex include lowercase range' do
|
457
|
+
expect(turkish_words
|
458
|
+
.join(' ')
|
459
|
+
.scan(/^[a-z\s]+$/)
|
460
|
+
).to eq([turkish_words.join(' ')])
|
198
461
|
end
|
199
462
|
|
200
|
-
it
|
201
|
-
expect(turkish_words
|
463
|
+
it 'matches Turkish characters when regex include uppercase range' do
|
464
|
+
expect(turkish_words
|
465
|
+
.join(' ')
|
466
|
+
.upcase
|
467
|
+
.scan(/^[A-Z\s]+$/)
|
468
|
+
).to eq([turkish_words.join(' ').upcase])
|
202
469
|
end
|
203
470
|
|
204
471
|
it "matches Turkish characters when regex include '\\w'" do
|
205
|
-
expect(turkish_words
|
472
|
+
expect(turkish_words
|
473
|
+
.join(' ')
|
474
|
+
.scan(/\W+/)
|
475
|
+
.map(&:strip)
|
476
|
+
.all?(&:empty?)
|
477
|
+
).to eq(true)
|
206
478
|
end
|
207
479
|
end
|
208
480
|
|
209
|
-
describe
|
210
|
-
tr_chars =
|
481
|
+
describe '#=~' do
|
482
|
+
tr_chars = ALPHA[:tr_lower] + ALPHA[:tr_upper]
|
211
483
|
|
212
484
|
it "matches Turkish characters when regex include '\\w'" do
|
213
|
-
expect(tr_chars
|
485
|
+
expect(tr_chars
|
486
|
+
.split(//)
|
487
|
+
.all? { |ch| (ch =~ /\w/).zero? }
|
488
|
+
).to eq(true)
|
214
489
|
end
|
215
490
|
|
216
|
-
it
|
217
|
-
expect(
|
491
|
+
it 'matches Turkish characters when regex include lowercase range' do
|
492
|
+
expect(ALPHA[:tr_lower]
|
493
|
+
.split(//)
|
494
|
+
.all? { |ch| (ch =~ /[a-z]/).zero? }
|
495
|
+
).to eq(true)
|
218
496
|
end
|
219
497
|
|
220
|
-
it
|
221
|
-
expect(
|
498
|
+
it 'matches Turkish characters when regex include uppercase range' do
|
499
|
+
expect(ALPHA[:tr_upper]
|
500
|
+
.split(//)
|
501
|
+
.all? { |ch| (ch =~ /[A-Z]/).zero? }
|
502
|
+
).to eq(true)
|
222
503
|
end
|
223
504
|
|
224
505
|
it "doesn't match Turkish characters when regex include '\\W'" do
|
225
|
-
expect(tr_chars
|
506
|
+
expect(tr_chars
|
507
|
+
.split(//)
|
508
|
+
.all? { |ch| (ch =~ /\W/).nil? }
|
509
|
+
).to eq(true)
|
510
|
+
end
|
511
|
+
end
|
512
|
+
|
513
|
+
describe '#sub' do
|
514
|
+
context 'non-destructive version' do
|
515
|
+
it 'does not affect the object' do
|
516
|
+
word = 'ağapaşa ağa'
|
517
|
+
|
518
|
+
expect { word.sub(/\w+/, 'bey') }
|
519
|
+
.to_not change { word }
|
520
|
+
end
|
521
|
+
|
522
|
+
it 'matches Turkish characters, and replaces them' do
|
523
|
+
expect('ağapaşa ağa'.sub(/\w+/, 'bey'))
|
524
|
+
.to eq('bey ağa')
|
525
|
+
|
526
|
+
expect('ağapaşa ağa şapka'.sub(/\W+/, 'bey'))
|
527
|
+
.to eq('ağapaşabeyağa şapka')
|
528
|
+
|
529
|
+
expect('ağapaşaağa'.sub(/[a-h]+/, 'bey'))
|
530
|
+
.to eq('beypaşaağa')
|
531
|
+
end
|
532
|
+
end
|
533
|
+
|
534
|
+
context 'destructive version' do
|
535
|
+
it 'affects the object' do
|
536
|
+
word = 'ağapaşa ağa'
|
537
|
+
|
538
|
+
expect { word.sub!(/\w+/, 'bey') }
|
539
|
+
.to change { word }
|
540
|
+
.from('ağapaşa ağa')
|
541
|
+
.to('bey ağa')
|
542
|
+
end
|
543
|
+
end
|
544
|
+
end
|
545
|
+
|
546
|
+
describe '#gsub' do
|
547
|
+
context 'non-destructive version' do
|
548
|
+
it 'does not affect the object' do
|
549
|
+
word = 'ağapaşa ağa'
|
550
|
+
|
551
|
+
expect { word.gsub(/\w+/, 'bey') }
|
552
|
+
.to_not change { word }
|
553
|
+
end
|
554
|
+
|
555
|
+
it 'matches Turkish characters, and replaces them' do
|
556
|
+
expect('ağapaşa ağa'.gsub(/\w+/, 'bey'))
|
557
|
+
.to eq('bey bey')
|
558
|
+
|
559
|
+
expect('ağapaşa ağa şapka'.gsub(/\W+/, 'bey'))
|
560
|
+
.to eq('ağapaşabeyağabeyşapka')
|
561
|
+
|
562
|
+
expect('ağapaşaağa'.gsub(/[a-h]+/, 'bey'))
|
563
|
+
.to eq('beypbeyşbey')
|
564
|
+
end
|
565
|
+
end
|
566
|
+
|
567
|
+
context 'destructive version' do
|
568
|
+
it 'affects the object' do
|
569
|
+
word = 'ağapaşa ağa'
|
570
|
+
expect { word.gsub!(/\w+/, 'bey') }
|
571
|
+
.to change { word }
|
572
|
+
.from('ağapaşa ağa')
|
573
|
+
.to('bey bey')
|
574
|
+
end
|
226
575
|
end
|
227
576
|
end
|
228
577
|
end
|
229
578
|
|
230
579
|
describe Array do
|
231
|
-
let(:unsorted_array1)
|
232
|
-
|
580
|
+
let(:unsorted_array1) do
|
581
|
+
%w(bağcılar bahçelievler şimdi çüNKÜ olmalı üç\ kere düş ılık duy)
|
582
|
+
end
|
583
|
+
|
584
|
+
let(:sorted_array1) do
|
585
|
+
%w(bağcılar bahçelievler çüNKÜ duy düş ılık olmalı şimdi üç\ kere)
|
586
|
+
end
|
587
|
+
|
233
588
|
let(:unsorted_array2) { %w(iki Üç dört ılık İğne iyne Ul) }
|
234
589
|
let(:sorted_array2) { %w(İğne Ul Üç dört ılık iki iyne) }
|
235
590
|
|
236
|
-
describe
|
237
|
-
context
|
238
|
-
it
|
239
|
-
expect{ unsorted_array1.sort }
|
591
|
+
describe '#sort' do
|
592
|
+
context 'with non-destructive version' do
|
593
|
+
it 'does not change the original value of the array' do
|
594
|
+
expect { unsorted_array1.sort }
|
595
|
+
.to_not change { unsorted_array1 }
|
240
596
|
end
|
241
597
|
|
242
|
-
it
|
243
|
-
expect(unsorted_array1.sort)
|
598
|
+
it 'sorts array in alphabetical order' do
|
599
|
+
expect(unsorted_array1.sort)
|
600
|
+
.to eq(sorted_array1)
|
244
601
|
end
|
245
602
|
|
246
|
-
it
|
247
|
-
expect(unsorted_array2.sort)
|
603
|
+
it 'sorts array in alphabetical order' do
|
604
|
+
expect(unsorted_array2.sort)
|
605
|
+
.to eq(sorted_array2)
|
248
606
|
end
|
249
607
|
end
|
250
608
|
|
251
|
-
context
|
252
|
-
it
|
253
|
-
expect{ unsorted_array1.sort! }
|
254
|
-
|
609
|
+
context 'with destructive version' do
|
610
|
+
it 'changes the original value of the array' do
|
611
|
+
expect { unsorted_array1.sort! }
|
612
|
+
.to change { unsorted_array1 }
|
613
|
+
expect(unsorted_array1)
|
614
|
+
.to eq(sorted_array1)
|
255
615
|
end
|
256
616
|
end
|
257
617
|
end
|