-
Notifications
You must be signed in to change notification settings - Fork 496
Easier doctests of non-exported functions #2126
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
Comments
You can set up shared setup code to all docstrings in a module: https://documenter.juliadocs.org/dev/man/doctests/#Module-level-metadata But if this about actually dynamically figuring out what to import, then I don't think we really have any mechanism for that. |
Hmmm, I'm not quite sure how much of a help docmeta is. I suppose one could do What I was hoping for was dynamic importing of just the function being tested. I can think of two other ways this might be done as a setup step:
|
I don't think we'd want to I would also argue that importing everything would be the more useful behavior anyway, as opposed to just importing a single function, since doctests often reference other functions from the same module as well, and not just the one the docstring is attached to. So the sort of "evaluate in the current module context (but still in a different module)" behavior seems good (and useful) to me. So maybe it could even be a built-in option here. |
Particularly considering multi-step doctests which introduce variable bindings, the I'm glad to hear that "importing everything via |
I don't have the time to work on this ATM (or any time soon likely), but now it's been a few years I think it may be worth bumping this and stating that I still think this would be a worthwhile QoL improvement. I've found myself using |
But what is the plan here? Importing everything surely can't be default behavior. It'd be breaking as hell among other things, and it would also render All in all this seems like a valid but somewhat niche requirement (BTW we also use The way I understood @mortenpi is that if you want this, you can add a (global) for i in names(Module) # possibly add `all=true`
@eval import Module: $i
end Alas, that would not require any change to But it seems both of you have something else in mind, but what then? A flag for |
I guess the main ask here is to make it a bit easier to have somewhat complex setup code, but that needs to change slightly per doctest, but without having to write a whole complex One pattern that I tend to use in more complex cases is to have my setup code in a reusable function in function do_my_setup!() ... end and then I call my doctests with
Potentially you could also pass arguments here, like the name of the function you're testing (though you'd have to keep that in sync automatically). To help with that, one potential extension of this pattern could be something where you have a function that Documenter will pass some context to, that could identify which doctest it is etc: function do_my_setup!(::DocumenterDocTestContext) ... end I think the syntax for passing a function reference to
|
Just to say: for Oscar, Nemo, AbstractAlgebra we use a similar method as that described by @mortenpi (I'll focus on Oscar in the examples): we have functions like
This way we have consistent setup for our doctests in all situations. I wish this was a bit more "automatic"... indeed simplifying 3 is the main motivation for my PR #2697 resp. issue #2512 Basically every package I've worked on uses doctests extensively and as a result I found myself implementing solutions to this again and again. I am wondering whether it would make sense to generalize this solution and add something to Documenter make it easier to achieve... Like, Or slightly more general: "If there is a function The As to the function find_doctestsetup(mod::Module)
isdefined(mod, :doctestsetup) && return mod.doctestsetup
parentmodule(mod) === mod && return nothing
return find_doctestsetup(parentmodule(mod))
end (CC @lgoettgens @thofma ) |
This sounds brilliant to me. I'm a big fan of taking common "and then you'll want to do X" steps and promoting them into part of what you get OOTB. |
Hello,
I've recently started adding more doctests to unexported functions in my code, and I seem to be repeating the same boilerplate-y
setup
quite a bit, e.g.Is there some way this boilerplate could be avoided?
The text was updated successfully, but these errors were encountered: