Skip to content

Commit

Permalink
Move Mockall and GoogleTest slides to Android
Browse files Browse the repository at this point in the history
The recommendation to use these crates is really an AOSP
recommendation. People can still use them for general-purpose Rust,
but there are many other useful crates in the wider Rust ecosystem.
  • Loading branch information
mgeisler committed Feb 27, 2024
1 parent dc4a1eb commit f23f0c6
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 42 deletions.
12 changes: 8 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
members = [
"mdbook-course",
"mdbook-exerciser",
"src/android/testing",
"src/bare-metal/useful-crates/allocator-example",
"src/bare-metal/useful-crates/zerocopy-example",
"src/borrowing",
Expand Down
3 changes: 3 additions & 0 deletions book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ use-boolean-and = true
'exercises/day-2/strings-iterators.html' = '../../iterators/exercise.html'
'testing/integration-tests.html' = '../testing/other.html'
'testing/doc-tests.html' = '../testing/other.html'
'testing/googletest.html' = '../android/testing/googletest.html'
'testing/mockall.html' = '../android/testing/mockall.html'
'testing/useful-crates.html' = '../testing.html'
'exercises/day-1/luhn.html' = '../../testing/exercise.html'
'memory-management/stack-vs-heap.html' = '../memory-management/review.html'
'memory-management/stack.html' = '../memory-management/review.html'
Expand Down
6 changes: 3 additions & 3 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,6 @@
- [Testing](testing.md)
- [Test Modules](testing/unit-tests.md)
- [Other Types of Tests](testing/other.md)
- [Useful Crates](testing/useful-crates.md)
- [GoogleTest](testing/googletest.md)
- [Mocking](testing/mocking.md)
- [Compiler Lints and Clippy](testing/lints.md)
- [Exercise: Luhn Algorithm](testing/exercise.md)
- [Solution](testing/solution.md)
Expand Down Expand Up @@ -231,6 +228,9 @@
- [Sending Objects](android/aidl/types/objects.md)
- [Parcelables](android/aidl/types/parcelables.md)
- [Sending Files](android/aidl/types/file-descriptor.md)
- [Testing](android/testing.md)
- [GoogleTest](android/testing/googletest.md)
- [Mocking](android/testing/mocking.md)
- [Logging](android/logging.md)
- [Interoperability](android/interoperability.md)
- [With C](android/interoperability/with-c.md)
Expand Down
5 changes: 5 additions & 0 deletions src/android/build_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ EOF

pkill -f birthday_server

run_example <<EOF
# ANCHOR: libleftpad_test
atest --host libleftpad_test
# ANCHOR_END: libleftpad_test
EOF

run_example <<EOF
# ANCHOR: hello_rust_logs
Expand Down
37 changes: 37 additions & 0 deletions src/android/testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Testing in Android

Building on [Testing](../testing.md), we will now look at how unit tests work in
AOSP. Use the `rust_test` module for your unit tests:

_testing/Android.bp_:

```javascript
{{#include testing/Android.bp}}
```

_testing/src/lib.rs_:

```rust
{{#include testing/src/lib.rs:leftpad}}
```

You can now run the test with

```shell
{{#include build_all.sh:libleftpad_test}}
```

The output looks like this:

```text
INFO: Elapsed time: 2.666s, Critical Path: 2.40s
INFO: 3 processes: 2 internal, 1 linux-sandbox.
INFO: Build completed successfully, 3 total actions
//comprehensive-rust-android/testing:libleftpad_test_host PASSED in 2.3s
PASSED libleftpad_test.tests::long_string (0.0s)
PASSED libleftpad_test.tests::short_string (0.0s)
Test cases: finished with 2 passing and 0 failing out of 2 test cases
```

Notice how you only mention the root of the library crate. Tests are found
recursively in nested modules.
13 changes: 13 additions & 0 deletions src/android/testing/Android.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
rust_library {
name: "libleftpad",
crate_name: "leftpad",
srcs: ["src/lib.rs"],
}

rust_test {
name: "libleftpad_test",
crate_name: "leftpad_test",
srcs: ["src/lib.rs"],
host_supported: true,
test_suites: ["general-tests"],
}
21 changes: 21 additions & 0 deletions src/android/testing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "android-testing"
version = "0.1.0"
edition = "2021"
publish = false

[[example]]
name = "googletest-example"
crate-type = ["staticlib"]
path = "googletest.rs"
test = true

[[example]]
name = "mockall-example"
crate-type = ["staticlib"]
path = "mockall.rs"
test = true

[dependencies]
googletest = "0.11.0"
mockall = "0.12.1"
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,4 @@ Difference(-actual / +expected):

[prelude]: https://docs.rs/googletest/latest/googletest/prelude/index.html

- GoogleTest is available for use in AOSP.

</details>
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions src/testing/mocking.md → src/android/testing/mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ to use traits, which you can then quickly mock:

<details>

- The advice here is for Android (AOSP) where Mockall is the recommended mocking
library. There are other
- Mockall is the recommended mocking library in Android (AOSP). There are other
[mocking libraries available on crates.io](https://crates.io/keywords/mock),
in particular in the area of mocking HTTP services. The other mocking
libraries work in a similar fashion as Mockall, meaning that they make it easy
Expand Down
36 changes: 36 additions & 0 deletions src/android/testing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// ANCHOR: leftpad
//! Left-padding library.
/// Left-pad `s` to `width`.
pub fn leftpad(s: &str, width: usize) -> String {
format!("{s:>width$}")
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn short_string() {
assert_eq!(leftpad("foo", 5), " foo");
}

#[test]
fn long_string() {
assert_eq!(leftpad("foobar", 6), "foobar");
}
}
16 changes: 0 additions & 16 deletions src/testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@ version = "0.1.0"
edition = "2021"
publish = false

[[example]]
name = "googletest-example"
crate-type = ["staticlib"]
path = "googletest.rs"
test = true

[[example]]
name = "mockall-example"
crate-type = ["staticlib"]
path = "mockall.rs"
test = true

[[bin]]
name = "luhn"
path = "exercise.rs"

[dependencies]
googletest = "0.11.0"
mockall = "0.12.1"
15 changes: 0 additions & 15 deletions src/testing/useful-crates.md

This file was deleted.

0 comments on commit f23f0c6

Please sign in to comment.