summaryrefslogtreecommitdiff
path: root/spec/logic/helpers/hash_spec.rb
blob: 5e4ec601488f5e78e6f563e900ccf3d5f34a34be (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
# 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 'stringify keys' do
    pp described_class.stringify_keys({ a: 'b', c: { d: [:e] } })
    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