Why do assert_eq and assert_ne exist when a simple assert is enough?

assert!(a == b)accepts fewer characters than assert_eq!(a, b), and, in my opinion, more readable.

Error messages are more or less the same:

thread 'main' panicked at 'assertion failed: `(left == right)` (left: `1`, right: `2`)', src\main.rs:41

or

thread 'main' panicked at 'assertion failed: 1 == 2', src\main.rs:41

Actually, this issue concerns not only rust; I continue to see these various assertion macros or functions in unit testing systems:

  • Cpputest has CHECKand CHECK_FALSEand CHECK_EQUALetc.
  • Googletest has EXPECT_GTand EXPECT_EQetc.
  • JUnit has assertEqualsand assertFalsecontinues to work.

Often, there is also a statement for some specific type, such as a string or an array. What's the point?

+4
source share
1

thread 'main' panicked at 'assertion failed: 1 == 2',

, , assert_eq!. :

let s = "Hello".to_string();
assert!(&s == "Bye");

:

'assertion failed: &s == "Bye"'    

, , assert_eq!:

let s = "Hello".to_string();
assert_eq!(&s, "Bye");

:

'assertion failed: `(left == right)` (left: `"Hello"`, right: `"Bye"`)'

, . .

+7

Source: https://habr.com/ru/post/1676292/


All Articles