File: deconstruct_keys_spec.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (44 lines) | stat: -rw-r--r-- 1,705 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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