Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added text normalization specs, truncate_title does not add page titl…
…e when site title is too long
- Loading branch information
Showing
3 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'spec_helper' | ||
|
||
describe MetaTags::TextNormalizer, '.normalize_title' do | ||
context 'when site_title is blank' do | ||
it 'should return title when site_title is blank' do | ||
expect(subject.normalize_title(nil, 'title', '-')).to eq('title') | ||
expect(subject.normalize_title('', 'title', '-')).to eq('title') | ||
end | ||
|
||
it 'should join title parts with separator' do | ||
expect(subject.normalize_title('', %w[title subtitle], '-')).to eq('title-subtitle') | ||
end | ||
|
||
it 'should reverse title parts when reverse is true' do | ||
expect(subject.normalize_title('', %w[title subtitle], '-', true)).to eq('subtitle-title') | ||
end | ||
end | ||
|
||
context 'when site_title is specified' do | ||
it 'should join title and site_title with separator' do | ||
expect(subject.normalize_title('site', 'title', '-')).to eq('site-title') | ||
end | ||
|
||
it 'should join title parts and site_title with separator' do | ||
expect(subject.normalize_title('site', %w[title subtitle], '-')).to eq('site-title-subtitle') | ||
end | ||
|
||
it 'should reverse title parts when reverse is true' do | ||
expect(subject.normalize_title('site', %w[title subtitle], '-', true)).to eq('subtitle-title-site') | ||
end | ||
|
||
it 'should not add title when site title is longer than limit' do | ||
site_title = 'a' * (MetaTags.config.title_limit - 2) | ||
expect(subject.normalize_title(site_title, 'title', '---')).to eq(site_title[0..-2]) | ||
end | ||
|
||
it 'should truncate title when limit is reached' do | ||
site_title = 'a' * 20 | ||
title = 'b' * (MetaTags.config.title_limit + 10) | ||
expect(subject.normalize_title(site_title, title, '-')).to eq("#{site_title}-#{'b' * (MetaTags.config.title_limit - 21)}") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'spec_helper' | ||
|
||
describe MetaTags::TextNormalizer, '.truncate_array' do | ||
it 'should return array as is when limit is not specified' do | ||
arr = %w[a] | ||
expect(subject.truncate_array(arr, nil)).to be(arr) | ||
expect(subject.truncate_array(arr, 0)).to be(arr) | ||
end | ||
|
||
it 'should return a new array when limit is specified' do | ||
arr = %w[a] | ||
expect(subject.truncate_array(arr, 1)).to_not be(arr) | ||
end | ||
|
||
context 'when separator is empty string' do | ||
it 'should return the whole array when total size is less than or equal to limit' do | ||
arr = %w[a a] | ||
expect(subject.truncate_array(arr, 5)).to eq(arr) | ||
expect(subject.truncate_array(arr, 2)).to eq(arr) | ||
end | ||
|
||
it 'should truncate array to specified limit' do | ||
arr = %w[a a a a a] | ||
expect(subject.truncate_array(arr, 3)).to eq(%w[a a a]) | ||
end | ||
|
||
it 'should truncate last word to match the limit' do | ||
arr = %w[a a aaaa aa] | ||
expect(subject.truncate_array(arr, 4)).to eq(%w[a a aa]) | ||
end | ||
|
||
it 'should use natural separator when truncating a long word' do | ||
arr = ['a', 'aa aaaa', 'aa'] | ||
expect(subject.truncate_array(arr, 7)).to eq(%w[a aa]) | ||
end | ||
end | ||
|
||
context 'when separator is specified' do | ||
it 'should return the whole array when total size is less than or equal to limit' do | ||
arr = %w[a a] | ||
expect(subject.truncate_array(arr, 5, '-')).to eq(arr) | ||
expect(subject.truncate_array(arr, 3, '-')).to eq(arr) | ||
end | ||
|
||
it 'should truncate array to specified limit' do | ||
arr = %w[a a a a a] | ||
expect(subject.truncate_array(arr, 3, '-')).to eq(%w[a a]) | ||
end | ||
|
||
it 'should truncate last word to match the limit' do | ||
arr = %w[a a aaaa aa] | ||
expect(subject.truncate_array(arr, 5, '-')).to eq(%w[a a a]) | ||
end | ||
|
||
it 'should use natural separator when truncating a long word' do | ||
arr = ['a', 'aa aaaa', 'aa'] | ||
expect(subject.truncate_array(arr, 7, '-')).to eq(%w[a aa]) | ||
end | ||
end | ||
end |