### Console Output Example: Unknown Serde Field Source: https://github.com/dtolnay/trybuild/blob/master/README.md This example demonstrates a compiler error for an unrecognized field attribute within a serde macro invocation. ```console error: unknown serde field attribute `qqq` --> $DIR/unknown-attribute.rs:5:13 | 5 | | ^^^ ``` -------------------------------- ### Basic Compile-Fail Test Setup Source: https://github.com/dtolnay/trybuild/blob/master/README.md Set up a basic compile-fail test by creating a test function that uses trybuild::TestCases::new() and the compile_fail method. This will compile each .rs file in the specified directory and assert that it fails with an expected error message. ```rust #[test] fn ui() { let t = trybuild::TestCases::new(); t.compile_fail("tests/ui/*.rs"); } ``` -------------------------------- ### Console Output Example: Missing Repr Source: https://github.com/dtolnay/trybuild/blob/master/README.md This example shows a typical compiler error message for a missing #[repr(C)] or #[repr(transparent)] attribute when using the RefCast trait. ```console error: RefCast trait requires #[repr(C)] or #[repr(transparent)] --> $DIR/missing-repr.rs:3:10 | 3 | | ^^^^^^^ ``` -------------------------------- ### Console Output Example: Writing to Readonly Field Source: https://github.com/dtolnay/trybuild/blob/master/README.md This example shows a compiler error (E0594) when attempting to assign a value to a field marked as readonly, which is only allowed for reading. ```console error[E0594]: cannot assign to data in a `&` reference --> $DIR/write-a-readonly.rs:17:26 | 17 | | ^^^^^^^^ cannot assign ``` -------------------------------- ### Console Output Example: Double Comma in JSON Macro Source: https://github.com/dtolnay/trybuild/blob/master/README.md This example illustrates a compiler error caused by a double comma within the json! macro, indicating a syntax issue in the input. ```console error: no rules expected the token `,` --> $DIR/double-comma.rs:4:38 | 4 | | ^ no rules expected this token in macro call ``` -------------------------------- ### TOML Configuration: Rust Source Component Source: https://github.com/dtolnay/trybuild/blob/master/README.md This TOML configuration ensures the 'rust-src' component is installed for the Rust toolchain, which is necessary for the compiler to render standard library source snippets in diagnostic output. ```toml [toolchain] components = ["rust-src"] ``` -------------------------------- ### Test Thread Safety Errors with trybuild Source: https://context7.com/dtolnay/trybuild/llms.txt Tests compile-time detection of thread safety violations, such as attempting to share non-Send types across threads. This example shows a scenario where a raw mutable pointer is used in a spawned thread, which is not thread-safe. ```rust # Test file: tests/ui/compile-fail-3.rs use std::ptr; use std::thread; fn main() { let x = ptr::null_mut(); thread::spawn(|| { println!("{:?}", x) }); } # Expected stderr: tests/ui/compile-fail-3.stderr # error[E0277]: `*mut _` cannot be shared between threads safely # --> tests/ui/compile-fail-3.rs:7:5 # | # 7 | thread::spawn(|| { # | ^^^^^^^^^^^^^ `*mut _` cannot be shared between threads safely # | # = help: the trait `Sync` is not implemented for `*mut _` # = note: required because of the requirements on the impl of `Send` for `&*mut _` ``` -------------------------------- ### Running Individual Tests Source: https://context7.com/dtolnay/trybuild/llms.txt Demonstrates how to run specific trybuild tests using cargo test filters. ```APIDOC ## Running Individual Tests ### Description Execute a specific test file by name using `cargo test` with filters. This is useful for debugging individual failing tests. ### Method Command Line ### Endpoint N/A ### Parameters - **filter** (string) - Optional - Filter for test names. `trybuild=` can be used to filter trybuild tests. - **test_name** (string) - Optional - Specific test file name to run. ### Request Example ```bash # Run only the test named 'example.rs' within the 'ui' test function cargo test -- ui trybuild=example.rs # Run all trybuild tests cargo test ``` ### Response Command line output indicating test success or failure. ### Response Example ``` running 1 test test ui running 0 tests test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s ``` ``` -------------------------------- ### Running a Specific Compile-Fail Test File Source: https://github.com/dtolnay/trybuild/blob/master/README.md To test only a single source file, use the cargo test command with the -- ui=example.rs flag, where 'ui' is the test function name and 'example.rs' is the target file. ```bash cargo test -- ui trybuild=example.rs ``` -------------------------------- ### Initialize Trybuild Test Runner Source: https://context7.com/dtolnay/trybuild/llms.txt Creates a new test runner instance. Tests are executed when the `TestCases` instance goes out of scope. ```rust use trybuild::TestCases; #[test] fn ui() { // Create a new test runner let t = TestCases::new(); // Tests are executed when `t` goes out of scope // Add test cases using pass() and compile_fail() t.pass("tests/ui/valid-usage.rs"); t.compile_fail("tests/ui/invalid-usage.rs"); } // When `t` drops at the end of the function, all tests are run ``` -------------------------------- ### Add trybuild to dev-dependencies Source: https://github.com/dtolnay/trybuild/blob/master/README.md Include trybuild in your Cargo.toml under [dev-dependencies] to use it for testing. ```toml [dev-dependencies] trybuild = "1.0" ``` -------------------------------- ### TestCases::new() Source: https://context7.com/dtolnay/trybuild/llms.txt Creates a new test runner instance that collects test cases and executes them when dropped. This is the entry point for all trybuild tests. ```APIDOC ## TestCases::new ### Description Creates a new test runner instance that collects test cases and executes them when dropped. The `TestCases` struct is the entry point for all trybuild tests and manages the collection and execution of both pass and compile-fail test cases. ### Method Associated function (constructor) ### Endpoint N/A (Rust function) ### Parameters None ### Request Example ```rust let t = trybuild::TestCases::new(); ``` ### Response N/A (Returns a `TestCases` struct instance) ### Response Example ```rust // TestCases struct instance ``` ``` -------------------------------- ### Update Expected Output Files with trybuild Source: https://context7.com/dtolnay/trybuild/llms.txt Two workflows for generating and updating `.stderr` files containing expected compiler output. Method 1 involves deleting stderr files and running tests, then moving generated files from 'wip' to the test directory. Method 2 uses the `TRYBUILD=overwrite` environment variable to directly update all stderr files. ```bash # Method 1: Delete stderr files and run tests # Missing stderr files are written to a 'wip' directory rm tests/ui/compile-fail-2.stderr cargo test # Move files from wip/ to tests/ui/ mv wip/compile-fail-2.stderr tests/ui/ # Method 2: Overwrite mode - directly update all stderr files TRYBUILD=overwrite cargo test # Review changes after overwrite git diff tests/ui/*.stderr ``` -------------------------------- ### pass() Source: https://context7.com/dtolnay/trybuild/llms.txt Registers a test case that is expected to compile successfully and execute without panicking. The test file must contain a `main` function. ```APIDOC ## pass ### Description Registers a test case that is expected to compile successfully and execute without panicking. The test file must contain a `main` function. Tests can be specified as individual files or using glob patterns to match multiple files at once. ### Method `TestCases::pass` ### Endpoint N/A (Rust function) ### Parameters - **path** (string) - Required - Path to the test source file or a glob pattern. ### Request Example ```rust let t = trybuild::TestCases::new(); t.pass("tests/ui/valid-usage.rs"); t.pass("tests/ui/valid-*.rs"); ``` ### Response N/A (Modifies the `TestCases` instance) ### Response Example ```rust // No direct response, test execution happens on drop of TestCases ``` ``` -------------------------------- ### Run Individual Trybuild Tests Source: https://context7.com/dtolnay/trybuild/llms.txt Execute a specific test file by name using `cargo test` with filters. Useful for debugging individual failing tests. ```bash # Run only the test named 'example.rs' within the 'ui' test function cargo test -- ui trybuild=example.rs # Run all trybuild tests cargo test ``` -------------------------------- ### Combined Pass and Compile-Fail Tests Source: https://github.com/dtolnay/trybuild/blob/master/README.md Use the trybuild::TestCases::new() to set up tests that include both expected to pass and expected to fail compilation scenarios. This is useful for workshops or managing test cases incrementally. ```rust #[test] fn ui() { let t = trybuild::TestCases::new(); t.pass("tests/01-parse-header.rs"); t.pass("tests/02-parse-body.rs"); t.compile_fail("tests/03-expand-four-errors.rs"); t.pass("tests/04-paste-ident.rs"); t.pass("tests/05-repeat-section.rs"); //t.pass("tests/06-make-work-in-function.rs"); //t.pass("tests/07-init-array.rs"); //t.compile_fail("tests/08-ident-span.rs"); } ``` -------------------------------- ### Ensure Consistent CI Environments with rust-src Source: https://context7.com/dtolnay/trybuild/llms.txt Configure `rust-toolchain.toml` to ensure consistent compiler output across development and CI environments by requiring the `rust-src` component. This is crucial for reliable testing with tools like trybuild. ```toml # rust-toolchain.toml [toolchain] components = ["rust-src"] ``` -------------------------------- ### Glob Pattern Matching for Tests Source: https://context7.com/dtolnay/trybuild/llms.txt Tests multiple source files at once using glob patterns. All matching files are compiled individually and tested against their corresponding `.stderr` files. ```rust use trybuild::TestCases; #[test] fn ui() { let t = TestCases::new(); // Test all .rs files in the ui directory t.compile_fail("tests/ui/*.rs"); // Or more specific patterns t.pass("tests/passing/*.rs"); t.compile_fail("tests/errors/**/*.rs"); } ``` -------------------------------- ### Test Procedural Macro Error Messages with trybuild Source: https://context7.com/dtolnay/trybuild/llms.txt Tests that a procedural macro emits correct error messages with proper span information. The `.stderr` file uses `$DIR` as a placeholder for the directory path. This snippet demonstrates how to use `trybuild::TestCases` to compile and check for expected errors. ```rust # Test file: tests/ui/missing-repr.rs # #[derive(RefCast)] # struct Foo(i32); # Expected stderr: tests/ui/missing-repr.stderr # error: RefCast trait requires #[repr(C)] or #[repr(transparent)] # --> $DIR/missing-repr.rs:3:10 # | # 3 | #[derive(RefCast)] # | ^^^^^^^ #[test] fn ui() { let t = trybuild::TestCases::new(); t.compile_fail("tests/ui/missing-repr.rs"); } ``` -------------------------------- ### Register a Passing Test Case Source: https://context7.com/dtolnay/trybuild/llms.txt Registers a test case that is expected to compile and execute successfully. Supports individual files or glob patterns. ```rust use trybuild::TestCases; #[test] fn ui() { let t = TestCases::new(); // Test a single file that should compile and run t.pass("tests/ui/run-pass-0.rs"); // Test multiple files using a glob pattern t.pass("tests/ui/valid-*.rs"); // Workshop-style sequential test cases t.pass("tests/01-parse-header.rs"); t.pass("tests/02-parse-body.rs"); t.pass("tests/04-paste-ident.rs"); t.pass("tests/05-repeat-section.rs"); } // Example test file: tests/ui/run-pass-0.rs // fn main() {} ``` -------------------------------- ### compile_fail() Source: https://context7.com/dtolnay/trybuild/llms.txt Registers a test case that is expected to fail compilation with specific error messages. The compiler output is compared against a corresponding `.stderr` file. ```APIDOC ## compile_fail ### Description Registers a test case that is expected to fail compilation with specific error messages. The compiler output is compared against a corresponding `.stderr` file with the same name as the test file. If the error messages match, the test passes; if they differ or if compilation unexpectedly succeeds, the test fails. ### Method `TestCases::compile_fail` ### Endpoint N/A (Rust function) ### Parameters - **path** (string) - Required - Path to the test source file or a glob pattern. ### Request Example ```rust let t = trybuild::TestCases::new(); t.compile_fail("tests/ui/compile-fail-2.rs"); t.compile_fail("tests/ui/*.rs"); ``` ### Response N/A (Modifies the `TestCases` instance) ### Response Example ```rust // No direct response, test execution happens on drop of TestCases ``` ``` -------------------------------- ### Register a Compile-Fail Test Case Source: https://context7.com/dtolnay/trybuild/llms.txt Registers a test case expected to fail compilation with specific error messages. Output is compared against a corresponding `.stderr` file. ```rust use trybuild::TestCases; #[test] fn ui() { let t = TestCases::new(); // Single file expected to fail compilation t.compile_fail("tests/ui/compile-fail-2.rs"); // Multiple compile-fail tests using glob t.compile_fail("tests/ui/*.rs"); } // Example test file: tests/ui/compile-fail-2.rs // compile_error!("ERROR"); // // fn main() {} // Corresponding stderr file: tests/ui/compile-fail-2.stderr // error: ERROR // --> tests/ui/compile-fail-2.rs:1:1 // | // 1 | compile_error!("ERROR"); // | ^^^^^^^^^^^^^^^^^^^^^^^ ``` -------------------------------- ### Mix Pass and Compile-Fail Tests Source: https://context7.com/dtolnay/trybuild/llms.txt Combines pass and compile_fail tests in a single function for comprehensive suites or workshops. Tests can be commented out for incremental development. ```rust use trybuild::TestCases; #[test] fn test() { let t = TestCases::new(); // Mix of pass and compile_fail tests t.pass("tests/ui/run-pass-0.rs"); t.pass("tests/ui/print-stdout.rs"); t.pass("tests/ui/run-pass-1.rs"); t.compile_fail("tests/ui/run-pass-3.rs"); t.pass("tests/ui/run-pass-5.rs"); t.compile_fail("tests/ui/compile-fail-1.rs"); t.pass("tests/ui/run-fail.rs"); t.compile_fail("tests/ui/compile-fail-2.rs"); t.compile_fail("tests/ui/compile-fail-3.rs"); // Workshop-style: comment out tests not yet implemented //t.pass("tests/06-make-work-in-function.rs"); //t.compile_fail("tests/08-ident-span.rs"); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.