summaryrefslogtreecommitdiff
path: root/spec/hashie/hash_spec.rb
blob: 02822586210c1bfe5bbbbbc36cdcd93dac922110 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require 'spec_helper'

describe Hash do
  it 'is convertible to a Hashie::Mash' do
    mash = Hashie::Hash[some: 'hash'].to_mash
    expect(mash.is_a?(Hashie::Mash)).to be_truthy
    expect(mash.some).to eq 'hash'
  end

  it '#stringify_keys! turns all keys into strings' do
    hash = Hashie::Hash[a: 'hey', 123 => 'bob']
    hash.stringify_keys!
    expect(hash).to eq Hashie::Hash['a' => 'hey', '123' => 'bob']
  end

  it '#stringify_keys! turns all keys into strings recursively' do
    hash = Hashie::Hash[a: 'hey', 123 => { 345 => 'hey' }]
    hash.stringify_keys!
    expect(hash).to eq Hashie::Hash['a' => 'hey', '123' => { '345' => 'hey' }]
  end

  it '#stringify_keys returns a hash with stringified keys' do
    hash = Hashie::Hash[a: 'hey', 123 => 'bob']
    stringified_hash = hash.stringify_keys
    expect(hash).to eq Hashie::Hash[a: 'hey', 123 => 'bob']
    expect(stringified_hash).to eq Hashie::Hash['a' => 'hey', '123' => 'bob']
  end

  it '#to_hash returns a hash with same keys' do
    hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3]]
    stringified_hash = hash.to_hash
    expect(stringified_hash).to eq('a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3])
  end

  it '#to_hash with stringify_keys set to true returns a hash with stringified_keys' do
    hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3]]
    symbolized_hash = hash.to_hash(stringify_keys: true)
    expect(symbolized_hash).to eq('a' => 'hey', '123' => 'bob', 'array' => [1, 2, 3])
  end

  it '#to_hash with symbolize_keys set to true returns a hash with symbolized keys' do
    hash = Hashie::Hash['a' => 'hey', 123 => 'bob', 'array' => [1, 2, 3]]
    symbolized_hash = hash.to_hash(symbolize_keys: true)
    expect(symbolized_hash).to eq(a: 'hey', :"123" => 'bob', array: [1, 2, 3])
  end

  it "#to_hash should not blow up when #to_hash doesn't accept arguments" do
    class BareCustomMash < Hashie::Mash
      def to_hash
        {}
      end
    end

    h = Hashie::Hash.new
    h[:key] = BareCustomMash.new
    expect { h.to_hash }.not_to raise_error
  end

  describe 'when the value is an object that respond_to to_hash' do
    class ClassRespondsToHash
      def to_hash(options = {})
        Hashie::Hash['a' => 'hey', b: 'bar', 123 => 'bob', 'array' => [1, 2, 3]].to_hash(options)
      end
    end

    it '#to_hash returns a hash with same keys' do
      hash = Hashie::Hash[
        'a' => 'hey',
        123 => 'bob',
        'array' => [1, 2, 3],
        subhash: ClassRespondsToHash.new
      ]
      stringified_hash = hash.to_hash

      expected = {
        'a' => 'hey',
        123 => 'bob',
        'array' => [1, 2, 3],
        subhash: { 'a' => 'hey', b: 'bar', 123 => 'bob', 'array' => [1, 2, 3] }
      }

      expect(stringified_hash).to eq(expected)
    end

    it '#to_hash with stringify_keys set to true returns a hash with stringified_keys' do
      hash = Hashie::Hash[
        'a' => 'hey',
        123 => 'bob',
        'array' => [1, 2, 3],
        subhash: ClassRespondsToHash.new
      ]
      symbolized_hash = hash.to_hash(stringify_keys: true)

      expected = {
        'a' => 'hey',
        '123' => 'bob',
        'array' => [1, 2, 3],
        'subhash' => { 'a' => 'hey', 'b' => 'bar', '123' => 'bob', 'array' => [1, 2, 3] }
      }

      expect(symbolized_hash).to eq(expected)
    end

    it '#to_hash with symbolize_keys set to true returns a hash with symbolized keys' do
      hash = Hashie::Hash[
        'a' => 'hey',
        123 => 'bob',
        'array' => [1, 2, 3],
        subhash: ClassRespondsToHash.new
      ]
      symbolized_hash = hash.to_hash(symbolize_keys: true)

      expected = {
        a: 'hey',
        :"123" => 'bob',
        array: [1, 2, 3],
        subhash: { a: 'hey', b: 'bar', :'123' => 'bob', array: [1, 2, 3] }
      }

      expect(symbolized_hash).to eq(expected)
    end
  end
end