Skip to content

eq matcher with temporary values #9

@almondtools

Description

@almondtools

I am not certain how to succintly express an assert in following context (shortened, abstract example):

Assume following context:

#[derive(PartialEq, Debug)]
struct S {
  val:u32
}
impl S {
  pub fn new(val:u32) -> Self {
    S {val:val}
  }
}

The test (1):

fn test() {
  let s = S::new(42);
  
  assert_that!(s, eq(S::new(42)));
  assert_that!(s, not(eq(S::new(41))));
}

will not compile because s is consumed in the first assert statement.

The test (2):

fn test() {
  let s = S::new(42);
  
  assert_that!(&s, eq(S::new(42)));
  assert_that!(&s, not(eq(S::new(41))));
}

will not compile because the eq-matcher does not support unwrapping references.

The test (3):

fn test() {
  let s = S::new(42);
  
  assert_that!(&s, eq(&S::new(42)));
  assert_that!(&s, not(eq(&S::new(41))));
}

will not compile with 'temporary value dropped while borrowed'.

I could fix test (3) by introducing temporary variables, yet the test gets quite ugly with temporary variables.

My questions:
Is there another solution to test what I expect?
Is it planned to support something similar to test (2)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions