Skip to content

Commit

Permalink
add deconstruct_keys for Date and DateTime
Browse files Browse the repository at this point in the history
add spec for `Time`
  • Loading branch information
AI-Mozi committed May 11, 2023
1 parent aca4695 commit 8217f19
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
31 changes: 31 additions & 0 deletions library/date/deconstruct_keys_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require_relative '../../spec_helper'
require 'date'

ruby_version_is "3.2" do
describe "Date#deconstruct_keys" do
it "returns whole hash for nil as an argument" do
d = Date.new(2022, 10, 5)
d.deconstruct_keys(nil).should == { year: 2022, month: 10, day: 5, yday: 278, wday: 3 }
end

it "returns only specified keys" do
d = Date.new(2022, 10, 5)
d.deconstruct_keys([:year, :month]).should == { year: 2022, month: 10 }
end

it "requires one argument" do
-> {
Date.new(2022, 10, 5).deconstruct_keys
}.should raise_error(ArgumentError)
end

it "accepts only array or nil as an argument" do
d = Date.new(2022, 10, 5)

-> { d.deconstruct_keys(1) }.should raise_error(TypeError)
-> { d.deconstruct_keys("asd") }.should raise_error(TypeError)
-> { d.deconstruct_keys(:x) }.should raise_error(TypeError)
-> { d.deconstruct_keys({}) }.should raise_error(TypeError)
end
end
end
34 changes: 34 additions & 0 deletions library/datetime/deconstruct_keys_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative '../../spec_helper'
require 'date'

ruby_version_is "3.2" do
describe "DateTime#deconstruct_keys" do
it "returns whole hash for nil as an argument" do
d = DateTime.new(2022, 10, 5, 13, 30)
res = { year: 2022, month: 10, day: 5, yday: 278, wday: 3, hour: 13,
min: 30, sec: 0, sec_fraction: (0/1), zone: "+00:00" }
d.deconstruct_keys(nil).should == res
end

it "returns only specified keys" do
d = DateTime.new(2022, 10, 5, 13, 39)
d.deconstruct_keys([:zone, :hour]).should == { zone: "+00:00", hour: 13 }
end

it "requires one argument" do
-> {
DateTime.new(2022, 10, 5, 13, 30).deconstruct_keys
}.should raise_error(ArgumentError)
end

it "accepts only array or nil as an argument" do
d = DateTime.new(2022, 10, 5, 13, 30)

-> { d.deconstruct_keys(1) }.should raise_error(TypeError)
-> { d.deconstruct_keys("asd") }.should raise_error(TypeError)
-> { d.deconstruct_keys(:x) }.should raise_error(TypeError)
-> { d.deconstruct_keys({}) }.should raise_error(TypeError)
end
end
end

34 changes: 34 additions & 0 deletions library/time/deconstruct_keys_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative '../../spec_helper'
require 'time'

ruby_version_is "3.2" do
describe "Time#deconstruct_keys" do
it "returns whole hash for nil as an argument" do
d = Time.new(2022, 10, 5, 13, 30)
res = { year: 2022, month: 10, day: 5, yday: 278, wday: 3, hour: 13,
min: 30, sec: 0, subsec: 0, dst: true, zone: "CEST" }
d.deconstruct_keys(nil).should == res
end

it "returns only specified keys" do
d = Time.new(2022, 10, 5, 13, 39)
d.deconstruct_keys([:zone, :subsec]).should == { zone: "CEST", subsec: 0 }
end

it "requires one argument" do
-> {
Time.new(2022, 10, 5, 13, 30).deconstruct_keys
}.should raise_error(ArgumentError)
end

it "accepts only array or nil as an argument" do
d = Time.new(2022, 10, 5, 13, 30)

-> { d.deconstruct_keys(1) }.should raise_error(TypeError)
-> { d.deconstruct_keys("asd") }.should raise_error(TypeError)
-> { d.deconstruct_keys(:x) }.should raise_error(TypeError)
-> { d.deconstruct_keys({}) }.should raise_error(TypeError)
end
end
end

0 comments on commit 8217f19

Please sign in to comment.