Skip to content

Commit 13afdff

Browse files
hovinenhovinenb
authored andcommitted
Move most unit tests of declarative macro matchers into integration tests.
This should help to prevent future regressions of the type reported in #86 where the unit tests work only because they were compiled in the same crate as the declarative macros. That setup namely makes it too easy to forget to reference the crate when invoking another declarative macro in the expansion. By moving these tests to integration tests, they are compiled from outside the original crate and subject to the same rules as downstream dependencies. See also https://doc.rust-lang.org/rust-by-example/testing/integration_testing.html. Also introduces the use of indoc in some of the moved tests in order to improve formatting. See https://crates.io/crates/indoc.
1 parent 79701ac commit 13afdff

13 files changed

+1185
-991
lines changed

googletest/src/matchers/all_matcher.rs

Lines changed: 9 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -152,45 +152,12 @@ mod tests {
152152
#[cfg(not(google3))]
153153
use googletest::matchers;
154154
use googletest::{
155-
google_test,
155+
all, google_test,
156156
matcher::{Matcher, MatcherResult},
157157
verify_that, Result,
158158
};
159159
use indoc::indoc;
160-
use matchers::{contains_substring, displays_as, ends_with, eq, err, not, starts_with};
161-
162-
#[google_test]
163-
fn matches_any_value_when_list_is_empty() -> Result<()> {
164-
verify_that!((), all!())
165-
}
166-
167-
#[google_test]
168-
fn matches_value_with_single_matching_component() -> Result<()> {
169-
verify_that!(123, all!(eq(123)))
170-
}
171-
172-
#[google_test]
173-
fn does_not_match_value_with_single_non_matching_component() -> Result<()> {
174-
verify_that!(123, not(all!(eq(456))))
175-
}
176-
177-
#[google_test]
178-
fn matches_value_with_two_matching_components() -> Result<()> {
179-
verify_that!("A string", all!(starts_with("A"), ends_with("string")))
180-
}
181-
182-
#[google_test]
183-
fn does_not_match_value_with_one_non_matching_component_among_two_components() -> Result<()> {
184-
verify_that!(123, not(all!(eq(123), eq(456))))
185-
}
186-
187-
#[google_test]
188-
fn supports_trailing_comma() -> Result<()> {
189-
verify_that!(
190-
"An important string",
191-
all!(starts_with("An"), contains_substring("important"), ends_with("string"),)
192-
)
193-
}
160+
use matchers::{displays_as, ends_with, eq, starts_with};
194161

195162
#[google_test]
196163
fn description_shows_more_than_one_matcher() -> Result<()> {
@@ -202,9 +169,9 @@ mod tests {
202169
matcher.describe(MatcherResult::Matches),
203170
eq(indoc!(
204171
"
205-
has all the following properties:
206-
* starts with prefix \"A\"
207-
* ends with suffix \"string\""
172+
has all the following properties:
173+
* starts with prefix \"A\"
174+
* ends with suffix \"string\""
208175
))
209176
)
210177
}
@@ -214,7 +181,10 @@ mod tests {
214181
let first_matcher = starts_with("A");
215182
let matcher: internal::AllMatcher<String, 1> = all!(first_matcher);
216183

217-
verify_that!(matcher.describe(MatcherResult::Matches), eq("starts with prefix \"A\""))
184+
verify_that!(
185+
matcher.describe(MatcherResult::Matches),
186+
eq("starts with prefix \"A\"")
187+
)
218188
}
219189

220190
#[google_test]
@@ -240,39 +210,4 @@ mod tests {
240210
displays_as(eq("which does not start with \"Another\""))
241211
)
242212
}
243-
244-
#[google_test]
245-
fn mismatch_description_two_failed_matchers() -> Result<()> {
246-
verify_that!(
247-
all!(starts_with("One"), starts_with("Two")).explain_match("Three"),
248-
displays_as(eq(
249-
"\n * which does not start with \"One\"\n * which does not start with \"Two\""
250-
))
251-
)
252-
}
253-
254-
#[google_test]
255-
fn mismatch_description_empty_matcher() -> Result<()> {
256-
verify_that!(all!().explain_match("Three"), displays_as(eq("which is anything")))
257-
}
258-
259-
#[google_test]
260-
fn all_multiple_failed_assertions() -> Result<()> {
261-
let result = verify_that!(4, all![eq(1), eq(2), eq(3)]);
262-
verify_that!(
263-
result,
264-
err(displays_as(contains_substring(indoc!(
265-
"
266-
Value of: 4
267-
Expected: has all the following properties:
268-
* is equal to 1
269-
* is equal to 2
270-
* is equal to 3
271-
Actual: 4,
272-
* which isn't equal to 1
273-
* which isn't equal to 2
274-
* which isn't equal to 3"
275-
))))
276-
)
277-
}
278213
}

