-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add slide on GoogleTest #1528
Merged
Add slide on GoogleTest #1528
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
minutes: 5 | ||
--- | ||
|
||
# GoogleTest | ||
|
||
The [GoogleTest](https://docs.rs/googletest/) crate allows for flexible test | ||
assertions using _matchers_: | ||
|
||
```rust,ignore | ||
{{#include googletest.rs:test_elements_are}} | ||
``` | ||
|
||
If we change the last element to `"!"`, the test fails with a structured error | ||
message pin-pointing the error: | ||
|
||
<!-- mdbook-xgettext: skip --> | ||
|
||
```text | ||
---- test_elements_are stdout ---- | ||
Value of: value | ||
Expected: has elements: | ||
0. is equal to "foo" | ||
1. is less than "xyz" | ||
2. starts with prefix "!" | ||
Actual: ["foo", "bar", "baz"], | ||
where element #2 is "baz", which does not start with "!" | ||
at src/testing/googletest.rs:6:5 | ||
Error: See failure output above | ||
``` | ||
|
||
<details> | ||
|
||
- GoogleTest is not part of the Rust Playground, so you need to run this example | ||
in a local environment. Use `cargo add googletest` to quickly add it to an | ||
existing Cargo project. | ||
|
||
- The `use googletest::prelude::*;` line imports a number of | ||
[commonly used macros and types][prelude]. | ||
|
||
- This just scratches the surface, there are many builtin matchers. | ||
|
||
- A particularly nice feature is that mismatches in multi-line strings strings | ||
are shown as a diff: | ||
|
||
```rust,ignore | ||
{{#include googletest.rs:test_multiline_string_diff}} | ||
``` | ||
|
||
shows a color-coded diff (colors not shown here): | ||
|
||
<!-- mdbook-xgettext: skip --> | ||
```text | ||
Value of: haiku | ||
Expected: is equal to "Memory safety found,\nRust's silly humor guides the way,\nSecure code you'll write." | ||
Actual: "Memory safety found,\nRust's strong typing guides the way,\nSecure code you'll write.", | ||
which isn't equal to "Memory safety found,\nRust's silly humor guides the way,\nSecure code you'll write." | ||
Difference(-actual / +expected): | ||
Memory safety found, | ||
-Rust's strong typing guides the way, | ||
+Rust's silly humor guides the way, | ||
Secure code you'll write. | ||
at src/testing/googletest.rs:17:5 | ||
``` | ||
|
||
- The crate is a Rust port of | ||
[GoogleTest for C++](https://google.github.io/googletest/). | ||
|
||
[prelude]: https://docs.rs/googletest/latest/googletest/prelude/index.html | ||
|
||
- GoogleTest is available for use in AOSP. | ||
|
||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// ANCHOR: test_elements_are | ||
use googletest::prelude::*; | ||
|
||
#[googletest::test] | ||
fn test_elements_are() { | ||
let value = vec!["foo", "bar", "baz"]; | ||
expect_that!(value, elements_are!(eq("foo"), lt("xyz"), starts_with("b"))); | ||
} | ||
// ANCHOR_END: test_elements_are | ||
|
||
#[should_panic] | ||
// ANCHOR: test_multiline_string_diff | ||
#[test] | ||
fn test_multiline_string_diff() { | ||
let haiku = "Memory safety found,\n\ | ||
Rust's strong typing guides the way,\n\ | ||
Secure code you'll write."; | ||
assert_that!( | ||
haiku, | ||
eq("Memory safety found,\n\ | ||
Rust's silly humor guides the way,\n\ | ||
Secure code you'll write.") | ||
); | ||
} | ||
// ANCHOR_END: test_multiline_string_diff |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why a staticlib? Are you planning to link it into C code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the type used as an example in Examples. It allows me to avoid an empty (and unwanted)
main
function.