Skip to content

Commit

Permalink
Updated specs in preparation for issue #88.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdchapman committed Nov 23, 2019
1 parent 6649dea commit c7b7caa
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 85 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ items/static/assets/components/
tmp/
.bundle
*.gem
/coverage/
/.yardoc/
coverage
.yardoc
var/company.ldif
2 changes: 1 addition & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-r ./spec/spec_helper.rb
--format Fuubar
--color
--require spec_helper
8 changes: 0 additions & 8 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,6 @@ RSpec/FilePath:
- 'spec/helpers/dictionaries_spec.rb'
- 'spec/helpers/link_to_spec.rb'

# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: implicit, each, example
RSpec/HookArgument:
Exclude:
- 'spec/spec_helper.rb'

# Offense count: 17
# Configuration parameters: IgnoreSharedExamples.
RSpec/NamedSubject:
Expand Down
16 changes: 12 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
require 'rubocop/rake_task'
# frozen_string_literal: true

require 'rake/testtask'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'

RuboCop::RakeTask.new(:rubocop) do |task|
task.formatters = ['simple']
task.fail_on_error = false
end

Rake::TestTask.new(:test_all) do |task|
task.test_files = Dir['test/**/test_*.rb']
task.libs << 'test'
task.verbose = false
end

RSpec::Core::RakeTask.new(:spec) do |task|
task.verbose = false
end

desc 'Run all tests and specs'
task test: [:spec]
task test: %i[spec test_all]

task default: [:test, :rubocop]
task default: %i[test rubocop]
22 changes: 15 additions & 7 deletions spec/helpers/atom_feed_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require 'helpers/atom_feed'

RSpec.describe LifePreserver::AtomFeed, helper: true do
RSpec.describe LifePreserver::Helpers::AtomFeed, helper: true do
before do
allow(ctx.dependency_tracker).to receive(:enter)
allow(ctx.dependency_tracker).to receive(:exit)
Expand All @@ -12,9 +14,10 @@
let(:item_attributes) { {} }

before do
ctx.item = ctx.create_item('Feed', item_attributes, '/feed/')
ctx.create_rep(ctx.item, '/feed.xml')
ctx.create_item('Feed', item_attributes, '/feed')
ctx.create_rep(ctx.items['/feed'], '/feed.xml')

ctx.item = ctx.items['/feed']
ctx.config[:base_url] = base_url
end

Expand All @@ -40,29 +43,30 @@
end

context 'without feed_url' do
it 'returns base_url + path' do
it 'returns base URL + path' do
expect(subject).to eql('http://url.base/feed.xml')
end
end
end
end

describe '#atom_tag_for' do
subject { helper.atom_tag_for(ctx.items['/stuff/']) }
subject { helper.atom_tag_for(ctx.items['/stuff']) }

let(:item_attributes) { { created_at: '2015-05-19 12:34:56' } }
let(:item_rep_path) { '/stuff.xml' }
let(:base_url) { 'http://url.base' }

before do
item = ctx.create_item('Stuff', item_attributes, '/stuff/')
ctx.create_rep(item, item_rep_path)
ctx.create_item('Stuff', item_attributes, '/stuff')
ctx.create_rep(ctx.items['/stuff'], item_rep_path)

ctx.config[:base_url] = base_url
end

context 'item with path' do
let(:item_rep_path) { '/stuff.xml' }

it { is_expected.to eql('tag:url.base,2015-05-19:/stuff.xml') }
end

Expand All @@ -76,25 +80,29 @@

context 'base URL without subdir' do
let(:base_url) { 'http://url.base' }

it { is_expected.to eql('tag:url.base,2015-05-19:/stuff.xml') }
end

context 'base URL with subdir' do
let(:base_url) { 'http://url.base/sub' }

it { is_expected.to eql('tag:url.base,2015-05-19:/sub/stuff.xml') }
end

context 'created_at is date' do
let(:item_attributes) do
{ created_at: Date.parse('2015-05-19 12:34:56') }
end

it { is_expected.to eql('tag:url.base,2015-05-19:/stuff.xml') }
end

context 'created_at is time' do
let(:item_attributes) do
{ created_at: Time.parse('2015-05-19 12:34:56') }
end

it { is_expected.to eql('tag:url.base,2015-05-19:/stuff.xml') }
end

Expand Down
13 changes: 12 additions & 1 deletion spec/helpers/dates_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# frozen_string_literal: true

require 'helpers/dates'

