Skip to content
Newer
Older
100644 80 lines (62 sloc) 3.15 KB
93a9106 @KL-7 Move deep_symbolize_keys to TwitterClrd::Utils.
KL-7 authored Apr 27, 2012
1 # encoding: UTF-8
2
3 # Copyright 2012 Twitter, Inc
4 # http://www.apache.org/licenses/LICENSE-2.0
5
6 require 'spec_helper'
7
8 describe TwitterCldr::Utils do
9 describe '#deep_symbolize_keys' do
10
11 let(:hash) { { 'foo' => { 'bar' => { 'baz' => 'woot' }, :ar => [1, 2] }, 42 => { 'baz' => 'wat' } } }
12
13 let(:symbolized_hash) { { :foo => { :bar => { :baz => 'woot' }, :ar => [1, 2] }, 42 => { :baz => 'wat' } } }
14
15 it 'symbolizes string keys of a hash' do
16 TwitterCldr::Utils.deep_symbolize_keys(hash).should == symbolized_hash
17 end
18
19 it 'deeply symbolizes elements of an array' do
20 TwitterCldr::Utils.deep_symbolize_keys([1, hash, 'foo', :bar]).should == [1, symbolized_hash, 'foo', :bar]
21 end
22
23 it 'deeply symbolizes elements of an array nested in a hash' do
24 TwitterCldr::Utils.deep_symbolize_keys({ 'foo' => [1, hash] }).should == { :foo => [1, symbolized_hash] }
25 end
26
27 it 'leaves arguments of other types alone' do
28 ['foo', :bar, 42].each { |arg| TwitterCldr::Utils.deep_symbolize_keys(arg).should == arg }
29 end
30
31 end
f6b1d13 Added NFC and NFKC algorithms, refactored Shared::UnicodeData::Attrib…
Cameron Dutro authored Jun 15, 2012
32
37f4962 Squashing
Cameron Dutro authored Jul 18, 2012
33 describe "#deep_merge!" do
34 it "combines two non-nested hashes with different keys" do
35 first = { :foo => "bar" }
36 TwitterCldr::Utils.deep_merge!(first, { :bar => "baz" }).should == { :foo => "bar", :bar => "baz" }
37 end
38
39 it "combines two non-nested hashes with the same keys" do
40 first = { :foo => "bar" }
41 TwitterCldr::Utils.deep_merge!(first, { :foo => "baz" }).should == { :foo => "baz" }
42 end
43
44 it "combines two nested hashes" do
45 first = { :foo => "bar", :second => { :bar => "baz", :twitter => "rocks" } }
46 second = { :foo => "baz", :third => { :whassup => "cool" }, :second => { :twitter => "rules" } }
47 TwitterCldr::Utils.deep_merge!(first, second)
48 first.should == { :foo => "baz", :second => { :bar => "baz", :twitter => "rules" }, :third => { :whassup => "cool" } }
49 end
50
51 it "replaces arrays with simple types" do
52 first = [1, 2, 3]
53 TwitterCldr::Utils.deep_merge!(first, [4, 5, 6]).should == [4, 5, 6]
54 end
55
56 it "merges a nested hash with a few simple array replacements" do
57 first = { :foo => "bar", :second => { :bar => "baz", :twitter => [1, 2, 3] } }
58 second = { :foo => [18], :third => { :whassup => "cool" }, :second => { :twitter => [4, 5, 6] } }
59 TwitterCldr::Utils.deep_merge!(first, second)
60 first.should == { :foo => [18], :second => { :bar => "baz", :twitter => [4, 5, 6] }, :third => { :whassup => "cool" } }
61 end
62
63 it "merges hashes within arrays" do
64 first = [1, { :foo => "bar" }, { :bar => "baz" }, 8]
65 second = [2, { :foo => "bar2" }, { :bar => "baz2", :twitter => "rules" }, 8, 9]
66 TwitterCldr::Utils.deep_merge!(first, second)
67 first.should == [2, { :foo => "bar2" }, { :bar => "baz2", :twitter => "rules" }, 8, 9]
68 end
69 end
70
f6b1d13 Added NFC and NFKC algorithms, refactored Shared::UnicodeData::Attrib…
Cameron Dutro authored Jun 15, 2012
71 describe "#compute_cache_key" do
72 it "returns a ruby hash of all the pieces concatenated with pipe characters" do
73 TwitterCldr::Utils.compute_cache_key("space", "the", "final", "frontier").should == "space|the|final|frontier".hash
74 end
75
76 it "returns zero if no arguments are passed" do
77 TwitterCldr::Utils.compute_cache_key.should == 0
78 end
79 end
93a9106 @KL-7 Move deep_symbolize_keys to TwitterClrd::Utils.
KL-7 authored Apr 26, 2012
80 end
Something went wrong with that request. Please try again.