@@ -21,6 +21,7 @@ defmodule AccessTest do
21
21
22
22
test "for nil" do
23
23
assert nil [ :foo ] == nil
24
+ assert Access . fetch ( nil , :foo ) == :error
24
25
assert Access . get ( nil , :foo ) == nil
25
26
assert_raise ArgumentError , "could not put/update key :foo on a nil value" , fn ->
26
27
Access . get_and_update ( nil , :foo , fn nil -> { :ok , :bar } end )
@@ -32,6 +33,13 @@ defmodule AccessTest do
32
33
assert [ foo: [ bar: :baz ] ] [ :foo ] [ :bar ] == :baz
33
34
assert [ foo: [ bar: :baz ] ] [ :fuu ] [ :bar ] == nil
34
35
36
+ assert Access . fetch ( [ foo: :bar ] , :foo ) == { :ok , :bar }
37
+ assert Access . fetch ( [ foo: :bar ] , :bar ) == :error
38
+
39
+ assert_raise FunctionClauseError , fn ->
40
+ Access . fetch ( [ { "foo" , :bar } ] , "foo" )
41
+ end
42
+
35
43
assert Access . get ( [ foo: :bar ] , :foo ) == :bar
36
44
assert Access . get_and_update ( [ ] , :foo , fn nil -> { :ok , :baz } end ) == { :ok , [ foo: :baz ] }
37
45
assert Access . get_and_update ( [ foo: :bar ] , :foo , fn :bar -> { :ok , :baz } end ) == { :ok , [ foo: :baz ] }
@@ -43,6 +51,9 @@ defmodule AccessTest do
43
51
assert % { 1.0 => 1.0 } [ 1.0 ] == 1.0
44
52
assert % { 1 => 1 } [ 1.0 ] == nil
45
53
54
+ assert Access . fetch ( % { foo: :bar } , :foo ) == { :ok , :bar }
55
+ assert Access . fetch ( % { foo: :bar } , :bar ) == :error
56
+
46
57
assert Access . get ( % { foo: :bar } , :foo ) == :bar
47
58
assert Access . get_and_update ( % { } , :foo , fn nil -> { :ok , :baz } end ) == { :ok , % { foo: :baz } }
48
59
assert Access . get_and_update ( % { foo: :bar } , :foo , fn :bar -> { :ok , :baz } end ) == { :ok , % { foo: :baz } }
0 commit comments