Skip to content

Commit 66f27f7

Browse files
committed
fix tests
1 parent a7639a1 commit 66f27f7

File tree

7 files changed

+297
-74
lines changed

7 files changed

+297
-74
lines changed

ext/DocumenterReference.jl

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ using MarkdownAST: MarkdownAST
2121
2222
Enumeration of documentation element types recognized by the API reference generator.
2323
24-
Types include: abstract types, constants, functions, macros, modules, and structs.
24+
# Values
25+
26+
- `DOCTYPE_ABSTRACT_TYPE`: An abstract type declaration
27+
- `DOCTYPE_CONSTANT`: A constant binding (including non-function, non-type values)
28+
- `DOCTYPE_FUNCTION`: A function or callable
29+
- `DOCTYPE_MACRO`: A macro (name starts with `@`)
30+
- `DOCTYPE_MODULE`: A submodule
31+
- `DOCTYPE_STRUCT`: A concrete struct type
2532
"""
2633
@enum(
2734
DocType,
@@ -39,16 +46,20 @@ Types include: abstract types, constants, functions, macros, modules, and struct
3946
Internal configuration for API reference generation.
4047
4148
# Fields
42-
- `current_module::Module`: The module being documented
43-
- `subdirectory::String`: Output directory for generated API pages
44-
- `modules::Dict{Module,<:Vector}`: Mapping of modules to document
45-
- `sort_by::Function`: Custom sort function for symbols
46-
- `exclude::Set{Symbol}`: Symbol names to exclude from documentation
47-
- `public::Bool`: Flag to generate public API page
48-
- `private::Bool`: Flag to generate private API page
49-
- `title::String`: Top-level title for API reference
50-
- `source_files::Vector{String}`: Absolute source file paths used to filter documented symbols (empty means no filtering)
51-
- `filename::String`: Base filename (without extension) used for the underlying markdown file in single-page cases
49+
50+
- `current_module::Module`: The module being documented.
51+
- `subdirectory::String`: Output directory for generated API pages.
52+
- `modules::Dict{Module,<:Vector}`: Mapping of modules to extras (reserved for future use).
53+
- `sort_by::Function`: Custom sort function for symbols.
54+
- `exclude::Set{Symbol}`: Symbol names to exclude from documentation.
55+
- `public::Bool`: Flag to generate public API page.
56+
- `private::Bool`: Flag to generate private API page.
57+
- `title::String`: Title displayed at the top of the generated page.
58+
- `title_in_menu::String`: Title displayed in the navigation menu.
59+
- `source_files::Vector{String}`: Absolute source file paths used to filter documented symbols (empty means no filtering).
60+
- `filename::String`: Base filename (without extension) for the markdown file.
61+
- `include_without_source::Bool`: If `true`, include symbols whose source file cannot be determined.
62+
- `doc_modules::Vector{Module}`: Additional modules to search for docstrings (e.g., `Base`).
5263
"""
5364
struct _Config
5465
current_module::Module
@@ -66,6 +77,14 @@ struct _Config
6677
doc_modules::Vector{Module} # Additional modules to search for docstrings (e.g., Base)
6778
end
6879

80+
"""
81+
CONFIG::Vector{_Config}
82+
83+
Global configuration storage for API reference generation.
84+
85+
Each call to [`automatic_reference_documentation`](@ref) appends a new `_Config`
86+
entry to this vector. Use [`reset_config!`](@ref) to clear it between builds.
87+
"""
6988
const CONFIG = _Config[]
7089