RSpec.describe LifePreserver::Dates, helper: true do
RSpec.describe LifePreserver::Helpers::Dates, helper: true do
before do
allow(ctx.dependency_tracker).to receive(:enter)
allow(ctx.dependency_tracker).to receive(:exit)
end

describe '#attribute_to_time' do
subject { helper.attribute_to_time(arg) }

Expand All @@ -13,21 +20,25 @@

context 'with Time instance' do
let(:arg) { around_noon_utc }

it { is_expected.to eql(around_noon_utc) }
end

context 'with Date instance' do
let(:arg) { Date.new(2015, 11, 7) }

it { is_expected.to eql(beginning_of_day_utc) }
end

context 'with DateTime instance' do
let(:arg) { DateTime.new(2015, 11, 7, 13, 31, 16) }

it { is_expected.to eql(around_noon_utc) }
end

context 'with string' do
let(:arg) { '2015-11-7 13:31:16' }

it { is_expected.to eql(around_noon_local) }
end
end
Expand Down
44 changes: 26 additions & 18 deletions spec/helpers/dictionaries_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require 'helpers/dictionaries'
# frozen_string_literal: true

require 'ffi/hunspell'
require 'helpers/dictionaries'

RSpec.describe LifePreserver::Dictionaries, helper: true, chdir: false do
let(:directories) { ['etc/dictionaries'] }
RSpec.describe LifePreserver::Helpers::Dictionaries, helper: true, chdir: false do
let(:directories) { ['var/dictionaries'] }
let(:default_lang) { 'en_US' }

before do
Expand All @@ -20,7 +22,7 @@

after do
# clear out dictionary cache
LifePreserver::Dictionaries.class_variable_set(:@@dictionaries, {})
LifePreserver::Helpers::Dictionaries.class_variable_set(:@@dictionary_cache, {})
end

describe '.dictionary' do
Expand All @@ -40,48 +42,49 @@
end

context 'with a hunspell language code' do
subject { helper.dictionary(lang).lang }
subject { helper.dictionary(lang) }

context 'with a supported language value' do
let(:lang) { 'en_US' }

it 'selects the given language' do
expect(subject).to eq(lang)
expect(subject.lang).to eq(lang)
end
end

context 'with an unsupported language value' do
let(:lang) { 'zz_YY' }

it 'raises an exception' do
expect { subject }.to raise_error(RuntimeError)
it 'should be nil' do
expect(subject).to be_nil
end
end

context 'with a nil value' do
let(:lang) { nil }

it 'selects the default dictionary language' do
expect(subject).to eq(FFI::Hunspell.lang)
expect(subject.lang).to eq(FFI::Hunspell.lang)
end
end

context 'with a BCP 47 language tag' do
let(:lang) { 'en-Latn-US-x-twain' }

it 'maps to the hunspell value' do
expect(subject).to eq('en_US')
expect(subject.lang).to eq('en_US')
end
end
end

context 'with a non-existent base dictionary' do
let(:lang) { 'en_UK' }

before do
ctx.create_item('content', { kind: 'extra-dictionary' }, '/dir1/en_UK.dic')
Locale.set_app_language_tags('en_US', 'en_GB', 'fr_FR')
ctx.create_item('content', { kind: 'extra-dictionary' }, '/dir3/fr_FR.dic')
end

let(:lang) { 'fr_FR' }

it 'does not accept an extra dictionary as a base dictionary' do
expect { subject }.to raise_error(RuntimeError)
end
Expand Down Expand Up @@ -124,31 +127,36 @@
end
end

describe '.find_closest_lang' do
subject { helper.find_closest_lang(arg).to_s }
describe '.find_simple_locale' do
subject { helper.find_simple_locale(arg).to_s }

context 'using a nil parameter' do
let(:arg) { nil }

it { is_expected.to eq(default_lang) }
end

context 'using an empty string' do
let(:arg) { '' }

it { is_expected.to eq(default_lang) }
end

context 'passing a simple BCP 47 language tag' do
context 'passing a simple BCP47 language tag' do
let(:arg) { 'es' }

it { is_expected.to eq('es_ES') }
end

context 'passing a full BCP 47 tag' do
context 'passing a full BCP47 tag' do
let(:arg) { 'en-GB' }

it { is_expected.to eq('en_GB') }
end

context 'passing an extended BCP 47 tag' do
context 'passing an extended BCP47 tag' do
let(:arg) { 'en-US-x-twain' }

it { is_expected.to eq('en_US') }
end
end
Expand Down
Loading

0 comments on commit c7b7caa

Please sign in to comment.