ReactivePlusPlus
ReactiveX implementation for C++20
Loading...
Searching...
No Matches
mock_observer.hpp
1// ReactivePlusPlus library
2//
3// Copyright Aleksey Loginov 2023 - present.
4// Distributed under the Boost Software License, Version 1.0.
5// (See accompanying file LICENSE_1_0.txt or copy at
6// https://www.boost.org/LICENSE_1_0.txt)
7//
8// Project home: https://github.com/victimsnino/ReactivePlusPlus
9//
10
11#pragma once
12
13#include <rpp/disposables/fwd.hpp>
14
15#include <rpp/observers/observer.hpp>
16
17#include <vector>
18
19template<typename Type>
21{
22public:
23 static constexpr auto preferred_disposables_mode = rpp::details::observers::disposables_mode::Auto;
24
25 explicit mock_observer_strategy(bool copy_values = true)
26 : m_state{std::make_shared<state>(copy_values)}
27 {
28 }
29
30 void on_next(const Type& v) const noexcept
31 {
32 ++m_state->m_on_next_const_ref_count;
33 if (m_state->m_copy_values)
34 m_state->vals.push_back(v);
35 }
36
37 void on_next(Type&& v) const noexcept
38 {
39 ++m_state->m_on_next_move_count;
40 if (m_state->m_copy_values)
41 m_state->vals.push_back(std::move(v));
42 }
43
44 void on_error(const std::exception_ptr&) const noexcept { ++m_state->m_on_error_count; }
45 void on_completed() const noexcept { ++m_state->m_on_completed_count; }
46
47 static bool is_disposed() noexcept { return false; }
48 static void set_upstream(const rpp::disposable_wrapper&) noexcept {}
49
50 size_t get_total_on_next_count() const { return m_state->m_on_next_const_ref_count + m_state->m_on_next_move_count; }
51 size_t get_on_next_const_ref_count() const { return m_state->m_on_next_const_ref_count; }
52 size_t get_on_next_move_count() const { return m_state->m_on_next_move_count; }
53 size_t get_on_error_count() const { return m_state->m_on_error_count; }
54 size_t get_on_completed_count() const { return m_state->m_on_completed_count; }
55
56 std::vector<Type> get_received_values() const { return m_state->vals; }
57
58 auto get_observer() const { return rpp::observer<Type, mock_observer_strategy<Type>>{*this}; }
60
61private:
62 struct state
63 {
64 explicit state(bool copy_values)
65 : m_copy_values{copy_values}
66 {
67 }
68
69 bool m_copy_values = true;
70 size_t m_on_next_const_ref_count = 0;
71 size_t m_on_next_move_count = 0;
72 size_t m_on_error_count = 0;
73 size_t m_on_completed_count = 0;
74
75 std::vector<Type> vals{};
76 };
77
78 std::shared_ptr<state> m_state{};
79};
Definition mock_observer.hpp:21
Base class for any observer used in RPP. It handles core callbacks of observers. Objects of this clas...
Definition observer.hpp:172