7190
"""
@@ -78,8 +97,26 @@ function reset_config!()
7897
return nothing
7998
end
8099

100+
"""
101+
APIBuilder <: Documenter.Builder.DocumentPipeline
102+
103+
Custom Documenter pipeline stage for automatic API reference generation.
104+
105+
This builder is inserted into the Documenter pipeline at order `0.0` (before
106+
most other stages) to generate API reference pages from the configurations
107+
stored in [`CONFIG`](@ref).
108+
"""
81109
abstract type APIBuilder <: Documenter.Builder.DocumentPipeline end
82110

111+
"""
112+
Documenter.Selectors.order(::Type{APIBuilder}) -> Float64
113+
114+
Return the pipeline order for [`APIBuilder`](@ref).
115+
116+
# Returns
117+
118+
- `Float64`: Always `0.0`, placing this stage early in the Documenter pipeline.
119+
"""
83120
Documenter.Selectors.order(::Type{APIBuilder}) = 0.0
84121

85122
"""
@@ -116,7 +153,19 @@ end
116153
"""
117154
_build_page_path(subdirectory::String, filename::String) -> String
118155
119-
Build the page path, handling the case where subdirectory is "." or empty.
156+
Build the page path by joining subdirectory and filename.
157+
158+
Handles special cases where `subdirectory` is `"."` or empty, returning just
159+
the filename in those cases.
160+
161+
# Arguments
162+
163+
- `subdirectory::String`: Directory path (may be `"."` or empty).
164+
- `filename::String`: The filename to append.
165+
166+
# Returns
167+
168+
- `String`: The combined path, or just `filename` if subdirectory is `"."` or empty.
120169
"""
121170
function _build_page_path(subdirectory::String, filename::String)
122171
if subdirectory == "." || subdirectory == ""
@@ -137,6 +186,8 @@ end
137186
title::String = "API Reference",
138187
filename::String = "",
139188
source_files::Vector{String} = String[],
189+
include_without_source::Bool = false,
190+
doc_modules::Vector{Module} = Module[],
140191
)
141192
142193
Automatically creates the API reference documentation for one or more modules and
@@ -469,15 +520,22 @@ end
469520
"""
470521
_iterate_over_symbols(f, config, symbol_list)
471522
472-
Iterate over symbols, apply a function to each documented symbol.
523+
Iterate over symbols, applying a function to each documented symbol.
473524
474-
Filters symbols based on exclusion list, checks for documentation, and applies the
475-
provided function to each valid symbol. Issues warnings for undocumented symbols.
525+
Filters symbols based on:
526+
1. Exclusion list (`config.exclude`)
527+
2. Presence of documentation (warns and skips undocumented symbols)
528+
3. Source file filtering (`config.source_files`)
476529
477530
# Arguments
478-
- `f::Function`: Function to apply to each (symbol, type) pair
479-
- `config::_Config`: Configuration containing exclusion rules and module info
480-
- `symbol_list::Vector`: List of (Symbol, DocType) pairs to process
531+
532+
- `f::Function`: Callback function `f(key::Symbol, type::DocType)` applied to each valid symbol.
533+
- `config::_Config`: Configuration containing exclusion rules, module info, and source file filters.
534+
- `symbol_list::Vector{Pair{Symbol,DocType}}`: List of symbol-type pairs to process.
535+
536+
# Returns
537+
538+
- `nothing`
481539
"""
482540
function _iterate_over_symbols(f, config, symbol_list)
483541
current_module = config.current_module
@@ -487,9 +545,14 @@ function _iterate_over_symbols(f, config, symbol_list)
487545
continue
488546
end
489547
binding = Base.Docs.Binding(current_module, key)
490-
doc = Base.Docs.doc(binding)
491-
missing_doc =
492-
doc === nothing || occursin("No documentation found.", string(doc))
548+
missing_doc = false
549+
if isdefined(Base.Docs, :hasdoc)
550+
missing_doc = !Base.Docs.hasdoc(binding)
551+
else
552+
doc = Base.Docs.doc(binding)
553+
missing_doc =
554+
doc === nothing || occursin("No documentation found.", string(doc))
555+
end
493556
if missing_doc
494557
if type == DOCTYPE_MODULE
495558
mod = getfield(current_module, key)
@@ -552,16 +615,23 @@ function _to_string(x::DocType)
552615
end
553616

554617
"""
555-
_build_api_page(document, config)
618+
_build_api_page(document::Documenter.Document, config::_Config)
556619
557-
Generate public and private API reference pages for a module.
620+
Generate public and/or private API reference pages for a module.
558621
559-
Creates two markdown pages: one listing exported symbols and one for internal symbols.
560-
Each page includes an overview and docstring references for all documented symbols.
622+
Creates markdown pages listing symbols with their docstrings. When both
623+
`config.public` and `config.private` are `true`, two separate pages are
624+
generated (`public.md` and `private.md`). Otherwise, a single page is created
625+
using `config.filename`.
561626
562627
# Arguments
563-
- `document::Documenter.Document`: Documenter document object
564-
- `config::_Config`: Configuration for the module being documented
628+
629+
- `document::Documenter.Document`: The Documenter document object to add pages to.
630+
- `config::_Config`: Configuration specifying the module, output paths, and filtering options.
631+
632+
# Returns
633+
634+
- `nothing`
565635
"""
566636
function _build_api_page(document::Documenter.Document, config::_Config)
567637
subdir = config.subdirectory

