Monday, April 29, 2024
HomeJavaTotally different take a look at scopes in Rust

Totally different take a look at scopes in Rust


Rust differentiates between “unit” exams and “integration” exams. I added double quotes as a result of I consider the semantics could be deceptive. Right here’s what they imply:

  • Unit exams are written in the identical file as the primary. You annotate them with the #[test] macro and name cargo take a look at as seen above
  • Integration exams are exterior to the code to check. You annotate code to be a part of integration exams with the #[cfg(test)] macro.

Evaluates boolean mixtures of configuration flags at compile-time.

Along with the #[cfg] attribute, this macro is supplied to permit boolean expression analysis of configuration flags. This steadily results in much less duplicated code.

The cfg macro affords numerous out-of-the-box configuration variables:

Variable Description Instance

target_arch

Goal’s CPU structure

target_feature

Platform characteristic out there for the present compilation goal

target_os

Goal’s working system

target_family

Extra generic description of a goal, such because the household of the working programs or architectures that the goal typically falls into

target_env

Additional disambiguating details about the goal platform with details about the ABI or libc used

target_endian

target_pointer_width

Goal’s pointer width in bits

target_vendor

Vendor of the goal

take a look at

Enabled when compiling the take a look at harness

proc_macro

When the crate compiled is being compiled with the proc_macro

panic

Relying on the panic technique

You might have seen the take a look at flag among the many many variables. To put in writing an integration take a look at, annotate the code with the #[cfg(test)] macro:

#[cfg(test)]
fn test_something() {
    // No matter
}

One also can use the macro to offer various code within the take a look at context:

fn howdy() -> &'static str {
    return "Hey world";
}

#[cfg(test)]
fn howdy() -> &'static str {
    return "Hey take a look at";
}

The above snippet works throughout cargo run however not throughout cargo take a look at. Within the first case, the second operate is ignored; within the second, it’s not, and Rust tries to compile two features with the identical signature.

error[E0428]: the title `howdy` is outlined a number of occasions
  --> src/lib.rs:10:1
   |
5  | fn howdy() -> &'static str {
   | -------------------------- earlier definition of the worth `howdy` right here
...
10 | fn howdy() -> &'static str {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^ `howdy` redefined right here
   |
   = observe: `howdy` have to be outlined solely as soon as within the worth namespace of this module

Luckily, the cfg macro affords boolean logic. Therefore we will negate the take a look at config for the primary operate:

fn fundamental() {
    println!("{}", howdy());
}

#[cfg(not(test))]
fn howdy() -> &'static str {
    return "Hey world";
}

#[cfg(test)]
fn howdy() -> &'static str {
    return "Hey take a look at";
}

#[test]
fn test_hello() {
    assert_eq!(howdy(), "Hey take a look at");
}

  • cargo run yields Hey world
  • cargo take a look at compiles then executes the take a look at efficiently

Whereas it solves our downside, it has apparent flaws:

  • It’s binary – take a look at context or not
  • It doesn’t scale: after a selected measurement, the sheer variety of annotations will make the venture unmanageable
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments