@@ -3,126 +3,147 @@ Code.require_file "../test_helper.exs", __DIR__
3
3
defmodule Kernel.DocsTest do
4
4
use ExUnit.Case
5
5
6
+ import PathHelpers
7
+
6
8
test "attributes format" do
7
9
defmodule DocAttributes do
8
10
@ moduledoc "Module doc"
9
11
assert @ moduledoc == "Module doc"
10
- assert Module . get_attribute ( __MODULE__ , :moduledoc ) == { 8 , "Module doc" }
12
+ assert Module . get_attribute ( __MODULE__ , :moduledoc ) == { __ENV__ . line - 2 , "Module doc" }
11
13
12
14
@ typedoc "Type doc"
13
15
assert @ typedoc == "Type doc"
14
- assert Module . get_attribute ( __MODULE__ , :typedoc ) == { 12 , "Type doc" }
16
+ assert Module . get_attribute ( __MODULE__ , :typedoc ) == { __ENV__ . line - 2 , "Type doc" }
15
17
@ type foobar :: any
16
18
17
19
@ doc "Function doc"
18
20
assert @ doc == "Function doc"
19
- assert Module . get_attribute ( __MODULE__ , :doc ) == { 17 , "Function doc" }
21
+ assert Module . get_attribute ( __MODULE__ , :doc ) == { __ENV__ . line - 2 , "Function doc" }
20
22
def foobar ( ) do
21
23
:ok
22
24
end
23
25
end
24
26
end
25
27
26
- test "compiled with docs" do
27
- deftestmodule ( SampleDocs )
28
-
29
- docs = Code . get_docs ( SampleDocs , :all )
30
- assert Code . get_docs ( SampleDocs , :docs ) == docs [ :docs ]
31
- assert Code . get_docs ( SampleDocs , :moduledoc ) == docs [ :moduledoc ]
32
- assert Code . get_docs ( SampleDocs , :type_docs ) == docs [ :type_docs ]
33
- assert Code . get_docs ( SampleDocs , :callback_docs ) == docs [ :callback_docs ]
34
-
35
- assert [ { { :arg_names , 5 } , _ , :def ,
36
- [ { :list1 , [ ] , Elixir } ,
37
- { :list2 , [ ] , Elixir } ,
38
- { :map1 , [ ] , Elixir } ,
39
- { :list3 , [ ] , Elixir } ,
40
- { :map2 , [ ] , Elixir } ] , nil } ,
41
- { { :foo , 1 } , _ , :def , [ { :arg , [ ] , nil } ] , "Function doc" } ,
42
- { { :foobar , 0 } , _ , :def , [ ] , nil } ,
43
- { { :qux , 1 } , _ , :def , [ { :bool , [ ] , Elixir } ] , false } ,
44
- { { :with_defaults , 4 } , _ , :def ,
45
- [ { :int , [ ] , Elixir } ,
46
- { :\\ , [ ] , [ { :arg , [ ] , nil } , 0 ] } ,
47
- { :\\ , [ ] , [ { :year , [ ] , nil } , 2015 ] } ,
48
- { :\\ , [ ] , [ { :fun , [ ] , nil } , { :& , _ , [ { :/ , _ , [ { :>= , _ , nil } , 2 ] } ] } ] } ] , nil } ] = docs [ :docs ]
49
-
50
- assert { _ , "Module doc" } = docs [ :moduledoc ]
51
-
52
- assert [ { { :bar , 1 } , _ , :opaque , "Opaque type doc" } ,
53
- { { :foo , 1 } , _ , :type , "Type doc" } ] = docs [ :type_docs ]
54
-
55
- assert [ { { :bar , 0 } , _ , :callback , false } ,
56
- { { :baz , 2 } , _ , :callback , nil } ,
57
- { { :foo , 1 } , _ , :callback , "Callback doc" } ,
58
- { { :qux , 1 } , _ , :macrocallback , "Macrocallback doc" } ] = docs [ :callback_docs ]
59
- end
60
-
61
28
test "compiled without docs" do
62
29
Code . compiler_options ( docs: false )
63
30
64
- deftestmodule ( WithoutSampleDocs )
31
+ write_beam ( defmodule WithoutDocs do
32
+ @ moduledoc "Module doc"
65
33
66
- assert Code . get_docs ( WithoutSampleDocs , :docs ) == nil
67
- assert Code . get_docs ( WithoutSampleDocs , :moduledoc ) == nil
68
- assert Code . get_docs ( WithoutSampleDocs , :type_docs ) == nil
69
- assert Code . get_docs ( WithoutSampleDocs , :callback_docs ) == nil
34
+ @ doc "Some doc"
35
+ def foobar ( arg ) , do: arg
36
+ end )
37
+
38
+ assert Code . get_docs ( WithoutDocs , :docs ) == nil
39
+ assert Code . get_docs ( WithoutDocs , :moduledoc ) == nil
40
+ assert Code . get_docs ( WithoutDocs , :type_docs ) == nil
41
+ assert Code . get_docs ( WithoutDocs , :callback_docs ) == nil
70
42
after
71
43
Code . compiler_options ( docs: true )
72
44
end
73
45
74
46
test "compiled in memory does not have accessible docs" do
75
- defmodule WithoutDocs do
47
+ defmodule InMemoryDocs do
76
48
@ moduledoc "Module doc"
77
49
78
50
@ doc "Some doc"
79
51
def foobar ( arg ) , do: arg
80
52
end
81
53
82
- assert Code . get_docs ( NoDocs , :docs ) == nil
83
- assert Code . get_docs ( NoDocs , :moduledoc ) == nil
84
- assert Code . get_docs ( NoDocs , :callback_docs ) == nil
54
+ assert Code . get_docs ( InMemoryDocs , :docs ) == nil
55
+ assert Code . get_docs ( InMemoryDocs , :moduledoc ) == nil
56
+ assert Code . get_docs ( InMemoryDocs , :callback_docs ) == nil
85
57
end
86
58
87
- defp deftestmodule ( name ) do
88
- import PathHelpers
59
+ describe "compiled with docs" do
60
+ test "infers signatures" do
61
+ write_beam ( defmodule SignatureDocs do
62
+ def arg_names ( [ ] , [ ] , % { } , [ ] , % { } ) , do: false
63
+
64
+ @ year 2015
65
+ def with_defaults ( @ year , arg \\ 0 , year \\ @ year , fun \\ & >= / 2 ) do
66
+ { fun , arg + year }
67
+ end
68
+
69
+ def with_struct ( % URI { } ) , do: :ok
70
+
71
+ def with_underscore ( { _ , _ } = _two_tuple ) , do: :ok
72
+ def with_underscore ( _ ) , do: :error
73
+
74
+ def only_underscore ( _ ) , do: :ok
75
+
76
+ def two_good_names ( first , :ok ) , do: first
77
+ def two_good_names ( second , :error ) , do: second
78
+ end )
79
+
80
+ assert [ { { :arg_names , 5 } , _ , :def ,
81
+ [ { :list1 , _ , Elixir } ,
82
+ { :list2 , _ , Elixir } ,
83
+ { :map1 , _ , Elixir } ,
84
+ { :list3 , _ , Elixir } ,
85
+ { :map2 , _ , Elixir } ] , nil } ,
86
+ { { :only_underscore , 1 } , _ , :def ,
87
+ [ { :_ , _ , Elixir } ] , nil } ,
88
+ { { :two_good_names , 2 } , _ , :def ,
89
+ [ { :first , _ , nil } ,
90
+ { :atom , _ , Elixir } ] , nil } ,
91
+ { { :with_defaults , 4 } , _ , :def ,
92
+ [ { :int , _ , Elixir } ,
93
+ { :\\ , _ , [ { :arg , _ , nil } , 0 ] } ,
94
+ { :\\ , _ , [ { :year , _ , nil } , 2015 ] } ,
95
+ { :\\ , _ , [ { :fun , _ , nil } , { :& , _ , [ { :/ , _ , [ { :>= , _ , nil } , 2 ] } ] } ] } ] , nil } ,
96
+ { { :with_struct , 1 } , _ , :def , [ { :uri , _ , Elixir } ] , nil } ,
97
+ { { :with_underscore , 1 } , _ , :def , [ { :two_tuple , _ , nil } ] , nil } ] = Code . get_docs ( SignatureDocs , :docs )
98
+ end
89
99
90
- write_beam ( defmodule name do
91
- @ moduledoc "Module doc"
100
+ test "includes docs for functions, modules, types and callbacks" do
101
+ write_beam ( defmodule SampleDocs do
102
+ @ moduledoc "Module doc"
92
103
93
- @ typedoc "Type doc"
94
- @ type foo ( any ) :: any
104
+ @ typedoc "Type doc"
105
+ @ type foo ( any ) :: any
95
106
96
- @ typedoc "Opaque type doc"
97
- @ opaque bar ( any ) :: any
107
+ @ typedoc "Opaque type doc"
108
+ @ opaque bar ( any ) :: any
98
109
99
- @ doc "Callback doc"
100
- @ callback foo ( any ) :: any
110
+ @ doc "Callback doc"
111
+ @ callback foo ( any ) :: any
101
112
102
- @ doc false
103
- @ callback bar ( ) :: term
113
+ @ doc false
114
+ @ callback bar ( ) :: term
115
+ @ callback baz ( any , term ) :: any
104
116
105
- @ callback baz ( any , term ) :: any
117
+ @ doc "Macrocallback doc"
118
+ @ macrocallback qux ( any ) :: any
106
119
107
- @ doc "Macrocallback doc"
108
- @ macrocallback qux ( any ) :: any
120
+ @ doc "Function doc"
121
+ def foo ( arg ) do
122
+ arg + 1
123
+ end
109
124
110
- @ doc "Function doc"
111
- def foo ( arg ) do
112
- arg + 1
113
- end
125
+ @ doc false
126
+ def bar ( true ) , do: false
127
+ end )
114
128
115
- @ doc false
116
- def qux ( true ) , do: false
129
+ docs = Code . get_docs ( SampleDocs , :all )
130
+ assert Code . get_docs ( SampleDocs , :docs ) == docs [ :docs ]
131
+ assert Code . get_docs ( SampleDocs , :moduledoc ) == docs [ :moduledoc ]
132
+ assert Code . get_docs ( SampleDocs , :type_docs ) == docs [ :type_docs ]
133
+ assert Code . get_docs ( SampleDocs , :callback_docs ) == docs [ :callback_docs ]
117
134
118
- def foobar ( ) , do: nil
135
+ assert [ { { :bar , 1 } , _ , :def , [ { :bool , _ , Elixir } ] , false } ,
136
+ { { :foo , 1 } , _ , :def , [ { :arg , _ , nil } ] , "Function doc" } ] = docs [ :docs ]
119
137
120
- def arg_names ( [ ] , [ ] , % { } , [ ] , % { } ) , do: false
138
+ assert { _ , "Module doc" } = docs [ :moduledoc ]
121
139
122
- @ year 2015
123
- def with_defaults ( @ year , arg \\ 0 , year \\ @ year , fun \\ & >= / 2 ) do
124
- { fun , arg + year }
125
- end
126
- end )
140
+ assert [ { { :bar , 1 } , _ , :opaque , "Opaque type doc" } ,
141
+ { { :foo , 1 } , _ , :type , "Type doc" } ] = docs [ :type_docs ]
142
+
143
+ assert [ { { :bar , 0 } , _ , :callback , false } ,
144
+ { { :baz , 2 } , _ , :callback , nil } ,
145
+ { { :foo , 1 } , _ , :callback , "Callback doc" } ,
146
+ { { :qux , 1 } , _ , :macrocallback , "Macrocallback doc" } ] = docs [ :callback_docs ]
147
+ end
127
148
end
128
149
end
0 commit comments