a0b84fb74a8095753e3a0a863b8302504588d9ee
[awesomized/libmemcached] / test / lib / ReturnMatcher.hpp
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include "test/lib/common.hpp"
19
20 class ReturnMatcher : public Catch::MatcherBase<memcached_return_t> {
21 public:
22 explicit ReturnMatcher(memcached_st *memc_,
23 memcached_return_t expected_ = MEMCACHED_SUCCESS)
24 : memc{memc_}
25 , expected{expected_}
26 , actual{new actual_st} {}
27
28 ~ReturnMatcher() override {
29 if (actual) {
30 delete actual;
31 }
32 actual = nullptr;
33 }
34 ReturnMatcher(const ReturnMatcher &other) {
35 expected = other.expected;
36 memc = other.memc;
37 actual = new actual_st(other.actual->v);
38 }
39
40 bool match(const memcached_return_t &arg) const override;
41 ReturnMatcher success();
42 ReturnMatcher operator()(memcached_return_t expected_);
43 ReturnMatcher &operator=(memcached_st *memc_);
44
45 protected:
46 string describe() const override;
47
48 private:
49 memcached_st *memc;
50 memcached_return_t expected{MEMCACHED_SUCCESS};
51
52 struct actual_st {
53 memcached_return_t v;
54 explicit actual_st(memcached_return_t _v = MEMCACHED_SUCCESS)
55 : v{_v} {}
56 };
57 actual_st *actual;
58 };
59
60 class LoneReturnMatcher {
61 public:
62 ReturnMatcher returns;
63 explicit LoneReturnMatcher(memcached_st *memc)
64 : returns{memc} {}
65 };