forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocs.jl
536 lines (397 loc) · 9.24 KB
/
docs.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# This file is a part of Julia. License is MIT: http://julialang.org/license
@doc "Doc abstract type" ->
abstract C74685 <: AbstractArray
@test stringmime("text/plain", Docs.doc(C74685))=="Doc abstract type\n"
macro macro_doctest() end
@doc "Helps test if macros can be documented with `@doc \"...\" -> @...`." ->
:@macro_doctest
@test (@doc @macro_doctest) != nothing
# issue #11548
module ModuleMacroDoc
macro m() end
end
@doc ("I am a module";) ModuleMacroDoc
@doc ("I am a macro";) :@ModuleMacroDoc.m
@test (@doc ModuleMacroDoc) == "I am a module"
@test (@doc ModuleMacroDoc.@m) == "I am a macro"
# General tests for docstrings.
module DocsTest
"DocsTest"
DocsTest
"f-1"
function f(x)
x
end
"f-2"
f(x, y) = x + y
"s-1"
@generated function s(x)
:(x)
end
"s-2"
@generated s(x, y) = :(x + y)
"g"
function g end
"AT"
abstract AT
"BT"
bitstype 8 BT
"T"
type T <: AT
"T.x"
x
"T.y"
y :: Int
end
"IT"
immutable IT
"IT.x"
x :: Int
"IT.y"
y
end
"TA"
typealias TA Union{T, IT}
"@mac"
macro mac() end
"G"
G = :G
"K"
const K = :K
# Adding docstrings to methods after definition.
t(x::AbstractString) = x
t(x::Int, y) = y
t{S <: Integer}(x::S) = x
"t-1"
t(::AbstractString)
"t-2"
t(::Int, ::Any)
"t-3"
t{S <: Integer}(::S)
"FieldDocs"
type FieldDocs
"one"
one
doc"two"
two
three
end
"h/0-3"
h(x = 1, y = 2, z = 3) = x + y + z
# Issue #12700.
module Inner
macro m() end
end
import .Inner.@m
"Inner.@m"
:@m
type Foo
x
end
# value with no docs
const val = Foo(1.0)
end
import Base.Docs: meta
function docstrings_equal(d1, d2)
io1 = IOBuffer()
io2 = IOBuffer()
writemime(io1, MIME"text/markdown"(), d1)
writemime(io2, MIME"text/markdown"(), d2)
takebuf_string(io1) == takebuf_string(io2)
end
@test meta(DocsTest)[DocsTest] == doc"DocsTest"
# Check that plain docstrings store a module reference.
# https://github.com/JuliaLang/julia/pull/13017#issuecomment-138618663
@test meta(DocsTest)[DocsTest].meta[:module] == DocsTest
let f = DocsTest.f
funcdoc = meta(DocsTest)[f]
@test funcdoc.main == nothing
@test docstrings_equal(funcdoc.meta[Tuple{Any}], doc"f-1")
@test docstrings_equal(funcdoc.meta[Tuple{Any,Any}], doc"f-2")
end
let s = DocsTest.s
funcdoc = meta(DocsTest)[s]
@test funcdoc.main == nothing
@test docstrings_equal(funcdoc.meta[Tuple{Any,}], doc"s-1")
@test docstrings_equal(funcdoc.meta[Tuple{Any,Any}], doc"s-2")
end
let g = DocsTest.g
funcdoc = meta(DocsTest)[g]
@test docstrings_equal(funcdoc.meta[Union{}], doc"g")
end
let h = DocsTest.h
funcdoc = meta(DocsTest)[h]
sig = Union{Tuple{}, Tuple{Any}, Tuple{Any, Any}, Tuple{Any, Any, Any}}
@test docstrings_equal(funcdoc.meta[sig], doc"h/0-3")
end
let AT = DocsTest.AT
@test meta(DocsTest)[AT] == doc"AT"
end
let BT = DocsTest.BT
@test meta(DocsTest)[BT] == doc"BT"
end
let T = DocsTest.T
typedoc = meta(DocsTest)[T]
@test docstrings_equal(typedoc.main, doc"T")
@test docstrings_equal(typedoc.fields[:x], doc"T.x")
@test docstrings_equal(typedoc.fields[:y], doc"T.y")
end
let IT = DocsTest.IT
typedoc = meta(DocsTest)[IT]
@test docstrings_equal(typedoc.main, doc"IT")
@test docstrings_equal(typedoc.fields[:x], doc"IT.x")
@test docstrings_equal(typedoc.fields[:y], doc"IT.y")
end
@test @doc(DocsTest.TA) == doc"TA"
@test @doc(DocsTest.@mac) == doc"@mac"
@test @doc(DocsTest.G) == doc"G"
@test @doc(DocsTest.K) == doc"K"
let d1 = @doc(DocsTest.t(::AbstractString)),
d2 = doc"t-1"
@test docstrings_equal(d1,d2)
end
let d1 = @doc(DocsTest.t(::AbstractString)),
d2 = doc"t-1"
@test docstrings_equal(d1,d2)
end
let d1 = @doc(DocsTest.t(::Int, ::Any)),
d2 = doc"t-2"
@test docstrings_equal(d1,d2)
end
let d1 = @doc(DocsTest.t{S <: Integer}(::S)),
d2 = doc"t-3"
@test docstrings_equal(d1,d2)
end
let fields = meta(DocsTest)[DocsTest.FieldDocs].fields
@test haskey(fields, :one) && fields[:one] == doc"one"
@test haskey(fields, :two) && fields[:two] == doc"two"
end
"BareModule"
baremodule BareModule
"f/1"
f(x) = x
"g/1"
function g(x) end
"h"
function h end
"@m"
macro m() end
"C"
const C = 1
"A"
abstract A
"T"
type T
"x"
x
"y"
y
end
end
@test docstrings_equal(@doc(BareModule), doc"BareModule")
@test docstrings_equal(@doc(BareModule.f), doc"f/1")
@test docstrings_equal(@doc(BareModule.g), doc"g/1")
@test docstrings_equal(@doc(BareModule.@m), doc"@m")
@test docstrings_equal(@doc(BareModule.C), doc"C")
@test docstrings_equal(@doc(BareModule.A), doc"A")
@test docstrings_equal(@doc(BareModule.T), doc"T")
# test that when no docs exist, they fallback to
# the docs for the typeof(value)
let d1 = @doc(DocsTest.val)
@test d1 !== nothing
end
# Issue #12700.
@test @doc(DocsTest.@m) == doc"Inner.@m"
# issue 11993
# Check if we are documenting the expansion of the macro
macro m1_11993()
end
macro m2_11993()
symbol("@m1_11993")
end
@doc "This should document @m1... since its the result of expansion" @m2_11993
@test (@doc @m1_11993) !== nothing
let d = (@doc @m2_11993)
@test docstrings_equal(d, doc"""
No documentation found.
```julia
@m2_11993()
```
""")
end
@doc "Now @m2... should be documented" :@m2_11993
@test (@doc @m2_11993) !== nothing
"Document inline function"
@inline f1_11993() = nothing
@test (@doc f1_11993) !== nothing
f1_11993()
@doc "Document inline function with old syntax" ->
@inline f2_11993() = nothing
@test (@doc f2_11993) !== nothing
f2_11993()
# issue #11798
module I11798
"read"
read(x) = x
end
let fd = Base.Docs.meta(I11798)[I11798.read],
d1 = fd.meta[fd.order[1]],
d2 = doc"read"
@test docstrings_equal(d1,d2)
end
module I12515
immutable EmptyType{T} end
"A new method"
Base.collect{T}(::Type{EmptyType{T}}) = "borked"
end
let fd = meta(I12515)[Base.collect]
@test fd.order[1] == Tuple{Type{I12515.EmptyType{TypeVar(:T, Any, true)}}}
end
# PR #12593
"$(1 + 1)"
f12593_1() = 1
"$(1 + 1) 2"
f12593_2() = 1
@test (@doc f12593_1) !== nothing
@test (@doc f12593_2) !== nothing
@test Docs.doc(svdvals, Tuple{Vector{Float64}}) === nothing
@test Docs.doc(svdvals, Tuple{Float64}) !== nothing
# crude test to make sure we sort docstring output by method specificity
@test !docstrings_equal(Docs.doc(getindex, Tuple{Dict{Int,Int},Int}),
Docs.doc(getindex, Tuple{Type{Int64},Int}))
# test that macro documentation works
@test (Docs.@repl @assert) !== nothing
# Simple tests for apropos:
@test contains(sprint(apropos, "pearson"), "cov")
@test contains(sprint(apropos, r"ind(exes|ices)"), "eachindex")
@test contains(sprint(apropos, "print"), "Profile.print")
# Issue #13068.
module I13068
module A
export foo
"""
foo from A
"""
foo(::Int) = 1
end
module B
import ..A: foo
export foo
"""
foo from B
"""
foo(::Float64) = 2
end
end
@test docstrings_equal(
@doc(I13068.A.foo),
doc"""
foo from A
foo from B
"""
)
@test docstrings_equal(Docs.doc(I13068.A.foo, Tuple{Int}), doc"foo from A")
@test docstrings_equal(Docs.doc(I13068.A.foo, Tuple{Float64}), doc"foo from B")
@test Docs.doc(I13068.A.foo, Tuple{Char}) === nothing
# Undocumented DataType Summaries.
module Undocumented
abstract A
abstract B <: A
type C <: A end
immutable D <: B
one
two::UTF8String
three::Float64
end
f = () -> nothing
undocumented() = 1
undocumented(x) = 2
undocumented(x,y) = 3
end
@test docstrings_equal(@doc(Undocumented.A), doc"""
No documentation found.
**Summary:**
```julia
abstract Undocumented.A <: Any
```
**Subtypes:**
```julia
Undocumented.B
Undocumented.C
```
""")
@test docstrings_equal(@doc(Undocumented.B), doc"""
No documentation found.
**Summary:**
```julia
abstract Undocumented.B <: Undocumented.A
```
**Subtypes:**
```julia
Undocumented.D
```
""")
@test docstrings_equal(@doc(Undocumented.C), doc"""
No documentation found.
**Summary:**
```julia
type Undocumented.C <: Undocumented.A
```
""")
@test docstrings_equal(@doc(Undocumented.D), doc"""
No documentation found.
**Summary:**
```julia
immutable Undocumented.D <: Undocumented.B
```
**Fields:**
```julia
one :: Any
two :: UTF8String
three :: Float64
```
""")
let d = @doc Undocumented.f
io = IOBuffer()
writemime(io, MIME"text/markdown"(), d)
@test startswith(takebuf_string(io),"""
No documentation found.
`Undocumented.f` is an anonymous `Function`.
""")
end
let d = @doc Undocumented.undocumented
io = IOBuffer()
writemime(io, MIME"text/markdown"(), d)
@test startswith(takebuf_string(io), """
No documentation found.
`Undocumented.undocumented` is a generic `Function`.
""")
end
# Bindings.
import Base.Docs: @var, Binding
let x = Binding(Base, symbol("@time"))
@test @var(@time) == x
@test @var(Base.@time) == x
@test @var(Base.Pkg.@time) == x
end
let x = Binding(Base.LinAlg, :norm)
@test @var(norm) == x
@test @var(Base.norm) == x
@test @var(Base.LinAlg.norm) == x
@test @var(Base.Pkg.Dir.norm) == x
end
let x = Binding(Core, :Int)
@test @var(Int) == x
@test @var(Base.Int) == x
@test @var(Core.Int) == x
@test @var(Base.Pkg.Resolve.Int) == x
end
let x = Binding(Base, :Pkg)
@test @var(Pkg) == x
@test @var(Base.Pkg) == x
@test @var(Main.Pkg) == x
end
let x = Binding(Base, :VERSION)
@test @var(VERSION) == x
@test @var(Base.VERSION) == x
end