File tree Expand file tree Collapse file tree 3 files changed +66
-2
lines changed Expand file tree Collapse file tree 3 files changed +66
-2
lines changed Original file line number Diff line number Diff line change 15
15
a . dig ( 0 , -1 , 0 ) . should == 2
16
16
end
17
17
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
+
18
23
it "raises a TypeError for a non-numeric index" do
19
24
lambda {
20
25
[ 'a' ] . dig ( :first )
21
26
} . should raise_error ( TypeError )
22
27
end
23
28
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
+
24
36
it "raises without any args" do
25
37
lambda {
26
38
[ 10 ] . dig ( )
27
39
} . should raise_error ( ArgumentError )
28
40
end
29
41
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
+
30
47
it "calls #dig on the result of #at with the remaining arguments" do
31
48
h = [ [ nil , [ nil , nil , 42 ] ] ]
32
49
h [ 0 ] . should_receive ( :dig ) . with ( 1 , 2 ) . and_return ( 42 )
Original file line number Diff line number Diff line change 10
10
h . dig ( 1 ) . should be_nil
11
11
end
12
12
13
- it "does recurse " do
13
+ it "returns the nested value specified by the sequence of keys " do
14
14
h = { foo : { bar : { baz : 1 } } }
15
15
h . dig ( :foo , :bar , :baz ) . should == 1
16
16
h . dig ( :foo , :bar , :nope ) . should be_nil
17
17
h . dig ( :foo , :baz ) . should be_nil
18
18
h . dig ( :bar , :baz , :foo ) . should be_nil
19
19
end
20
20
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
+
21
31
it "raises without args" do
22
32
lambda { { the : 'borg' } . dig ( ) } . should raise_error ( ArgumentError )
23
33
end
@@ -47,6 +57,5 @@ def obj.dig(*args); [ 42 ] end
47
57
h [ :foo ] . should_receive ( :dig ) . with ( :bar , :baz ) . and_return ( 42 )
48
58
h . dig ( :foo , :bar , :baz ) . should == 42
49
59
end
50
-
51
60
end
52
61
end
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments