Skip to content

Commit 26599e7

Browse files
committed
Improve elixir application doctest coverage
1 parent 52aa1fd commit 26599e7

File tree

19 files changed

+682
-479
lines changed

19 files changed

+682
-479
lines changed

lib/elixir/lib/access.ex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ defimpl Access, for: List do
2525
2626
## Examples
2727
28-
keywords = [a: 1, b: 2]
29-
keywords[:a] #=> 1
28+
iex> keywords = [a: 1, b: 2]
29+
...> keywords[:a]
30+
1
3031
31-
star_ratings = [{1.0, "★"}, {1.5, "★☆"}, {2.0, "★★"}]
32-
star_ratings[1.5] #=> "★☆"
32+
iex> star_ratings = [{1.0, "★"}, {1.5, "★☆"}, {2.0, "★★"}]
33+
...> star_ratings[1.5]
34+
"★☆"
3335
3436
"""
3537
def access([], _key), do: nil

lib/elixir/lib/binary/chars.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ defimpl Binary.Chars, for: List do
5151
5252
## Examples
5353
54-
to_binary 'foo' #=> "foo"
55-
to_binary ["foo", 'bar'] #=> "foobar"
54+
iex> to_binary 'foo'
55+
"foo"
56+
iex> to_binary ["foo", 'bar']
57+
"foobar"
5658
5759
"""
5860
def to_binary(thing) do

lib/elixir/lib/binary/inspect.ex

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,12 @@ defimpl Binary.Inspect, for: Atom do
116116
117117
## Examples
118118
119-
inspect(:foo) #=> ":foo"
120-
inspect(nil) #=> "nil"
121-
inspect(Foo.Bar) #=> "Foo.Bar"
119+
iex> inspect(:foo)
120+
":foo"
121+
iex> inspect(nil)
122+
"nil"
123+
iex> inspect(Foo.Bar)
124+
"Foo.Bar"
122125
123126
"""
124127

@@ -191,8 +194,10 @@ defimpl Binary.Inspect, for: BitString do
191194
192195
## Examples
193196
194-
inspect("bar") #=> "bar"
195-
inspect("f\"oo") #=> "f\"oo"
197+
iex> inspect("bar")
198+
"\"bar\""
199+
iex> inspect("f\"oo")
200+
"\"f\\\"oo\""
196201
197202
"""
198203

@@ -256,9 +261,12 @@ defimpl Binary.Inspect, for: List do
256261
257262
## Examples
258263
259-
inspect('bar') #=> 'bar'
260-
inspect([0|'bar']) #=> "[0,98,97,114]"
261-
inspect([:foo,:bar]) #=> "[:foo, :bar]"
264+
iex> inspect('bar')
265+
"'bar'"
266+
iex> inspect([0|'bar'])
267+
"[0,98,97,114]"
268+
iex> inspect([:foo,:bar])
269+
"[:foo,:bar]"
262270
263271
"""
264272

@@ -298,8 +306,10 @@ defimpl Binary.Inspect, for: Tuple do
298306
299307
## Examples
300308
301-
inspect({1,2,3}) #=> "{1,2,3}"
302-
inspect(ArgumentError.new) #=> ArgumentError[message: "argument error"]
309+
iex> inspect({1,2,3})
310+
"{1,2,3}"
311+
iex> inspect(ArgumentError.new)
312+
"ArgumentError[message: \\\"argument error\\\"]"
303313
304314
"""
305315

@@ -377,7 +387,8 @@ defimpl Binary.Inspect, for: Number do
377387
378388
## Examples
379389
380-
inspect(1) #=> "1"
390+
iex> inspect(1)
391+
"1"
381392
382393
"""
383394

@@ -396,7 +407,8 @@ defimpl Binary.Inspect, for: Regex do
396407
397408
## Examples
398409
399-
inspect(%r/foo/m) #=> "%r\"foo\"m"
410+
iex> inspect(%r/foo/m)
411+
"%r\"foo\"m"
400412
401413
"""
402414

