knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::PrologTestsBase Class Reference

#include <PrologTests.h>

Inheritance diagram for knowrob::PrologTestsBase:
Collaboration diagram for knowrob::PrologTestsBase:

Static Protected Member Functions

static void runPrologTests (const std::shared_ptr< knowrob::PrologReasoner > &reasoner, const std::string &target)
 
static void runPrologTests (const std::shared_ptr< knowrob::PrologReasoner > &reasoner, const std::string &target)
 

Detailed Description

A baseclass for prolog test fixtures.

Definition at line 20 of file PrologTests.h.

Member Function Documentation

◆ runPrologTests() [1/2]

void PrologTestsBase::runPrologTests ( const std::shared_ptr< knowrob::PrologReasoner > &  reasoner,
const std::string &  target 
)
staticprotected

Definition at line 23 of file PrologTests.cpp.

25  {
26  bool hasResult = false;
27  int numTests = 0;
28  int numFailedTests = 0;
29 
30  for (const auto &t: reasoner->runTests(PrologEngine::getPrologPath(target))) {
31  hasResult = true;
32  numTests += 1;
33 
34  // each result is a term element/3
35  ASSERT_EQ(t->termType(), TermType::FUNCTION);
36  auto *pElem = (Function *) t.get();
37  ASSERT_EQ(*pElem->functor(), *Atom::Tabled("element"));
38  ASSERT_EQ(pElem->arity(), 3);
39  ASSERT_EQ(*(pElem->arguments()[0]), *Atom::Tabled("testcase"));
40 
41  // the second argument has the form:
42  // [name=::string, file=::string, line=::int, time=::double]
43  OptionList trace(pElem->arguments()[1]);
44  ASSERT_TRUE(trace.contains("file"));
45  ASSERT_TRUE(trace.contains("line"));
46  ASSERT_TRUE(trace.contains("name"));
47  const auto &name = trace.getString("name", "");
48  const char *file = trace.getString("file", "").data();
49  const long line = trace.getLong("line", 0);
50 
51  // the third argument is a list of failures.
52  auto *failureList = (ListTerm *) pElem->arguments()[2].get();
53  if (!failureList->isNIL()) {
54  numFailedTests += 1;
55  }
56  for (const auto &failureTerm: (*failureList)) {
57  ASSERT_EQ(failureTerm->termType(), TermType::FUNCTION);
58  // each failure is a term element/3
59  auto *errElem = (Function *) failureTerm.get();
60  ASSERT_EQ(*errElem->functor(), *Atom::Tabled("element"));
61  ASSERT_EQ(errElem->arity(), 3);
62  ASSERT_EQ(*(errElem->arguments()[0]), *Atom::Tabled("failure"));
63 
64  OptionList errOpts(errElem->arguments()[1]);
65  ASSERT_TRUE(errOpts.contains("type"));
66  ASSERT_TRUE(errOpts.contains("message"));
67 
68  std::ostringstream message_os;
69  message_os << errOpts.getString("type", "");
70  message_os << ": ";
71  message_os << errOpts.getString("message", "");
72  auto message = message_os.str();
73 
74  std::ostringstream summary_os;
75  summary_os << "test: ";
76  summary_os << name;
77  auto summary = summary_os.str();
78 
79  // the second argument has the form: [ type=error|failure, message='...' ]
80  // the third argument is the exact same message, which is a bit strange.
81  //ADD_FAILURE_AT(file,line) << message;
82  GTEST_MESSAGE_AT_(file, line, summary.c_str(), \
83  testing::TestPartResult::kNonFatalFailure) << unescapeString(message);
84  }
85  }
86  EXPECT_TRUE(hasResult);
87  KB_INFO1(target.c_str(), 1, "[plunit] {}/{} tests succeeded for target '{}'.", (numTests - numFailedTests),
88  numTests, target);
89 }
#define KB_INFO1(file, line,...)
Definition: Logger.h:47
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
static std::filesystem::path getPrologPath(const std::filesystem::path &filename)

◆ runPrologTests() [2/2]

static void knowrob::PrologTestsBase::runPrologTests ( const std::shared_ptr< knowrob::PrologReasoner > &  reasoner,
const std::string &  target 
)
staticprotected

The documentation for this class was generated from the following files: