Classes | Typedefs | Functions
test_make_tool.cxx File Reference
#include "dunecore/ArtSupport/Tool/TestTool.h"
#include "art/Utilities/ToolMacros.h"
#include "fhiclcpp/ParameterSet.h"
#include "fhiclcpp/intermediate_table.h"
#include "fhiclcpp/make_ParameterSet.h"
#include "art/Utilities/make_tool.h"
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cassert>

Go to the source code of this file.

Classes

class  INotTestTool
 

Typedefs

using Index = unsigned int
 

Functions

int test_make_tool (bool doCrash, bool useExistingFcl)
 
int main (int narg, char **carg)
 

Typedef Documentation

using Index = unsigned int

Definition at line 34 of file test_make_tool.cxx.

Function Documentation

int main ( int  narg,
char **  carg 
)

Definition at line 141 of file test_make_tool.cxx.

141  {
142  bool doCrash = narg > 1;
143  bool keepFcl = narg > 2;
144  int rstat = test_make_tool(doCrash, keepFcl);
145  return rstat;
146 }
int test_make_tool(bool doCrash, bool useExistingFcl)
int test_make_tool ( bool  doCrash,
bool  useExistingFcl 
)

Definition at line 58 of file test_make_tool.cxx.

58  {
59  const string myname = "test_make_tool: ";
60  cout << myname << "Starting test" << endl;
61 #ifdef NDEBUG
62  cout << myname << "NDEBUG must be off." << endl;
63  abort();
64 #endif
65  string line = "-----------------------------";
66  string scfg;
67 
68  cout << myname << line << endl;
69  string fclfile = "test_make_tool.fcl";
70  if ( ! useExistingFcl ) {
71  cout << myname << "Creating top-level FCL." << endl;
72  ofstream fout(fclfile.c_str());
73  fout << "tools: {" << endl;
74  fout << " mytool1: {" << endl;
75  fout << " tool_type: TestTool" << endl;
76  fout << " Label: \"Tool 1\"" << endl;
77  fout << " }" << endl;
78  fout << " mytool2: {" << endl;
79  fout << " tool_type: TestTool" << endl;
80  fout << " Label: \"Tool 2\"" << endl;
81  fout << " }" << endl;
82  fout << "}" << endl;
83  fout.close();
84  } else {
85  cout << myname << "Using existing top-level FCL." << endl;
86  }
87 
88  cet::filepath_lookup policy("FHICL_FILE_PATH");
89  auto psTop = fhicl::ParameterSet::make
90  (fhicl::parse_document(fclfile, policy));
91  fhicl::ParameterSet pstools = psTop.get<fhicl::ParameterSet>("tools");
92  fhicl::ParameterSet pstool1 = pstools.get<fhicl::ParameterSet>("mytool1");
93  fhicl::ParameterSet pstool2 = pstools.get<fhicl::ParameterSet>("mytool2");
94 
95  cout << myname << line << endl;
96  cout << "Retrieve with the correct interface type." << endl;
97  std::unique_ptr<ITestTool> pt1 = art::make_tool<ITestTool>(pstool1);
98  cout << myname << "Tool pointer: " << pt1.get() << endl;
99  cout << myname << "Tool type: " << pt1->mytype() << endl;
100  assert( pt1->mytype() == "TestTool" );
101  cout << myname << "Tool label: " << pt1->label() << endl;
102  assert( pt1->label() == "Tool 1" );
103 
104  cout << myname << line << endl;
105  cout << "Retrieve with the incorrect interface type." << endl;
106  bool exceptionRaised = false;
107  bool nullReturn = false;
108  std::unique_ptr<INotTestTool> pt2;
109  try {
110  pt2 = art::make_tool<INotTestTool>(pstool2);
111  } catch (...) {
112  exceptionRaised = true;
113  nullReturn = pt2 == nullptr;
114  pt2.reset(nullptr);
115  }
116  if ( pt2 ) {
117  cout << myname << "Tool retrieval succeeded. This is not good." << endl;
118  cout << myname << "Tool pointer: " << pt2.get() << endl;
119  //assert( pt2.get() == nullptr );
120  cout << myname << "Tool type: " << pt2->mytype() << endl;
121  //assert( pt2->mytype() == "BadTestTool" );
122  cout << myname << "Tool label: " << pt2->label() << endl;
123  assert( pt2->label() == "Tool 2" );
124  if ( doCrash ) {
125  cout << myname << "Trying to fetch extra label..." << endl;
126  cout << myname << "Tool extra label: " << pt2->extraLabel() << endl;
127  }
128  } else {
129  cout << myname << "Tool retrieval failed. As it should." << endl;
130  if ( exceptionRaised ) cout << myname << " Exception was raised." << endl;
131  if ( nullReturn ) cout << myname << " Tool pointer was null." << endl;
132  }
133 
134  cout << myname << line << endl;
135  cout << myname << "Done." << endl;
136  return 0;
137 }
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
virtual std::string mytype()
T get(std::string const &key) const
Definition: ParameterSet.h:271
virtual std::string label()=0
virtual std::string mytype() const
Definition: ITestTool.h:19
virtual std::string extraLabel()
virtual std::string label()
intermediate_table parse_document(std::string const &filename, cet::filepath_maker &maker)
Definition: parse.cc:720
void line(double t, double *p, double &x, double &y, double &z)
QTextStream & endl(QTextStream &s)