Ferrous API Reference

All Types / Ferrous.Testing

class CompileAssert

public static class CompileAssert

In-memory Roslyn compile-pass and compile-fail test harness inspired by Rust's trybuild. Verifies that C# snippets targeting [Typestate], [Consuming], [BorrowMut], [NoDefault], [Closed], and other Ferrous contracts either compile cleanly or fail with specific expected diagnostic IDs.

Methods

public static CompileResult Succeeds(string source, CompileAssertOptions? options = null)

Compiles source with Ferrous source generators and analyzers and asserts that compilation produces no error diagnostics (and no warnings when CompileAssertOptions.TreatWarningsAsErrors is enabled).

Parameters
source
The C# source code to compile.
options
Optional compilation configuration.

Returns: The resulting diagnostics and generated sources.

public static CompileResult Fails(string source, params string[] expectedDiagnosticIds)

Compiles source with Ferrous source generators and analyzers and asserts that every diagnostic ID in expectedDiagnosticIds is emitted (or that at least one error/Ferrous diagnostic is emitted when no IDs are specified).

Parameters
source
The C# source code expected to fail compilation or emit diagnostics.
expectedDiagnosticIds
Expected diagnostic IDs (e.g., "FE0200", "FE0300").

Returns: The resulting diagnostics and generated sources.

public static CompileResult Fails(string source, CompileAssertOptions? options, params string[] expectedDiagnosticIds)

Compiles source with the specified options and asserts that every diagnostic ID in expectedDiagnosticIds is emitted.

Parameters
source
The C# source code expected to fail compilation or emit diagnostics.
options
Optional compilation configuration.
expectedDiagnosticIds
Expected diagnostic IDs (e.g., "FE0200", "FE0300").

Returns: The resulting diagnostics and generated sources.