/*
* tests/query/BuggyQuoteUTest.cxxtest
*
* Copyright (C) 2014 Linas Vepstas
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License v3 as
* published by the Free Software Foundation and including the exceptions
* at http://opencog.org/wiki/Licenses
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, write to:
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <opencog/guile/SchemeEval.h>
#include <opencog/atomspace/AtomSpace.h>
#include <opencog/util/Logger.h>
#include "imply.h"
using namespace opencog;
class BuggyQuoteUTest: public CxxTest::TestSuite
{
private:
AtomSpacePtr as;
SchemeEval* eval;
public:
BuggyQuoteUTest(void)
{
logger().set_level(Logger::DEBUG);
logger().set_print_to_stdout_flag(true);
as = createAtomSpace();
eval = SchemeEval::get_scheme_evaluator(as);
eval->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")");
}
~BuggyQuoteUTest()
{
// Erase the log file if no assertions failed.
if (!CxxTest::TestTracker::tracker().suiteFailed())
std::remove(logger().get_filename().c_str());
}
void setUp(void);
void tearDown(void);
void test_bad_quote(void);
void test_good_form(void);
};
void BuggyQuoteUTest::tearDown(void)
{
as->clear();
}
void BuggyQuoteUTest::setUp(void)
{
as->clear();
}
#define getarity(hand) hand->get_arity()
/*
* BuggyQuote unit test. Test binding to unquoted links.
* This test explicitly tests the bug described in
* https://github.com/opencog/opencog/issues/1092
* The old pattern matcher would throw a runtime exception in the
* atomsapce, since we gave it a link with an undefined handle in it.
* The new one throws an invalid parameter exception in the pattern
* matcher, because the pattern was crazy.
*/
void BuggyQuoteUTest::test_bad_quote(void)
{
logger().debug("BEGIN TEST: %s", __FUNCTION__);
eval->eval("(load-from-path \"tests/query/buggy-crime.scm\")");
// This should throw an exception from scheme.
// Disable logging so as to avoid the hopeless spew of errors.
logger().set_level(Logger::NONE);
bool caught = false;
try
{
// We expect the atomspace itself to throw an exception, when
// the bad bindlink is inserted into it.
Handle qrule = eval->eval_h("(query_rule_bad)");
// TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != qrule);
// bindlink(as, qrule);
}
catch (const RuntimeException& e)
{
caught = true;
}
logger().set_level(Logger::INFO);
TS_ASSERT_EQUALS(true, caught);
}
void BuggyQuoteUTest::test_good_form(void)
{
logger().debug("BEGIN TEST: %s", __FUNCTION__);
eval->eval("(load-from-path \"tests/query/buggy-crime.scm\")");
Handle qrule = eval->eval_h("query_rule_good");
TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != qrule);
Handle rslt = HandleCast(qrule->execute(as.get()));
logger().debug() << "Result is this:\n" << rslt->to_string();
// See buggy-crime.scm for explanation for why this is 1 not 2
TS_ASSERT_EQUALS(1, getarity(rslt));
}