From ed84752a018eaa438654a0273eda6da3198b92fb Mon Sep 17 00:00:00 2001 From: arizz Date: Tue, 8 Mar 2016 10:10:01 +0100 Subject: [PATCH 01/13] fixed bug on date --- spec/lib/strict_period_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/strict_period_spec.rb b/spec/lib/strict_period_spec.rb index 27a671a..ce2d1ab 100644 --- a/spec/lib/strict_period_spec.rb +++ b/spec/lib/strict_period_spec.rb @@ -15,7 +15,7 @@ it "accepts anchor date formatted as '%Y-%m-%d' string" do expect(strict_period.anchor).to be_truthy expect(strict_period.anchor).to be_an_instance_of Time - expect(strict_period.anchor.strftime('%Y-%m-%d')).to eq now.strftime('%Y-%m-%d') + expect(strict_period.anchor.strftime('%Y-%m-%d')).to eq Time.utc(2016,3,7).strftime('%Y-%m-%d') end end From b1b99a26254215982457efc22f7f65de15511ae7 Mon Sep 17 00:00:00 2001 From: arizz Date: Tue, 8 Mar 2016 10:11:52 +0100 Subject: [PATCH 02/13] added spec for previous_weeks --- spec/lib/strict_period_spec.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/spec/lib/strict_period_spec.rb b/spec/lib/strict_period_spec.rb index ce2d1ab..180c43c 100644 --- a/spec/lib/strict_period_spec.rb +++ b/spec/lib/strict_period_spec.rb @@ -74,7 +74,25 @@ end end - describe "#previous_weeks", pending => true do + describe "#previous_weeks" do + let(:anchor) { Time.utc(2015,3,7).strftime('%Y-%m-%d') } + let(:number_of_weeks) { 2 } + + context "when there isn't specified number_of_weeks" do + it { expect(strict_period.previous_weeks).to be_kind_of(Array) } + it { expect(strict_period.previous_weeks).to eq [["2015-02-23", "2015-03-01"]] } + it { expect(Time.parse(strict_period.previous_weeks[0][0]).monday?).to eq true } + it { expect(Time.parse(strict_period.previous_weeks[0][1]).sunday?).to eq true } + end + + context "when there's specified number_of_weeks" do + it { expect(strict_period.previous_weeks).to be_kind_of(Array) } + it { expect(strict_period.previous_weeks(number_of_weeks)).to eq [["2015-02-16", "2015-02-22"],["2015-02-23", "2015-03-01"]] } + it { expect(Time.parse(strict_period.previous_weeks(number_of_weeks)[0][0]).monday?).to eq true } + it { expect(Time.parse(strict_period.previous_weeks(number_of_weeks)[0][1]).sunday?).to eq true } + it { expect(Time.parse(strict_period.previous_weeks(number_of_weeks)[1][0]).monday?).to eq true } + it { expect(Time.parse(strict_period.previous_weeks(number_of_weeks)[1][1]).sunday?).to eq true } + end end describe "#next_weeks", pending => true do From 2c2fc44444aa04d0e87ed5bd5a41a9988e0b6f8f Mon Sep 17 00:00:00 2001 From: arizz Date: Tue, 8 Mar 2016 10:12:26 +0100 Subject: [PATCH 03/13] added spec for next_weeks --- spec/lib/strict_period_spec.rb | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/spec/lib/strict_period_spec.rb b/spec/lib/strict_period_spec.rb index 180c43c..f54569c 100644 --- a/spec/lib/strict_period_spec.rb +++ b/spec/lib/strict_period_spec.rb @@ -95,6 +95,31 @@ end end - describe "#next_weeks", pending => true do + describe "#next_weeks" do + context "when the actual next weeks is in the future" do + it { expect(strict_period.next_weeks).to be_kind_of(Array) } + it { expect(strict_period.next_weeks).to eq [] } + end + + context "when the actual next weeks is in the past" do + let(:anchor) { Time.utc(2015,3,7).strftime('%Y-%m-%d') } + let(:number_of_weeks) { 2 } + + context "when there isn't specified number_of_weeks" do + it { expect(strict_period.next_weeks).to be_kind_of(Array) } + it { expect(strict_period.next_weeks).to eq [["2015-03-09", "2015-03-15"]] } + it { expect(Time.parse(strict_period.next_weeks[0][0]).monday?).to eq true } + it { expect(Time.parse(strict_period.next_weeks[0][1]).sunday?).to eq true } + end + + context "when there's specified number_of_weeks" do + it { expect(strict_period.next_weeks(number_of_weeks)).to be_kind_of(Array) } + it { expect(strict_period.next_weeks(number_of_weeks)).to eq [["2015-03-09", "2015-03-15"],["2015-03-16", "2015-03-22"]] } + it { expect(Time.parse(strict_period.next_weeks(number_of_weeks)[0][0]).monday?).to eq true } + it { expect(Time.parse(strict_period.next_weeks(number_of_weeks)[0][1]).sunday?).to eq true } + it { expect(Time.parse(strict_period.next_weeks(number_of_weeks)[1][0]).monday?).to eq true } + it { expect(Time.parse(strict_period.next_weeks(number_of_weeks)[1][1]).sunday?).to eq true } + end + end end end From e9c16767c6b1e49320ad437eb85db3dd4a7da8c2 Mon Sep 17 00:00:00 2001 From: arizz Date: Tue, 8 Mar 2016 10:19:56 +0100 Subject: [PATCH 04/13] added spec for past_only param --- spec/lib/strict_period_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/lib/strict_period_spec.rb b/spec/lib/strict_period_spec.rb index f54569c..cf55541 100644 --- a/spec/lib/strict_period_spec.rb +++ b/spec/lib/strict_period_spec.rb @@ -32,6 +32,15 @@ it { expect{ strict_period }.to raise_error ArgumentError } end + + context "when past_only is not provided" do + it { expect(strict_period.past_only).to eq true } + end + + context "when past_only is provided" do + let(:strict_period) { described_class.new(anchor: anchor, past_only: false) } + it { expect(strict_period.past_only).to eq false } + end end describe "#_anchor_at" do From fa9152bc901a8b13200b8d0b7f8a57846169060e Mon Sep 17 00:00:00 2001 From: arizz Date: Tue, 8 Mar 2016 14:14:20 +0100 Subject: [PATCH 05/13] added exception when using with wrong params --- lib/strict_periods/period_pickers/week_picker.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/strict_periods/period_pickers/week_picker.rb b/lib/strict_periods/period_pickers/week_picker.rb index a720534..1ad3bb6 100644 --- a/lib/strict_periods/period_pickers/week_picker.rb +++ b/lib/strict_periods/period_pickers/week_picker.rb @@ -11,7 +11,7 @@ def initialize(anchor: nil, past_only: true) @steps = 0 @anchor = anchor @past_only = past_only - raise ArgumentError if @anchor.nil? + raise ArgumentError if @anchor.nil? || !@anchor.is_a?(Time) _offset_anchor end From 1781bd5fa3d802fca83895c139df415324bf868f Mon Sep 17 00:00:00 2001 From: arizz Date: Tue, 8 Mar 2016 14:15:00 +0100 Subject: [PATCH 06/13] added spec for initializer of week picker --- spec/lib/period_pickers/week_picker_spec.rb | 32 ++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/spec/lib/period_pickers/week_picker_spec.rb b/spec/lib/period_pickers/week_picker_spec.rb index 7a99dc0..5d2347e 100644 --- a/spec/lib/period_pickers/week_picker_spec.rb +++ b/spec/lib/period_pickers/week_picker_spec.rb @@ -3,6 +3,36 @@ require 'time' describe StrictPeriods::PeriodPickers::WeekPicker do - describe "#new", pending => true do + + let(:anchor) { Time.utc(2016,3,7) } + let(:week_picker) { StrictPeriods::PeriodPickers::WeekPicker.new(anchor: anchor) } + + describe "#new" do + context "when a valid anchor date is provided" do + it "accepts anchor date formatted as '%Y-%m-%d' string" do + expect(week_picker.anchor).to be_truthy + expect(week_picker.anchor).to be_an_instance_of Time + expect(week_picker.anchor).to eq Time.utc(2016,3,7) + end + end + + context "when no anchor is provided at all" do + let(:anchor) { nil } + it { expect{ week_picker }.to raise_error ArgumentError } + end + + context "when an invalid anchor is provided" do + let(:anchor) { "INVALID" } + it { expect{ week_picker }.to raise_error ArgumentError } + end + + context "when past_only is not provided" do + it { expect(week_picker.past_only).to eq true } + end + + context "when past_only is provided" do + let(:week_picker) { StrictPeriods::PeriodPickers::WeekPicker.new(anchor: anchor, past_only: false) } + it { expect(week_picker.past_only).to eq false } + end end end From 95475d99696a822d352a45d900b8da737af86697 Mon Sep 17 00:00:00 2001 From: arizz Date: Tue, 8 Mar 2016 14:16:01 +0100 Subject: [PATCH 07/13] added test on steps variable --- spec/lib/period_pickers/week_picker_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/lib/period_pickers/week_picker_spec.rb b/spec/lib/period_pickers/week_picker_spec.rb index 5d2347e..401bdf9 100644 --- a/spec/lib/period_pickers/week_picker_spec.rb +++ b/spec/lib/period_pickers/week_picker_spec.rb @@ -13,6 +13,7 @@ expect(week_picker.anchor).to be_truthy expect(week_picker.anchor).to be_an_instance_of Time expect(week_picker.anchor).to eq Time.utc(2016,3,7) + expect(week_picker.instance_variable_get(:@steps)).to eq 0 end end From 4e0120acaf4d621e906fe51bcb08b3f7ba4c2b74 Mon Sep 17 00:00:00 2001 From: arizz Date: Tue, 8 Mar 2016 14:53:44 +0100 Subject: [PATCH 08/13] added test for minus and plus operands --- spec/lib/period_pickers/week_picker_spec.rb | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/spec/lib/period_pickers/week_picker_spec.rb b/spec/lib/period_pickers/week_picker_spec.rb index 401bdf9..590332a 100644 --- a/spec/lib/period_pickers/week_picker_spec.rb +++ b/spec/lib/period_pickers/week_picker_spec.rb @@ -36,4 +36,37 @@ it { expect(week_picker.past_only).to eq false } end end + + describe "#operands" do + let(:number_of_weeks) { 2 } + context "when using minus operand" do + before do + week_picker - number_of_weeks + end + it { expect(week_picker.anchor).to eq Time.utc(2016,2,22) } + it { expect(week_picker.instance_variable_get(:@steps)).to eq -number_of_weeks } + + context "_offset_anchor should be the same" do + before do + week_picker.send(:_offset_anchor) + end + it { expect(week_picker.instance_variable_get(:@offset)).to eq 60*60*24*(7 - 1)} + end + end + + context "when using plus operand" do + before do + week_picker + number_of_weeks + end + it { expect(week_picker.anchor).to eq Time.utc(2016,3,21) } + it { expect(week_picker.instance_variable_get(:@steps)).to eq number_of_weeks } + + context "_offset_anchor should be the same" do + before do + week_picker.send(:_offset_anchor) + end + it { expect(week_picker.instance_variable_get(:@offset)).to eq 60*60*24*(7 - 1)} + end + end + end end From 377a3c6fe4fa4c01ba52e86d9697f0d99dae9fce Mon Sep 17 00:00:00 2001 From: arizz Date: Tue, 8 Mar 2016 14:54:05 +0100 Subject: [PATCH 09/13] added test for reset! method --- spec/lib/period_pickers/week_picker_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/lib/period_pickers/week_picker_spec.rb b/spec/lib/period_pickers/week_picker_spec.rb index 590332a..b69ae06 100644 --- a/spec/lib/period_pickers/week_picker_spec.rb +++ b/spec/lib/period_pickers/week_picker_spec.rb @@ -69,4 +69,12 @@ end end end + + describe "#reset!" do + before do + week_picker.send(:reset!) + end + it { expect(week_picker.instance_variable_get(:@offset)).to eq 60*60*24*(7 - 1)} + it { expect(week_picker.instance_variable_get(:@steps)).to eq 0 } + end end From a509dab6ace61bb73a7f0f3c8ce0035f853ea9c7 Mon Sep 17 00:00:00 2001 From: arizz Date: Tue, 8 Mar 2016 14:54:34 +0100 Subject: [PATCH 10/13] added test for _offset_anchor private method --- spec/lib/period_pickers/week_picker_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/lib/period_pickers/week_picker_spec.rb b/spec/lib/period_pickers/week_picker_spec.rb index b69ae06..f08cec1 100644 --- a/spec/lib/period_pickers/week_picker_spec.rb +++ b/spec/lib/period_pickers/week_picker_spec.rb @@ -77,4 +77,11 @@ it { expect(week_picker.instance_variable_get(:@offset)).to eq 60*60*24*(7 - 1)} it { expect(week_picker.instance_variable_get(:@steps)).to eq 0 } end + + describe "_offset_anchor" do + before do + week_picker.send(:_offset_anchor) + end + it { expect(week_picker.instance_variable_get(:@offset)).to eq 60*60*24*(7 - 1)} + end end From bd0b8b692419d6b7abaebc123ec9360adf72a6f0 Mon Sep 17 00:00:00 2001 From: arizz Date: Tue, 8 Mar 2016 14:55:08 +0100 Subject: [PATCH 11/13] added test for _reset_steps private method --- spec/lib/period_pickers/week_picker_spec.rb | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/lib/period_pickers/week_picker_spec.rb b/spec/lib/period_pickers/week_picker_spec.rb index f08cec1..60fc4d8 100644 --- a/spec/lib/period_pickers/week_picker_spec.rb +++ b/spec/lib/period_pickers/week_picker_spec.rb @@ -84,4 +84,31 @@ end it { expect(week_picker.instance_variable_get(:@offset)).to eq 60*60*24*(7 - 1)} end + + describe "_reset_steps" do + let(:number_of_weeks) { 2 } + + context "when no action was done" do + before do + week_picker.send(:_reset_steps) + end + it { expect(week_picker.instance_variable_get(:@steps)).to eq 0 } + end + + context "when weeks was subtracted" do + before do + week_picker - number_of_weeks + week_picker.send(:_reset_steps) + end + it { expect(week_picker.instance_variable_get(:@steps)).to eq 0 } + end + + context "when weeks was added" do + before do + week_picker + number_of_weeks + week_picker.send(:_reset_steps) + end + it { expect(week_picker.instance_variable_get(:@steps)).to eq 0 } + end + end end From 77eb601cc2bc56664da77a89490faa7c0158dbfa Mon Sep 17 00:00:00 2001 From: arizz Date: Tue, 8 Mar 2016 14:55:23 +0100 Subject: [PATCH 12/13] added test for _reset_offset private method --- spec/lib/period_pickers/week_picker_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/lib/period_pickers/week_picker_spec.rb b/spec/lib/period_pickers/week_picker_spec.rb index 60fc4d8..d8ac92f 100644 --- a/spec/lib/period_pickers/week_picker_spec.rb +++ b/spec/lib/period_pickers/week_picker_spec.rb @@ -111,4 +111,12 @@ it { expect(week_picker.instance_variable_get(:@steps)).to eq 0 } end end + + describe "_reset_offset" do + before do + week_picker.send(:_reset_offset) + week_picker.send(:_reset_steps) + end + it { expect(week_picker.instance_variable_get(:@offset)).to eq 60*60*24*(7 - 1)} + end end From 2088fcb029b39d60b4eed126efebfc89deb47caf Mon Sep 17 00:00:00 2001 From: arizz Date: Tue, 8 Mar 2016 21:22:43 +0100 Subject: [PATCH 13/13] corrected descriptions --- spec/lib/strict_period_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/lib/strict_period_spec.rb b/spec/lib/strict_period_spec.rb index cf55541..3c27bce 100644 --- a/spec/lib/strict_period_spec.rb +++ b/spec/lib/strict_period_spec.rb @@ -87,14 +87,14 @@ let(:anchor) { Time.utc(2015,3,7).strftime('%Y-%m-%d') } let(:number_of_weeks) { 2 } - context "when there isn't specified number_of_weeks" do + context "when number_of_weeks value is not provided" do it { expect(strict_period.previous_weeks).to be_kind_of(Array) } it { expect(strict_period.previous_weeks).to eq [["2015-02-23", "2015-03-01"]] } it { expect(Time.parse(strict_period.previous_weeks[0][0]).monday?).to eq true } it { expect(Time.parse(strict_period.previous_weeks[0][1]).sunday?).to eq true } end - context "when there's specified number_of_weeks" do + context "when number_of_weeks value is provided" do it { expect(strict_period.previous_weeks).to be_kind_of(Array) } it { expect(strict_period.previous_weeks(number_of_weeks)).to eq [["2015-02-16", "2015-02-22"],["2015-02-23", "2015-03-01"]] } it { expect(Time.parse(strict_period.previous_weeks(number_of_weeks)[0][0]).monday?).to eq true } @@ -105,23 +105,23 @@ end describe "#next_weeks" do - context "when the actual next weeks is in the future" do + context "when next weeks are in the future" do it { expect(strict_period.next_weeks).to be_kind_of(Array) } it { expect(strict_period.next_weeks).to eq [] } end - context "when the actual next weeks is in the past" do + context "when next weeks are in the past" do let(:anchor) { Time.utc(2015,3,7).strftime('%Y-%m-%d') } let(:number_of_weeks) { 2 } - context "when there isn't specified number_of_weeks" do + context "when number_of_weeks value is not provided" do it { expect(strict_period.next_weeks).to be_kind_of(Array) } it { expect(strict_period.next_weeks).to eq [["2015-03-09", "2015-03-15"]] } it { expect(Time.parse(strict_period.next_weeks[0][0]).monday?).to eq true } it { expect(Time.parse(strict_period.next_weeks[0][1]).sunday?).to eq true } end - context "when there's specified number_of_weeks" do + context "when number_of_weeks value is provided" do it { expect(strict_period.next_weeks(number_of_weeks)).to be_kind_of(Array) } it { expect(strict_period.next_weeks(number_of_weeks)).to eq [["2015-03-09", "2015-03-15"],["2015-03-16", "2015-03-22"]] } it { expect(Time.parse(strict_period.next_weeks(number_of_weeks)[0][0]).monday?).to eq true }