aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/gtest_extensions/probe.h
blob: 7d1fee83d38b4d0398ec6096c7398e2ef0c4c03e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#pragma once

#include <util/system/yassert.h>

#include <library/cpp/testing/common/probe.h>

#include <gtest/gtest.h>
#include <gmock/gmock.h>

namespace testing {
    using NTesting::TProbe;
    using NTesting::TProbeState;
    using NTesting::TCoercibleToProbe;

    // A helper functor which extracts from probe-like objectss their state.
    struct TProbableTraits {
        static const TProbeState& ExtractState(const TProbeState& probe) {
            return probe;
        }

        static const TProbeState& ExtractState(const TProbeState* probe) {
            return *probe;
        }

        static const TProbeState& ExtractState(const TProbe& probe) {
            return *probe.State;
        }

        static const TProbeState& ExtractState(const TCoercibleToProbe& probe) {
            return *probe.State;
        }
    };

    void PrintTo(const TProbeState& state, ::std::ostream* os);

    inline void PrintTo(const TProbe& probe, ::std::ostream* os) {
        PrintTo(TProbableTraits::ExtractState(probe), os);
    }

    inline void PrintTo(const TCoercibleToProbe& probe, ::std::ostream* os) {
        PrintTo(TProbableTraits::ExtractState(probe), os);
    }

    MATCHER(IsAlive, "is alive") {
        Y_UNUSED(result_listener);
        const auto& state = TProbableTraits::ExtractState(arg);
        return state.Destructors < state.Constructors + state.CopyConstructors + state.CopyAssignments;
    }

    MATCHER(IsDead, "is dead") {
        Y_UNUSED(result_listener);
        const auto& state = TProbableTraits::ExtractState(arg);
        return state.Destructors == state.Constructors + state.CopyConstructors + state.CopyAssignments;
    }

    MATCHER_P2(HasCopyMoveCounts, copyCount, moveCount, "" + \
        PrintToString(copyCount) + " copy constructors and " + \
        PrintToString(moveCount) + " move constructors were called") {
        Y_UNUSED(result_listener);
        const auto& state = TProbableTraits::ExtractState(arg);
        return state.CopyConstructors == copyCount && state.MoveConstructors == moveCount;
    }

    MATCHER(NoCopies, "no copies were made") {
        Y_UNUSED(result_listener);
        const auto& state = TProbableTraits::ExtractState(arg);
        return 0 == state.CopyConstructors && 0 == state.CopyAssignments;
    }

    MATCHER(NoMoves, "no moves were made") {
        Y_UNUSED(result_listener);
        const auto& state = TProbableTraits::ExtractState(arg);
        return 0 == state.MoveConstructors && 0 == state.MoveAssignments;
    }

    MATCHER(NoAssignments, "no assignments were made") {
        Y_UNUSED(result_listener);
        const auto& state = TProbableTraits::ExtractState(arg);
        return 0 == state.CopyAssignments && 0 == state.MoveAssignments;
    }
}