From 8719340a5af4003d78f2cf3999e92ac68c20c7f8 Mon Sep 17 00:00:00 2001 From: AI-Mozi Date: Wed, 10 May 2023 17:54:14 +0200 Subject: [PATCH] add `deconstruct_keys` for `Date` and `DateTime` add spec for `Time` add more specs, move `Time` specs to core --- core/time/deconstruct_keys_spec.rb | 44 ++++++++++++++++++++++ library/date/deconstruct_keys_spec.rb | 43 ++++++++++++++++++++++ library/datetime/deconstruct_keys_spec.rb | 45 +++++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 core/time/deconstruct_keys_spec.rb create mode 100644 library/date/deconstruct_keys_spec.rb create mode 100644 library/datetime/deconstruct_keys_spec.rb diff --git a/core/time/deconstruct_keys_spec.rb b/core/time/deconstruct_keys_spec.rb new file mode 100644 index 0000000000..fbb0ec2164 --- /dev/null +++ b/core/time/deconstruct_keys_spec.rb @@ -0,0 +1,44 @@ +require_relative '../../spec_helper' + +ruby_version_is "3.2" do + describe "Time#deconstruct_keys" do + it "returns whole hash for nil as an argument" do + d = Time.utc(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: false, zone: "UTC" } + d.deconstruct_keys(nil).should == res + end + + it "returns only specified keys" do + d = Time.utc(2022, 10, 5, 13, 39) + d.deconstruct_keys([:zone, :subsec]).should == { zone: "UTC", subsec: 0 } + end + + it "requires one argument" do + -> { + Time.new(2022, 10, 5, 13, 30).deconstruct_keys + }.should raise_error(ArgumentError) + end + + it "it raises error when argument is neither nil nor array" do + d = Time.new(2022, 10, 5, 13, 30) + + -> { d.deconstruct_keys(1) }.should raise_error(TypeError, "wrong argument type Integer (expected Array or nil)") + -> { d.deconstruct_keys("asd") }.should raise_error(TypeError, "wrong argument type String (expected Array or nil)") + -> { d.deconstruct_keys(:x) }.should raise_error(TypeError, "wrong argument type Symbol (expected Array or nil)") + -> { d.deconstruct_keys({}) }.should raise_error(TypeError, "wrong argument type Hash (expected Array or nil)") + end + + it "returns {} when passed []" do + Time.new(2022, 10, 5, 13, 30).deconstruct_keys([]).should == {} + end + + it "ignores non-Symbol keys" do + Time.new(2022, 10, 5, 13, 30).deconstruct_keys(['year', []]).should == {} + end + + it "ignores not existing Symbol keys" do + Time.new(2022, 10, 5, 13, 30).deconstruct_keys([:year, :a]).should == { year: 2022 } + end + end +end diff --git a/library/date/deconstruct_keys_spec.rb b/library/date/deconstruct_keys_spec.rb new file mode 100644 index 0000000000..fc9caaf332 --- /dev/null +++ b/library/date/deconstruct_keys_spec.rb @@ -0,0 +1,43 @@ +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 "it raises error when argument is neither nil nor array" do + d = Date.new(2022, 10, 5) + + -> { d.deconstruct_keys(1) }.should raise_error(TypeError, "wrong argument type Integer (expected Array or nil)") + -> { d.deconstruct_keys("asd") }.should raise_error(TypeError, "wrong argument type String (expected Array or nil)") + -> { d.deconstruct_keys(:x) }.should raise_error(TypeError, "wrong argument type Symbol (expected Array or nil)") + -> { d.deconstruct_keys({}) }.should raise_error(TypeError, "wrong argument type Hash (expected Array or nil)") + end + + it "returns {} when passed []" do + Date.new(2022, 10, 5).deconstruct_keys([]).should == {} + end + + it "ignores non-Symbol keys" do + Date.new(2022, 10, 5).deconstruct_keys(['year', []]).should == {} + end + + it "ignores not existing Symbol keys" do + Date.new(2022, 10, 5).deconstruct_keys([:year, :a]).should == { year: 2022 } + end + end +end diff --git a/library/datetime/deconstruct_keys_spec.rb b/library/datetime/deconstruct_keys_spec.rb new file mode 100644 index 0000000000..c6c0f71f55 --- /dev/null +++ b/library/datetime/deconstruct_keys_spec.rb @@ -0,0 +1,45 @@ +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 "it raises error when argument is neither nil nor array" do + d = DateTime.new(2022, 10, 5, 13, 30) + + -> { d.deconstruct_keys(1) }.should raise_error(TypeError, "wrong argument type Integer (expected Array or nil)") + -> { d.deconstruct_keys("asd") }.should raise_error(TypeError, "wrong argument type String (expected Array or nil)") + -> { d.deconstruct_keys(:x) }.should raise_error(TypeError, "wrong argument type Symbol (expected Array or nil)") + -> { d.deconstruct_keys({}) }.should raise_error(TypeError, "wrong argument type Hash (expected Array or nil)") + end + + it "returns {} when passed []" do + DateTime.new(2022, 10, 5, 13, 30).deconstruct_keys([]).should == {} + end + + it "ignores non-Symbol keys" do + DateTime.new(2022, 10, 5, 13, 30).deconstruct_keys(['year', []]).should == {} + end + + it "ignores not existing Symbol keys" do + DateTime.new(2022, 10, 5, 13, 30).deconstruct_keys([:year, :a]).should == { year: 2022 } + end + end +end