-
Notifications
You must be signed in to change notification settings - Fork 6
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
FR: Documentation tests #34
Comments
Considering that most of the ecosystem uses tidy-like doc comments in some form or fashion, parsing such tests should be relatively easy. It is probably enough to use /// Frobnicates the input
///
/// ```example
/// // Verify frobnication!
/// #assert(frobnicate(0) != 0)
/// ```
///
/// -> int
#let frobnicate(input) = input + 42 There are some considerations. Identifying and Querying doc testsAt the very least, a Regardless of this, every test needs a unique and somewhat stable identifier. Identifying the item itself should be relatively easy, it should be the file path and top-level item name. This means that we exclude inner functions, renames and re-exports and other non-standard items for now. We need some kind of separator for doc tests, perhaps However, if we want to allow refering to individual examples, then we likely need a label mechanism. Otherwise, adding/removing or reordering examples will affect other example's identifiers, making them unstable. Refering to individual examples will likely be left out in the MVP and be added later if at all, but it should be considered for the syntax of discriminators. Binding availabilityMost doc generators allow adding a prelude which is not shown in the doc comment to make examples concise. We can't easily know where these are defined because they usually get put into the manual. An example would be We need to let the user define these in the config, we can't know the defaults every doc generator defines because neither do we know all doc generators (some may be internal) nor is it feasible to maintain this if we did know them all. This also doesn't account for any other bindings a suer may add in their manual config. ReferencesShould we store references for doc tests and have the user update them? We could treat this like regression tests with a different structure, create the references somewhere else and have them be regression tests, but this means we need another mechanism to mark if a test should be compiled or compared. cc: @jneug, could you tell me how cc: @Mc-Zen, does tidy do anything special here, other than adding the function itself? How do you guys handle name conflicts, shadowing and module paths, i.e. using a function from I'm going to move this to |
Hi, this is indeed a really tricky matter. Personally, I'm not a huge fan of doc tests and just added doc test functionality to tidy upon a request. Imho, tests and docs should really be separated. On the other hand, I understand the need to ensure that the doc examples pass and for that usecase, I'm all in! ScopeWith tidy, the scope of the PreambleCurrently, tidy supports a Execute-only codeInstead, tidy has a special syntax (an idea inherited from the Typst source) where you can write additional lines starting with >>> import draw: * // executed but not displayed
#cetz.canvas({
line(..)
}) This provides way more flexibility, leads to more predictable code and presumably better integration with tooling. Another important aspect to consider here (especially for testing) is layout. Most Typst output depends on the size of the parent container. Usually, a code example in a documentation should just take as much space as it needs, i.e., it should be placed inside a container with automatic dimensions. But in certain cases you'd want a fixed container, for example to demonstrate the usage of |
Do you already have a specific design in mind? I would maybe suggest, that doc tests need their own test suite that needs to be added manually, like
where |
Does this receive any defaults? Is the item which is documented included by default, for example? Otherwise, the example I wrote would already fail by tidy standards.
Ok, that sounds good. I would prefer this too, especially for more elaborate examples which may refer back to values from previous ones.
Presumably so, I haven't thought about this yet, but if references are to be supported, then my first instinct would be to let the user decide what the default page size is for their doc tests. Perhaps in the manifest config.
I don't want to add extra directories unless I have to, but if I want to support references, I'll likely have to add this extra indirection. I've already thought about a structure like this for template tests. But I don't want a magi file which activates doc tests, if people want to disable doc tests they can simply wrap their expressions in With all that said, does tidy only run |
No, it doesn't. You explicitly need to provide it. The reason is that depending on the number of definitions, some packages prefer to use the
Personally, I feel like I'd prefer doc-example testing to be opt-in.
Yes, The
|
Mantys was very much inspired by Tidy in that regard. Nothing is added by default, but users can provide an The #show: #mantys(
// ...
examples-scope: (
// Scope for all examples
scope: (
my-module: mymodule
),
// Preamble for all examples
imports: (
// #import my-module: *
my-module: "*",
// #import my-other-module: func-a, func-b
my-other-module: ("func-a", "func-b")
)
)
) Users may also set
I like this. Maybe would warrant a separate package like self-example that standardizes the compilation of examples.
That was my request back in the day when there was no good testing solution like tytanic. I'm all in favor of separating concerns. Back then, I was developing codetastic and had all these small functions that generated checksums for qr-codes and stuff. For these cases, the doctests were pretty useful, since they had no output and pretty much were a bunch of asserts. Since tytanic can now do compile-only and panic tests, the need for doctests is superfluous. Maybe their use should not be encouraged by supporting them in tytanic and rather provide a tutorial on how to move from doctests to tytanic tests? |
So from what I can gather at this moment, by default the scope is quite literally empty and the example I wrote in my first comment would at the very least need to import
Maybe, on the other hand, it's more of a convention for packages to use than a useful feature on its own.
I don't think that makes them superfluous, examples are often copy-pasted verbatim by a user, ensuring they compile is important, moving them either requires removing them in the docs, or syncing changes between docs and tests. But because they are just part of a comment, the author may not notice that an example in file Thank you two for your swift answers. |
Hm, maybe I misunderstood. I was talking about Tidy doc-tests like: /// #test(
/// `num.my-square(2) == 4`,
/// `num.my-square(4) == 16`,
/// )
#let my-square(n) = n * n |
Ah, yes indeed, those would likely be moved to standalone tytanic tests. I'm just thinking about example code blocks like in the first comment. |
At some point, possibly after 0.1.0,
tytanic
should support doc tests by searching in doc comments for code blocks marked eitherexample
ortest
, run them and possibly compare them if this is desired.The text was updated successfully, but these errors were encountered: