Skip to content

Commit e3b92d8

Browse files
hovinenbcopybara-github
authored andcommitted
Support trailing commas in the elements_are!, unordered_elements_are!, contains_each!, and is_contained_in! macros.
This brings the behaviour of those macros in line with other procedural macros accepting variable argument lists as well as Rust in general. PiperOrigin-RevId: 503913288
1 parent 0dc126a commit e3b92d8

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

googletest/src/matchers/elements_are_matcher.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/// Do not use with unordered containers. Prefer unordered_elements_are!(...).
2222
#[macro_export]
2323
macro_rules! elements_are {
24-
($($matcher:expr),*) => {{
24+
($($matcher:expr),* $(,)?) => {{
2525
#[cfg(google3)]
2626
use $crate::internal::ElementsAre;
2727
#[cfg(not(google3))]
@@ -128,6 +128,12 @@ mod tests {
128128
verify_that!(value, elements_are![eq(1), eq(2), eq(3)])
129129
}
130130

131+
#[google_test]
132+
fn elements_are_supports_trailing_comma() -> Result<()> {
133+
let value = vec![1, 2, 3];
134+
verify_that!(value, elements_are![eq(1), eq(2), eq(3),])
135+
}
136+
131137
#[google_test]
132138
fn elements_are_returns_no_match_when_expected_and_actual_sizes_differ() -> Result<()> {
133139
let value = vec![1, 2];

googletest/src/matchers/unordered_elements_are_matcher.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
/// matchers.
4343
#[macro_export]
4444
macro_rules! unordered_elements_are {
45-
($($matcher:expr),*) => {{
45+
($($matcher:expr),* $(,)?) => {{
4646
#[cfg(google3)]
4747
use $crate::internal::{UnorderedElementsAre, Requirements};
4848
#[cfg(not(google3))]
@@ -85,7 +85,7 @@ macro_rules! unordered_elements_are {
8585
/// matchers did not have corresponding unique elements in the container.
8686
#[macro_export]
8787
macro_rules! contains_each {
88-
($($matcher:expr),*) => {{
88+
($($matcher:expr),* $(,)?) => {{
8989
#[cfg(google3)]
9090
use $crate::internal::{UnorderedElementsAre, Requirements};
9191
#[cfg(not(google3))]
@@ -130,7 +130,7 @@ macro_rules! contains_each {
130130
/// container elements did not have corresponding matchers.
131131
#[macro_export]
132132
macro_rules! is_contained_in {
133-
($($matcher:expr),*) => {{
133+
($($matcher:expr),* $(,)?) => {{
134134
#[cfg(google3)]
135135
use $crate::internal::{UnorderedElementsAre, Requirements};
136136
#[cfg(not(google3))]
@@ -734,6 +734,12 @@ mod tests {
734734
verify_that!(value, unordered_elements_are![eq(1), eq(2), eq(3)])
735735
}
736736

737+
#[google_test]
738+
fn unordered_elements_are_matches_vector_with_trailing_comma() -> Result<()> {
739+
let value = vec![1, 2, 3];
740+
verify_that!(value, unordered_elements_are![eq(1), eq(2), eq(3),])
741+
}
742+
737743
#[google_test]
738744
fn unordered_elements_are_matches_size() -> Result<()> {
739745
let value = vec![1, 2];
@@ -811,6 +817,11 @@ Actual: [1, 4, 3], whose element #1 does not match any expected elements and no
811817
verify_that!(vec![2, 3, 4], contains_each!(eq(2), eq(3), eq(4)))
812818
}
813819

820+
#[google_test]
821+
fn contains_each_supports_trailing_comma() -> Result<()> {
822+
verify_that!(vec![2, 3, 4], contains_each!(eq(2), eq(3), eq(4),))
823+
}
824+
814825
#[google_test]
815826
fn contains_each_matches_when_no_matchers_present() -> Result<()> {
816827
verify_that!(vec![2, 3, 4], contains_each!())
@@ -867,6 +878,11 @@ Actual: [1, 4, 3], whose element #1 does not match any expected elements and no
867878
verify_that!(vec![2, 3, 4], is_contained_in!(eq(2), eq(3), eq(4)))
868879
}
869880

881+
#[google_test]
882+
fn is_contained_supports_trailing_comma() -> Result<()> {
883+
verify_that!(vec![2, 3, 4], is_contained_in!(eq(2), eq(3), eq(4),))
884+
}
885+
870886
#[google_test]
871887
fn is_contained_in_matches_when_container_is_empty() -> Result<()> {
872888
verify_that!(vec![], is_contained_in!(eq(2), eq(3), eq(4)))

0 commit comments

Comments
 (0)