Skip to content

Commit 065e422

Browse files
ahmadsheriferegon
authored andcommitted
Add specs for #dig implmenetation for Hash, Array and Struct classes
1 parent 0be9395 commit 065e422

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

core/array/dig_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,35 @@
1515
a.dig(0, -1, 0).should == 2
1616
end
1717

18+
it "returns the nested value specified if the sequence includes a key" do
19+
a = [42, { foo: :bar }]
20+
a.dig(1, :foo).should == :bar
21+
end
22+
1823
it "raises a TypeError for a non-numeric index" do
1924
lambda {
2025
['a'].dig(:first)
2126
}.should raise_error(TypeError)
2227
end
2328

29+
it "raises a TypeError if any intermediate step does not respond to #dig" do
30+
a = [1, 2]
31+
lambda {
32+
a.dig(0, 1)
33+
}.should raise_error(TypeError)
34+
end
35+
2436
it "raises without any args" do
2537
lambda {
2638
[10].dig()
2739
}.should raise_error(ArgumentError)
2840
end
2941

42+
it "returns nil if any intermediate step is nil" do
43+
a = [[1, [2, 3]]]
44+
a.dig(1, 2, 3).should == nil
45+
end
46+
3047
it "calls #dig on the result of #at with the remaining arguments" do
3148
h = [[nil, [nil, nil, 42]]]
3249
h[0].should_receive(:dig).with(1, 2).and_return(42)

core/hash/dig_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,24 @@
1010
h.dig(1).should be_nil
1111
end
1212

13-
it "does recurse" do
13+
it "returns the nested value specified by the sequence of keys" do
1414
h = { foo: { bar: { baz: 1 } } }
1515
h.dig(:foo, :bar, :baz).should == 1
1616
h.dig(:foo, :bar, :nope).should be_nil
1717
h.dig(:foo, :baz).should be_nil
1818
h.dig(:bar, :baz, :foo).should be_nil
1919
end
2020

21+
it "returns the nested value specified if the sequence includes an index" do
22+
h = { foo: [1, 2, 3] }
23+
h.dig(:foo, 2).should == 3
24+
end
25+
26+
it "returns nil if any intermediate step is nil" do
27+
h = { foo: { bar: { baz: 1 } } }
28+
h.dig(:foo, :zot, :xyz).should == nil
29+
end
30+
2131
it "raises without args" do
2232
lambda { { the: 'borg' }.dig() }.should raise_error(ArgumentError)
2333
end
@@ -47,6 +57,5 @@ def obj.dig(*args); [ 42 ] end
4757
h[:foo].should_receive(:dig).with(:bar, :baz).and_return(42)
4858
h.dig(:foo, :bar, :baz).should == 42
4959
end
50-
5160
end
5261
end

core/struct/dig_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require File.expand_path('../../../spec_helper', __FILE__)
2+
3+
describe "Struct#dig" do
4+
before(:each) do
5+
@klass = Struct.new(:a)
6+
@instance = @klass.new(@klass.new({ b: [1, 2, 3] }))
7+
end
8+
9+
it "returns the nested value specified by the sequence of keys" do
10+
@instance.dig(:a, :a).should == { b: [1, 2, 3] }
11+
end
12+
13+
it "returns the nested value specified if the sequence includes an index" do
14+
@instance.dig(:a, :a, :b, 0).should == 1
15+
end
16+
17+
it "returns nil if any intermediate step is nil" do
18+
@instance.dig(:b, 0).should == nil
19+
end
20+
21+
it "raises a TypeError if any intermediate step does not respond to #dig" do
22+
instance = @klass.new(1)
23+
lambda {
24+
instance.dig(:a, 3)
25+
}.should raise_error(TypeError)
26+
end
27+
28+
it "calls #dig on any intermediate step with the rest of the sequence as arguments" do
29+
obj = Object.new
30+
instance = @klass.new(obj)
31+
32+
def obj.dig(*args)
33+
{dug: args}
34+
end
35+
36+
instance.dig(:a, :bar, :baz).should == { dug: [:bar, :baz] }
37+
end
38+
end

0 commit comments

Comments
 (0)