Skip to content

Commit 61495bc

Browse files
gribozavrcopybara-github
authored andcommitted
Make the #[gtest] macro tolerate user-declared std and core modules
PiperOrigin-RevId: 665225837
1 parent d5cf76c commit 61495bc

File tree

5 files changed

+242
-9
lines changed

5 files changed

+242
-9
lines changed

googletest_macro/src/lib.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,17 @@ pub fn gtest(
7575
let mut parsed_fn = parse_macro_input!(input as ItemFn);
7676
let attrs = parsed_fn.attrs.drain(..).collect::<Vec<_>>();
7777
let (mut sig, block) = (parsed_fn.sig, parsed_fn.block);
78-
let (outer_return_type, trailer) =
79-
if attrs.iter().any(|attr| attr.path().is_ident("should_panic")) {
80-
(quote! { () }, quote! { .unwrap(); })
81-
} else {
82-
(
83-
quote! { std::result::Result<(), googletest::internal::test_outcome::TestFailure> },
84-
quote! {},
85-
)
86-
};
78+
let (outer_return_type, trailer) = if attrs
79+
.iter()
80+
.any(|attr| attr.path().is_ident("should_panic"))
81+
{
82+
(quote! { () }, quote! { .unwrap(); })
83+
} else {
84+
(
85+
quote! { ::std::result::Result<(), googletest::internal::test_outcome::TestFailure> },
86+
quote! {},
87+
)
88+
};
8789
let output_type = match sig.output.clone() {
8890
ReturnType::Type(_, output_type) => Some(output_type),
8991
ReturnType::Default => None,

integration_tests/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,8 @@ test = false
399399
name = "verify_predicate_with_failure_as_method_in_submodule"
400400
path = "src/verify_predicate_with_failure_as_method_in_submodule.rs"
401401
test = false
402+
403+
[[bin]]
404+
name = "macro_hygiene"
405+
path = "src/macro_hygiene.rs"
406+
test = false

integration_tests/src/integration_tests.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,48 @@ mod tests {
18341834
expect_that!(123, eq(123));
18351835
}
18361836

1837+
#[gtest]
1838+
fn macros_are_hygenic() -> Result<()> {
1839+
let output = run_external_process_in_tests_directory("macro_hygiene")?;
1840+
1841+
verify_that!(
1842+
output,
1843+
all!(
1844+
contains_substring("test tests::verify_that_works ... ok"),
1845+
contains_substring("test tests::verify_pred_works ... ok"),
1846+
contains_substring("test tests::fail_works ... FAILED"),
1847+
contains_substring("test tests::succeed_works ... ok"),
1848+
contains_substring("test tests::add_failure_works ... FAILED"),
1849+
contains_substring("test tests::add_failure_at_works ... FAILED"),
1850+
contains_substring("test tests::verify_true_works ... ok"),
1851+
contains_substring("test tests::expect_true_works ... ok"),
1852+
contains_substring("test tests::verify_false_works ... ok"),
1853+
contains_substring("test tests::expect_false_works ... ok"),
1854+
contains_substring("test tests::verify_eq_works ... ok"),
1855+
contains_substring("test tests::expect_eq_works ... ok"),
1856+
contains_substring("test tests::verify_ne_works ... ok"),
1857+
contains_substring("test tests::expect_ne_works ... ok"),
1858+
contains_substring("test tests::verify_lt_works ... ok"),
1859+
contains_substring("test tests::expect_lt_works ... ok"),
1860+
contains_substring("test tests::verify_le_works ... ok"),
1861+
contains_substring("test tests::expect_le_works ... ok"),
1862+
contains_substring("test tests::verify_gt_works ... ok"),
1863+
contains_substring("test tests::expect_gt_works ... ok"),
1864+
contains_substring("test tests::verify_ge_works ... ok"),
1865+
contains_substring("test tests::expect_ge_works ... ok"),
1866+
contains_substring("test tests::verify_float_eq_works ... ok"),
1867+
contains_substring("test tests::expect_float_eq_works ... ok"),
1868+
contains_substring("test tests::verify_near_works ... ok"),
1869+
contains_substring("test tests::expect_near_works ... ok"),
1870+
contains_substring("test tests::assert_that_works ... ok"),
1871+
contains_substring("test tests::assert_pred_works ... ok"),
1872+
contains_substring("test tests::expect_that_works ... ok"),
1873+
contains_substring("test tests::expect_pred_works ... ok"),
1874+
contains_substring("test result: FAILED. 27 passed; 3 failed;")
1875+
)
1876+
)
1877+
}
1878+
18371879
fn run_external_process_in_tests_directory_with_args(
18381880
name: &'static str,
18391881
args: &[&'static str],
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
fn main() {}
16+
17+
// Shadow important modules that the macros use.
18+
mod std {}
19+
mod ctor {}
20+
21+
#[cfg(test)]
22+
mod tests {
23+
use googletest::prelude::*;
24+
25+
#[gtest]
26+
fn verify_that_works() -> Result<()> {
27+
verify_that!(2 + 2, eq(4))
28+
}
29+
30+
#[gtest]
31+
fn verify_pred_works() -> Result<()> {
32+
fn pred() -> bool {
33+
true
34+
}
35+
verify_pred!(pred())
36+
}
37+
38+
#[gtest]
39+
fn fail_works() -> Result<()> {
40+
fail!()
41+
}
42+
43+
#[gtest]
44+
fn succeed_works() {
45+
succeed!();
46+
}
47+
48+
#[gtest]
49+
fn add_failure_works() {
50+
add_failure!("message");
51+
}
52+
53+
#[gtest]
54+
fn add_failure_at_works() {
55+
add_failure_at!("hello.rs", 12, 34, "message");
56+
}
57+
58+
#[gtest]
59+
fn verify_true_works() -> Result<()> {
60+
verify_true!(true)
61+
}
62+
63+
#[gtest]
64+
fn expect_true_works() {
65+
expect_true!(true);
66+
}
67+
68+
#[gtest]
69+
fn verify_false_works() -> Result<()> {
70+
verify_false!(false)
71+
}
72+
73+
#[gtest]
74+
fn expect_false_works() {
75+
expect_false!(false);
76+
}
77+
78+
#[gtest]
79+
fn verify_eq_works() -> Result<()> {
80+
verify_eq!(2 + 2, 4)
81+
}
82+
83+
#[gtest]
84+
fn expect_eq_works() {
85+
expect_eq!(2 + 2, 4);
86+
}
87+
88+
#[gtest]
89+
fn verify_ne_works() -> Result<()> {
90+
verify_ne!(2 + 2, 5)
91+
}
92+
93+
#[gtest]
94+
fn expect_ne_works() {
95+
expect_ne!(2 + 2, 5);
96+
}
97+
98+
#[gtest]
99+
fn verify_lt_works() -> Result<()> {
100+
verify_lt!(2 + 2, 5)
101+
}
102+
103+
#[gtest]
104+
fn expect_lt_works() {
105+
expect_lt!(2 + 2, 5);
106+
}
107+
108+
#[gtest]
109+
fn verify_le_works() -> Result<()> {
110+
verify_le!(2 + 2, 4)
111+
}
112+
113+
#[gtest]
114+
fn expect_le_works() {
115+
expect_le!(2 + 2, 4);
116+
}
117+
118+
#[gtest]
119+
fn verify_gt_works() -> Result<()> {
120+
verify_gt!(2 + 2, 3)
121+
}
122+
123+
#[gtest]
124+
fn expect_gt_works() {
125+
expect_gt!(2 + 2, 3);
126+
}
127+
128+
#[gtest]
129+
fn verify_ge_works() -> Result<()> {
130+
verify_ge!(2 + 2, 4)
131+
}
132+
133+
#[gtest]
134+
fn expect_ge_works() {
135+
expect_ge!(2 + 2, 4);
136+
}
137+
138+
#[gtest]
139+
fn verify_float_eq_works() -> Result<()> {
140+
verify_float_eq!(1.0, 1.0)
141+
}
142+
143+
#[gtest]
144+
fn expect_float_eq_works() {
145+
expect_float_eq!(1.0, 1.0);
146+
}
147+
148+
#[gtest]
149+
fn verify_near_works() -> Result<()> {
150+
verify_near!(42.0, 42.001, 0.1)
151+
}
152+
153+
#[gtest]
154+
fn expect_near_works() {
155+
expect_near!(42.0, 42.001, 0.1);
156+
}
157+
158+
#[gtest]
159+
fn assert_that_works() {
160+
assert_that!(2 + 2, eq(4));
161+
}
162+
163+
#[gtest]
164+
fn assert_pred_works() {
165+
fn pred() -> bool {
166+
true
167+
}
168+
assert_pred!(pred());
169+
}
170+
171+
#[gtest]
172+
fn expect_that_works() {
173+
expect_that!(2 + 2, eq(4));
174+
}
175+
176+
#[gtest]
177+
fn expect_pred_works() {
178+
fn pred() -> bool {
179+
true
180+
}
181+
expect_pred!(pred());
182+
}
183+
}

run_integration_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ INTEGRATION_TEST_BINARIES=(
9696
"two_non_fatal_failures"
9797
"verify_predicate_with_failure"
9898
"verify_predicate_with_failure_as_method_in_submodule"
99+
"macro_hygiene"
99100
)
100101

101102
cargo build

0 commit comments

Comments
 (0)