From d10bbbc5c88fe2d2a4cbad8d8f683516fd80424c Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Thu, 30 Nov 2023 10:15:48 +0100 Subject: [PATCH] Move Mockall and GoogleTest slides to Android 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. --- src/SUMMARY.md | 6 ++-- src/android/build_all.sh | 5 ++++ src/android/testing.md | 37 +++++++++++++++++++++++++ src/android/testing/Android.bp | 13 +++++++++ src/{ => android}/testing/googletest.md | 2 -- src/{ => android}/testing/googletest.rs | 0 src/{ => android}/testing/mockall.rs | 0 src/{ => android}/testing/mocking.md | 3 +- src/android/testing/src/lib.rs | 36 ++++++++++++++++++++++++ src/testing/useful-crates.md | 15 ---------- 10 files changed, 95 insertions(+), 22 deletions(-) create mode 100644 src/android/testing.md create mode 100644 src/android/testing/Android.bp rename src/{ => android}/testing/googletest.md (97%) rename src/{ => android}/testing/googletest.rs (100%) rename src/{ => android}/testing/mockall.rs (100%) rename src/{ => android}/testing/mocking.md (94%) create mode 100644 src/android/testing/src/lib.rs delete mode 100644 src/testing/useful-crates.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 11b19264535..34fabf44c53 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -172,9 +172,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) @@ -216,6 +213,9 @@ - [Deploy](android/aidl/deploy.md) - [Client](android/aidl/client.md) - [Changing API](android/aidl/changing.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 d15fde6e4e9..55b75ffe300 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 1c62cde7cbc..74f9042eed0 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 00000000000..4ce7b1c6085 --- /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/useful-crates.md b/src/testing/useful-crates.md deleted file mode 100644 index 2314a5b3b0d..00000000000 --- 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.