2013-02-05 13:16:51 -06:00
# encoding: utf-8
2015-10-11 04:41:23 -05:00
require 'rails_helper'
2013-02-05 13:16:51 -06:00
require 'slug'
describe Slug do
2015-04-13 09:50:41 -05:00
describe '#for' do
context 'ascii generator' do
before { SiteSetting . slug_generation_method = 'ascii' }
it 'generates the slug' do
2015-05-04 06:48:37 -05:00
expect ( Slug . for ( " hello world " ) ) . to eq ( 'hello-world' )
2015-04-13 09:50:41 -05:00
end
it 'generates default slug when nothing' do
2015-05-04 06:48:37 -05:00
expect ( Slug . for ( '' ) ) . to eq ( 'topic' )
2015-04-13 09:50:41 -05:00
end
it " doesn't generate slugs that are just numbers " do
2015-05-04 06:48:37 -05:00
expect ( Slug . for ( '123' ) ) . to eq ( 'topic' )
2015-04-13 09:50:41 -05:00
end
end
context 'encoded generator' do
before { SiteSetting . slug_generation_method = 'encoded' }
after { SiteSetting . slug_generation_method = 'ascii' }
it 'generates the slug' do
2015-05-04 06:48:37 -05:00
expect ( Slug . for ( " 熱帶風暴畫眉 " ) ) . to eq ( '熱帶風暴畫眉' )
2015-04-13 09:50:41 -05:00
end
it 'generates default slug when nothing' do
2015-05-04 06:48:37 -05:00
expect ( Slug . for ( '' ) ) . to eq ( 'topic' )
2015-04-13 09:50:41 -05:00
end
it " doesn't generate slugs that are just numbers " do
2015-05-04 06:48:37 -05:00
expect ( Slug . for ( '123' ) ) . to eq ( 'topic' )
2015-04-13 09:50:41 -05:00
end
end
context 'none generator' do
before { SiteSetting . slug_generation_method = 'none' }
after { SiteSetting . slug_generation_method = 'ascii' }
it 'generates the slug' do
expect ( Slug . for ( " hello world " , 'category' ) ) . to eq ( 'category' )
2015-05-04 06:48:37 -05:00
expect ( Slug . for ( " hello world " ) ) . to eq ( 'topic' )
expect ( Slug . for ( '' ) ) . to eq ( 'topic' )
expect ( Slug . for ( '123' ) ) . to eq ( 'topic' )
2015-04-13 09:50:41 -05:00
end
end
2013-02-05 13:16:51 -06:00
end
2015-04-13 09:50:41 -05:00
describe '#ascii_generator' do
before { SiteSetting . slug_generation_method = 'ascii' }
2013-02-05 13:16:51 -06:00
2015-04-13 09:50:41 -05:00
it 'replaces spaces with hyphens' do
expect ( Slug . ascii_generator ( " hello world " ) ) . to eq ( 'hello-world' )
end
2013-02-05 13:16:51 -06:00
2015-04-13 09:50:41 -05:00
it 'changes accented characters' do
expect ( Slug . ascii_generator ( 'àllo' ) ) . to eq ( 'allo' )
end
2013-02-05 13:16:51 -06:00
2015-04-13 09:50:41 -05:00
it 'replaces symbols' do
expect ( Slug . ascii_generator ( 'evil#trout' ) ) . to eq ( 'evil-trout' )
end
2013-02-05 13:16:51 -06:00
2015-04-13 09:50:41 -05:00
it 'handles a.b.c properly' do
expect ( Slug . ascii_generator ( " a.b.c " ) ) . to eq ( " a-b-c " )
end
2013-02-06 19:52:14 -06:00
2015-04-13 09:50:41 -05:00
it 'handles double dots right' do
expect ( Slug . ascii_generator ( " a....b.....c " ) ) . to eq ( " a-b-c " )
end
2013-02-05 13:16:51 -06:00
2015-04-13 09:50:41 -05:00
it 'strips trailing punctuation' do
expect ( Slug . ascii_generator ( " hello... " ) ) . to eq ( " hello " )
end
2013-02-11 20:34:38 -06:00
2015-04-13 09:50:41 -05:00
it 'strips leading punctuation' do
expect ( Slug . ascii_generator ( " ...hello " ) ) . to eq ( " hello " )
end
2013-02-14 16:46:11 -06:00
2015-04-13 09:50:41 -05:00
it 'handles our initial transliteration' do
from = " àáäâčďèéëěêìíïîľĺňòóöôŕřšťůùúüûýžñç "
to = " aaaacdeeeeeiiiillnoooorrstuuuuuyznc "
expect ( Slug . ascii_generator ( from ) ) . to eq ( to )
end
2013-05-30 10:54:02 -05:00
2015-04-13 09:50:41 -05:00
it 'replaces underscores' do
expect ( Slug . ascii_generator ( " o_o_o " ) ) . to eq ( " o-o-o " )
end
it " doesn't keep single quotes within word " do
expect ( Slug . ascii_generator ( " Jeff hate's this " ) ) . to eq ( " jeff-hates-this " )
end
it 'generates null when nothing' do
expect ( Slug . ascii_generator ( '' ) ) . to eq ( '' )
end
2013-05-30 10:54:02 -05:00
2015-04-13 09:50:41 -05:00
it " keeps number unchanged " do
expect ( Slug . ascii_generator ( '123' ) ) . to eq ( '123' )
end
2013-06-02 18:08:34 -05:00
end
2015-04-13 09:50:41 -05:00
describe '#encoded_generator' do
before { SiteSetting . slug_generation_method = 'encoded' }
after { SiteSetting . slug_generation_method = 'ascii' }
it 'generates precentage encoded string' do
2015-05-04 06:48:37 -05:00
expect ( Slug . encoded_generator ( " Jeff hate's !~-_|,= # this " ) ) . to eq ( " Jeff-hates-this " )
expect ( Slug . encoded_generator ( " 뉴스피드 " ) ) . to eq ( " 뉴스피드 " )
expect ( Slug . encoded_generator ( " آموزش اضافه کردن لینک اختیاری به هدر " ) ) . to eq ( " آموزش-اضافه-کردن-لینک-اختیاری-به-هدر " )
expect ( Slug . encoded_generator ( " 熱帶風暴畫眉 " ) ) . to eq ( " 熱帶風暴畫眉 " )
2015-04-13 09:50:41 -05:00
end
it 'reject RFC 3986 reserved character and blank' do
2015-05-04 06:48:37 -05:00
expect ( Slug . encoded_generator ( " :/? # []@!$ &'()*+,;=% -_`~. " ) ) . to eq ( " " )
expect ( Slug . encoded_generator ( " - English and Chinese title with special characters / 中文标题 !@:? \\ :'` # ^& $%&*()` -- " ) ) . to eq ( " English-and-Chinese-title-with-special-characters-中文标题 " )
2015-04-13 09:50:41 -05:00
end
it 'generates null when nothing' do
expect ( Slug . encoded_generator ( '' ) ) . to eq ( '' )
end
it " keeps number unchanged " do
expect ( Slug . encoded_generator ( '123' ) ) . to eq ( '123' )
end
2014-09-16 06:15:05 -05:00
end
2015-04-13 09:50:41 -05:00
describe '#none_generator' do
before { SiteSetting . slug_generation_method = 'none' }
after { SiteSetting . slug_generation_method = 'ascii' }
it 'generates nothing' do
expect ( Slug . none_generator ( " Jeff hate's this " ) ) . to eq ( '' )
expect ( Slug . none_generator ( nil ) ) . to eq ( '' )
expect ( Slug . none_generator ( '' ) ) . to eq ( '' )
expect ( Slug . none_generator ( '31' ) ) . to eq ( '' )
end
end
2013-02-05 13:16:51 -06:00
end