bind_test_util.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TEST_BIND_TEST_UTIL_H_
  5. #define BASE_TEST_BIND_TEST_UTIL_H_
  6. #include <type_traits>
  7. #include <utility>
  8. #include "base/bind.h"
  9. #include "base/strings/string_piece.h"
  10. namespace base {
  11. class Location;
  12. namespace internal {
  13. template <typename Callable,
  14. typename Signature = decltype(&Callable::operator())>
  15. struct HasConstCallOperatorImpl : std::false_type {};
  16. template <typename Callable, typename R, typename... Args>
  17. struct HasConstCallOperatorImpl<Callable, R (Callable::*)(Args...) const>
  18. : std::true_type {};
  19. template <typename Callable>
  20. constexpr bool HasConstCallOperator =
  21. HasConstCallOperatorImpl<std::decay_t<Callable>>::value;
  22. template <typename F, typename Signature>
  23. struct BindLambdaHelper;
  24. template <typename F, typename R, typename... Args>
  25. struct BindLambdaHelper<F, R(Args...)> {
  26. static R Run(const std::decay_t<F>& f, Args... args) {
  27. return f(std::forward<Args>(args)...);
  28. }
  29. static R RunOnce(std::decay_t<F>&& f, Args... args) {
  30. return f(std::forward<Args>(args)...);
  31. }
  32. };
  33. } // namespace internal
  34. // A variant of BindRepeating() that can bind capturing lambdas for testing.
  35. // This doesn't support extra arguments binding as the lambda itself can do.
  36. template <typename Lambda,
  37. std::enable_if_t<internal::HasConstCallOperator<Lambda>>* = nullptr>
  38. decltype(auto) BindLambdaForTesting(Lambda&& lambda) {
  39. using Signature = internal::ExtractCallableRunType<std::decay_t<Lambda>>;
  40. return BindRepeating(&internal::BindLambdaHelper<Lambda, Signature>::Run,
  41. std::forward<Lambda>(lambda));
  42. }
  43. // A variant of BindRepeating() that can bind mutable capturing lambdas for
  44. // testing. This doesn't support extra arguments binding as the lambda itself
  45. // can do. Since a mutable lambda potentially can invalidate its state after
  46. // being run once, this method returns a OnceCallback instead of a
  47. // RepeatingCallback.
  48. template <typename Lambda,
  49. std::enable_if_t<!internal::HasConstCallOperator<Lambda>>* = nullptr>
  50. decltype(auto) BindLambdaForTesting(Lambda&& lambda) {
  51. static_assert(
  52. std::is_rvalue_reference<Lambda&&>() &&
  53. !std::is_const<std::remove_reference_t<Lambda>>(),
  54. "BindLambdaForTesting requires non-const rvalue for mutable lambda "
  55. "binding. I.e.: base::BindLambdaForTesting(std::move(lambda)).");
  56. using Signature = internal::ExtractCallableRunType<std::decay_t<Lambda>>;
  57. return BindOnce(&internal::BindLambdaHelper<Lambda, Signature>::RunOnce,
  58. std::move(lambda));
  59. }
  60. // Returns a closure that fails on destruction if it hasn't been run.
  61. OnceClosure MakeExpectedRunClosure(const Location& location,
  62. StringPiece message = StringPiece());
  63. RepeatingClosure MakeExpectedRunAtLeastOnceClosure(
  64. const Location& location,
  65. StringPiece message = StringPiece());
  66. // Returns a closure that fails the test if run.
  67. RepeatingClosure MakeExpectedNotRunClosure(const Location& location,
  68. StringPiece message = StringPiece());
  69. } // namespace base
  70. #endif // BASE_TEST_BIND_TEST_UTIL_H_