googletest/src/matchers/elements_are_matcher.rs

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -153,112 +153,3 @@ pub mod internal {
153153
}
154154
}
155155
}
156-
157-
#[cfg(test)]
158-
mod tests {
159-
#[cfg(not(google3))]
160-
use crate as googletest;
161-
use googletest::matcher::Matcher;
162-
#[cfg(not(google3))]
163-
use googletest::matchers;
164-
use googletest::{google_test, verify_that, Result};
165-
use indoc::indoc;
166-
use matchers::{contains_substring, displays_as, eq, err, not};
167-
168-
#[google_test]
169-
fn elements_are_matches_vector() -> Result<()> {
170-
let value = vec![1, 2, 3];
171-
verify_that!(value, elements_are![eq(1), eq(2), eq(3)])
172-
}
173-
174-
#[google_test]
175-
fn elements_are_matches_slice() -> Result<()> {
176-
let value = vec![1, 2, 3];
177-
let slice = value.as_slice();
178-
verify_that!(*slice, elements_are![eq(1), eq(2), eq(3)])
179-
}
180-
181-
#[google_test]
182-
fn elements_are_matches_array() -> Result<()> {
183-
verify_that!([1, 2, 3], elements_are![eq(1), eq(2), eq(3)])
184-
}
185-
186-
#[google_test]
187-
fn elements_are_supports_trailing_comma() -> Result<()> {
188-
let value = vec![1, 2, 3];
189-
verify_that!(value, elements_are![eq(1), eq(2), eq(3),])
190-
}
191-
192-
#[google_test]
193-
fn elements_are_returns_no_match_when_expected_and_actual_sizes_differ() -> Result<()> {
194-
let value = vec![1, 2];
195-
verify_that!(value, not(elements_are![eq(1), eq(2), eq(3)]))
196-
}
197-
198-
#[google_test]
199-
fn elements_are_produces_correct_failure_message() -> Result<()> {
200-
let result = verify_that!(vec![1, 4, 3], elements_are![eq(1), eq(2), eq(3)]);
201-
verify_that!(
202-
result,
203-
err(displays_as(contains_substring(indoc!(
204-
"
205-
Value of: vec![1, 4, 3]
206-
Expected: has elements:
207-
0. is equal to 1
208-
1. is equal to 2
209-
2. is equal to 3
210-
Actual: [
211-
1,
212-
4,
213-
3,
214-
], where element #1 is 4, which isn't equal to 2"
215-
))))
216-
)
217-
}
218-
219-
#[google_test]
220-
fn elements_are_produces_correct_failure_message_nested() -> Result<()> {
221-
let result = verify_that!(
222-
vec![vec![0, 1], vec![1, 2]],
223-
elements_are![elements_are![eq(1), eq(2)], elements_are![eq(2), eq(3)]]
224-
);
225-
verify_that!(
226-
result,
227-
err(displays_as(contains_substring(indoc!(
228-
"
229-
Value of: vec![vec! [0, 1], vec! [1, 2]]
230-
Expected: has elements:
231-
0. has elements:
232-
0. is equal to 1
233-
1. is equal to 2
234-
1. has elements:
235-
0. is equal to 2
236-
1. is equal to 3
237-
Actual: [
238-
[
239-
0,
240-
1,
241-
],
242-
[
243-
1,
244-
2,
245-
],
246-
], where:
247-
* element #0 is [0, 1], where:
248-
* element #0 is 0, which isn't equal to 1
249-
* element #1 is 1, which isn't equal to 2
250-
* element #1 is [1, 2], where:
251-
* element #0 is 1, which isn't equal to 2
252-
* element #1 is 2, which isn't equal to 3"
253-
))))
254-
)
255-
}
256-
257-
#[google_test]
258-
fn elements_are_explain_match_wrong_size() -> Result<()> {
259-
verify_that!(
260-
elements_are![eq(1)].explain_match(&vec![1, 2]),
261-
displays_as(eq("whose size is 2"))
262-
)
263-
}
264-
}

googletest/src/matchers/field_matcher.rs

Lines changed: 5 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ pub mod internal {
121121
field_path: &'static str,
122122
inner: InnerMatcher,
123123
) -> impl Matcher<OuterT> {
124-
FieldMatcher { field_accessor, field_path, inner }
124+
FieldMatcher {
125+
field_accessor,
126+
field_path,
127+
inner,
128+
}
125129
}
126130

