forked from aldebaran/libqi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_module.cpp
More file actions
68 lines (55 loc) · 1.53 KB
/
test_module.cpp
File metadata and controls
68 lines (55 loc) · 1.53 KB
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
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <qi/anymodule.hpp>
#include <qipython/common.hpp>
#include <qipython/pyguard.hpp>
qiLogCategory("TestQiPython.Module");
namespace py = pybind11;
namespace
{
void globalExec(const std::string& str)
{
qi::py::GILAcquire _lock;
py::exec(str.c_str());
}
testing::AssertionResult assertGlobalExec(const std::string& str)
{
try
{
globalExec(str);
}
catch (const std::exception& ex)
{
return testing::AssertionFailure() << ex.what();
}
return testing::AssertionSuccess() << "the code was executed successfully";
}
bool sameModule(const qi::ModuleInfo& lhs, const qi::ModuleInfo& rhs)
{
return lhs.name == rhs.name && lhs.path == rhs.path && lhs.type == rhs.type;
}
} // namespace
MATCHER(ModuleEq, "") { return sameModule(std::get<0>(arg), std::get<1>(arg)); }
TEST(Module, listModules)
{
qi::py::GILAcquire _lock;
auto qi = py::globals()["qi"];
auto modulesFromPython =
qi::AnyReference::from(qi.attr("listModules")()).to<std::vector<qi::ModuleInfo>>();
using namespace testing;
ASSERT_THAT(modulesFromPython, Pointwise(ModuleEq(), qi::listModules()));
}
TEST(ModuleFromCpp, importModule)
{
EXPECT_TRUE(assertGlobalExec("qi.module('moduletest')"));
}
TEST(ModuleFromCpp, importAbsentModuleThrows)
{
EXPECT_FALSE(assertGlobalExec("qi.module('nobody_here')"));
}
TEST(ModuleFromCpp, callModuleHiddenMethod)
{
EXPECT_TRUE(assertGlobalExec("module=qi.module('moduletest')\nmodule._hidden()"));
}