<commit_before>///<reference path="testReference.ts" />

var assert = chai.assert;

function generateBasicTable(nRows, nCols) {
  // makes a table with exactly nRows * nCols children in a regular grid, with each
  // child being a basic component
  var table = new Plottable.Table();
  var rows: Plottable.Component[][] = [];
  var components: Plottable.Component[] = [];
  for(var i=0; i<nRows; i++) {
    for(var j=0; j<nCols; j++) {
      var r = new Plottable.Component();
      table.addComponent(i, j, r);
      components.push(r);
    }
  }
  return {"table": table, "components": components};
}

describe("Tables", () => {
  it("tables are classed properly", () => {
    var table = new Plottable.Table();
    assert.isTrue(table.classed("table"));
  });

  it("padTableToSize works properly", () => {
    var t = new Plottable.Table();
    assert.deepEqual((<any> t).rows, [], "the table rows is an empty list");
    (<any> t).padTableToSize(1,1);
    var rows = (<any> t).rows;
    var row = rows[0];
    var firstComponent = row[0];
    assert.lengthOf(rows, 1, "there is one row");
    assert.lengthOf(row, 1, "the row has one element");
    assert.isTrue(firstComponent.constructor.name === "Component", "the row only has a null component");

    (<any> t).padTableToSize(5,2);
    assert.lengthOf(rows, 5, "there are five rows");
    rows.forEach((r) => assert.lengthOf(r, 2, "there are two columsn per row"));
    assert.equal(rows[0][0], firstComponent, "the first component is unchanged");
  });

  it("table constructor can take a list of lists of components", () => {
    var c0 = new Plottable.Component();
    var row1 = [null, c0];
    var row2 = [new Plottable.Component(), null];
    var table = new Plottable.Table([row1, row2]);
    assert.isTrue((<any> table).rows[0][0].constructor.name === "Component", "the first element was turned into a null component");
    assert.equal((<any> table).rows[0][1], c0, "the component is in the right spot");
    var c1 = new Plottable.Component();
    table.addComponent(2, 2, c1);
    assert.equal((<any> table).rows[2][2], c1, "the inserted component went to the right spot");
  });

  it("tables can be constructed by adding components in matrix style", () => {
    var table = new Plottable.Table();
    var c1 = new Plottable.Component();
    var c2 = new Plottable.Component();
    table.addComponent(0, 0, c1);
    table.addComponent(1, 1, c2);
    var rows = (<any> table).rows;
    assert.lengthOf(rows, 2, "there are two rows");
    assert.lengthOf(rows[0], 2, "two cols in first row");
    assert.lengthOf(rows[1], 2, "two cols in second row");
    assert.equal(rows[0][0], c1, "first component added correctly");
    assert.equal(rows[1][1], c2, "second component added correctly");
    assert.isTrue(rows[0][1].constructor.name === "Component", "added a null component to 0,1");
    assert.isTrue(rows[1][0].constructor.name === "Component", "added a null component to 1,0");
  });

  it("base components are overwritten by the addComponent constructor, and other components are not", () => {
    var c0 = new Plottable.Component();
    var c1 = new Plottable.Table();
    var c2 = new Plottable.Table();
    var t = new Plottable.Table();
    t.addComponent(0, 0, c0);
    t.addComponent(0, 2, c1);
    t.addComponent(0, 0, c2);
    assert.equal((<any> t).rows[0][0], c2, "the baseComponent was overwritten by the table");
    assert.throws(() => t.addComponent(0, 2, c2), Error, "component already exists");
  });


  it("tables with insufficient space throw Insufficient Space", () => {
    var svg = generateSVG(200, 200);
    var c = new Plottable.Component().rowMinimum(300).colMinimum(300);
    var t = new Plottable.Table().addComponent(0, 0, c);
    t.anchor(svg);
    assert.throws(() => t.computeLayout(), Error, "Insufficient Space");
    svg.remove();
  });

  it("basic table with 2 rows 2 cols lays out properly", () => {
    var tableAndcomponents = generateBasicTable(2,2);
    var table = tableAndcomponents.table;
    var components = tableAndcomponents.components;
    // force the components to have non-fixed layout; eg. as if they were renderers
    components.forEach((c) => {
      c.fixedWidthVal = false;
      c.fixedHeightVal = false;
    });

    var svg = generateSVG();
    table.renderTo(svg);

    var elements = components.map((r) => r.element);
    var translates = elements.map((e) => getTranslate(e));
    assert.deepEqual(translates[0], [0, 0], "first element is centered at origin");
    assert.deepEqual(translates[1], [200, 0], "second element is located properly");
    assert.deepEqual(translates[2], [0, 200], "third element is located properly");
    assert.deepEqual(translates[3], [200, 200], "fourth element is located properly");
    var bboxes = elements.map((e) => Plottable.Utils.getBBox(e));
    bboxes.forEach((b) => {<commit_msg>Add a unit test to catch #180 (prior to fixing it, verify fails)<commit_after>///<reference path="testReference.ts" />

var assert = chai.assert;

function generateBasicTable(nRows, nCols) {
  // makes a table with exactly nRows * nCols children in a regular grid, with each
  // child being a basic component
  var table = new Plottable.Table();
  var rows: Plottable.Component[][] = [];
  var components: Plottable.Component[] = [];
  for(var i=0; i<nRows; i++) {
    for(var j=0; j<nCols; j++) {
      var r = new Plottable.Component();
      table.addComponent(i, j, r);
      components.push(r);
    }
  }
  return {"table": table, "components": components};
}

describe("Tables", () => {
  it("tables are classed properly", () => {
    var table = new Plottable.Table();
    assert.isTrue(table.classed("table"));
  });

  it("padTableToSize works properly", () => {
    var t = new Plottable.Table();
    assert.deepEqual((<any> t).rows, [], "the table rows is an empty list");
    (<any> t).padTableToSize(1,1);
    var rows = (<any> t).rows;
    var row = rows[0];
    var firstComponent = row[0];
    assert.lengthOf(rows, 1, "there is one row");
    assert.lengthOf(row, 1, "the row has one element");
    assert.isTrue(firstComponent.constructor.name === "Component", "the row only has a null component");

    (<any> t).padTableToSize(5,2);
    assert.lengthOf(rows, 5, "there are five rows");
    rows.forEach((r) => assert.lengthOf(r, 2, "there are two columsn per row"));
    assert.equal(rows[0][0], firstComponent, "the first component is unchanged");
  });

  it("table constructor can take a list of lists of components", () => {
    var c0 = new Plottable.Component();
    var row1 = [null, c0];
    var row2 = [new Plottable.Component(), null];
    var table = new Plottable.Table([row1, row2]);
    assert.isTrue((<any> table).rows[0][0].constructor.name === "Component", "the first element was turned into a null component");
    assert.equal((<any> table).rows[0][1], c0, "the component is in the right spot");
    var c1 = new Plottable.Component();
    table.addComponent(2, 2, c1);
    assert.equal((<any> table).rows[2][2], c1, "the inserted component went to the right spot");
  });

  it("tables can be constructed by adding components in matrix style", () => {
    var table = new Plottable.Table();
    var c1 = new Plottable.Component();
    var c2 = new Plottable.Component();
    table.addComponent(0, 0, c1);
    table.addComponent(1, 1, c2);
    var rows = (<any> table).rows;
    assert.lengthOf(rows, 2, "there are two rows");
    assert.lengthOf(rows[0], 2, "two cols in first row");
    assert.lengthOf(rows[1], 2, "two cols in second row");
    assert.equal(rows[0][0], c1, "first component added correctly");
    assert.equal(rows[1][1], c2, "second component added correctly");
    assert.isTrue(rows[0][1].constructor.name === "Component", "added a null component to 0,1");
    assert.isTrue(rows[1][0].constructor.name === "Component", "added a null component to 1,0");
  });

  it("base components are overwritten by the addComponent constructor, and other components are not", () => {
    var c0 = new Plottable.Component();
    var c1 = new Plottable.Table();
    var c2 = new Plottable.Table();
    var t = new Plottable.Table();
    t.addComponent(0, 0, c0);
    t.addComponent(0, 2, c1);
    t.addComponent(0, 0, c2);
    assert.equal((<any> t).rows[0][0], c2, "the baseComponent was overwritten by the table");
    assert.throws(() => t.addComponent(0, 2, c2), Error, "component already exists");
  });

  it("addComponent works even if a component is added with a high column and low row index", () => {
    // Solves #180, a weird bug
    var t = new Plottable.Table();
    var svg = generateSVG();
    t.addComponent(1, 0, new Plottable.Component());
    t.addComponent(0, 2, new Plottable.Component());
    t.renderTo(svg); //would throw an error without the fix (tested);
    svg.remove();
  });


  it("tables with insufficient space throw Insufficient Space", () => {
    var svg = generateSVG(200, 200);
    var c = new Plottable.Component().rowMinimum(300).colMinimum(300);
    var t = new Plottable.Table().addComponent(0, 0, c);
    t.anchor(svg);
    assert.throws(() => t.computeLayout(), Error, "Insufficient Space");
    svg.remove();
  });

  it("basic table with 2 rows 2 cols lays out properly", () => {
    var tableAndcomponents = generateBasicTable(2,2);
    var table = tableAndcomponents.table;
    var components = tableAndcomponents.components;
    // force the components to have non-fixed layout; eg. as if they were renderers
    components.forEach((c) => {
      c.fixedWidthVal = false;
      c.fixedHeightVal = false;
    });

    var svg = generateSVG();
    table.renderTo(svg);

    var elements = components.map((r) => r.element);
    var translates = elements.map((e) => getTranslate(e));
    assert.deepEqual(translates[0], [0, 0], "first element is centered at origin");
    assert.deepEqual(translates[1], [200, 0], "second element is located properly");
    assert.deepEqual(translates[2], [0, 200], "third element is located properly");
    assert.deepEqual(translates[3], [200, 200], "fourth element is located properly");
    var bboxes = elements.map((e) => Plottable.Utils.getBBox(e));
    bboxes.forEach((b) => {
{'fraction_non_alphanumeric': 0.16117936117936119, 'fraction_numerical': 0.023587223587223587, 'mean_word_length': 3.715477293790547, 'pattern_counts': {'":': 4, '<': 27, '<?xml version=': 0, '>': 56, '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': '4849295', 'n_tokens_mistral': 3359, 'n_tokens_neox': 3222, 'n_words': 980}