/*
* tests/atoms/QuotationUTest.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/atoms/base/Atom.h>
#include <opencog/atomspace/AtomSpace.h>
#include <cxxtest/TestSuite.h>
using namespace opencog;
#define al _asp->add_link
#define an _asp->add_node
// Test quotations.
//
class QuotationUTest : public CxxTest::TestSuite
{
private:
AtomSpacePtr _asp;
SchemeEval* _eval;
public:
QuotationUTest() :_asp(createAtomSpace()), _eval(SchemeEval::get_scheme_evaluator(_asp))
{
logger().set_level(Logger::DEBUG);
logger().set_print_to_stdout_flag(true);
_eval->eval("(add-to-load-path \"" PROJECT_SOURCE_DIR "\")");
}
void setUp() {}
void tearDown() {}
void test_root_local_quote_over_consumption();
void test_lambda_quote_over_consumption();
};
/**
* LocalQuote at the root of an AndLink in a pattern is used to
* indicate that a literal AndLink should be matched instead of
* interpreting as a logic connector.
*/
void QuotationUTest::test_root_local_quote_over_consumption()
{
logger().info("BEGIN TEST: %s", __FUNCTION__);
std::string rs = _eval->eval("(load-from-path \"tests/atoms/scope/put-and.scm\")");
logger().debug() << "rs = " << rs;
Handle and_sha_abs = _eval->eval_h("and-shallow-abstraction"),
result = and_sha_abs->is_executable()
? HandleCast(and_sha_abs->execute(_asp.get()))
: and_sha_abs,
expected = and_sha_abs;
logger().debug() << "result = " << oc_to_string(result);
logger().debug() << "expected = " << oc_to_string(expected);
TS_ASSERT(content_eq(result, expected));
}
/**
* Executing
*
* (QuoteLink
* (LambdaLink
* (UnquoteLink
* (VariableNode "$vardecl"))
* (UnquoteLink
* (VariableNode "$body"))))
*
* should return
*
* (LambdaLink
* (VariableNode "$vardecl")
* (VariableNode "$body"))
*/
void QuotationUTest::test_lambda_quote_over_consumption()
{
logger().info("BEGIN TEST: %s", __FUNCTION__);
std::string rs = _eval->eval("(load-from-path \"tests/atoms/scope/quotations.scm\")");
logger().debug() << "rs = " << rs;
Handle quoted_lambda = _eval->eval_h("quoted-lambda");
Handle result = HandleCast(quoted_lambda->execute(_asp.get()));
Handle expected = _eval->eval_h("raw-lambda");
logger().debug() << "result = " << oc_to_string(result);
logger().debug() << "expected = " << oc_to_string(expected);
TS_ASSERT_EQUALS(result, expected);
logger().info("END TEST: %s", __FUNCTION__);
}
#undef al
#undef an