127131
struct FieldMatcher<OuterT, InnerT, InnerMatcher> {
@@ -165,137 +169,3 @@ pub mod internal {
165169
}
166170
}
167171
}
168-
169-
#[cfg(test)]
170-
mod tests {
171-
#[cfg(not(google3))]
172-
use crate as googletest;
173-
use googletest::matcher::{Matcher, MatcherResult};
174-
#[cfg(not(google3))]
175-
use googletest::matchers;
176-
use googletest::{google_test, verify_that, Result};
177-
use matchers::{container_eq, contains_substring, displays_as, eq, err, not};
178-
179-
#[derive(Debug)]
180-
struct IntField {
181-
int: i32,
182-
}
183-
184-
#[google_test]
185-
fn field_matches_integer_field() -> Result<()> {
186-
verify_that!(IntField { int: 32 }, field!(IntField.int, eq(32)))
187-
}
188-
189-
#[derive(Debug)]
190-
struct StringField {
191-
strink: String,
192-
}
193-
194-
#[google_test]
195-
fn field_matches_string_field() -> Result<()> {
196-
verify_that!(
197-
StringField { strink: "yes".to_string() },
198-
field!(StringField.strink, eq("yes"))
199-
)
200-
}
201-
202-
#[google_test]
203-
fn field_error_message_shows_field_name_and_inner_matcher() -> Result<()> {
204-
let matcher = field!(IntField.int, eq(31));
205-
206-
verify_that!(
207-
matcher.describe(MatcherResult::Matches),
208-
eq("has field `int`, which is equal to 31")
209-
)
210-
}
211-
212-
mod sub {
213-
#[derive(Debug)]
214-
pub struct SubStruct {
215-
pub field: i32,
216-
}
217-
}
218-
219-
#[google_test]
220-
fn struct_in_other_module_matches() -> Result<()> {
221-
verify_that!(sub::SubStruct { field: 32 }, field!(sub::SubStruct.field, eq(32)))
222-
}
223-
224-
#[derive(Debug)]
225-
struct Tuple(i32, String);
226-
227-
#[google_test]
228-
fn tuple_matches_with_index() -> Result<()> {
229-
verify_that!(Tuple(32, "yes".to_string()), field!(Tuple.0, eq(32)))
230-
}
231-
232-
#[google_test]
233-
fn matches_enum_value() -> Result<()> {
234-
#[derive(Debug)]
235-
enum AnEnum {
236-
AValue(u32),
237-
}
238-
let value = AnEnum::AValue(123);
239-
240-
verify_that!(value, field!(AnEnum::AValue.0, eq(123)))
241-
}
242-
243-
#[google_test]
244-
fn shows_correct_failure_message_for_wrong_struct_entry() -> Result<()> {
245-
#[derive(Debug)]
246-
struct AStruct {
247-
a: Vec<u32>,
248-
}
249-
let value = AStruct { a: vec![1] };
250-
251-
let result = verify_that!(value, field!(AStruct.a, container_eq([])));
252-
253-
verify_that!(
254-
result,
255-
err(displays_as(contains_substring(
256-
"which has field `a`, which contains the unexpected element 1"
257-
)))
258-
)
259-
}
260-
261-
#[google_test]
262-
fn does_not_match_enum_value_with_wrong_enum_variant() -> Result<()> {
263-
#[derive(Debug)]
264-
enum AnEnum {
265-
#[allow(dead_code)] // This variant is intentionally unused.
266-
AValue(u32),
267-
AnotherValue,
268-
}
269-
let value = AnEnum::AnotherValue;
270-
271-
verify_that!(value, not(field!(AnEnum::AValue.0, eq(123))))
272-
}
273-
274-
#[google_test]
275-
fn shows_correct_failure_message_for_wrong_enum_value() -> Result<()> {
276-
#[derive(Debug)]
277-
enum AnEnum {
278-
#[allow(dead_code)] // This variant is intentionally unused.
279-
AValue {
280-
a: u32,
281-
},
282-
AnotherValue,
283-
}
284-
let value = AnEnum::AnotherValue;
285-
286-
let result = verify_that!(value, field!(AnEnum::AValue.a, eq(123)));
287-
288-
verify_that!(result, err(displays_as(contains_substring("which has no field `a`"))))
289-
}
290-
291-
#[google_test]
292-
fn matches_struct_like_enum_value() -> Result<()> {
293-
#[derive(Debug)]
294-
enum AnEnum {
295-
AValue { a_field: u32 },
296-
}
297-
let value = AnEnum::AValue { a_field: 123 };
298-
299-
verify_that!(value, field!(AnEnum::AValue.a_field, eq(123)))
300-
}
301-
}

0 commit comments

Comments
 (0)