summaryrefslogtreecommitdiff
path: root/spec/logic/helpers/hash_spec.rb
blob: 0da92fb57ac5895c37ec946587fa11b05c536ffe (about) (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
# frozen_string_literal: true

require_relative '../../../logic/helpers/hash'

RSpec.describe NanoBot::Logic::Helpers::Hash do
  it 'symbolizes keys' do
    expect(described_class.symbolize_keys({ 'a' => 'b', 'c' => { 'd' => ['e'] } })).to eq(
      { a: 'b', c: { d: ['e'] } }
    )
  end

  it 'deep merges' do
    expect(described_class.deep_merge(
             { a: { x: 1, y: 2 }, b: 3 },
             { a: { y: 99, z: 4 }, c: 5 }
           )).to eq(
             { a: { x: 1, y: 99, z: 4 }, b: 3, c: 5 }
           )
  end

  it 'stringify keys' do
    expect(described_class.stringify_keys({ a: 'b', c: { d: [:e] } })).to eq(
      { 'a' => 'b', 'c' => { 'd' => [:e] } }
    )
  end

  it 'fetch a path of keys' do
    expect(described_class.fetch({ a: 'b', c: { d: ['e'] } }, %i[c d])).to eq(
      ['e']
    )

    expect(described_class.fetch({ a: 'b', c: { d: ['e'] } }, %i[c e])).to be_nil

    expect(described_class.fetch({ a: 'b', c: { d: ['e'] } }, %i[a b])).to be_nil
  end
end