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 diff --git a/spec/lib/period_pickers/week_picker_spec.rb b/spec/lib/period_pickers/week_picker_spec.rb index 7a99dc0..d8ac92f 100644 --- a/spec/lib/period_pickers/week_picker_spec.rb +++ b/spec/lib/period_pickers/week_picker_spec.rb @@ -3,6 +3,120 @@ 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) + expect(week_picker.instance_variable_get(:@steps)).to eq 0 + 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 + + 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 + + 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 + + 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 + + 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 + + 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 diff --git a/spec/lib/strict_period_spec.rb b/spec/lib/strict_period_spec.rb index 27a671a..3c27bce 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 @@ -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 @@ -74,9 +83,52 @@ 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 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 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 } + 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 + describe "#next_weeks" 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 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 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 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 } + 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