Belle II Software development
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 <analysis/VariableManager/Manager.h>
13#include <analysis/VariableManager/Utility.h>
14#include <framework/utilities/CutNodes.h>
15#include <framework/utilities/TestHelpers.h>
16
17#include <gtest/gtest.h>
18
19
20using namespace Belle2;
21namespace {
22 namespace py = boost::python;
23 using VarVariant = Variable::Manager::VarVariant;
24 using VariableManager = Variable::Manager;
26 struct MockObjectType {
28 explicit MockObjectType(const double& d) : m_value{d} {}
29 explicit MockObjectType(const int& i) : m_value{i} {}
30 explicit MockObjectType(const bool& b) : m_value{b} {}
31 VarVariant m_value;
32 };
33
39 class MockVariableType {
40 public:
42 VarVariant function(const MockObjectType* object) const
43 {
44 return m_function(object);
45 }
46 explicit MockVariableType(const std::string& name) : m_name{name}
47 {
48 m_function = [](const MockObjectType * object) -> VarVariant {
49 if (object != nullptr)
50 {
51 return object->m_value;
52 } else
53 {
54 return std::numeric_limits<double>::quiet_NaN();
55 }
56 };
57 }
58
59 MockVariableType(const std::string& name, std::function<VarVariant(const MockObjectType*)> function) : m_name{name}, m_function{function}
60 {}
61
63 std::string m_name;
64 std::function<VarVariant(const MockObjectType*)> m_function;
65 };
66
71 class MockVariableManager {
72 public:
74 using Object = MockObjectType;
76 using Var = MockVariableType;
77
79 typedef std::variant<double, int, bool> VarVariant;
80
81
83 static MockVariableManager& Instance()
84 {
85 static MockVariableManager instance;
86 return instance;
87 }
88
90 Var* getVariable(const std::string& name)
91 {
92 if (name == "mocking_variable") {
93 return &m_mocking_variable;
94 } else {
95 return nullptr;
96 }
97 }
98
99 Var* getVariable(const std::string& functionName, const std::vector<std::string>& functionArguments)
100 {
101 (void) functionName;
102 (void) functionArguments;
103 return &m_mocking_variable;
104 }
105
107 Var m_mocking_variable{"mocking_variable"};
108 };
109
110
111 TEST(NodeFactory, FactoryDeathTest)
112 {
113 Py_Initialize();
114 // Make empty tuple
115 py::tuple tuple = py::tuple();
116 EXPECT_B2FATAL(Belle2::NodeFactory::compile_expression_node<MockVariableManager>(tuple));
117
118 // Make UnaryExpressionNode which doesn't have the correct length
119 tuple = py::make_tuple(static_cast<int>(NodeType::UnaryExpressionNode));
120 EXPECT_B2FATAL(Belle2::NodeFactory::compile_expression_node<MockVariableManager>(tuple));
121
122 // Make BinaryExpressionNode which doesn't have the correct length
123 tuple = py::make_tuple(static_cast<int>(NodeType::BinaryExpressionNode));
124 EXPECT_B2FATAL(Belle2::NodeFactory::compile_expression_node<MockVariableManager>(tuple));
125
126 // Make IntegerNode which doesn't have the correct length
127 tuple = py::make_tuple(static_cast<int>(NodeType::IntegerNode));
128 EXPECT_B2FATAL(Belle2::NodeFactory::compile_expression_node<MockVariableManager>(tuple));
129
130// Make DoubleNode which doesn't have the correct length
131 tuple = py::make_tuple(static_cast<int>(NodeType::DoubleNode));
132 EXPECT_B2FATAL(Belle2::NodeFactory::compile_expression_node<MockVariableManager>(tuple));
133
134 // Make BooleanNode which doesn't have the correct length
135 tuple = py::make_tuple(static_cast<int>(NodeType::BooleanNode));
136 EXPECT_B2FATAL(Belle2::NodeFactory::compile_expression_node<MockVariableManager>(tuple));
137
138 // Make IdentifierNode which doesn't have the correct length
139 tuple = py::make_tuple(static_cast<int>(NodeType::IdentifierNode));
140 EXPECT_B2FATAL(Belle2::NodeFactory::compile_expression_node<MockVariableManager>(tuple));
141
142 // Make FunctionNode which doesn't have the correct length
143 tuple = py::make_tuple(static_cast<int>(NodeType::FunctionNode));
144 EXPECT_B2FATAL(Belle2::NodeFactory::compile_expression_node<MockVariableManager>(tuple));
145
146 // Make Identifier Tuple which should trigger a runtime error
147 tuple = py::make_tuple(static_cast<int>(NodeType::IdentifierNode), "THISDOESNOTEXIST");
148 EXPECT_THROW(Belle2::NodeFactory::compile_expression_node<MockVariableManager>(tuple), std::runtime_error);
149
150 }
151}
Wrapper class for static node compile functions.
Definition: NodeFactory.h:64
Global list of available variables.
Definition: Manager.h:101
std::variant< double, int, bool > VarVariant
NOTE: the python interface is documented manually in analysis/doc/Variables.rst (because we use ROOT ...
Definition: Manager.h:111
Abstract base class for different kinds of events.