Skip to content

Commit 5ae614f

Browse files
Googlercopybara-github
authored andcommitted
Use tuples-are-matchers support to specialize verify_eq on tuples.
This adds special cases in `verify_eq!` so that, when doing `eq` matching on sequences of tuple elements, the matcher applies tuple matching, distributing `eq` pointwise. This improves the ergonomics of tests that check for tuple contents, such as: ```rust let hash_map: std::collections::HashMap<String, String> = std::collections::HashMap::from([ ("a".into(), "A".into()), ("b".into(), "B".into())]); verify_eq!(hash_map, {("a", "A"), ("b", "B")}) ``` because the matcher for `&str` is compatible with `&String`. The specialization applies on the inner structure; without it, the general matcher on tuples doesn't apply in the above case due to the definition of `PartialEq` on whole tuples, which is currently limited by rust-lang/rust#105092. PiperOrigin-RevId: 695528259
1 parent c13878c commit 5ae614f

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

googletest/src/assertions.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,45 @@ macro_rules! expect_false {
578578
/// `verify_that!(actual, unordered_elements_are![eq(e1), eq(e2), ...])`
579579
#[macro_export]
580580
macro_rules! verify_eq {
581+
// Specialization for ordered sequences of tuples:
582+
($actual:expr, [ $( ( $($tuple_elt:expr),* ) ),+ $(,)? ] $(,)?) => {
583+
verify_that!(&$actual, [
584+
$(
585+
// tuple matching
586+
(
587+
$(
588+
$crate::matchers::eq(&$tuple_elt)
589+
),*
590+
)
591+
),*
592+
])
593+
};
594+
595+
// Specialization for unordered sequences of tuples:
596+
($actual:expr, { $( ( $($tuple_elt:expr),* ) ),+ $(,)?} $(,)?) => {
597+
verify_that!(&$actual, {
598+
$(
599+
// tuple matching
600+
(
601+
$(
602+
$crate::matchers::eq(&$tuple_elt)
603+
),*
604+
)
605+
),*
606+
})
607+
};
608+
609+
// Ordered sequences:
581610
($actual:expr, [$($expected:expr),+ $(,)?] $(,)?) => {
582611
verify_that!(&$actual, [$($crate::matchers::eq(&$expected)),*])
583612
};
613+
614+
// Unordered sequences:
584615
($actual:expr, {$($expected:expr),+ $(,)?} $(,)?) => {
585616
verify_that!(&$actual, {$($crate::matchers::eq(&$expected)),*})
586617
};
618+
619+
// General case:
587620
($actual:expr, $expected:expr $(,)?) => {
588621
verify_that!(&$actual, $crate::matchers::eq(&$expected))
589622
};
@@ -1494,3 +1527,87 @@ pub mod internal {
14941527
Err(crate::internal::test_outcome::TestAssertionFailure::create(message))
14951528
}
14961529
}
1530+
1531+
#[cfg(test)]
1532+
mod tests {
1533+
use crate::prelude::*;
1534+
1535+
#[test]
1536+
fn verify_of_hash_maps_with_str_string_matching() -> Result<()> {
1537+
let hash_map: std::collections::HashMap<String, String> =
1538+
std::collections::HashMap::from([("a".into(), "A".into()), ("b".into(), "B".into())]);
1539+
verify_eq!(hash_map, {("a", "A"), ("b", "B")})
1540+
}
1541+
1542+
#[test]
1543+
fn verify_of_hash_maps_with_ad_hoc_struct() -> Result<()> {
1544+
#[derive(PartialEq, Debug)]
1545+
struct Greek(String);
1546+
1547+
let hash_map: std::collections::HashMap<String, Greek> = std::collections::HashMap::from([
1548+
("a".into(), Greek("Alpha".into())),
1549+
("b".into(), Greek("Beta".into())),
1550+
]);
1551+
verify_eq!(hash_map, {
1552+
("b", Greek("Beta".into())),
1553+
("a", Greek("Alpha".into())),
1554+
})
1555+
}
1556+
1557+
#[test]
1558+
fn verify_of_hash_maps_with_i32s() -> Result<()> {
1559+
let hash_map: std::collections::HashMap<i32, i32> =
1560+
std::collections::HashMap::from([(1, 1), (2, 4), (-1, 1), (-3, 9)]);
1561+
verify_eq!(hash_map, {
1562+
(-3, 9),
1563+
(-1, 1),
1564+
(1, 1),
1565+
(2, 4),
1566+
})
1567+
}
1568+
1569+
#[test]
1570+
fn verify_eq_of_unordered_pairs() -> Result<()> {
1571+
verify_eq!(vec![(1, 2), (2, 3)], {(1, 2), (2, 3)})?;
1572+
verify_eq!(vec![(1, 2), (2, 3)], {(2, 3), (1, 2)})
1573+
}
1574+
1575+
#[test]
1576+
fn verify_eq_of_unordered_structs() -> Result<()> {
1577+
#[derive(PartialEq, Debug)]
1578+
struct P(i32, i32);
1579+
1580+
verify_eq!(vec![P(1, 1), P(1, 2), P(3, 7)],
1581+
{P(1, 1), P(1, 2), P(3, 7)})?;
1582+
verify_eq!(vec![P(1, 1), P(1, 2), P(3, 7)],
1583+
{P(3,7), P(1, 1), P(1, 2)})
1584+
}
1585+
1586+
#[test]
1587+
fn verify_eq_of_ordered_pairs() -> Result<()> {
1588+
verify_eq!(vec![(1, 2), (2, 3)], [(1, 2), (2, 3)])
1589+
}
1590+
1591+
#[test]
1592+
fn verify_eq_of_ordered_structs() -> Result<()> {
1593+
#[derive(PartialEq, Debug)]
1594+
struct P(i32, i32);
1595+
1596+
verify_eq!(vec![P(1, 1), P(1, 2), P(3, 7)], [P(1, 1), P(1, 2), P(3, 7)])
1597+
}
1598+
1599+
#[test]
1600+
fn verify_eq_of_ordered_pairs_order_matters() -> Result<()> {
1601+
let result = verify_eq!(vec![(1, 2), (2, 3)], [(2, 3), (1, 2)]);
1602+
verify_that!(result, err(anything()))
1603+
}
1604+
1605+
#[test]
1606+
fn verify_eq_of_ordered_structs_order_matters() -> Result<()> {
1607+
#[derive(PartialEq, Debug)]
1608+
struct P(i32, i32);
1609+
1610+
let result = verify_eq!(vec![P(1, 1), P(1, 2), P(3, 7)], [P(3, 7), P(1, 1), P(1, 2)]);
1611+
verify_that!(result, err(anything()))
1612+
}
1613+
}

0 commit comments

Comments
 (0)