1
- Code . require_file "test_helper.exs" , __DIR__
1
+ Code . require_file ( "test_helper.exs" , __DIR__ )
2
2
3
3
defmodule KeywordTest do
4
4
use ExUnit.Case , async: true
5
5
6
- doctest Keyword
6
+ doctest ( Keyword )
7
7
8
8
test "has a literal syntax" do
9
9
assert [ B: 1 ] == [ { :B , 1 } ]
@@ -13,66 +13,83 @@ defmodule KeywordTest do
13
13
end
14
14
15
15
test "is a :: operator on ambiguity" do
16
- assert [ { ::: , _ , [ { :a , _ , _ } , { :b , _ , _ } ] } ] = quote ( do: [ a :: b ] )
16
+ assert [ { ::: , _ , [ { :a , _ , _ } , { :b , _ , _ } ] } ] = quote ( do: [ a :: b ] )
17
17
end
18
18
19
19
test "supports optional comma" do
20
- [ a: 1 ,
21
- b: 2 ,
22
- c: 3 , ]
20
+ [ a: 1 , b: 2 , c: 3 ]
23
21
end
24
22
25
23
test "implements (almost) all functions in Map" do
26
- assert Map . __info__ ( :functions ) -- Keyword . __info__ ( :functions ) ==
27
- [ from_struct: 1 ]
24
+ assert Map . __info__ ( :functions ) -- Keyword . __info__ ( :functions ) == [ from_struct: 1 ]
28
25
end
29
26
30
27
test "get_and_update/3 raises on bad return value from the argument function" do
31
- assert_raise RuntimeError , "the given function must return a two-element tuple or :pop, got: 1" , fn ->
28
+ message = "the given function must return a two-element tuple or :pop, got: 1"
29
+
30
+ assert_raise RuntimeError , message , fn ->
32
31
Keyword . get_and_update ( [ a: 1 ] , :a , fn value -> value end )
33
32
end
34
33
35
- assert_raise RuntimeError , "the given function must return a two-element tuple or :pop, got: nil" , fn ->
34
+ message = "the given function must return a two-element tuple or :pop, got: nil"
35
+
36
+ assert_raise RuntimeError , message , fn ->
36
37
Keyword . get_and_update ( [ ] , :a , fn value -> value end )
37
38
end
38
39
end
39
40
40
41
test "get_and_update!/3 raises on bad return value from the argument function" do
41
- assert_raise RuntimeError , "the given function must return a two-element tuple or :pop, got: 1" , fn ->
42
+ message = "the given function must return a two-element tuple or :pop, got: 1"
43
+
44
+ assert_raise RuntimeError , message , fn ->
42
45
Keyword . get_and_update! ( [ a: 1 ] , :a , fn value -> value end )
43
46
end
44
47
end
45
48
46
49
test "merge/2" do
47
- assert Keyword . merge ( [ a: 1 , b: 2 ] , [ c: 11 , d: 12 ] ) == [ a: 1 , b: 2 , c: 11 , d: 12 ]
48
- assert Keyword . merge ( [ ] , [ c: 11 , d: 12 ] ) == [ c: 11 , d: 12 ]
50
+ assert Keyword . merge ( [ a: 1 , b: 2 ] , c: 11 , d: 12 ) == [ a: 1 , b: 2 , c: 11 , d: 12 ]
51
+ assert Keyword . merge ( [ ] , c: 11 , d: 12 ) == [ c: 11 , d: 12 ]
49
52
assert Keyword . merge ( [ a: 1 , b: 2 ] , [ ] ) == [ a: 1 , b: 2 ]
50
53
51
- assert_raise ArgumentError , "expected a keyword list as the first argument, got: [1, 2]" , fn ->
52
- Keyword . merge ( [ 1 , 2 ] , [ c: 11 , d: 12 ] )
54
+ message = "expected a keyword list as the first argument, got: [1, 2]"
55
+
56
+ assert_raise ArgumentError , message , fn ->
57
+ Keyword . merge ( [ 1 , 2 ] , c: 11 , d: 12 )
53
58
end
54
59
55
- assert_raise ArgumentError , "expected a keyword list as the first argument, got: [1 | 2]" , fn ->
56
- Keyword . merge ( [ 1 | 2 ] , [ c: 11 , d: 12 ] )
60
+ message = "expected a keyword list as the first argument, got: [1 | 2]"
61
+
62
+ assert_raise ArgumentError , message , fn ->
63
+ Keyword . merge ( [ 1 | 2 ] , c: 11 , d: 12 )
57
64
end
58
65
59
- assert_raise ArgumentError , "expected a keyword list as the second argument, got: [11, 12, 0]" , fn ->
66
+ message = "expected a keyword list as the second argument, got: [11, 12, 0]"
67
+
68
+ assert_raise ArgumentError , message , fn ->
60
69
Keyword . merge ( [ a: 1 , b: 2 ] , [ 11 , 12 , 0 ] )
61
70
end
62
71
63
- assert_raise ArgumentError , "expected a keyword list as the second argument, got: [11 | 12]" , fn ->
72
+ message = "expected a keyword list as the second argument, got: [11 | 12]"
73
+
74
+ assert_raise ArgumentError , message , fn ->
64
75
Keyword . merge ( [ a: 1 , b: 2 ] , [ 11 | 12 ] )
65
76
end
66
77
67
78
# duplicate keys in keywords1 are kept if key is not present in keywords2
68
- assert Keyword . merge ( [ a: 1 , b: 2 , a: 3 ] , [ c: 11 , d: 12 ] ) == [ a: 1 , b: 2 , a: 3 , c: 11 , d: 12 ]
69
- assert Keyword . merge ( [ a: 1 , b: 2 , a: 3 ] , [ a: 11 ] ) == [ b: 2 , a: 11 ]
79
+ assert Keyword . merge ( [ a: 1 , b: 2 , a: 3 ] , c: 11 , d: 12 ) == [ a: 1 , b: 2 , a: 3 , c: 11 , d: 12 ]
80
+ assert Keyword . merge ( [ a: 1 , b: 2 , a: 3 ] , a: 11 ) == [ b: 2 , a: 11 ]
70
81
71
82
# duplicate keys in keywords2 are always kept
72
- assert Keyword . merge ( [ a: 1 , b: 2 ] , [ c: 11 , c: 12 , d: 13 ] ) == [ a: 1 , b: 2 , c: 11 , c: 12 , d: 13 ]
83
+ assert Keyword . merge ( [ a: 1 , b: 2 ] , c: 11 , c: 12 , d: 13 ) == [ a: 1 , b: 2 , c: 11 , c: 12 , d: 13 ]
73
84
74
85
# any key in keywords1 is removed if key is present in keyword2
75
- assert Keyword . merge ( [ a: 1 , b: 2 , c: 3 , c: 4 ] , [ c: 11 , c: 12 , d: 13 ] ) == [ a: 1 , b: 2 , c: 11 , c: 12 , d: 13 ]
86
+ assert Keyword . merge ( [ a: 1 , b: 2 , c: 3 , c: 4 ] , c: 11 , c: 12 , d: 13 ) == [
87
+ a: 1 ,
88
+ b: 2 ,
89
+ c: 11 ,
90
+ c: 12 ,
91
+ d: 13
92
+ ]
76
93
end
77
94
78
95
test "merge/3" do
@@ -82,35 +99,64 @@ defmodule KeywordTest do
82
99
assert Keyword . merge ( [ ] , [ c: 11 , d: 12 ] , fun ) == [ c: 11 , d: 12 ]
83
100
assert Keyword . merge ( [ a: 1 , b: 2 ] , [ ] , fun ) == [ a: 1 , b: 2 ]
84
101
85
- assert_raise ArgumentError , "expected a keyword list as the first argument, got: [1, 2]" , fn ->
102
+ message = "expected a keyword list as the first argument, got: [1, 2]"
103
+
104
+ assert_raise ArgumentError , message , fn ->
86
105
Keyword . merge ( [ 1 , 2 ] , [ c: 11 , d: 12 ] , fun )
87
106
end
88
107
89
- assert_raise ArgumentError , "expected a keyword list as the first argument, got: [1 | 2]" , fn ->
108
+ message = "expected a keyword list as the first argument, got: [1 | 2]"
109
+
110
+ assert_raise ArgumentError , message , fn ->
90
111
Keyword . merge ( [ 1 | 2 ] , [ c: 11 , d: 12 ] , fun )
91
112
end
92
113
93
- assert_raise ArgumentError , "expected a keyword list as the second argument, got: [{:x, 1}, :y, :z]" , fn ->
114
+ message = "expected a keyword list as the second argument, got: [{:x, 1}, :y, :z]"
115
+
116
+ assert_raise ArgumentError , message , fn ->
94
117
Keyword . merge ( [ a: 1 , b: 2 ] , [ { :x , 1 } , :y , :z ] , fun )
95
118
end
96
119
97
- assert_raise ArgumentError , "expected a keyword list as the second argument, got: [:x | :y]" , fn ->
120
+ message = "expected a keyword list as the second argument, got: [:x | :y]"
121
+
122
+ assert_raise ArgumentError , message , fn ->
98
123
Keyword . merge ( [ a: 1 , b: 2 ] , [ :x | :y ] , fun )
99
124
end
100
125
101
- assert_raise ArgumentError , "expected a keyword list as the second argument, got: [{:x, 1} | :y]" , fn ->
126
+ message = "expected a keyword list as the second argument, got: [{:x, 1} | :y]"
127
+
128
+ assert_raise ArgumentError , message , fn ->
102
129
Keyword . merge ( [ a: 1 , b: 2 ] , [ { :x , 1 } | :y ] , fun )
103
130
end
104
131
105
132
# duplicate keys in keywords1 are left untouched if key is not present in keywords2
106
- assert Keyword . merge ( [ a: 1 , b: 2 , a: 3 ] , [ c: 11 , d: 12 ] , fun ) == [ a: 1 , b: 2 , a: 3 , c: 11 , d: 12 ]
133
+ assert Keyword . merge ( [ a: 1 , b: 2 , a: 3 ] , [ c: 11 , d: 12 ] , fun ) == [
134
+ a: 1 ,
135
+ b: 2 ,
136
+ a: 3 ,
137
+ c: 11 ,
138
+ d: 12
139
+ ]
140
+
107
141
assert Keyword . merge ( [ a: 1 , b: 2 , a: 3 ] , [ a: 11 ] , fun ) == [ b: 2 , a: 12 ]
108
142
109
143
# duplicate keys in keywords2 are always kept
110
- assert Keyword . merge ( [ a: 1 , b: 2 ] , [ c: 11 , c: 12 , d: 13 ] , fun ) == [ a: 1 , b: 2 , c: 11 , c: 12 , d: 13 ]
144
+ assert Keyword . merge ( [ a: 1 , b: 2 ] , [ c: 11 , c: 12 , d: 13 ] , fun ) == [
145
+ a: 1 ,
146
+ b: 2 ,
147
+ c: 11 ,
148
+ c: 12 ,
149
+ d: 13
150
+ ]
111
151
112
152
# every key in keywords1 is replaced with fun result if key is present in keyword2
113
- assert Keyword . merge ( [ a: 1 , b: 2 , c: 3 , c: 4 ] , [ c: 11 , c: 50 , d: 13 ] , fun ) == [ a: 1 , b: 2 , c: 14 , c: 54 , d: 13 ]
153
+ assert Keyword . merge ( [ a: 1 , b: 2 , c: 3 , c: 4 ] , [ c: 11 , c: 50 , d: 13 ] , fun ) == [
154
+ a: 1 ,
155
+ b: 2 ,
156
+ c: 14 ,
157
+ c: 54 ,
158
+ d: 13
159
+ ]
114
160
end
115
161
116
162
test "merge/2 and merge/3 behave exactly the same way" do
@@ -123,7 +169,7 @@ defmodule KeywordTest do
123
169
{ [ a: 1 , b: 2 , a: 3 ] , [ c: 11 , d: 12 ] } ,
124
170
{ [ a: 1 , b: 2 , a: 3 ] , [ a: 11 ] } ,
125
171
{ [ a: 1 , b: 2 ] , [ c: 11 , c: 12 , d: 13 ] } ,
126
- { [ a: 1 , b: 2 , c: 3 , c: 4 ] , [ c: 11 , c: 12 , d: 13 ] } ,
172
+ { [ a: 1 , b: 2 , c: 3 , c: 4 ] , [ c: 11 , c: 12 , d: 13 ] }
127
173
]
128
174
129
175
args_error = [
@@ -133,7 +179,7 @@ defmodule KeywordTest do
133
179
{ [ a: 1 , b: 2 ] , [ 11 | 12 ] } ,
134
180
{ [ a: 1 , b: 2 ] , [ { :x , 1 } , :y , :z ] } ,
135
181
{ [ a: 1 , b: 2 ] , [ :x | :y ] } ,
136
- { [ a: 1 , b: 2 ] , [ { :x , 1 } | :y ] } ,
182
+ { [ a: 1 , b: 2 ] , [ { :x , 1 } | :y ] }
137
183
]
138
184
139
185
for { arg1 , arg2 } <- args do
0 commit comments