Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added specs #1

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion lib/strict_periods/period_pickers/week_picker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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? || [email protected]_a?(Time)

_offset_anchor
end
Expand Down
116 changes: 115 additions & 1 deletion spec/lib/period_pickers/week_picker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 55 additions & 3 deletions spec/lib/strict_period_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

end
end

Expand All @@ -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
Expand Down Expand Up @@ -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