mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2026-02-01 17:57:02 +00:00
105 lines
18 KiB
HTML
105 lines
18 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="A trait for setting up test databases from fixture strings."><title>WithFixture in test_fixture - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="test_fixture" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.0 (254b59607 2026-01-19)" data-channel="1.93.0" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">WithFixture</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../test_fixture/index.html">test_<wbr>fixture</a><span class="version">0.0.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">With<wbr>Fixture</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#fixture-syntax" title="Fixture Syntax">Fixture Syntax</a><ul><li><a href="#basic-structure" title="Basic Structure">Basic Structure</a></li><li><a href="#file-metadata" title="File Metadata">File Metadata</a></li><li><a href="#global-meta-must-appear-at-the-top-in-order" title="Global Meta (must appear at the top, in order)">Global Meta (must appear at the top, in order)</a></li><li><a href="#cursor-markers" title="Cursor Markers">Cursor Markers</a></li></ul></li><li><a href="#examples" title="Examples">Examples</a><ul><li><a href="#single-file-with-cursor-position" title="Single file with cursor position">Single file with cursor position</a></li><li><a href="#multiple-crates-with-dependencies" title="Multiple crates with dependencies">Multiple crates with dependencies</a></li><li><a href="#using-minicore-for-lang-items" title="Using minicore for lang items">Using minicore for lang items</a></li><li><a href="#using-test-proc-macros" title="Using test proc macros">Using test proc macros</a></li></ul></li></ul><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.test_crate" title="test_crate">test_crate</a></li><li><a href="#method.with_files" title="with_files">with_files</a></li><li><a href="#method.with_files_extra_proc_macros" title="with_files_extra_proc_macros">with_files_extra_proc_macros</a></li><li><a href="#method.with_many_files" title="with_many_files">with_many_files</a></li><li><a href="#method.with_position" title="with_position">with_position</a></li><li><a href="#method.with_range" title="with_range">with_range</a></li><li><a href="#method.with_range_or_offset" title="with_range_or_offset">with_range_or_offset</a></li><li><a href="#method.with_single_file" title="with_single_file">with_single_file</a></li></ul><h3><a href="#dyn-compatibility">Dyn Compatibility</a></h3><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate test_<wbr>fixture</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="index.html">test_fixture</a></div><h1>Trait <span class="trait">With<wbr>Fixture</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/test_fixture/lib.rs.html#142-232">Source</a> </span></div><pre class="rust item-decl"><code>pub trait WithFixture:
|
|
<a class="trait" href="https://doc.rust-lang.org/1.93.0/core/default/trait.Default.html" title="trait core::default::Default">Default</a>
|
|
+ ExpandDatabase
|
|
+ SourceDatabase
|
|
+ 'static {
|
|
// Provided methods
|
|
fn <a href="#method.with_single_file" class="fn">with_single_file</a>(ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>) -> (Self, EditionedFileId) { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.with_many_files" class="fn">with_many_files</a>(ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>) -> (Self, <a class="struct" href="https://doc.rust-lang.org/1.93.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><EditionedFileId>) { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.with_files" class="fn">with_files</a>(ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>) -> Self { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.with_files_extra_proc_macros" class="fn">with_files_extra_proc_macros</a>(
|
|
ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>,
|
|
proc_macros: <a class="struct" href="https://doc.rust-lang.org/1.93.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><(<a class="struct" href="https://doc.rust-lang.org/1.93.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>, ProcMacro)>,
|
|
) -> Self { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.with_position" class="fn">with_position</a>(ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>) -> (Self, FilePosition) { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.with_range" class="fn">with_range</a>(ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>) -> (Self, FileRange) { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.with_range_or_offset" class="fn">with_range_or_offset</a>(
|
|
ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>,
|
|
) -> (Self, EditionedFileId, RangeOrOffset) { ... }
|
|
<span class="item-spacer"></span> fn <a href="#method.test_crate" class="fn">test_crate</a>(&self) -> Crate { ... }
|
|
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A trait for setting up test databases from fixture strings.</p>
|
|
<p>Fixtures are strings containing Rust source code with optional metadata that describe
|
|
a project setup. This is the primary way to write tests for rust-analyzer without
|
|
having to depend on the entire sysroot.</p>
|
|
<h2 id="fixture-syntax"><a class="doc-anchor" href="#fixture-syntax">§</a>Fixture Syntax</h2><h3 id="basic-structure"><a class="doc-anchor" href="#basic-structure">§</a>Basic Structure</h3>
|
|
<p>A fixture without metadata is parsed into a single source file (<code>/main.rs</code>).
|
|
Metadata is added after a <code>//-</code> comment prefix.</p>
|
|
<div class="example-wrap"><pre class="language-text"><code>//- /main.rs
|
|
fn main() {
|
|
println!("Hello");
|
|
}</code></pre></div>
|
|
<p>Note that the fixture syntax is optional and can be omitted if the test only requires
|
|
a simple single file.</p>
|
|
<h3 id="file-metadata"><a class="doc-anchor" href="#file-metadata">§</a>File Metadata</h3>
|
|
<p>Each file can have the following metadata after <code>//-</code>:</p>
|
|
<ul>
|
|
<li><strong>Path</strong> (required): Must start with <code>/</code>, e.g., <code>/main.rs</code>, <code>/lib.rs</code>, <code>/foo/bar.rs</code></li>
|
|
<li><strong><code>crate:<name></code></strong>: Defines a new crate with this file as its root
|
|
<ul>
|
|
<li>Optional version: <code>crate:foo@0.1.0,https://example.com/repo.git</code></li>
|
|
</ul>
|
|
</li>
|
|
<li><strong><code>deps:<crate1>,<crate2></code></strong>: Dependencies (requires <code>crate:</code>)</li>
|
|
<li><strong><code>extern-prelude:<crate1>,<crate2></code></strong>: Limits extern prelude to specified crates</li>
|
|
<li><strong><code>edition:<year></code></strong>: Rust edition (2015, 2018, 2021, 2024). Defaults to current.</li>
|
|
<li><strong><code>cfg:<key>=<value>,<flag></code></strong>: Configuration options, e.g., <code>cfg:test,feature="foo"</code></li>
|
|
<li><strong><code>env:<KEY>=<value></code></strong>: Environment variables</li>
|
|
<li><strong><code>crate-attr:<attr></code></strong>: Crate-level attributes, e.g., <code>crate-attr:no_std</code></li>
|
|
<li><strong><code>new_source_root:local|library</code></strong>: Starts a new source root</li>
|
|
<li><strong><code>library</code></strong>: Marks crate as external library (not workspace member)</li>
|
|
</ul>
|
|
<h3 id="global-meta-must-appear-at-the-top-in-order"><a class="doc-anchor" href="#global-meta-must-appear-at-the-top-in-order">§</a>Global Meta (must appear at the top, in order)</h3>
|
|
<ul>
|
|
<li><strong><code>//- toolchain: nightly|stable</code></strong>: Sets the Rust toolchain (default: stable)</li>
|
|
<li><strong><code>//- target_data_layout: <layout></code></strong>: LLVM data layout string</li>
|
|
<li><strong><code>//- target_arch: <arch></code></strong>: Target architecture (default: x86_64)</li>
|
|
<li><strong><code>//- proc_macros: <name1>,<name2></code></strong>: Enables predefined test proc macros</li>
|
|
<li><strong><code>//- minicore: <flag1>, <flag2></code></strong>: Includes subset of libcore</li>
|
|
</ul>
|
|
<h3 id="cursor-markers"><a class="doc-anchor" href="#cursor-markers">§</a>Cursor Markers</h3>
|
|
<p>Use <code>$0</code> to mark cursor position(s) in the fixture:</p>
|
|
<ul>
|
|
<li>Single <code>$0</code>: marks a position (use with <a href="trait.WithFixture.html#method.with_position"><code>with_position</code></a>)</li>
|
|
<li>Two <code>$0</code> markers: marks a range (use with <a href="trait.WithFixture.html#method.with_range"><code>with_range</code></a>)</li>
|
|
<li>Escape as <code>\$0</code> if you need a literal <code>$0</code></li>
|
|
</ul>
|
|
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2><h3 id="single-file-with-cursor-position"><a class="doc-anchor" href="#single-file-with-cursor-position">§</a>Single file with cursor position</h3><div class="example-wrap"><pre class="language-text"><code>r#"
|
|
fn main() {
|
|
let x$0 = 42;
|
|
}
|
|
"#</code></pre></div><h3 id="multiple-crates-with-dependencies"><a class="doc-anchor" href="#multiple-crates-with-dependencies">§</a>Multiple crates with dependencies</h3><div class="example-wrap"><pre class="language-text"><code>r#"
|
|
//- /main.rs crate:main deps:helper
|
|
use helper::greet;
|
|
fn main() { greet(); }
|
|
|
|
//- /lib.rs crate:helper
|
|
pub fn greet() {}
|
|
"#</code></pre></div><h3 id="using-minicore-for-lang-items"><a class="doc-anchor" href="#using-minicore-for-lang-items">§</a>Using minicore for lang items</h3><div class="example-wrap"><pre class="language-text"><code>r#"
|
|
//- minicore: option, result, iterator
|
|
//- /main.rs
|
|
fn foo() -> Option<i32> { Some(42) }
|
|
"#</code></pre></div>
|
|
<p>The available minicore flags are listed at the top of crates\test-utils\src\minicore.rs.</p>
|
|
<h3 id="using-test-proc-macros"><a class="doc-anchor" href="#using-test-proc-macros">§</a>Using test proc macros</h3><div class="example-wrap"><pre class="language-text"><code>r#"
|
|
//- proc_macros: identity, mirror
|
|
//- /main.rs crate:main deps:proc_macros
|
|
use proc_macros::identity;
|
|
|
|
#[identity]
|
|
fn foo() {}
|
|
"#</code></pre></div>
|
|
<p>Available proc macros: <code>identity</code> (attr), <code>DeriveIdentity</code> (derive), <code>input_replace</code> (attr),
|
|
<code>mirror</code> (bang), <code>shorten</code> (bang)</p>
|
|
</div></details><h2 id="provided-methods" class="section-header">Provided Methods<a href="#provided-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="method.with_single_file" class="method"><a class="src rightside" href="../src/test_fixture/lib.rs.html#145-154">Source</a><h4 class="code-header">fn <a href="#method.with_single_file" class="fn">with_single_file</a>(ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>) -> (Self, EditionedFileId)</h4></section></summary><div class="docblock"><p>See the trait documentation for more information on fixtures.</p>
|
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.with_many_files" class="method"><a class="src rightside" href="../src/test_fixture/lib.rs.html#158-171">Source</a><h4 class="code-header">fn <a href="#method.with_many_files" class="fn">with_many_files</a>(ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>) -> (Self, <a class="struct" href="https://doc.rust-lang.org/1.93.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><EditionedFileId>)</h4></section></summary><div class="docblock"><p>See the trait documentation for more information on fixtures.</p>
|
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.with_files" class="method"><a class="src rightside" href="../src/test_fixture/lib.rs.html#175-181">Source</a><h4 class="code-header">fn <a href="#method.with_files" class="fn">with_files</a>(ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>) -> Self</h4></section></summary><div class="docblock"><p>See the trait documentation for more information on fixtures.</p>
|
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.with_files_extra_proc_macros" class="method"><a class="src rightside" href="../src/test_fixture/lib.rs.html#185-195">Source</a><h4 class="code-header">fn <a href="#method.with_files_extra_proc_macros" class="fn">with_files_extra_proc_macros</a>(
|
|
ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>,
|
|
proc_macros: <a class="struct" href="https://doc.rust-lang.org/1.93.0/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><(<a class="struct" href="https://doc.rust-lang.org/1.93.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>, ProcMacro)>,
|
|
) -> Self</h4></section></summary><div class="docblock"><p>See the trait documentation for more information on fixtures.</p>
|
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.with_position" class="method"><a class="src rightside" href="../src/test_fixture/lib.rs.html#199-203">Source</a><h4 class="code-header">fn <a href="#method.with_position" class="fn">with_position</a>(ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>) -> (Self, FilePosition)</h4></section></summary><div class="docblock"><p>See the trait documentation for more information on fixtures.</p>
|
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.with_range" class="method"><a class="src rightside" href="../src/test_fixture/lib.rs.html#207-211">Source</a><h4 class="code-header">fn <a href="#method.with_range" class="fn">with_range</a>(ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>) -> (Self, FileRange)</h4></section></summary><div class="docblock"><p>See the trait documentation for more information on fixtures.</p>
|
|
</div></details><details class="toggle method-toggle" open><summary><section id="method.with_range_or_offset" class="method"><a class="src rightside" href="../src/test_fixture/lib.rs.html#215-227">Source</a><h4 class="code-header">fn <a href="#method.with_range_or_offset" class="fn">with_range_or_offset</a>(
|
|
ra_fixture: &<a class="primitive" href="https://doc.rust-lang.org/1.93.0/std/primitive.str.html">str</a>,
|
|
) -> (Self, EditionedFileId, RangeOrOffset)</h4></section></summary><div class="docblock"><p>See the trait documentation for more information on fixtures.</p>
|
|
</div></details><section id="method.test_crate" class="method"><a class="src rightside" href="../src/test_fixture/lib.rs.html#229-231">Source</a><h4 class="code-header">fn <a href="#method.test_crate" class="fn">test_crate</a>(&self) -> Crate</h4></section></div><h2 id="dyn-compatibility" class="section-header">Dyn Compatibility<a href="#dyn-compatibility" class="anchor">§</a></h2><div class="dyn-compatibility-info"><p>This trait is <b>not</b> <a href="https://doc.rust-lang.org/1.93.0/reference/items/traits.html#dyn-compatibility">dyn compatible</a>.</p><p><i>In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.</i></p></div><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-WithFixture-for-DB" class="impl"><a class="src rightside" href="../src/test_fixture/lib.rs.html#234">Source</a><a href="#impl-WithFixture-for-DB" class="anchor">§</a><h3 class="code-header">impl<DB: ExpandDatabase + SourceDatabase + <a class="trait" href="https://doc.rust-lang.org/1.93.0/core/default/trait.Default.html" title="trait core::default::Default">Default</a> + 'static> <a class="trait" href="trait.WithFixture.html" title="trait test_fixture::WithFixture">WithFixture</a> for DB</h3></section></div><script src="../trait.impl/test_fixture/trait.WithFixture.js" async></script></section></div></main></body></html> |