Belle II Software light-2509-fornax
node_factory.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <functional>
9#include <variant>
10#include <boost/python.hpp>
11
12#include <framework/utilities/CutNodes.h>
13#include <framework/utilities/TestHelpers.h>
14
15#include <gtest/gtest.h>
16
17
18using namespace Belle2;
19namespace {
20 namespace py = boost::python;
22 typedef std::variant<double, int, bool> VarVariant;
24 struct MockObjectType {
26 explicit MockObjectType(const double& d) : m_value{d} {}
27 explicit MockObjectType(const int& i) : m_value{i} {}
28 explicit MockObjectType(const bool& b) : m_value{b} {}
29 VarVariant m_value;
30 };
31
37 class MockVariableType {
38 public:
40 VarVariant function(const MockObjectType* object) const
41 {
42 return m_function(object);
43 }
44 explicit MockVariableType(const std::string& name) : m_name{name}
45 {
46 m_function = [](const MockObjectType * object) -> VarVariant {
47 if (object != nullptr)
48 {
49 return object->m_value;
50 } else
51 {
52 return std::numeric_limits<double>::quiet_NaN();
53 }
54 };
55 }
56
57 MockVariableType(const std::string& name, std::function<VarVariant(const MockObjectType*)> function) : m_name{name}, m_function{function}
58 {}
59
61 std::string m_name;
62 std::function<VarVariant(const MockObjectType*)> m_function;
63 };
64
69 class MockVariableManager {
70 public:
72 using Object = MockObjectType;
74 using Var = MockVariableType;
76 typedef std::variant<double, int, bool> VarVariant;
77
79 static MockVariableManager& Instance()
80 {
81 static MockVariableManager instance;
82 return instance;
83 }
84
86 Var* getVariable(const std::string& name)
87 {
88 if (name == "mocking_variable") {
89 return &m_mocking_variable;
90 } else {
91 return nullptr;
92 }
93 }
94
95 Var* getVariable(const std::string& functionName, const std::vector<std::string>& functionArguments)
96 {
97 (void) functionName;
98 (void) functionArguments;
99 return &m_mocking_variable;
100 }
101
103 Var m_mocking_variable{"mocking_variable"};
104 };
105
106
107 TEST(NodeFactory, FactoryDeathTest)
108 {
109 Py_Initialize();
110 // Make empty tuple
111 py::tuple tuple = py::tuple();
113
114 // Make UnaryExpressionNode which doesn't have the correct length
115 tuple = py::make_tuple(static_cast<int>(NodeType::UnaryExpressionNode));
117
118 // Make BinaryExpressionNode which doesn't have the correct length
119 tuple = py::make_tuple(static_cast<int>(NodeType::BinaryExpressionNode));
121
122 // Make IntegerNode which doesn't have the correct length
123 tuple = py::make_tuple(static_cast<int>(NodeType::IntegerNode));
125
126// Make DoubleNode which doesn't have the correct length
127 tuple = py::make_tuple(static_cast<int>(NodeType::DoubleNode));
129
130 // Make BooleanNode which doesn't have the correct length
131 tuple = py::make_tuple(static_cast<int>(NodeType::BooleanNode));
133
134 // Make IdentifierNode which doesn't have the correct length
135 tuple = py::make_tuple(static_cast<int>(NodeType::IdentifierNode));
137
138 // Make FunctionNode which doesn't have the correct length
139 tuple = py::make_tuple(static_cast<int>(NodeType::FunctionNode));
141
142 // Make Identifier Tuple which should trigger a runtime error
143 tuple = py::make_tuple(static_cast<int>(NodeType::IdentifierNode), "THISDOESNOTEXIST");
144 EXPECT_THROW(Belle2::NodeFactory::compile_expression_node<MockVariableManager>(tuple), std::runtime_error);
145
146 }
147}
Wrapper class for static node compile functions.
Definition NodeFactory.h:64
static std::unique_ptr< const AbstractExpressionNode< AVariableManager > > compile_expression_node(Nodetuple tuple)
This template function creates a ExpressionNode from a boost::python::tuple The Python Parser encodes...
Abstract base class for different kinds of events.