~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to tests/unit-tests/shared_library_test.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-10-10 14:01:26 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20141010140126-n1czko8na1kuz4ll
Tags: upstream-0.8.0+14.10.20141010
ImportĀ upstreamĀ versionĀ 0.8.0+14.10.20141010

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <gtest/gtest.h>
22
22
 
23
23
#include <boost/exception/diagnostic_information.hpp>
24
 
 
25
24
#include <stdexcept>
26
25
 
 
26
#include "mir_test_framework/executable_path.h"
 
27
 
 
28
namespace mtf = mir_test_framework;
 
29
 
27
30
namespace
28
31
{
29
32
class HasSubstring
30
33
{
31
34
public:
32
35
    HasSubstring(char const* substring) : substring(substring) {}
 
36
    HasSubstring(std::string const& substring) : substring{substring.c_str()} {}
33
37
 
34
38
    friend::testing::AssertionResult operator,(std::string const& target, HasSubstring const& match)
35
39
    {
48
52
    HasSubstring& operator=(HasSubstring const&) = delete;
49
53
};
50
54
 
51
 
#define EXPECT_THAT(target, condition) EXPECT_TRUE((target, condition))
52
 
 
53
 
char const* const nonexistent_library  = "nonexistent_library";
54
 
char const* const existing_library     = "libmirplatformgraphics.so";
55
 
char const* const nonexistent_function = "nonexistent_library";
56
 
char const* const existing_function    = "create_platform";
 
55
#define MIR_EXPECT_THAT(target, condition) EXPECT_TRUE((target, condition))
 
56
 
 
57
class SharedLibrary : public testing::Test
 
58
{
 
59
public:
 
60
    SharedLibrary()
 
61
        : nonexistent_library{"imma_totally_not_a_library"},
 
62
          existing_library{mtf::library_path() + "/" MIR_CLIENT_DRIVER_BINARY},
 
63
          nonexistent_function{"yo_dawg"},
 
64
          existing_function{"create_client_platform_factory"},
 
65
          existent_version{"MIR_CLIENTPLATFORM_1"},
 
66
          nonexistent_version{"GOATS_ON_THE_GREEN"}
 
67
    {
 
68
    }
 
69
 
 
70
    std::string const nonexistent_library;
 
71
    std::string const existing_library;
 
72
    std::string const nonexistent_function;
 
73
    std::string const existing_function;
 
74
    std::string const existent_version;
 
75
    std::string const nonexistent_version;
 
76
};
 
77
 
57
78
}
58
79
 
59
 
TEST(SharedLibrary, load_nonexistent_library_fails)
 
80
TEST_F(SharedLibrary, load_nonexistent_library_fails)
60
81
{
61
82
    EXPECT_THROW({ mir::SharedLibrary nonexistent(nonexistent_library); }, std::runtime_error);
62
83
}
63
84
 
64
 
TEST(SharedLibrary, load_nonexistent_library_fails_with_useful_info)
 
85
TEST_F(SharedLibrary, load_nonexistent_library_fails_with_useful_info)
65
86
{
66
87
    try
67
88
    {
71
92
    {
72
93
        auto info = boost::diagnostic_information(error);
73
94
 
74
 
        EXPECT_THAT(info, HasSubstring("cannot open shared object")) << "What went wrong";
75
 
        EXPECT_THAT(info, HasSubstring(nonexistent_library)) << "Name of library";
 
95
        MIR_EXPECT_THAT(info, HasSubstring("cannot open shared object")) << "What went wrong";
 
96
        MIR_EXPECT_THAT(info, HasSubstring(nonexistent_library)) << "Name of library";
76
97
    }
77
98
}
78
99
 
79
 
TEST(SharedLibrary, load_valid_library_works)
 
100
TEST_F(SharedLibrary, load_valid_library_works)
80
101
{
81
102
    mir::SharedLibrary existing(existing_library);
82
103
}
83
104
 
84
 
TEST(SharedLibrary, load_nonexistent_function_fails)
 
105
TEST_F(SharedLibrary, load_nonexistent_function_fails)
85
106
{
86
107
    mir::SharedLibrary existing(existing_library);
87
108
 
88
109
    EXPECT_THROW({ existing.load_function<void(*)()>(nonexistent_function); }, std::runtime_error);
89
110
}
90
111
 
91
 
TEST(SharedLibrary, load_nonexistent_function_fails_with_useful_info)
 
112
TEST_F(SharedLibrary, load_nonexistent_function_fails_with_useful_info)
92
113
{
93
114
    mir::SharedLibrary existing(existing_library);
94
115
 
100
121
    {
101
122
        auto info = boost::diagnostic_information(error);
102
123
 
103
 
        EXPECT_THAT(info, HasSubstring("undefined symbol")) << "What went wrong";
104
 
        EXPECT_THAT(info, HasSubstring(existing_library)) << "Name of library";
105
 
        EXPECT_THAT(info, HasSubstring(nonexistent_function)) << "Name of function";
 
124
        MIR_EXPECT_THAT(info, HasSubstring("undefined symbol")) << "What went wrong";
 
125
        MIR_EXPECT_THAT(info, HasSubstring(existing_library)) << "Name of library";
 
126
        MIR_EXPECT_THAT(info, HasSubstring(nonexistent_function)) << "Name of function";
106
127
    }
107
128
}
108
129
 
109
 
TEST(SharedLibrary, load_valid_function_works)
 
130
TEST_F(SharedLibrary, load_valid_function_works)
110
131
{
111
132
    mir::SharedLibrary existing(existing_library);
112
133
    existing.load_function<void(*)()>(existing_function);
113
134
}
114
135
 
 
136
TEST_F(SharedLibrary, load_valid_versioned_function_works)
 
137
{
 
138
    mir::SharedLibrary existing{existing_library};
 
139
    existing.load_function<void(*)()>(existing_function, existent_version);
 
140
}
 
141
 
 
142
TEST_F(SharedLibrary, load_invalid_versioned_function_fails_with_appropriate_error)
 
143
{
 
144
    mir::SharedLibrary existing{existing_library};
 
145
 
 
146
    try
 
147
    {
 
148
        existing.load_function<void(*)()>(existing_function, nonexistent_version);
 
149
    }
 
150
    catch (std::exception const& error)
 
151
    {
 
152
        auto info = boost::diagnostic_information(error);
 
153
 
 
154
        MIR_EXPECT_THAT(info, HasSubstring("undefined symbol")) << "What went wrong";
 
155
        MIR_EXPECT_THAT(info, HasSubstring(nonexistent_version)) << "Version info";
 
156
        MIR_EXPECT_THAT(info, HasSubstring(existing_library)) << "Name of library";
 
157
        MIR_EXPECT_THAT(info, HasSubstring(existing_function)) << "Name of function";
 
158
    }
 
159
}