import chai = require("chai");
const expect = chai.expect;
import "mocha";
import StepParser from "../../src/_infra/AntlrStepParser";
import Action from "../../src/domain/Action";

describe("Infra - Step parser AST", () => {
    const parser = new StepParser();

    it("should build a and", () => {
        const stringTest = "click and search";
        return parser.parseStepExpression(stringTest).then((AST) => {
            expect(AST.label).to.eql("and");
            const left = AST.left.label as Action;
            const right = AST.right.label as Action;

            expect(left.prefix).to.eql("click");
            expect(right.prefix).to.eql("search");
        });
    });

    it("should build a or", () => {
        const stringTest = "click or search";
        return parser.parseStepExpression(stringTest).then((AST) => {
            expect(AST.label).to.eql("or");
            const left = AST.left.label as Action;
            const right = AST.right.label as Action;

            expect(left.prefix).to.eql("click");
            expect(right.prefix).to.eql("search");
        });
    });

    it("should build a =>", () => {
        const stringTest = "click => search";
        return parser.parseStepExpression(stringTest).then((AST) => {
            expect(AST.label).to.eql("=>");
            const left = AST.left.label as Action;
            const right = AST.right.label as Action;

            expect(left.prefix).to.eql("click");
            expect(right.prefix).to.eql("search");
        });
    });

    it("should build with parenthesis", () => {
        const stringTest = "(click => search) and searchbutton";
        return parser.parseStepExpression(stringTest).then((AST) => {
            expect(AST.label).to.eql("and");
            const parTree = AST.left;
            expect(parTree.label).to.eql("=>");
            const leftAction1 = parTree.left.label as Action;
            const leftAction2 = parTree.right.label as Action;
            expect(leftAction1.prefix).to.eql("click");
            expect(leftAction2.prefix).to.eql("search");

            const right = AST.right.label as Action;

            expect(right.prefix).to.eql("searchbutton");
        });
    });

});
