From 9682e89e7febb45bf20148c66f523c9eb39c2176 Mon Sep 17 00:00:00 2001 From: Matthias Viehweger Date: Tue, 26 Mar 2019 12:41:58 +0100 Subject: [PATCH] Repair selecting one date for display/upload --- gpuzzletime.gemspec | 2 ++ lib/gpuzzletime/app.rb | 8 ++++---- spec/gpuzzletime/app_spec.rb | 37 +++++++++++++++++++++++++++++++----- spec/spec_helper.rb | 1 + 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/gpuzzletime.gemspec b/gpuzzletime.gemspec index f66ea2d..db41380 100644 --- a/gpuzzletime.gemspec +++ b/gpuzzletime.gemspec @@ -22,7 +22,9 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'bundler' spec.add_development_dependency 'overcommit', '~> 0.45' + spec.add_development_dependency 'pry', '~> 0.12' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rspec', '~> 3.0' spec.add_development_dependency 'rubocop', '~> 0.50' + spec.add_development_dependency 'timecop', '~> 0.9' end diff --git a/lib/gpuzzletime/app.rb b/lib/gpuzzletime/app.rb index c719472..170c48e 100644 --- a/lib/gpuzzletime/app.rb +++ b/lib/gpuzzletime/app.rb @@ -132,10 +132,10 @@ def url_options(start, entry) def named_dates(date) case date - when 'yesterday' then Date.today.prev_day.to_s - when 'today' then Date.today.to_s - when 'last' then parse(read).to_h.keys.compact.sort[-2] || Date.today.prev_day.to_s - when /(\d{2}-){2}-\d{4}/ then date + when 'yesterday' then Date.today.prev_day.to_s + when 'today' then Date.today.to_s + when 'last' then parse(read).to_h.keys.compact.sort[-2] || Date.today.prev_day.to_s + when /\d{4}(-\d{2}){2}/ then date end end diff --git a/spec/gpuzzletime/app_spec.rb b/spec/gpuzzletime/app_spec.rb index 2935b2f..d256770 100644 --- a/spec/gpuzzletime/app_spec.rb +++ b/spec/gpuzzletime/app_spec.rb @@ -9,7 +9,7 @@ let(:argument) { 'all' } let(:timelog) do <<~TIMELOG - 2018-03-02 09:51: start + 2018-03-02 09:51: start ** 2018-03-02 11:40: 12345: prepare deployment -- webapp 2018-03-02 12:25: lunch ** 2018-03-02 13:15: 12345: prepare deployment -- webapp @@ -19,11 +19,13 @@ 2018-03-02 18:58: break ** 2018-03-02 20:08: 12345: prepare deployment -- webapp - 2018-03-03 14:00: start + 2018-03-03 14:00: start ** 2018-03-03 15:34: 23456: debug -- network 2018-03-03 18:46: studying 2018-03-03 20:08: dinner ** 2018-03-03 21:36: 12345: prepare deployment -- webapp + + 2018-03-05 09:00: start ** TIMELOG end @@ -52,9 +54,34 @@ expect { subject.run }.not_to output(/break/).to_stdout end - # it 'knows today by name' - # it 'knows yesterday by name' - # it 'knows the last day by name' + it 'knows today by name' do + today = '2018-03-03' + + Timecop.travel(today) do + expect(subject.send(:named_dates, 'today')).to eq(today) + end + end + + it 'knows yesterday by name' do + today = '2018-03-03' + yesterday = '2018-03-02' + + Timecop.travel(today) do + expect(subject.send(:named_dates, 'yesterday')).to eq(yesterday) + end + end + + it 'knows the last day by name' do + expect(subject).to receive(:read).at_least(:once).and_return(timelog) + last_day = '2018-03-03' # dependent on test-data of timelog above + + expect(subject.send(:named_dates, 'last')).to eq(last_day) + end + + it 'understands and accepts dates in YYYY-MM-DD format' do + date = '1970-01-01' + expect(subject.send(:named_dates, date)).to be date + end # it 'defaults to "last day"' # it 'can show parsed entries' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bc844d2..7e8b1da 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,3 +3,4 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) require 'gpuzzletime' +require 'timecop'