Skip to main content

TEST

Constant TEST 

Source
pub const TEST: &str = "//! Assertions for tests: `assert`, `assert_eq` and `assert_ne`.\n//!\n//! Each checks a condition and, if it does not hold, panics with a message\n//! that starts `assertion failed:`, which fails the test that called it. A\n//! test marked `@test(should_panic)` passes only if it panics, which is the\n//! way to check that something panics.\n//!\n//! These functions exist only when `nova test` compiles the program. There,\n//! every module sees them without an import, and a module\'s own definition\n//! of one of them shadows the one here. `nova run`, `nova build` and\n//! `nova check` leave out both `@test` functions and this module, so call\n//! these functions only from `@test` functions: called anywhere else, they\n//! make those commands fail, reporting a function they cannot find.\n//!\n//! The three assertions, each passing here:\n//!\n//! ```\n//! let total = 2 + 3\n//! assert(total > 4, \"total is more than 4\")\n//! assert_eq(total, 5)\n//! assert_ne(\"left\", \"right\")\n//!\n//! // A failing check panics with both values in their Debug form:\n//! // `assert_eq(\"a\", \"b\")` panics with `assertion failed: \"a\" != \"b\"`.\n//! assert_eq(\"a\".dbg(), \"\\\"a\\\"\")\n//! ```\n\n// std/test \u{2014} assertions for `@test` functions.\n//\n// Seeded ONLY when compiling under `nova test` (see `STD_TEST_MODULE`), not\n// into every program. A top-level `pub fn assert` in an always-embedded module\n// would be glob-imported and take the names `assert`, `assert_eq` and\n// `assert_ne` in every module of every program \u{2014} the same hazard that made\n// `join` hang off the separator (`\",\".join(parts)`) rather than being a free\n// function. The cost is that `assert` is unavailable in ordinary code; `panic`\n// already serves that need.\n//\n// Failure is `panic`, which aborts with exit non-zero and `nova: panic: ...` on\n// stderr. That is the \"panicked\" outcome `nova test` looks for, and it is why\n// there is no `assert_throws` here: catching a panic needs unwinding, and\n// `nova_rt_panic_str` calls `std::process::abort()`. `@test(should_panic)` is\n// the supported way to assert that something panics.\n\n/// Checks that `cond` is `true`.\n///\n/// Panics if it is `false`, with the message `assertion failed: ` followed by\n/// `msg`.\npub fn assert(cond: Bool, msg: String) { if !cond { panic(\"assertion failed: ${msg}\") } }\n\n/// Checks that `a` and `b` are equal.\n///\n/// Panics if `a.ne(b)` is `true`, with a message that shows both values in\n/// their `Debug` form either side of `!=`, such as\n/// `assertion failed: \"a\" != \"b\"`.\npub fn assert_eq<T: Eq + Debug>(a: T, b: T) {\n    if a.ne(b) { panic(\"assertion failed: ${a.dbg()} != ${b.dbg()}\") }\n}\n\n/// Checks that `a` and `b` are not equal.\n///\n/// Panics if `a.eq(b)` is `true`, with a message that shows both values in\n/// their `Debug` form either side of `==`, such as `assertion failed: 1 == 1`.\npub fn assert_ne<T: Eq + Debug>(a: T, b: T) {\n    if a.eq(b) { panic(\"assertion failed: ${a.dbg()} == ${b.dbg()}\") }\n}\n";
Expand description

std/test/lib.nova, seeded only under nova test.