"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const testeditor_1 = require("../testeditor");
const testmodel_1 = require("../testmodel");
const editor_1 = require("../../editor");
const util_1 = require("../../util");
describe("BalanceTree", () => {
    describe("insertBinaryExpression without binary child", () => {
        let root;
        let left;
        let right;
        let rootBox;
        let editor;
        let projection;
        const action = new testeditor_1.CoreTestActions();
        let model;
        let unit;
        let function1;
        beforeEach(done => {
            util_1.PiLogger.muteAllLogs();
            root = new testmodel_1.CoreTestPlusExpression();
            left = new testmodel_1.CoreTestStringLiteralExpression();
            left.value = "rootLeft";
            right = new testmodel_1.CoreTestStringLiteralExpression();
            right.value = "rootRight";
            root.left = left;
            root.right = right;
            model = new testmodel_1.CoreTestModel();
            unit = new testmodel_1.CoreTestUnit();
            model.units.push(unit);
            function1 = new testmodel_1.CoreTestFunction();
            unit.functions.push(function1);
            function1.expression = root;
            projection = new testeditor_1.CoreTestProjection();
            rootBox = projection.getBox(function1);
            editor = new editor_1.PiEditor(projection, action);
            editor.rootElement = unit;
            done();
        });
        describe("with lower priority", () => {
            it("should insert to the left of the operator correctly: <left> Insertion-point <operator> <right>", () => {
                const box = rootBox.findBox(root.piId(), util_1.BEFORE_BINARY_OPERATOR);
                const newExp = testmodel_1.CoretestComparisonExpression.create("<");
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(newExp);
                expect((function1.expression).asString()).toBe('("rootLeft" < (... + "rootRight"))');
            });
            it("should insert to the right of the operator correctly: <left> <operator> Insertion-point <right>", () => {
                const box = rootBox.findBox(root.piId(), util_1.AFTER_BINARY_OPERATOR);
                const newExp = testmodel_1.CoretestComparisonExpression.create("<");
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(newExp);
                expect((function1.expression).asString()).toBe('(("rootLeft" + ...) < "rootRight")');
            });
            it("should insert at left most of the expression correctly: Insertion-point <left> <operator> <right>", () => {
                const box = rootBox.findBox(left.piId(), util_1.LEFT_MOST);
                const newExp = testmodel_1.CoretestComparisonExpression.create("<");
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect((function1.expression).asString()).toBe('(... < ("rootLeft" + "rootRight"))');
            });
            it("should insert at right most of the expression correctly", () => {
                const box = rootBox.findBox(right.piId(), util_1.RIGHT_MOST);
                const newExp = testmodel_1.CoretestComparisonExpression.create("<");
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect((function1.expression).asString()).toBe('(("rootLeft" + "rootRight") < ...)');
            });
        });
        describe("with equal priority", () => {
            it("should insert left of the operator correctly: <left> Insertion-point <operator> <right>", () => {
                const box = rootBox.findBox(root.piId(), util_1.BEFORE_BINARY_OPERATOR);
                const newExp = new testmodel_1.CoreTestPlusExpression();
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(root);
                expect((function1.expression).asString()).toBe('(("rootLeft" + ...) + "rootRight")');
            });
            it("should insert 'right' correctly", () => {
                const box = rootBox.findBox(root.piId(), util_1.AFTER_BINARY_OPERATOR);
                const newExp = new testmodel_1.CoreTestPlusExpression();
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(newExp);
                expect((function1.expression).asString()).toBe('(("rootLeft" + ...) + "rootRight")');
            });
            it("should insert 'pre' correctly", () => {
                const box = rootBox.findBox(left.piId(), util_1.LEFT_MOST);
                const newExp = new testmodel_1.CoreTestPlusExpression();
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(root);
                expect((function1.expression).asString()).toBe('((... + "rootLeft") + "rootRight")');
            });
            it("should insert 'post' correctly", () => {
                const box = rootBox.findBox(right.piId(), util_1.RIGHT_MOST);
                const newExp = new testmodel_1.CoreTestPlusExpression();
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(newExp);
                expect((function1.expression).asString()).toBe('(("rootLeft" + "rootRight") + ...)');
            });
        });
        describe("with higher priority", () => {
            it("should insert 'left' correctly", () => {
                const box = rootBox.findBox(root.piId(), util_1.BEFORE_BINARY_OPERATOR);
                const newExp = new testmodel_1.CoreTestMultiplyExpression();
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(root);
                expect((function1.expression).asString()).toBe('(("rootLeft" * ...) + "rootRight")');
            });
            it("should insert 'right' correctly", () => {
                const box = rootBox.findBox(root.piId(), util_1.AFTER_BINARY_OPERATOR);
                const newExp = new testmodel_1.CoreTestMultiplyExpression();
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(root);
                expect((function1.expression).asString()).toBe('("rootLeft" + (... * "rootRight"))');
            });
            it("should insert 'pre' correctly", () => {
                const box = rootBox.findBox(left.piId(), util_1.LEFT_MOST);
                const newExp = new testmodel_1.CoreTestMultiplyExpression();
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(root);
                expect((function1.expression).asString()).toBe('((... * "rootLeft") + "rootRight")');
            });
            it("should insert 'post' correctly", () => {
                const box = rootBox.findBox(right.piId(), util_1.RIGHT_MOST);
                const newExp = new testmodel_1.CoreTestMultiplyExpression();
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(root);
                expect((function1.expression).asString()).toBe('("rootLeft" + ("rootRight" * ...))');
            });
        });
    });
    describe("insertBinaryExpression with binaryExpression child", () => {
        let root;
        let left;
        let multiLeft;
        let multiRight;
        let rootBox;
        let multiply;
        let projection;
        let editor;
        let model;
        let unit;
        let function1;
        beforeEach(done => {
            root = testmodel_1.CoretestComparisonExpression.create("<");
            left = new testmodel_1.CoreTestStringLiteralExpression();
            left.value = "rootLeft";
            multiRight = new testmodel_1.CoreTestStringLiteralExpression();
            multiRight.value = "multiplyRight";
            multiLeft = new testmodel_1.CoreTestStringLiteralExpression();
            multiLeft.value = "multiplyLeft";
            multiply = new testmodel_1.CoreTestMultiplyExpression();
            root.left = left;
            root.right = multiply;
            multiply.right = multiRight;
            multiply.left = multiLeft;
            model = new testmodel_1.CoreTestModel();
            unit = new testmodel_1.CoreTestUnit();
            model.units.push(unit);
            function1 = new testmodel_1.CoreTestFunction();
            unit.functions.push(function1);
            function1.expression = root;
            const action = new testeditor_1.CoreTestActions();
            projection = new testeditor_1.CoreTestProjection();
            rootBox = projection.getBox(function1);
            editor = new editor_1.PiEditor(projection, action);
            editor.rootElement = unit;
            util_1.PiLogger.muteAllLogs();
            done();
        });
        it("should initialize correctly", () => {
            expect((function1.expression).toString()).toBe('("rootLeft" < ("multiplyLeft" * "multiplyRight"))');
        });
        describe("with lower priority", () => {
            it("should insert 'right' of < correctly", () => {
                const box = rootBox.findBox(root.piId(), util_1.AFTER_BINARY_OPERATOR);
                const newExp = new testmodel_1.CoreTestPlusExpression();
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect((function1.expression).asString()).toBe('("rootLeft" < (... + ("multiplyLeft" * "multiplyRight")))');
            });
            it("should insert 'right' of * correctly", () => {
                const box = rootBox.findBox(multiply.piId(), util_1.AFTER_BINARY_OPERATOR);
                const newExp = new testmodel_1.CoreTestPlusExpression();
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect((function1.expression).asString()).toBe('("rootLeft" < (("multiplyLeft" * ...) + "multiplyRight"))');
            });
        });
        describe("with equal priority", () => {
            it("should insert 'right' of < correctly", () => {
                const box = rootBox.findBox(root.piId(), util_1.AFTER_BINARY_OPERATOR);
                const newExp = new testmodel_1.CoreTestMultiplyExpression();
                expect((function1.expression).toString()).toBe('("rootLeft" < ("multiplyLeft" * "multiplyRight"))');
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(root);
                expect((function1.expression).asString()).toBe('("rootLeft" < ((... * "multiplyLeft") * "multiplyRight"))');
            });
            it("should insert 'right' of * correctly", () => {
                const box = rootBox.findBox(multiply.piId(), util_1.AFTER_BINARY_OPERATOR);
                const newExp = new testmodel_1.CoreTestMultiplyExpression();
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(root);
                expect((function1.expression).asString()).toBe('("rootLeft" < (("multiplyLeft" * ...) * "multiplyRight"))');
            });
        });
        describe("with higher priority", () => {
            it("should insert 'right' of < correctly", () => {
                const box = rootBox.findBox(root.piId(), util_1.AFTER_BINARY_OPERATOR);
                const newExp = new testmodel_1.CoreTestPowerExpression();
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(root);
                expect((function1.expression).asString()).toBe('("rootLeft" < ((... ^ "multiplyLeft") * "multiplyRight"))');
            });
            it("should insert 'right' of * correctly", () => {
                const box = rootBox.findBox(multiply.piId(), util_1.AFTER_BINARY_OPERATOR);
                const newExp = new testmodel_1.CoreTestPowerExpression();
                util_1.BTREE.insertBinaryExpression(newExp, box, editor);
                expect(function1.expression).toBe(root);
                expect((function1.expression).asString()).toBe('("rootLeft" < ("multiplyLeft" * (... ^ "multiplyRight")))');
            });
        });
    });
});
//# sourceMappingURL=BalanceTreeUtilsTest.spec.js.map