Skip to content

Add option to doctest individual functions #1213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
MasonProtter opened this issue Dec 12, 2019 · 2 comments
Open

Add option to doctest individual functions #1213

MasonProtter opened this issue Dec 12, 2019 · 2 comments

Comments

@MasonProtter
Copy link

MasonProtter commented Dec 12, 2019

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.

@mortenpi
Copy link
Member

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.

@fingolfin
Copy link
Collaborator

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants