|
| 1 | +# `rustdoc` tests |
| 2 | + |
| 3 | +This page is specifically about the `rustdoc` test suite, for other test suites used for testing rustdoc, see [Rustdoc § Tests](../rustdoc.md#tests). |
| 4 | + |
| 5 | +`htmldocck.py` is a custom checker script that uses [XPath] to verify the HTML output of rustdoc. |
| 6 | + |
| 7 | +[XPath]: https://en.wikipedia.org/wiki/XPath |
| 8 | + |
| 9 | +## Directives |
| 10 | +Directives to htmldocck are similar to those given to `compiletest` in that they take the form of `//@` comments. |
| 11 | + |
| 12 | +All `PATH`s in directives are relative to the the rustdoc output directory (`build/TARGET/test/rustdoc/TESTNAME`), |
| 13 | +so it is conventional to use a `#![crate_name = "foo"]` attribute to avoid writing paths. |
| 14 | +To avoid repetion, `-` can be used in any `PATH` argument to re-use the previous `PATH` argument. |
| 15 | + |
| 16 | +All arguments take the form of quoted strings, |
| 17 | +with the exception of `COUNT` and the special `-` form of `PATH`. |
| 18 | + |
| 19 | +Directives are assertions that place constraints on the generated HTML. |
| 20 | + |
| 21 | +All directives (except `files`) can be negated by putting a `!` in front of their name. |
| 22 | + |
| 23 | +Similar to shell commands, |
| 24 | +directives can extend across multiple lines if their last char is `\`. |
| 25 | +In this case, the start of the next line should be `//`, with no `@`. |
| 26 | + |
| 27 | +For example, `//@ !has 'foo/struct.Bar.html'` checks that crate `foo` does not have a page for a struct named `Bar` in the crate root. |
| 28 | + |
| 29 | +### `has` |
| 30 | + |
| 31 | +Usage 1: `//@ has PATH` |
| 32 | +Usage 2: `//@ has PATH XPATH PATTERN` |
| 33 | + |
| 34 | +In the first form, `has` checks that a given file exists. |
| 35 | + |
| 36 | +In the second form, `has` is an alias for `matches`, |
| 37 | +except `PATTERN` is a whitespace-normalized[^1] string instead of a regex. |
| 38 | + |
| 39 | +### `matches` |
| 40 | + |
| 41 | +Usage: `//@ matches PATH XPATH PATTERN` |
| 42 | + |
| 43 | +Checks that the text of each element selected by `XPATH` in `PATH` matches the python-flavored regex `PATTERN`. |
| 44 | + |
| 45 | +### `matchesraw` |
| 46 | + |
| 47 | +Usage: `//@ matchesraw PATH PATTERN` |
| 48 | + |
| 49 | +Checks that the contents of the file `PATH` matches the regex `PATTERN`. |
| 50 | + |
| 51 | +### `hasraw` |
| 52 | + |
| 53 | +Usage: `//@ hasraw PATH PATTERN` |
| 54 | + |
| 55 | +Same as `matchesraw`, except `PATTERN` is a whitespace-normalized[^1] string instead of a regex. |
| 56 | + |
| 57 | +### `count` |
| 58 | + |
| 59 | +Usage: `//@ count PATH XPATH COUNT` |
| 60 | + |
| 61 | +Checks that there are exactly `COUNT` matches for `XPATH` within the file `PATH`. |
| 62 | + |
| 63 | +### `snapshot` |
| 64 | + |
| 65 | +Usage: `//@ snapshot NAME PATH XPATH` |
| 66 | + |
| 67 | +Creates a snapshot test named NAME. |
| 68 | +A snapshot test captures a subtree of the DOM, at the location |
| 69 | +determined by the XPath, and compares it to a pre-recorded value |
| 70 | +in a file. The file's name is the test's name with the `.rs` extension |
| 71 | +replaced with `.NAME.html`, where NAME is the snapshot's name. |
| 72 | + |
| 73 | +htmldocck supports the `--bless` option to accept the current subtree |
| 74 | +as expected, saving it to the file determined by the snapshot's name. |
| 75 | +compiletest's `--bless` flag is forwarded to htmldocck. |
| 76 | + |
| 77 | +### `has-dir` |
| 78 | + |
| 79 | +Usage: `//@ has-dir PATH` |
| 80 | + |
| 81 | +Checks for the existance of directory `PATH`. |
| 82 | + |
| 83 | +### `files` |
| 84 | + |
| 85 | +Usage: `//@ files PATH ENTRIES` |
| 86 | + |
| 87 | +Checks that the directory `PATH` contains exactly `ENTRIES`. |
| 88 | +`ENTRIES` is a python list of strings inside a quoted string, |
| 89 | +as if it were to be parsed by `eval`. |
| 90 | +(note that the list is actually parsed by `shlex.split`, |
| 91 | +so it cannot contain arbitrary python expressions). |
| 92 | + |
| 93 | +Example: `//@ files "foo/bar" '["index.html", "sidebar-items.js"]'` |
| 94 | + |
| 95 | +[^1]: Whitespace normalization means that all spans of consecutive whitespace are replaced with a single space. The files themselves are also whitespace-normalized. |
| 96 | + |
| 97 | +## Limitations |
| 98 | +All `XPATH` arguments must start with `//` due to a flaw in the implemention. |
| 99 | + |
| 100 | +Only well-formed HTML can be parsed (hopefully rustdoc doesn't output mismatched tags). |
| 101 | + |
0 commit comments