-
Notifications
You must be signed in to change notification settings - Fork 556
Start documenting tests/rustdoc-json #2422
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
Merged
aDotInTheVoid
merged 5 commits into
rust-lang:master
from
aDotInTheVoid:tenthousandyears
Sep 5, 2025
+84
−2
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,83 @@ | ||
# The `rustdoc-json` test suite | ||
|
||
> **FIXME**: This section is a stub. It will be populated by [PR #2422](https://github.com/rust-lang/rustc-dev-guide/pull/2422/). | ||
This page is specifically about the test suite named `rustdoc-json`, which tests rustdoc's [json output]. | ||
For other test suites used for testing rustdoc, see [§Rustdoc test suites](../tests/compiletest.md#rustdoc-test-suites). | ||
|
||
Tests are run with compiletest, and have access to the usual set of [directives](../tests/directives.md). | ||
Frequenly used directives here are: | ||
aDotInTheVoid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- [`//@ aux-build`][aux-build] to have dependencies. | ||
- `//@ edition: 2021` (or some other edition). | ||
- `//@ compile-flags: --document-hidden-items` to enable [document private items]. | ||
|
||
Each crate's json output is checked by 2 programs: [jsondoclint](#jsondocck) and [jsondocck](#jsondocck). | ||
|
||
## jsondoclint | ||
|
||
[jsondoclint] checks that all [`Id`]s exist in the `index` (or `paths`). | ||
This makes sure there are no dangling [`Id`]s. | ||
|
||
<!-- TODO: It does some more things too? | ||
Also, talk about how it works | ||
--> | ||
|
||
## jsondocck | ||
|
||
[jsondocck] processes direcives given in comments, to assert that the values in the output are expected. | ||
It's alot like [htmldocck](./rustdoc-test-suite.md) in that way. | ||
|
||
It uses [JSONPath] as a query language, which takes a path, and returns a *list* of values that that path is said to match to. | ||
|
||
### Directives | ||
|
||
- `//@ has <path>`: Checks `<path>` exists, i.e. matches at least 1 value. | ||
- `//@ !has <path>`: Checks `<path>` doesn't exist, i.e. matches 0 values. | ||
- `//@ has <path> <value>`: Check `<path>` exists, and at least 1 of the matches is equal to the given `<value>` | ||
- `//@ !has <path> <value>`: Checks `<path>` exists, but none of the matches equal the given `<value>`. | ||
- `//@ is <path> <value>`: Check `<path>` matches exactly one value, and it's equal to the given `<value>`. | ||
- `//@ is <path> <value> <value>...`: Check that `<path>` matches to exactly every given `<value>`. | ||
Ordering doesn't matter here. | ||
- `//@ !is <path> <value>`: Check `<path>` matches exactly one value, and that value is not equal to the given `<value>`. | ||
- `//@ count <path> <number>`: Check that `<path>` matches to `<number>` of values. | ||
- `//@ set <name> = <path>`: Check that `<path>` matches exactly one value, and store that value to the variable called `<name>`. | ||
|
||
These are defined in [`directive.rs`]. | ||
|
||
### Values | ||
|
||
Values can be either JSON values, or variables. | ||
|
||
- JSON values are JSON literals, e.g. `true`, `"string"`, `{"key": "value"}`. | ||
These often need to be quoted using `'`, to be processed as 1 value. See [§Argument spliting](#argument-spliting) | ||
- Variables can be used to store the value in one path, and use it in later queries. | ||
They are set with the `//@ set <name> = <path>` directive, and accessed with `$<name>` | ||
|
||
```rust | ||
//@ set foo = $some.path | ||
//@ is $.some.other.path $foo | ||
``` | ||
|
||
### Argument spliting | ||
|
||
Arguments to directives are split using the [shlex] crate, which implements POSIX shell escaping. | ||
This is because both `<path>` and `<value>` arguments to [directives](#directives) frequently have both | ||
whitespace and quote marks. | ||
|
||
To use the `@ is` with a `<path>` of `$.index[?(@.docs == "foo")].some.field` and a value of `"bar"` [^why_quote], you'd write: | ||
|
||
```rust | ||
//@ is '$.is[?(@.docs == "foo")].some.field' '"bar"' | ||
``` | ||
|
||
[^why_quote]: The value needs to be `"bar"` *after* shlex splitting, because we | ||
it needs to be a JSON string value. | ||
|
||
[json output]: https://doc.rust-lang.org/nightly/rustdoc/unstable-features.html#json-output | ||
[jsondocck]: https://github.com/rust-lang/rust/tree/master/src/tools/jsondocck | ||
[jsondoclint]: https://github.com/rust-lang/rust/tree/master/src/tools/jsondoclint | ||
[aux-build]: ../tests/compiletest.md#building-auxiliary-crates | ||
[`Id`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc_json_types/struct.Id.html | ||
[document private items]: https://doc.rust-lang.org/nightly/rustdoc/command-line-arguments.html#--document-private-items-show-items-that-are-not-public | ||
[`directive.rs`]: https://github.com/rust-lang/rust/blob/master/src/tools/jsondocck/src/directive.rs | ||
[shlex]: https://docs.rs/shlex/1.3.0/shlex/index.html | ||
[JSONPath]: https://www.rfc-editor.org/rfc/rfc9535.html |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you link to here from https://rustc-dev-guide.rust-lang.org/rustdoc.html#tests ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in #2298 (which added this page as a stub, and linked to it).