<commit_before>'use strict';
var requirejs = require("requirejs");
requirejs.config({
    baseUrl: __dirname + '/../../../build/standalone/lib',
    nodeRequire: require
});
var undo = requirejs("wed/undo");
var oop = requirejs("wed/oop");
var UndoList = undo.UndoList;
var Undo = undo.Undo;
var UndoGroup = undo.UndoGroup;
var chai = require("chai");
var assert = chai.assert;

function MyUndo(name, object) {
    Undo.call(this, "sets the field '" + this.name + "' from false to true'");
    this.object = object;
    this.name = name;

    object[name] = true;
}

oop.inherit(MyUndo, Undo);

(function () {
    this.undo = function () {
        this.object[this.name] = false;
    };

    this.redo = function () {
        this.object[this.name] = true;
    };
}).call(MyUndo.prototype);

function MyGroup(name) {
    UndoGroup.call(this, name);
}
oop.inherit(MyGroup, UndoGroup);

describe("UndoList", function () {

    var obj;
    var ul;

    beforeEach(function () {
        obj = {undo1: false, undo2: false};
        ul = new UndoList();
    });

    describe("canUndo", function () {
        it("returns false on new object", function () {
            assert.isFalse(ul.canUndo());
        });

        it("returns true when there is something to undo", function () {
            var undo1 = new MyUndo("undo1", obj);
            ul.record(undo1);
            assert.isTrue(ul.canUndo());
        });

        it("returns false when all is undone", function () {
            var undo1 = new MyUndo("undo1", obj);
            ul.record(undo1);
            var undo2 = new MyUndo("undo2", obj);
            ul.record(undo2);

            ul.undo();
            ul.undo();
            assert.isFalse(ul.canUndo());
        });

    });

    describe("canRedo", function () {
        it("returns false on new object", function () {
            assert.isFalse(ul.canRedo());
        });

        it("returns true when there is something to redo", function () {
            var undo1 = new MyUndo("undo1", obj);
            ul.record(undo1);
            ul.undo();
            assert.isTrue(ul.canRedo());
        });

        it("returns false when all is redone", function () {
            var undo1 = new MyUndo("undo1", obj);
            ul.record(undo1);
            var undo2 = new MyUndo("undo2", obj);
            ul.record(undo2);

            ul.undo();
            ul.undo();
            ul.redo();
            ul.redo();
            assert.isFalse(ul.canRedo());
        });
    });

    describe("endGroup", function () {
        it("throws an error when the object is new", function () {
            assert.Throw(ul.endGroup.bind(ul),
                         Error, "ending a non-existent group");
        });
        it("throws an error upon extra calls", function () {
            ul.startGroup(new MyGroup("group1"));
            ul.endGroup();
            assert.Throw(ul.endGroup.bind(ul),
                         Error, "ending a non-existent group");
        });
        it("ends groups in the proper order", function () {
            ul.startGroup(new MyGroup("group1"));
            ul.startGroup(new MyGroup("group2"));
            ul.endGroup();
            ul.endGroup();
            assert.equal(ul._list[0].toString(), "group1");
            assert.equal(ul._list.length, 1);
        });

    });

    describe("getGroup", function () {
        it("returns undefined when object is new", function () {
            assert.equal(ul.getGroup(), undefined);
        });
        it("returns undefined when all groups have ended", function () {
            ul.startGroup(new MyGroup("group1"));
            ul.endGroup();
            assert.equal(ul.getGroup(), undefined);
        });
        it("returns the group which is current", function () {
            var group1 = new MyGroup("group1");
            var group2 = new MyGroup("group2");
            ul.startGroup(group1);
            ul.startGroup(group2);
            assert.equal(ul.getGroup(), group2);
            ul.endGroup();
            assert.equal(ul.getGroup(), group1);
        });

    });


    describe("record", function () {
        it("records undo operations", function () {
            var undo1 = new MyUndo("undo1", obj);
            ul.record(undo1);
            var undo2 = new MyUndo("undo2", obj);
            ul.record(undo2);<commit_msg>Added test to make sure end() is called when a group ends.<commit_after>'use strict';
var requirejs = require("requirejs");
requirejs.config({
    baseUrl: __dirname + '/../../../build/standalone/lib',
    nodeRequire: require
});
var undo = requirejs("wed/undo");
var oop = requirejs("wed/oop");
var UndoList = undo.UndoList;
var Undo = undo.Undo;
var UndoGroup = undo.UndoGroup;
var chai = require("chai");
var assert = chai.assert;

function MyUndo(name, object) {
    Undo.call(this, "sets the field '" + this.name + "' from false to true'");
    this.object = object;
    this.name = name;

    object[name] = true;
}

oop.inherit(MyUndo, Undo);

(function () {
    this.undo = function () {
        this.object[this.name] = false;
    };

    this.redo = function () {
        this.object[this.name] = true;
    };
}).call(MyUndo.prototype);

function MyGroup(name) {
    UndoGroup.call(this, name);
}
oop.inherit(MyGroup, UndoGroup);

describe("UndoList", function () {

    var obj;
    var ul;

    beforeEach(function () {
        obj = {undo1: false, undo2: false};
        ul = new UndoList();
    });

    describe("canUndo", function () {
        it("returns false on new object", function () {
            assert.isFalse(ul.canUndo());
        });

        it("returns true when there is something to undo", function () {
            var undo1 = new MyUndo("undo1", obj);
            ul.record(undo1);
            assert.isTrue(ul.canUndo());
        });

        it("returns false when all is undone", function () {
            var undo1 = new MyUndo("undo1", obj);
            ul.record(undo1);
            var undo2 = new MyUndo("undo2", obj);
            ul.record(undo2);

            ul.undo();
            ul.undo();
            assert.isFalse(ul.canUndo());
        });

    });

    describe("canRedo", function () {
        it("returns false on new object", function () {
            assert.isFalse(ul.canRedo());
        });

        it("returns true when there is something to redo", function () {
            var undo1 = new MyUndo("undo1", obj);
            ul.record(undo1);
            ul.undo();
            assert.isTrue(ul.canRedo());
        });

        it("returns false when all is redone", function () {
            var undo1 = new MyUndo("undo1", obj);
            ul.record(undo1);
            var undo2 = new MyUndo("undo2", obj);
            ul.record(undo2);

            ul.undo();
            ul.undo();
            ul.redo();
            ul.redo();
            assert.isFalse(ul.canRedo());
        });
    });

    describe("endGroup", function () {
        it("throws an error when the object is new", function () {
            assert.Throw(ul.endGroup.bind(ul),
                         Error, "ending a non-existent group");
        });
        it("throws an error upon extra calls", function () {
            ul.startGroup(new MyGroup("group1"));
            ul.endGroup();
            assert.Throw(ul.endGroup.bind(ul),
                         Error, "ending a non-existent group");
        });
        it("ends groups in the proper order", function () {
            ul.startGroup(new MyGroup("group1"));
            ul.startGroup(new MyGroup("group2"));
            ul.endGroup();
            ul.endGroup();
            assert.equal(ul._list[0].toString(), "group1");
            assert.equal(ul._list.length, 1);
        });

        it("triggers the end() method on a group", function () {
            var group1 = new MyGroup("group1");
            var ended = false;
            group1.end = function () {
                ended = true;
            };
            ul.startGroup(group1);
            ul.endGroup();
            assert.isTrue(ended);
        });


    });

    describe("getGroup", function () {
        it("returns undefined when object is new", function () {
            assert.equal(ul.getGroup(), undefined);
        });
        it("returns undefined when all groups have ended", function () {
            ul.startGroup(new MyGroup("group1"));
            ul.endGroup();
            assert.equal(ul.getGroup(), undefined);
        });
        it("returns the group which is current", function () {
            var group1 = new MyGroup("group1");
            var group2 = new MyGroup("group2");
            ul.startGroup(group1);
            ul.startGroup(group2);
            assert.equal(ul.getGroup(), group2);
            ul.endGroup();
            assert.equal(ul.getGroup(), group1);
        });

    });


    describe("record", function () {
        it("records undo operations", function () {
            var undo1 = new MyUndo("undo1", obj);
            ul.record(undo1);
            var undo2 = new MyUndo("undo2", obj);
            ul.record(undo2);
{'fraction_non_alphanumeric': 0.15073165364726593, 'fraction_numerical': 0.009461987017273627, 'mean_word_length': 1.886630676405208, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2068955', 'n_tokens_mistral': 2681, 'n_tokens_neox': 2597, 'n_words': 620}