src/description.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ ERROR: AmbiguousDescription: the description (:f,) is ambiguous / incorrect
141141
"""
142142
function complete(list::Symbol...; descriptions::Tuple{Vararg{Description}})::Description
143143
n = length(descriptions)
144+
if n == 0
145+
throw(AmbiguousDescription(list))
146+
end
144147
table = zeros(Int8, n, 2)
145148
for i in 1:n
146149
table[i, 1] = length(intersect(list, descriptions[i]))

test/runtests.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ end
2222
# Main test set running multiple test suites with verbose output and timing information
2323
@testset verbose = true showtiming = true "Base" begin
2424
for name in (
25-
# :code_quality,
26-
# :default,
27-
# :description,
28-
# :exceptions,
29-
# :utils,
25+
:code_quality,
26+
:default,
27+
:description,
28+
:exceptions,
29+
:utils,
3030
:documenter_reference,
3131
)
3232
@testset "$(name)" begin

test/test_description.jl

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ function test_description()
99
@test descriptions[2] == (:b,)
1010
end
1111

12-
# # Type stability test for adding descriptions using the is_inferred macro
13-
# # Needs JET
14-
# @testset "Add Descriptions Type Stability" begin
15-
# @test_opt CTBase.add((), (:a,))
16-
# @test_inferred CTBase.add((), (:a,))
17-
# end
18-
1912
# Test building algorithm descriptions and completing partial descriptions
2013
@testset "Complete Descriptions with Algorithms" begin
2114
algorithms = ()
@@ -40,21 +33,6 @@ function test_description()
4033
(:descent, :gradient, :fixedstep)
4134
end
4235

43-
# # Type stability test for the complete function using the is_inferred macro
44-
# # Needs JET
45-
# @testset "Complete Descriptions Type Stability" begin
46-
# algorithms = ()
47-
# algorithms = CTBase.add(algorithms, (:descent, :bfgs, :bisection))
48-
# algorithms = CTBase.add(algorithms, (:descent, :bfgs, :backtracking))
49-
# algorithms = CTBase.add(algorithms, (:descent, :bfgs, :fixedstep))
50-
# algorithms = CTBase.add(algorithms, (:descent, :gradient, :bisection))
51-
# algorithms = CTBase.add(algorithms, (:descent, :gradient, :backtracking))
52-
# algorithms = CTBase.add(algorithms, (:descent, :gradient, :fixedstep))
53-
54-
# @test_opt CTBase.complete((:descent,); descriptions=algorithms)
55-
# @test_inferred CTBase.complete((:descent,); descriptions=algorithms)
56-
# end
57-
5836
# Test ambiguous or invalid description completions throw errors
5937
@testset "Ambiguous and Incorrect Description Errors" begin
6038
algorithms = ()
@@ -90,7 +68,6 @@ function test_description()
9068

9169
# instead of @inferred, check if the type is a subtype of Tuple{Vararg{Symbol}}
9270
@test typeof(result) <: Tuple{Vararg{Symbol}}
93-
# @test_opt CTBase.remove(x, y)
9471
end
9572

9673
# Test completion with descriptions of different sizes and inclusion priority
@@ -128,5 +105,24 @@ function test_description()
128105
@test output == expected
129106
end
130107

108+
@testset "Base.show Edge Cases" begin
109+
io = IOBuffer()
110+
descriptions = ()
111+
show(io, MIME"text/plain"(), descriptions)
112+
output = String(take!(io))
113+
@test output == ""
114+
115+
io = IOBuffer()
116+
descriptions = ((:a, :b),)
117+
show(io, MIME"text/plain"(), descriptions)
118+
output = String(take!(io))
119+
@test output == "(:a, :b)"
120+
end
121+
122+
@testset "Complete with Empty Descriptions" begin
123+
algorithms = ()
124+
@test_throws CTBase.AmbiguousDescription CTBase.complete(:a; descriptions=algorithms)
125+
end
126+
131127
return nothing
132128
end

0 commit comments

Comments
 (0)