Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 96 lines (76 sloc) 3.847 kb
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
# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

require 'spec_helper'

describe TwitterCldr::Utils do
  describe '#deep_symbolize_keys' do
    let(:hash) { { 'foo' => { 'bar' => { 'baz' => 'woot' }, :ar => [1, 2] }, 42 => { 'baz' => 'wat' } } }

    let(:symbolized_hash) { { :foo => { :bar => { :baz => 'woot' }, :ar => [1, 2] }, 42 => { :baz => 'wat' } } }

    it 'symbolizes string keys of a hash' do
      TwitterCldr::Utils.deep_symbolize_keys(hash).should == symbolized_hash
    end

    it 'deeply symbolizes elements of an array' do
      TwitterCldr::Utils.deep_symbolize_keys([1, hash, 'foo', :bar]).should == [1, symbolized_hash, 'foo', :bar]
    end

    it 'deeply symbolizes elements of an array nested in a hash' do
      TwitterCldr::Utils.deep_symbolize_keys({ 'foo' => [1, hash] }).should == { :foo => [1, symbolized_hash] }
    end

    it 'leaves arguments of other types alone' do
      ['foo', :bar, 42].each { |arg| TwitterCldr::Utils.deep_symbolize_keys(arg).should == arg }
    end
  end

  describe "#deep_merge!" do
    it "combines two non-nested hashes with different keys" do
      first = { :foo => "bar" }
      TwitterCldr::Utils.deep_merge!(first, { :bar => "baz" }).should == { :foo => "bar", :bar => "baz" }
    end

    it "combines two non-nested hashes with the same keys" do
      first = { :foo => "bar" }
      TwitterCldr::Utils.deep_merge!(first, { :foo => "baz" }).should == { :foo => "baz" }
    end

    it "combines two nested hashes" do
      first = { :foo => "bar", :second => { :bar => "baz", :twitter => "rocks" } }
      second = { :foo => "baz", :third => { :whassup => "cool" }, :second => { :twitter => "rules" } }
      TwitterCldr::Utils.deep_merge!(first, second)
      first.should == { :foo => "baz", :second => { :bar => "baz", :twitter => "rules" }, :third => { :whassup => "cool" } }
    end

    it "replaces arrays with simple types" do
      first = [1, 2, 3]
      TwitterCldr::Utils.deep_merge!(first, [4, 5, 6]).should == [4, 5, 6]
    end

    it "merges a nested hash with a few simple array replacements" do
      first = { :foo => "bar", :second => { :bar => "baz", :twitter => [1, 2, 3] } }
      second = { :foo => [18], :third => { :whassup => "cool" }, :second => { :twitter => [4, 5, 6] } }
      TwitterCldr::Utils.deep_merge!(first, second)
      first.should == { :foo => [18], :second => { :bar => "baz", :twitter => [4, 5, 6] }, :third => { :whassup => "cool" } }
    end

    it "merges hashes within arrays" do
      first = [1, { :foo => "bar" }, { :bar => "baz" }, 8]
      second = [2, { :foo => "bar2" }, { :bar => "baz2", :twitter => "rules" }, 8, 9]
      TwitterCldr::Utils.deep_merge!(first, second)
      first.should == [2, { :foo => "bar2" }, { :bar => "baz2", :twitter => "rules" }, 8, 9]
    end
  end

  describe "#compute_cache_key" do
    it "returns a ruby hash of all the pieces concatenated with pipe characters" do
      TwitterCldr::Utils.compute_cache_key("space", "the", "final", "frontier").should == "space|the|final|frontier".hash
    end

    it "returns zero if no arguments are passed" do
      TwitterCldr::Utils.compute_cache_key.should == 0
    end
  end

  describe '#traverse_hash' do
    it 'returns value from the hash at the given path' do
      TwitterCldr::Utils.traverse_hash({ :foo => { :bar => 2, 'baz' => { 4 => 42 } } }, [:foo, 'baz', 4]).should == 42
    end

    it 'returns nil if the value is missing' do
      TwitterCldr::Utils.traverse_hash({ :foo => { :bar => 2 } }, [:foo, :baz]).should be_nil
    end

    it 'returns nil path is empty' do
      TwitterCldr::Utils.traverse_hash({ :foo => 42 }, []).should be_nil
    end

    it 'returns nil if not a Hash is passed' do
      TwitterCldr::Utils.traverse_hash(42, [:foo, :bar]).should be_nil
    end
  end
end
Something went wrong with that request. Please try again.