Documenter.jl icon indicating copy to clipboard operation
Documenter.jl copied to clipboard

Add option to `doctest` individual functions

Open MasonProtter opened this issue 6 years ago • 2 comments

It'd be nice if one could do something the following at the REPL:

julia> begin
       """
          f(x)

       add `1` to `x`
       ```jldoctest
       julia> f(1)
       2
       ```
       """
       f(x) = x + 1
       end
f

julia> doctest(f)
┌ Info: Testing the documentation for f: total 1 test
└ Test passed!
true

This way, one can use the doctest functionality without having to do all the other things documenter seems to require like having the function in a package, making doc directories, etc.

MasonProtter avatar Dec 12 '19 19:12 MasonProtter

Good idea. It shouldn't be very hard to add methods that accept Function and DataType to doctest. Just need to construct the Binding and look up the docstring associated with the function/type. Implementation is up for grabs though.

mortenpi avatar Dec 14 '19 20:12 mortenpi

Just to say, in Oscar.jl we have this:

"""
    doctest(f::Function; set_meta::Bool = false)

Run all doctests for the given function `f`.
"""
function doctest(f::Function; set_meta::Bool = false, doctest = true)
  S = Symbol(f)
  doc, doctest = get_document(set_meta; doctest=doctest)

  withenv("COLUMNS"=>80, "LINES"=>24) do
    with_unicode(false) do
      #essentially inspired by Documenter/src/DocTests.jl
      pm = parentmodule(f)
      bm = Base.Docs.meta(pm)
      md = bm[Base.Docs.Binding(pm, S)]
      for s in md.order
        doctest(md.docs[s], Oscar, doc)
      end
    end
  end
end

Perhaps something like that would be suitable for addition to Documenter?

Actually we also have this super helpful helper:

"""
    doctest_fix(f::Function; set_meta::Bool = false)

Fixes all doctests for the given function `f`.

# Examples
The following call fixes all doctests for the function `symmetric_group`:
```julia
julia> Oscar.doctest_fix(symmetric_group)
```
"""
doctest_fix(f::Function; set_meta::Bool = false) = doctest(f; set_meta = set_meta, doctest = :fix)

(And then a bunch more which take a string indicating path and test/fix all files under that path)

fingolfin avatar Jun 11 '25 13:06 fingolfin