diff --git a/Cargo.lock b/Cargo.lock index 222ac10ea9fa..b8a042c69310 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,14 @@ dependencies = [ "url", ] +[[package]] +name = "android-testing" +version = "0.1.0" +dependencies = [ + "googletest", + "mockall", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -2372,10 +2380,6 @@ checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" [[package]] name = "testing" version = "0.1.0" -dependencies = [ - "googletest", - "mockall", -] [[package]] name = "thiserror" diff --git a/Cargo.toml b/Cargo.toml index 699306afd989..8ae4220fc8a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/book.toml b/book.toml index a45c63787f20..a329c19239f0 100644 --- a/book.toml +++ b/book.toml @@ -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' diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 13e5e575435b..f81f32627fbd 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) @@ -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) diff --git a/src/android/build_all.sh b/src/android/build_all.sh index d15fde6e4e9d..55b75ffe300d 100755 --- a/src/android/build_all.sh +++ b/src/android/build_all.sh @@ -117,6 +117,11 @@ EOF pkill -f birthday_server +run_example < diff --git a/src/testing/googletest.rs b/src/android/testing/googletest.rs similarity index 100% rename from src/testing/googletest.rs rename to src/android/testing/googletest.rs diff --git a/src/testing/mockall.rs b/src/android/testing/mockall.rs similarity index 100% rename from src/testing/mockall.rs rename to src/android/testing/mockall.rs diff --git a/src/testing/mocking.md b/src/android/testing/mocking.md similarity index 94% rename from src/testing/mocking.md rename to src/android/testing/mocking.md index 1c62cde7cbce..74f9042eed08 100644 --- a/src/testing/mocking.md +++ b/src/android/testing/mocking.md @@ -15,8 +15,7 @@ to use traits, which you can then quickly mock:
-- 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 diff --git a/src/android/testing/src/lib.rs b/src/android/testing/src/lib.rs new file mode 100644 index 000000000000..4ce7b1c60859 --- /dev/null +++ b/src/android/testing/src/lib.rs @@ -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"); + } +} diff --git a/src/testing/Cargo.toml b/src/testing/Cargo.toml index c8f18191407e..049f999caef9 100644 --- a/src/testing/Cargo.toml +++ b/src/testing/Cargo.toml @@ -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" diff --git a/src/testing/useful-crates.md b/src/testing/useful-crates.md deleted file mode 100644 index 2314a5b3b0d3..000000000000 --- a/src/testing/useful-crates.md +++ /dev/null @@ -1,15 +0,0 @@ ---- -minutes: 3 ---- - -# Useful Crates - -Rust comes with only basic support for writing tests. - -Here are some additional crates which we recommend for writing tests: - -- [googletest](https://docs.rs/googletest): Comprehensive test assertion library - in the tradition of GoogleTest for C++. -- [proptest](https://docs.rs/proptest): Property-based testing for Rust. -- [rstest](https://docs.rs/rstest): Support for fixtures and parameterised - tests.