How do I create custom assertions that reuse existing assertions #60
-
|
Given the following types: Coord {
x: u8;
y: u8;
}
Snake {
length: usize;
head: Coord;
body: Vec<Coord>;
}How would I implement the following as a custom assertion? let snake: Snake = ....; 'Some Snake
let expected_body = vec![Coord(1,1)];
assert_that!(snake).extracting(|s| s.length).is_equal_to(expect_body.len());
assert_that!(snake).extracting(|s| s.body).contains_exactly(expected_body);
assert_that!(snake).extracting(|s| s.head).contains_exactly(expected_body[0]);Ideally I'd like something like: assert_that!(snake).has_body(&[Coord(1,1)]); |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
|
I have thought about the easiest way this can be achieved and here is what I have come up so far: Currently there is no easy way to reuse existing assertions in combination with extracting fields from a custom struct. Another idea I have is to write a helper function for your tests like so: fn assert_snake_body(actual_snake: &Snake, expected_body: &[Coord]) {
assert_that!(snake).extracting(|s| s.length).is_equal_to(expect_body.len());
assert_that!(snake).extracting(|s| s.body).contains_exactly(expected_body);
assert_that!(snake).extracting(|s| s.head).is_equal_to(expected_body[0]);
}let snake: Snake = ....; 'Some Snake
let expected_body = vec![Coord(1,1)];
assert_snake_body(&snake, &expected_body); |
Beta Was this translation helpful? Give feedback.
-
|
Thanks @davidarkemp for your feedback. Its much appreciated. I've now tried a sightly changed variant of the helper function to assert snake bodies. It uses the assert_that!(snake).extracting(|s| s.body).named("snake.body").contains_exactly(expected_body);gives a failure result like: assertion failed: expected snake.body to contain exactly in order [Coord { x: 2, y: 1 }, Coord { x: 1, y: 1 }, Coord { x: 1, y: 2 }]
but was: [Coord { x: 2, y: 1 }, Coord { x: -1, y: 1 }, Coord { x: 1, y: 2 }]
expected: [Coord { x: 2, y: 1 }, Coord { x: 1, y: 1 }, Coord { x: 1, y: 2 }]
missing: [Coord { x: 1, y: 1 }]
extra: [Coord { x: -1, y: 1 }]
out-of-order: []Differences are the given field name is printed in "expected snake.body to..." and the "missing" and "extra" elements are also listed. |
Beta Was this translation helpful? Give feedback.
-
|
With fn assert_snake_body(snake: &Snake, expected_body: &[Coord]) {
let mut failures = verify_that!(snake)
.extracting(|s| s.length)
.named("snake.length")
.is_equal_to(expected_body.len())
.display_failures();
failures.extend(
verify_that!(snake)
.extracting(|s| &s.body)
.named("snake.body")
.contains_exactly(expected_body)
.display_failures(),
);
failures.extend(
verify_that!(snake)
.extracting(|s| s.head)
.named("snake.head")
.is_equal_to(expected_body[0])
.display_failures(),
);
assert!(failures.is_empty(), "{}", failures.join("\n"));
}This would give failure results like this: assertion failed: expected snake.length to be equal to 3
but was: 2
expected: 3
assertion failed: expected snake.body to contain exactly in order [Coord { x: 2, y: 1 }, Coord { x: 1, y: 1 }, Coord { x: 1, y: 2 }]
but was: [Coord { x: 2, y: 1 }, Coord { x: 1, y: 2 }, Coord { x: -1, y: 1 }]
expected: [Coord { x: 2, y: 1 }, Coord { x: 1, y: 1 }, Coord { x: 1, y: 2 }]
missing: [Coord { x: 1, y: 1 }]
extra: [Coord { x: -1, y: 1 }]
out-of-order: [Coord { x: 1, y: 2 }]
assertion failed: expected snake.head to be equal to Coord { x: 2, y: 1 }
but was: Coord { x: 3, y: 1 }
expected: Coord { x: 2, y: 1 }I for myself don't like that the "assertion failed: " phrase is repeated in every failure message. I have this on my list of future improvements. |
Beta Was this translation helpful? Give feedback.
-
|
Since version 0.13.0 of I have rewritten the example for the The actual implementation is similar to the helper function above. Main advantage ist that we can use the custom assertion methods in the tests. |
Beta Was this translation helpful? Give feedback.
Since version 0.13.0 of
assertingit is possible to write custom assertion methods with reusing of existing assertions.I have rewritten the example for the
Snakestruct here.The actual implementation is similar to the helper function above. Main advantage ist that we can use the custom assertion methods in the tests.