summaryrefslogtreecommitdiff
path: root/tests/callable_test.cpp
blob: 3cbbc5e4b6b9a08321d341f53b2cf32072dbabc9 (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
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.

#include <boost/test/unit_test.hpp>
#include <iostream>

#include <gras/callable.hpp>


struct MyClass : gras::Callable
{
    MyClass(void)
    {
        count = 0;
        this->register_call("test_args0_with_return", &MyClass::test_args0_with_return);
        this->register_call("test_args1_with_return", &MyClass::test_args1_with_return);
        this->register_call("test_args2_with_return", &MyClass::test_args2_with_return);
        this->register_call("test_args3_with_return", &MyClass::test_args3_with_return);
        this->register_call("test_args0_with_no_return", &MyClass::test_args0_with_no_return);
        this->register_call("test_args1_with_no_return", &MyClass::test_args1_with_no_return);
        this->register_call("test_args2_with_no_return", &MyClass::test_args2_with_no_return);
        this->register_call("test_args3_with_no_return", &MyClass::test_args3_with_no_return);
    }

    int test_args0_with_return(void)
    {
        return count;
    }

    int test_args1_with_return(const int &a0)
    {
        return a0;
    }

    int test_args2_with_return(const int &a0, const std::string &a1)
    {
        return a0 + a1.size();
    }

    int test_args3_with_return(const int &a0, const std::string &a1, const bool &a3)
    {
        return a3? a0 + a1.size() : 0;
    }

    void test_args0_with_no_return(void)
    {
        count++;
    }

    void test_args1_with_no_return(const int &a0)
    {
        count = a0;
    }

    void test_args2_with_no_return(const int &a0, const std::string &a1)
    {
        count = a0 + a1.size();
    }

    void test_args3_with_no_return(const int &a0, const std::string &a1, const bool &a2)
    {
        if (a2) count = a0 + a1.size();
        else count = 0;
    }

    int count;
};

BOOST_AUTO_TEST_CASE(test_registered_methods)
{
    MyClass my_class;
    {
        size_t ret = my_class.x<size_t>("test_args0_with_return");
        BOOST_CHECK_EQUAL(ret, size_t(0));
    }
    {
        size_t ret = my_class.x<size_t>("test_args1_with_return", 42);
        BOOST_CHECK_EQUAL(ret, size_t(42));
    }
    {
        size_t ret = my_class.x<size_t>("test_args2_with_return", 1, "OK");
        BOOST_CHECK_EQUAL(ret, size_t(3));
    }
    {
        size_t ret = my_class.x<size_t>("test_args3_with_return", 1, "OK", true);
        BOOST_CHECK_EQUAL(ret, size_t(3));
    }
    {
        size_t ret = my_class.x<size_t>("test_args3_with_return", 1, "OK", false);
        BOOST_CHECK_EQUAL(ret, size_t(0));
    }
    {
        my_class.x("test_args0_with_no_return"); //this adds 1 to count
        size_t ret = my_class.x<size_t>("test_args0_with_return");
        BOOST_CHECK_EQUAL(ret, size_t(1));
    }
    {
        my_class.x("test_args1_with_no_return", 42);
        size_t ret = my_class.x<size_t>("test_args0_with_return");
        BOOST_CHECK_EQUAL(ret, size_t(42));
    }
    {
        my_class.x("test_args2_with_no_return", 1, "OK");
        size_t ret = my_class.x<size_t>("test_args0_with_return");
        BOOST_CHECK_EQUAL(ret, size_t(3));
    }
    {
        my_class.x("test_args3_with_no_return", 1, "OK", true);
        size_t ret = my_class.x<size_t>("test_args0_with_return");
        BOOST_CHECK_EQUAL(ret, size_t(3));
    }
    {
        my_class.x("test_args3_with_no_return", 1, "OK", false);
        size_t ret = my_class.x<size_t>("test_args0_with_return");
        BOOST_CHECK_EQUAL(ret, size_t(0));
    }
}