lib/elixir/lib/bitwise.ex

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ defmodule Bitwise do
66
The easiest way to use is to simply import them into
77
your module:
88
9-
use Bitwise
9+
iex> use Bitwise
10+
...> bnot 1
11+
-2
1012
11-
bnot 1 #=> -2
12-
1 &&& 1 #=> 1
13+
iex> use Bitwise
14+
...> 1 &&& 1
15+
1
1316
1417
You can select to include only or skip operators by passing options:
1518
16-
use Bitwise, only_operators: true
17-
1 &&& 1 #=> 1
19+
iex> use Bitwise, only_operators: true
20+
...> 1 &&& 1
21+
1
1822
1923
"""
2024

lib/elixir/lib/code.ex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ defmodule Code do
8484
8585
## Examples
8686
87-
Code.eval "a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line
88-
#=> { 3, [ {:a, 1}, {:b, 2} ] }
87+
iex> Code.eval "a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line
88+
{ 3, [ {:a, 1}, {:b, 2} ] }
8989
9090
For convenience, you can my pass `__ENV__` as argument and
9191
all imports, requires and aliases will be automatically carried
9292
over:
9393
94-
Code.eval "a + b", [a: 1, b: 2], __ENV__
95-
#=> { 3, [ {:a, 1}, {:b, 2} ] }
94+
iex> Code.eval "a + b", [a: 1, b: 2], __ENV__
95+
{ 3, [ {:a, 1}, {:b, 2} ] }
9696
9797
"""
9898
def eval(string, binding // [], opts // [])
@@ -115,16 +115,16 @@ defmodule Code do
115115
116116
## Examples
117117
118-
contents = quote hygiene: false, do: a + b
119-
120-
Code.eval_quoted contents, [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line
121-
#=> { 3, [ {:a, 1}, {:b, 2} ] }
118+
iex> contents = quote hygiene: [vars: false], do: a + b
119+
...> Code.eval_quoted contents, [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line
120+
{ 3, [ {:a, 1}, {:b, 2} ] }
122121
123122
For convenience, you can my pass `__ENV__` as argument and
124123
all options will be automatically extracted from the environment:
125124
126-
Code.eval_quoted contents, [a: 1, b: 2], __ENV__
127-
#=> { 3, [ {:a, 1}, {:b, 2} ] }
125+
iex> contents = quote hygiene: [vars: false], do: a + b
126+
...> Code.eval_quoted contents, [a: 1, b: 2], __ENV__
127+
{ 3, [ {:a, 1}, {:b, 2} ] }
128128
129129
"""
130130
def eval_quoted(quoted, binding // [], opts // [])

lib/elixir/lib/dict.ex

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ defmodule Dict do
2424
dictionaries are also required to implement the `Access`
2525
protocol:
2626
27-
dict = HashDict.new
28-
dict = Dict.put dict, :hello, :world
29-
dict[:hello] #=> :world
27+
iex> dict = HashDict.new
28+
...> dict = Dict.put dict, :hello, :world
29+
...> dict[:hello]
30+
:world
3031
3132
And also the `Enum.Iterator` protocol, allowing one to write:
3233
@@ -77,8 +78,9 @@ defmodule Dict do
7778
7879
## Examples
7980
80-
d = new [a: 1, b: 2]
81-
Dict.keys d #=> [:a,:b]
81+
iex> d = HashDict.new [a: 1, b: 2]
82+
...> Enum.sort Dict.keys d
83+
[:a,:b]
8284
8385
"""
8486
@spec keys(t) :: [key]
@@ -91,8 +93,9 @@ defmodule Dict do
9193
9294
## Examples
9395
94-
d = new [a: 1, b: 2]
95-
Dict.values d #=> [1,2]
96+
iex> d = HashDict.new [a: 1, b: 2]
97+
...> Enum.sort Dict.values d
98+
[1,2]
9699
97100
"""
98101
@spec values(t) :: [value]
@@ -105,8 +108,9 @@ defmodule Dict do
105108
106109
## Examples
107110
108-
d = new [a: 1, b: 2]
109-
Dict.size d #=> 2
111+
iex> d = HashDict.new [a: 1, b: 2]
112+
...> Dict.size d
113+
2
110114
111115
"""
112116
@spec size(t) :: non_neg_integer
@@ -119,9 +123,13 @@ defmodule Dict do
119123
120124
## Examples
121125
122-
d = new [a: 1]
123-
Dict.has_key?(d, :a) #=> true
124-
Dict.has_key?(d, :b) #=> false
126+
iex> d = HashDict.new [a: 1]
127+
...> Dict.has_key?(d, :a)
128+
true
129+
130+
iex> d = HashDict.new [a: 1]
131+
...> Dict.has_key?(d, :b)
132+
false
125133
126134
"""
127135
@spec has_key?(t, key) :: boolean
@@ -135,11 +143,17 @@ defmodule Dict do
135143
136144
## Examples
137145
138-
d = new [a: 1]
139-
Dict.get d, :a #=> 1
140-
Dict.get d, :b #=> nil
141-
Dict.get d, :b, 3 #=> 3
146+
iex> d = HashDict.new [a: 1]
147+
...> Dict.get d, :a
148+
1
149+
150+
iex> d = HashDict.new [a: 1]
151+
...> Dict.get d, :b
152+
nil
142153
154+
iex> d = HashDict.new [a: 1]
155+
...> Dict.get d, :b, 3
156+
3
143157
"""
144158
@spec get(t, key, value) :: value
145159
def get(dict, key, default // nil) do
@@ -152,9 +166,12 @@ defmodule Dict do
152166
153167
## Examples
154168
155-
d = new [a: 1]
156-
Dict.get d, :a #=> 1
157-
Dict.get d, :b #=> raises KeyError[key: :b]
169+
iex> d = HashDict.new [a: 1]
170+
...> Dict.get d, :a
171+
1
172+
iex> d = HashDict.new [a: 1]
173+
...> Dict.get! d, :b
174+
** (KeyError) key not found: :b
158175
159176
"""
160177
@spec get!(t, key) :: value | no_return
@@ -168,9 +185,10 @@ defmodule Dict do
168185
169186
## Examples
170187
171-
d = new [a: 1, b: 2]
172-
Dict.put d, :a, 3
173-
#=> [a: 3, b: 2]
188+
iex> d = HashDict.new [a: 1, b: 2]
189+
...> d = Dict.put d, :a, 3
190+
...> Dict.get d, :a
191+
3
174192
175193
"""
176194
@spec put(t, key, value) :: t
@@ -183,9 +201,10 @@ defmodule Dict do
183201
184202
## Examples
185203
186-
d = new [a: 1, b: 2]
187-
Dict.put_new d, :a, 3
188-
#=> [a: 1, b: 2]
204+
iex> d = HashDict.new [a: 1, b: 2]
205+
...> d = Dict.put_new d, :a, 3
206+
...> Dict.get d, :a
207+
1
189208
190209
"""
191210
@spec put_new(t, key, value) :: t
@@ -199,11 +218,14 @@ defmodule Dict do
199218
200219
## Examples
201220
202-
d = new [a: 1, b: 2]
203-
Dict.delete d, :a #=> [b: 2]
221+
iex> d = HashDict.new [a: 1, b: 2]
222+
...> d = Dict.delete d, :a
223+
...> Dict.get d, :a
224+
nil
204225
205-
d = new [b: 2]
206-
Dict.delete d, :a #=> [b: 2]
226+
iex> d = HashDict.new [b: 2]
227+
...> Dict.delete(d, :a) == d
228+
true
207229
208230
"""
209231
@spec delete(t, key) :: t
@@ -217,10 +239,11 @@ defmodule Dict do
217239
218240
## Examples
219241
220-
d1 = new [a: 1, b: 2]
221-
d2 = new [a: 3, d: 4]
222-
Dict.merge d1, d2
223-
#=> [a: 3, b: 2, d: 4]
242+
iex> d1 = HashDict.new [a: 1, b: 2]
243+
...> d2 = HashDict.new [a: 3, d: 4]
244+
...> d = Dict.merge d1, d2
245+
...> [a: Dict.get(d, :a), b: Dict.get(d, :b), d: Dict.get(d, :d)]
246+
[a: 3, b: 2, d: 4]
224247
225248
"""
226249
@spec merge(t, t) :: t
@@ -235,12 +258,13 @@ defmodule Dict do
235258
236259
## Examples
237260
238-
d1 = new [a: 1, b: 2]
239-
d2 = new [a: 3, d: 4]
240-
Dict.merge d1, d2, fn _k, v1, v2 ->
241-
v1 + v2
242-
end
243-
#=> [a: 4, b: 2, d: 4]
261+
iex> d1 = HashDict.new [a: 1, b: 2]
262+
...> d2 = HashDict.new [a: 3, d: 4]
263+
...> d = Dict.merge d1, d2, fn _k, v1, v2 ->
264+
...> v1 + v2
265+
...> end
266+
...> [a: Dict.get(d, :a), b: Dict.get(d, :b), d: Dict.get(d, :d)]
267+
[a: 4, b: 2, d: 4]
244268
245269
"""
246270
@spec merge(t, t, (key, value, value -> value)) :: t
@@ -254,9 +278,10 @@ defmodule Dict do
254278
255279
## Examples
256280
257-
d = new [a: 1, b: 2]
258-
Dict.update d, :a, fn val -> -val end
259-
#=> [a: -1, b: 2]
281+
iex> d = HashDict.new [a: 1, b: 2]
282+
...> d = Dict.update d, :a, fn val -> -val end
283+
...> Dict.get d, :a
284+
-1
260285
261286
"""
262287
@spec update(t, key, (value -> value)) :: t
@@ -271,9 +296,10 @@ defmodule Dict do
271296
272297
## Examples
273298
274-
d = new [a: 1, b: 2]
275-
Dict.update d, :c, 3, fn val -> -val end
276-
#=> [a: 1, b: 2, c: 3]
299+
iex> d = HashDict.new [a: 1, b: 2]
300+
...> d = Dict.update d, :c, 3, fn val -> -val end
301+
...> Dict.get d, :c
302+
3
277303
278304
"""
279305
@spec update(t, key, value, (value -> value)) :: t

0 commit comments

Comments
 (0)