OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / query / IllPutUTest.cxxtest

101 lines · cxxtest ref:
/*
 * tests/query/IllPutUTest.cxxtest
 *
 * Copyright (C) 2018 OpenCog Foundation
 * 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>

using namespace opencog;

#define an _as->add_node
#define al _as->add_link

class IllPutUTest: public CxxTest::TestSuite
{
private:
	AtomSpacePtr _as;
	SchemeEval* _eval;

public:
	IllPutUTest() : _as(createAtomSpace()), _eval(SchemeEval::get_scheme_evaluator(_as))
	{
		logger().set_level(Logger::DEBUG);
		logger().set_print_to_stdout_flag(true);

		_eval->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")");

		_eval->eval("(use-modules (opencog logger))");
	}

	~IllPutUTest()
	{
		// Erase the log file if no assertions failed.
		if (!CxxTest::TestTracker::tracker().suiteFailed())
			std::remove(logger().get_filename().c_str());
	}

	void setUp();
	void tearDown();

	void test_ill_put();
};

void IllPutUTest::tearDown()
{
	_as->clear();
}

void IllPutUTest::setUp()
{
}

/**
 * Run a query evaluating PutLinks, including some ill-formed ones
 * (only detectable at run-time).
 */
void IllPutUTest::test_ill_put()
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	_eval->eval("(load-from-path \"tests/query/ill-put.scm\")");

	Handle get_put = _eval->eval_h("get-put");

	bool caught = false;
	try
	{
		Handle results = HandleCast(get_put->execute(_as.get()));
	}
	catch (const RuntimeException& ex)
	{
		 logger().set_level(Logger::INFO);
		 logger().info("Caught exception, just as expected: %s", ex.get_message());
		 caught = true;
	}
	TSM_ASSERT("Failed to catch expected exception", caught);

	logger().debug("END TEST: %s", __FUNCTION__);
}

#undef an
#undef al