/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 *
 * ast.cpp
 *
 *  Created on: Jun 11, 2015
 *      Author: pgess <v.melnychenko@xreate.org>
 */

#include "gtest/gtest.h"
#include "xreatemanager.h"
#include "main/Parser.h"

using namespace std;
using namespace xreate;
using namespace xreate::grammar::main;

TEST(AST, Containers1){
    FILE* input = fopen("scripts/containers/Containers_Implementation_LinkedList1.xreate","r");
    Scanner scanner(input);
    Parser parser(&scanner);
    parser.Parse();
    assert(!parser.errors->count && "Parser errors");

    fclose(input);
}

TEST(AST, InterfacesDataCFA) {
	XreateManager* man = XreateManager::prepare
				("interface(cfa){\n"
				"    operator map :: annotation1.\n"
				"}");

	auto answer = man->root->__interfacesData.equal_range(CFA);
	EXPECT_EQ(1, std::distance(answer.first, answer.second));

	Expression&& scheme = move(answer.first->second);

	EXPECT_EQ(Operator::MAP, scheme.op);
	EXPECT_EQ("annotation1", scheme.getOperands().at(0).getValueString());
}

TEST(AST, syntax_recognizeIdentifiers){
    XreateManager* man = XreateManager::prepare(R"Code(
            test= function(a:: num):: num; entry {
                a = b:: int.
                b = 8:: int.

                a
            }
    )Code");
}

TEST(AST, syntax_operatorIndex){
    XreateManager* man = XreateManager::prepare(R"Code(
            test= function(a:: num):: num; entry {
                b = a[1].
                b
            }
    )Code");
}

TEST(AST, Variants_switch){
XreateManager* man = XreateManager::prepare(R"Code(
    Color = type variant{Blue, White, Green}.

    main = function:: int {
        x = White()::Color.

        switch variant(x)::int
            case (Green) {0}
            case (White) {1}
            case (Blue){2}
    }
)Code");

    Expression e= man->root->findFunction("main")->getEntryScope()->getBody();
    ASSERT_EQ(4, e.getOperands().size());
    ASSERT_EQ(3, e.blocks.size());
}

TEST(AST, DISABLED_InterfacesDataDFA){

}

TEST(AST, DISABLED_InterfacesDataExtern){

}

//TODO xreate.atg: replace all Type<> as ExprAnnotations<>
