summaryrefslogtreecommitdiff
path: root/libs/log/test/run/attr_function.cpp
blob: 0d59a271775c8df4f992893e3f716845f9ab1936 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 *          Copyright Andrey Semashev 2007 - 2015.
 * Distributed under the Boost Software License, Version 1.0.
 *    (See accompanying file LICENSE_1_0.txt or copy at
 *          http://www.boost.org/LICENSE_1_0.txt)
 */
/*!
 * \file   attr_function.cpp
 * \author Andrey Semashev
 * \date   25.01.2009
 *
 * \brief  This header contains tests for the function attribute.
 */

#define BOOST_TEST_MODULE attr_function

#include <string>
#include <boost/mpl/vector.hpp>
#include <boost/utility/result_of.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/log/attributes/function.hpp>
#include <boost/log/utility/type_dispatch/static_type_dispatcher.hpp>

namespace logging = boost::log;
namespace attrs = logging::attributes;

namespace {

    // Type dispatcher for the supported types
    struct my_dispatcher :
        public logging::static_type_dispatcher<
            boost::mpl::vector< int, std::string >
        >
    {
        typedef logging::static_type_dispatcher<
            boost::mpl::vector< int, std::string >
        > base_type;

        enum type_expected
        {
            none_expected,
            int_expected,
            string_expected
        };

        my_dispatcher() : base_type(*this), m_Expected(none_expected), m_Int(0) {}

        void set_expected()
        {
            m_Expected = none_expected;
        }
        void set_expected(int value)
        {
            m_Expected = int_expected;
            m_Int = value;
        }
        void set_expected(std::string const& value)
        {
            m_Expected = string_expected;
            m_String = value;
        }

        // Implement visitation logic for all supported types
        void operator() (int const& value) const
        {
            BOOST_CHECK_EQUAL(m_Expected, int_expected);
            BOOST_CHECK_EQUAL(m_Int, value);
        }
        void operator() (std::string const& value) const
        {
            BOOST_CHECK_EQUAL(m_Expected, string_expected);
            BOOST_CHECK_EQUAL(m_String, value);
        }

    private:
        type_expected m_Expected;
        int m_Int;
        std::string m_String;
    };

    // A test function that returns an attribute value
    int get_attr_value()
    {
        return 10;
    }

    // A test functional object that returns an attribute value
    struct attr_value_generator
    {
        typedef std::string result_type;

        explicit attr_value_generator(unsigned int& count) : m_CallsCount(count) {}
        result_type operator() () const
        {
            ++m_CallsCount;
            return "Hello, world!";
        }

    private:
        unsigned int& m_CallsCount;
    };

} // namespace

// The test verifies that the attribute calls the specified functor and returns the value returned by the functor
BOOST_AUTO_TEST_CASE(calling)
{
    unsigned int call_count = 0;
    my_dispatcher disp;

    logging::attribute attr1 =
#ifndef BOOST_NO_RESULT_OF
        attrs::make_function(&get_attr_value);
#else
        attrs::make_function< int >(&get_attr_value);
#endif

    logging::attribute attr2 =
#ifndef BOOST_NO_RESULT_OF
        attrs::make_function(attr_value_generator(call_count));
#else
        attrs::make_function< std::string >(attr_value_generator(call_count));
#endif

    logging::attribute_value p1(attr1.get_value());
    logging::attribute_value p2(attr2.get_value());
    BOOST_CHECK_EQUAL(call_count, 1);
    logging::attribute_value p3(attr2.get_value());
    BOOST_CHECK_EQUAL(call_count, 2);

    disp.set_expected(10);
    BOOST_CHECK(p1.dispatch(disp));
    BOOST_CHECK(p1.dispatch(disp)); // check that the contained value doesn't change over time or upon dispatching

    disp.set_expected("Hello, world!");
    BOOST_CHECK(p2.dispatch(disp));
    BOOST_CHECK(p3.dispatch(disp));
}