100% coverage for the liquid filters

did committed Feb 03, 2015
commit d2a83e8578cdb3201dc72d7ef6537fc0c8f44b4d
Showing 7 changed files with 65 additions and 22 deletions
Gemfile +0 -7
@@ @@ -14,19 +14,12 @@ group :test do
gem 'rspec', '~> 3.1.0'
gem 'json_spec', '~> 1.1.4'
gem 'i18n-spec', '~> 0.6.0'
- # gem 'mocha', require: false
gem 'pry'
gem 'codeclimate-test-reporter', require: false
- # gem 'coveralls', require: false
- # gem 'simplecov', require: false
end
- # platform :jruby do
- # ruby '1.9.3', engine: 'jruby', engine_version: '1.7.11'
- # end
-
platform :ruby do
ruby '2.1.3'
end
locomotive/steam/liquid/filters/date.rb b/lib/locomotive/steam/liquid/filters/date.rb +7 -7
@@ @@ -34,8 +34,8 @@ module Locomotive
return '' if input.blank?
# make sure we deal with instances of Time
- to_time = to_time!(input)
- from_time = to_time!(from_time)
+ to_time = convert_to_time!(input)
+ from_time = convert_to_time!(from_time)
::I18n.with_options(scope: :'datetime.distance_in_words') do |locale|
_distance_of_time_in_words(locale, from_time, to_time, include_seconds)
@@ @@ -70,17 +70,17 @@ module Locomotive
private
- def to_time(input)
+ def convert_to_time(input)
case input
- when Date then input.to_time
- when String then Time.zone.parse(input)
+ when ::Date then input.to_time
+ when ::String then Time.zone.parse(input)
else
input
end
end
- def to_time!(input)
- to_time(input).to_time
+ def convert_to_time!(input)
+ convert_to_time(input).to_time
end
def _distance_of_time_in_words(locale, from_time, to_time, include_seconds = false)
spec/spec_helper.rb +0 -4
@@ @@ -15,10 +15,6 @@ SimpleCov.start do
add_group "Services", "lib/locomotive/steam/services"
end
- # disabled for now
- # require 'coveralls'
- # Coveralls.wear!
-
require 'rubygems'
require 'bundler/setup'
spec/unit/liquid/filters/date_spec.rb +29 -0
@@ @@ -85,6 +85,27 @@ describe Locomotive::Steam::Liquid::Filters::Date do
expect(distance_of_time_in_words(date)).to eq('over 5 years')
end
+ it 'prints the distance of time in words from a time' do
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/06/29 00:00:01', true)).to eq('less than 5 seconds')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/06/29 00:00:05', true)).to eq('less than 10 seconds')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/06/29 00:00:10', true)).to eq('less than 20 seconds')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/06/29 00:00:20', true)).to eq('half a minute')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/06/29 00:00:30', true)).to eq('half a minute')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/06/29 00:00:40', true)).to eq('less than a minute')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/06/29 00:01:00', true)).to eq('1 minute')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/06/29 00:00:01')).to eq('less than a minute')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/06/29 00:02:00')).to eq('2 minutes')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/06/29 00:45:00')).to eq('about 1 hour')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/06/29 01:32:00')).to eq('about 2 hours')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/06/30 00:00:00')).to eq('1 day')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/07/01 00:00:00')).to eq('2 days')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/08/01 00:00:00')).to eq('about 1 month')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2007/10/01 00:00:00')).to eq('3 months')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2008/06/29 00:00:00')).to eq('about 1 year')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2008/09/29 00:00:00')).to eq('over 1 year')
+ expect(distance_of_time_in_words('2007/06/29 00:00:00', '2009/03/29 00:00:00')).to eq('almost 2 years')
+ end
+
it 'prints the distance of time in words with a different from_time variable' do
expect(distance_of_time_in_words(date, '2010/11/25 00:00:00')).to eq('over 3 years')
end
@@ @@ -98,6 +119,14 @@ describe Locomotive::Steam::Liquid::Filters::Date do
expect(localized_date('')).to eq('')
end
+ it 'prints a date from a string' do
+ expect(localized_date('2007-06-29')).to eq('2007-06-29')
+ end
+
+ it 'prints a date from a not-formated string' do
+ expect(localized_date('29/06/2007')).to eq('2007-06-29')
+ end
+
it 'prints a date' do
expect(localized_date(date)).to eq('2007-06-29')
end
spec/unit/liquid/filters/misc_spec.rb +10 -0
@@ @@ -21,6 +21,16 @@ describe Locomotive::Steam::Liquid::Filters::Misc do
expect(default(nil, 42)).to eq 42
end
+ describe 'index' do
+
+ let(:array) { [1, 2, 3, 4] }
+ let(:position) { 2 }
+ subject { index(array, position) }
+
+ it { is_expected.to eq 3 }
+
+ end
+
describe 'split' do
let(:string) { nil }
spec/unit/liquid/filters/text_spec.rb +15 -0
@@ @@ -37,5 +37,20 @@ describe Locomotive::Steam::Liquid::Filters::Text do
expect(concat('hello', 'foo', 'bar')).to eq 'hellofoobar'
end
+ it 'encodes an input' do
+ expect(encode('http:://www.example.com?key=hello world')).to eq 'http%3A%3A%2F%2Fwww.example.com%3Fkey%3Dhello+world'
+ end
+
+ it 'replaces \n by <br/>' do
+ expect(multi_line("hello\nworld")).to eq 'hello<br/>world'
+ end
+
+ it 'right justifies and padds a string' do
+ expect(rjust('42', 4, '.')).to eq '..42'
+ end
+
+ it 'left justifies and padds a string' do
+ expect(ljust('42', 4, '.')).to eq '42..'
+ end
end
spec/unit/liquid/tags/snippet_spec.rb +4 -4
@@ @@ -18,13 +18,13 @@ describe Locomotive::Steam::Liquid::Tags::Snippet do
it { expect(listener.event_names.first).to eq :include }
- describe 'with an editable_element inside', pending: true do
+ # describe 'with an editable_element inside', pending: true do
- let(:snippet) { instance_double('Snippet', source: '{% editable_text company %}built by NoCoffee{% endeditable_text %}') }
+ # let(:snippet) { instance_double('Snippet', source: '{% editable_text company %}built by NoCoffee{% endeditable_text %}') }
- it { expect(listener.events.size).to eq 2 }
+ # it { expect(listener.events.size).to eq 2 }
- end
+ # end
end