1
1
# encoding: utf-8
2
+ require 'test_helper'
2
3
3
- require File . dirname ( __FILE__ ) + '/test_helper'
4
+ describe Slug do
5
+ before do
6
+ Article . delete_all
7
+ end
4
8
5
- class SlugTest < ActiveSupport ::TestCase
6
9
describe 'slug' do
7
10
it "bases slug on specified source column" do
8
- Article . delete_all
9
11
article = Article . create! ( :headline => 'Test Headline' )
10
12
assert_equal 'test-headline' , article . slug
11
13
end
12
14
13
15
it "bases slug on specified source column, even if it is defined as a method rather than database attribute" do
14
- Article . delete_all
15
16
article = Event . create! ( :title => 'Test Event' , :location => 'Portland' )
16
17
assert_equal 'test-event-portland' , article . slug
17
18
end
18
19
19
20
describe "slug column" do
20
21
it "saves slug to 'slug' column by default" do
21
- Article . delete_all
22
22
article = Article . create! ( :headline => 'Test Headline' )
23
23
assert_equal 'test-headline' , article . slug
24
24
end
@@ -43,30 +43,41 @@ class SlugTest < ActiveSupport::TestCase
43
43
end
44
44
end
45
45
46
+ describe 'generates a generic slug' do
47
+ before do
48
+ Generation . delete_all
49
+ end
50
+
51
+ it "if source column is empty" do
52
+ generation = Generation . create!
53
+ assert_equal 'generation' , generation . slug
54
+ end
55
+
56
+ it "if normalization makes source value empty" do
57
+ generation = Generation . create! ( :title => '$$$' )
58
+ assert_equal 'generation' , generation . slug
59
+ end
60
+
61
+ it "if source value contains no Latin characters" do
62
+ generation = Generation . create! ( :title => 'ローマ字がない' )
63
+ assert_equal 'generation' , generation . slug
64
+ end
65
+ end
66
+
46
67
describe 'validation' do
47
68
it "sets validation error if source column is empty" do
48
- Article . delete_all
49
69
article = Article . create
50
70
assert !article . valid?
51
71
assert article . errors [ :slug ]
52
72
end
53
73
54
74
it "sets validation error if normalization makes source value empty" do
55
- Article . delete_all
56
75
article = Article . create ( :headline => '$$$' )
57
76
assert !article . valid?
58
77
assert article . errors [ :slug ]
59
78
end
60
79
61
- it "doesn't update the slug even if the source column changes" do
62
- Article . delete_all
63
- article = Article . create! ( :headline => 'Test Headline' )
64
- article . update_attributes! ( :headline => 'New Headline' )
65
- assert_equal 'test-headline' , article . slug
66
- end
67
-
68
80
it "validates slug format on save" do
69
- Article . delete_all
70
81
article = Article . create! ( :headline => 'Test Headline' )
71
82
article . slug = 'A BAD $LUG.'
72
83
@@ -75,7 +86,6 @@ class SlugTest < ActiveSupport::TestCase
75
86
end
76
87
77
88
it "validates uniqueness of slug by default" do
78
- Article . delete_all
79
89
Article . create! ( :headline => 'Test Headline' )
80
90
article2 = Article . create! ( :headline => 'Test Headline' )
81
91
article2 . slug = 'test-headline'
@@ -91,17 +101,21 @@ class SlugTest < ActiveSupport::TestCase
91
101
92
102
assert article2 . valid?
93
103
end
104
+ end
94
105
95
- it "doesn't overwrite slug value on create if it was already specified" do
96
- Article . delete_all
97
- a = Article . create! ( :headline => 'Test Headline' , :slug => 'slug1' )
98
- assert_equal 'slug1' , a . slug
99
- end
106
+ it "doesn't overwrite slug value on create if it was already specified" do
107
+ a = Article . create! ( :headline => 'Test Headline' , :slug => 'slug1' )
108
+ assert_equal 'slug1' , a . slug
109
+ end
110
+
111
+ it "doesn't update the slug even if the source column changes" do
112
+ article = Article . create! ( :headline => 'Test Headline' )
113
+ article . update_attributes! ( :headline => 'New Headline' )
114
+ assert_equal 'test-headline' , article . slug
100
115
end
101
116
102
117
describe "resetting a slug" do
103
118
before do
104
- Article . delete_all
105
119
@article = Article . create ( :headline => 'test headline' )
106
120
@original_slug = @article . slug
107
121
end
@@ -126,123 +140,101 @@ class SlugTest < ActiveSupport::TestCase
126
140
end
127
141
128
142
describe "slug normalization" do
129
- it "lowercases strings" do
130
- Article . delete_all
143
+ before do
131
144
@article = Article . new
145
+ end
146
+
147
+ it "lowercases strings" do
132
148
@article . headline = 'AbC'
133
149
@article . save!
134
150
assert_equal "abc" , @article . slug
135
151
end
136
152
137
153
it "replaces whitespace with dashes" do
138
- Article . delete_all
139
- @article = Article . new
140
154
@article . headline = 'a b'
141
155
@article . save!
142
156
assert_equal 'a-b' , @article . slug
143
157
end
144
158
145
159
it "replaces 2spaces with 1dash" do
146
- Article . delete_all
147
- @article = Article . new
148
160
@article . headline = 'a b'
149
161
@article . save!
150
162
assert_equal 'a-b' , @article . slug
151
163
end
152
164
153
165
it "removes punctuation" do
154
- Article . delete_all
155
- @article = Article . new
156
166
@article . headline = 'abc!@#$%^&*•¶§∞¢££¡¿()><?""\':;][]\.,/'
157
167
@article . save!
158
168
assert_match 'abc' , @article . slug
159
169
end
160
170
161
171
it "strips trailing space" do
162
- Article . delete_all
163
- @article = Article . new
164
172
@article . headline = 'ab '
165
173
@article . save!
166
174
assert_equal 'ab' , @article . slug
167
175
end
168
176
169
177
it "strips leading space" do
170
- Article . delete_all
171
- @article = Article . new
172
178
@article . headline = ' ab'
173
179
@article . save!
174
180
assert_equal 'ab' , @article . slug
175
181
end
176
182
177
183
it "strips trailing dashes" do
178
- Article . delete_all
179
- @article = Article . new
180
184
@article . headline = 'ab-'
181
185
@article . save!
182
186
assert_match 'ab' , @article . slug
183
187
end
184
188
185
189
it "strips leading dashes" do
186
- Article . delete_all
187
- @article = Article . new
188
190
@article . headline = '-ab'
189
191
@article . save!
190
192
assert_match 'ab' , @article . slug
191
193
end
192
194
193
195
it "remove double-dashes" do
194
- Article . delete_all
195
- @article = Article . new
196
196
@article . headline = 'a--b--c'
197
197
@article . save!
198
198
assert_match 'a-b-c' , @article . slug
199
199
end
200
200
201
201
it "doesn't modify valid slug strings" do
202
- Article . delete_all
203
- @article = Article . new
204
202
@article . headline = 'a-b-c-d'
205
203
@article . save!
206
204
assert_match 'a-b-c-d' , @article . slug
207
205
end
208
206
209
207
it "doesn't insert dashes for periods in acronyms, regardless of where they appear in string" do
210
- Article . delete_all
211
- @article = Article . new
212
208
@article . headline = "N.Y.P.D. vs. N.S.A. vs. F.B.I."
213
209
@article . save!
214
210
assert_match 'nypd-vs-nsa-vs-fbi' , @article . slug
215
211
end
216
212
217
213
it "doesn't insert dashes for apostrophes" do
218
- Article . delete_all
219
- @article = Article . new
220
214
@article . headline = "Thomas Jefferson's Papers"
221
215
@article . save!
222
216
assert_match 'thomas-jeffersons-papers' , @article . slug
223
217
end
224
218
225
219
it "preserves numbers in slug" do
226
- Article . delete_all
227
- @article = Article . new
228
220
@article . headline = "2010 Election"
229
221
@article . save!
230
222
assert_match '2010-election' , @article . slug
231
223
end
232
224
end
233
225
234
226
describe "diacritics handling" do
235
- it "strips diacritics" do
236
- Article . delete_all
227
+ before do
237
228
@article = Article . new
229
+ end
230
+
231
+ it "strips diacritics" do
238
232
@article . headline = "açaí"
239
233
@article . save!
240
234
assert_equal "acai" , @article . slug
241
235
end
242
236
243
237
it "strips diacritics correctly " do
244
- Article . delete_all
245
- @article = Article . new
246
238
@article . headline = "ÀÁÂÃÄÅÆÇÈÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ"
247
239
@article . save!
248
240
expected = "aaaaaaaeceeeiiiidnoooooouuuuythssaaaaaaaeceeeeiiiidnoooooouuuuythy" . split ( // )
@@ -255,57 +247,55 @@ class SlugTest < ActiveSupport::TestCase
255
247
256
248
describe "sequence handling" do
257
249
it "doesn't add a sequence if saving first instance of slug" do
258
- Article . delete_all
259
250
article = Article . create! ( :headline => 'Test Headline' )
260
251
assert_equal 'test-headline' , article . slug
261
252
end
262
253
263
254
it "assigns a -1 suffix to the second instance of the slug" do
264
- Article . delete_all
265
255
Article . create! ( :headline => 'Test Headline' )
266
256
article_2 = Article . create! ( :headline => 'Test Headline' )
267
257
assert_equal 'test-headline-1' , article_2 . slug
268
258
end
269
259
270
260
it 'assigns a -2 suffux to the third instance of the slug containing numbers' do
271
- Article . delete_all
272
261
2 . times { |i | Article . create! :headline => '11111' }
273
262
article_3 = Article . create! :headline => '11111'
274
263
assert_equal '11111-2' , article_3 . slug
275
264
end
276
265
266
+ it "assigns a -12 suffix to the thirteenth instance of the slug" do
267
+ 12 . times { |i | Article . create! ( :headline => 'Test Headline' ) }
268
+ article_13 = Article . create! ( :headline => 'Test Headline' )
269
+ assert_equal 'test-headline-12' , article_13 . slug
270
+
271
+ 12 . times { |i | Article . create! ( :headline => 'latest from lybia' ) }
272
+ article_13 = Article . create! ( :headline => 'latest from lybia' )
273
+ assert_equal 'latest-from-lybia-12' , article_13 . slug
274
+ end
275
+
276
+ it "ignores partial matches when calculating sequence" do
277
+ article_1 = Article . create! ( :headline => 'Test Headline' )
278
+ assert_equal 'test-headline' , article_1 . slug
279
+ article_2 = Article . create! ( :headline => 'Test' )
280
+ assert_equal 'test' , article_2 . slug
281
+ article_3 = Article . create! ( :headline => 'Test' )
282
+ assert_equal 'test-1' , article_3 . slug
283
+ article_4 = Article . create! ( :headline => 'Test' )
284
+ assert_equal 'test-2' , article_4 . slug
285
+ end
286
+
277
287
it "knows about single table inheritance" do
278
- Article . delete_all
279
288
article = Article . create! ( :headline => 'Test Headline' )
280
289
story = Storyline . create! ( :headline => article . headline )
281
290
assert_equal 'test-headline-1' , story . slug
282
291
end
283
292
284
293
it "correctly slugs for partial matches" do
285
- Article . delete_all
286
294
rap_metal = Article . create! ( :headline => 'Rap Metal' )
287
295
assert_equal 'rap-metal' , rap_metal . slug
288
296
289
297
rap = Article . create! ( :headline => 'Rap' )
290
298
assert_equal ( 'rap' , rap . slug )
291
299
end
292
-
293
- it "assigns a -12 suffix to the thirteenth instance of the slug" do
294
- Article . delete_all
295
- 12 . times { |i | Article . create! ( :headline => 'Test Headline' ) }
296
- article_13 = Article . create! ( :headline => 'Test Headline' )
297
- assert_equal 'test-headline-12' , article_13 . slug
298
-
299
- 12 . times { |i | Article . create! ( :headline => 'latest from lybia' ) }
300
- article_13 = Article . create! ( :headline => 'latest from lybia' )
301
- assert_equal 'latest-from-lybia-12' , article_13 . slug
302
- end
303
-
304
- it 'assigns a -2 suffux to the third instance of the slug containing numbers' do
305
- Article . delete_all
306
- 2 . times { |i | Article . create! :headline => '11111' }
307
- article_3 = Article . create! :headline => '11111'
308
- assert_equal '11111-2' , article_3 . slug
309
- end
310
300
end
311
301
end
0 commit comments