Expand description

Routines for comparing and diffing output.

Patterns

Many of these functions support special markup to assist with comparing text that may vary or is otherwise uninteresting for the test at hand. The supported patterns are:

  • [..] is a wildcard that matches 0 or more characters on the same line (similar to .* in a regex). It is non-greedy.
  • [EXE] optionally adds .exe on Windows (empty string on other platforms).
  • [ROOT] is the path to the test directory’s root.
  • [CWD] is the working directory of the process that was run.
  • There is a wide range of substitutions (such as [COMPILING] or [WARNING]) to match cargo’s “status” output and allows you to ignore the alignment. See the source of substitute_macros for a complete list of substitutions.
  • [DIRTY-MSVC] (only when the line starts with it) would be replaced by [DIRTY] when cfg(target_env = "msvc") or the line will be ignored otherwise. Tests that work around issue 7358 can use this to avoid duplicating the with_stderr call like: if cfg!(target_env = "msvc") {e.with_stderr("...[DIRTY]...");} else {e.with_stderr("...");}.

Normalization

In addition to the patterns described above, the strings are normalized in such a way to avoid unwanted differences. The normalizations are:

  • Raw tab characters are converted to the string <tab>. This is helpful so that raw tabs do not need to be written in the expected string, and to avoid confusion of tabs vs spaces.
  • Backslashes are converted to forward slashes to deal with Windows paths. This helps so that all tests can be written assuming forward slashes. Other heuristics are applied to try to ensure Windows-style paths aren’t a problem.
  • Carriage returns are removed, which can help when running on Windows.

Structs

  • A single line string that supports [..] wildcard matching.

Functions

  • Convenience wrapper around match_exact which will panic on error.
  • Default snapbox Assertions
  • Compares JSON object for approximate equality. You can use [..] wildcard in strings (useful for OS-dependent things such as paths). You can use a "{...}" string literal as a wildcard for arbitrary nested JSON (useful for parts of object emitted by other programs (e.g., rustc) rather than Cargo itself).
  • Checks that the given string contains the given contiguous lines somewhere.
  • Checks that the given string contains the given contiguous lines somewhere, and should be repeated number times.
  • Checks that the given string does not contain the given contiguous lines anywhere.
  • Compares one string against another, checking that they both match.
  • Checks that the given string of JSON objects match the given set of expected JSON objects.
  • Checks that the given string of JSON objects match the given set of expected JSON objects, ignoring their order.
  • Checks that the given string contains the given lines, ignoring the order of the lines.
  • Checks that the given string has a line that contains the given patterns, and that line also does not contain the without patterns.
  • meta_cmp 🔒
  • Normalizes the output so that it can be compared against the expected value.
  • Normalizes the expected string so that it can be compared against the actual output.
  • Normalizes text for both actual and expected strings on Windows.