Reject empty pattern in `assert_.._matches`.
PiperOrigin-RevId: 521471750
diff --git a/common/token_stream_matchers.rs b/common/token_stream_matchers.rs
index 19fc515..c677a14 100644
--- a/common/token_stream_matchers.rs
+++ b/common/token_stream_matchers.rs
@@ -149,6 +149,16 @@
where
ToStringFn: Fn(TokenStream) -> Result<String>,
{
+ // `match_tokens` behaves as if the `pattern` implicitly had a wildcard `...` at
+ // the beginning and the end. Therefore an empty `pattern` is most
+ // likely a mistake.
+ assert!(
+ !pattern.is_empty(),
+ "Empty `pattern` is unexpected, because it always matches. \
+ (Maybe you used `// comment text` instead of `__COMMENT__ \"comment text\"? \
+ Or maybe you want to use `TokenStream::is_empty`?)"
+ );
+
let iter = input.clone().into_iter();
let mut best_mismatch = Mismatch::for_no_partial_match();
@@ -723,4 +733,32 @@
input:\n\n```\n[a b b]\n```"
);
}
+
+ #[test]
+ #[should_panic(expected = "Empty `pattern` is unexpected, because it always matches. \
+ (Maybe you used `// comment text` instead of `__COMMENT__ \"comment text\"? \
+ Or maybe you want to use `TokenStream::is_empty`?)")]
+ fn test_assert_cc_matches_panics_when_pattern_is_empty() {
+ assert_cc_matches!(
+ quote! { foo bar },
+ quote! {
+ // This comment will be stripped by `quote!`, but some test assertions
+ // mistakenly used the comment syntax instead of `__COMMENT__ "text"`
+ },
+ );
+ }
+
+ #[test]
+ #[should_panic(expected = "Empty `pattern` is unexpected, because it always matches. \
+ (Maybe you used `// comment text` instead of `__COMMENT__ \"comment text\"? \
+ Or maybe you want to use `TokenStream::is_empty`?)")]
+ fn test_assert_rs_matches_panics_when_pattern_is_empty() {
+ assert_rs_matches!(
+ quote! { foo bar },
+ quote! {
+ // This comment will be stripped by `quote!`, but some test assertions
+ // mistakenly used the comment syntax instead of `__COMMENT__ "text"`
+ },
+ );
+ }
}
diff --git a/rs_bindings_from_cc/ir_matchers.rs b/rs_bindings_from_cc/ir_matchers.rs
index 4202ee7..423ddf2 100644
--- a/rs_bindings_from_cc/ir_matchers.rs
+++ b/rs_bindings_from_cc/ir_matchers.rs
@@ -134,8 +134,8 @@
#[test]
fn test_optional_trailing_comma() {
- assert_ir_matches!(ir_from_cc("").unwrap(), quote! {});
- assert_ir_matches!(ir_from_cc("").unwrap(), quote! {},);
+ assert_ir_matches!(ir_from_cc("").unwrap(), quote! { FlatIR { ... }});
+ assert_ir_matches!(ir_from_cc("").unwrap(), quote! { FlatIR { ... }},);
assert_ir_not_matches!(ir_from_cc("").unwrap(), quote! {this pattern is not in the ir});
assert_ir_not_matches!(ir_from_cc("").unwrap(), quote! {this pattern is not in the ir},);
diff --git a/rs_bindings_from_cc/src_code_gen.rs b/rs_bindings_from_cc/src_code_gen.rs
index 6d0ad0b..235d145 100644
--- a/rs_bindings_from_cc/src_code_gen.rs
+++ b/rs_bindings_from_cc/src_code_gen.rs
@@ -7245,9 +7245,9 @@
#[test]
fn test_format_generic_params() -> Result<()> {
- assert_rs_matches!(
- format_generic_params(/* lifetimes= */ &[], std::iter::empty::<syn::Ident>()),
- quote! {}
+ assert!(
+ format_generic_params(/* lifetimes= */ &[], std::iter::empty::<syn::Ident>())
+ .is_empty(),
);
let idents = ["T1", "T2"].iter().map(|s| make_rs_ident(s));
@@ -8778,7 +8778,7 @@
#[test]
fn test_generate_doc_comment_with_no_comment_with_no_source_loc_with_source_loc_enabled() {
let actual = generate_doc_comment(None, None, SourceLocationDocComment::Enabled);
- assert_rs_matches!(actual, quote! {});
+ assert!(actual.is_empty());
}
#[test]
@@ -8814,7 +8814,7 @@
#[test]
fn test_no_generate_doc_comment_with_no_comment_with_no_source_loc_with_source_loc_disabled() {
let actual = generate_doc_comment(None, None, SourceLocationDocComment::Disabled);
- assert_rs_matches!(actual, quote! {});
+ assert!(actual.is_empty());
}
#[test]
@@ -8824,7 +8824,7 @@
Some("google3/some/header;l=13"),
SourceLocationDocComment::Disabled,
);
- assert_rs_matches!(actual, quote! {});
+ assert!(actual.is_empty());
}
#[test]