OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-atomspace / tests / query / VarTypeNotUTest.cxxtest

117 lines · cxxtest ref:
/*
 * tests/query/VarTypeNotUTest.cxxtest
 *
 * Copyright (C) 2009 Linas Vepstas <linasvepstas@gmail.com>
 * 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/util/Logger.h>
#include "test-types.h"

using namespace opencog;

class VarTypeNot :  public CxxTest::TestSuite
{
	private:
		AtomSpacePtr as;
		SchemeEval* eval;
		Handle vscope_good, vscope_bad;

	public:

		VarTypeNot(void)
		{
			logger().set_level(Logger::DEBUG);
			logger().set_print_to_stdout_flag(true);
#include "test-types.cc"
		}

		~VarTypeNot()
		{
			// 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_exec(void);
};

/*
 * This function sets up an implication link, and some data.
 */
#define an as->add_node
#define al as->add_link
void VarTypeNot::setUp(void)
{
	as = createAtomSpace();
	eval = SchemeEval::get_scheme_evaluator(as);
	eval->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")");

	eval->eval("(load-from-path \"tests/query/test_types.scm\")");
	eval->eval("(load-from-path \"tests/query/var-type-not.scm\")");
}

void VarTypeNot::tearDown(void)
{
}

/*
 * Basic implication link test.
 */
#define getlink(hand,pos) as->getOutgoing(hand,pos)
#define getarity(hand) as->getArity(hand)

void VarTypeNot::test_exec(void)
{
	logger().debug("BEGIN TEST: %s", __FUNCTION__);

	// Create an implication link that will be tested.
	vscope_good = eval->apply("rule-good", Handle::UNDEFINED);

	// This should throw an exception.
	// Disable logging so as to avoid the hopeless spew of errors.
	logger().set_level(Logger::NONE);

	// With the new atomspace design, we can't even insert the bad atom
	// without throwing. So really, the test doesn't get very far...
	bool caught = false;
	try
	{
		vscope_bad = eval->apply("rule-bad", Handle::UNDEFINED);
	}
   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().set_level(Logger::INFO);

	// Make sure the scheme file actually loaded!
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED != vscope_good);
	TSM_ASSERT("Failed to load test data", Handle::UNDEFINED == vscope_bad);

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