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

describe("Plots", () => {
  describe("RectanglePlot", () => {
    describe("RectanglePlot - basics", () => {
      let SVG_WIDTH = 300;
      let SVG_HEIGHT = 300;
      let DATA = [
        { x: 0, y: 0, x2: 1, y2: 1 },
        { x: 1, y: 1, x2: 2, y2: 2 },
        { x: 2, y: 2, x2: 3, y2: 3 },
        { x: 3, y: 3, x2: 4, y2: 4 },
        { x: 4, y: 4, x2: 5, y2: 5 }
      ];
      let VERIFY_CELLS = (cells: d3.Selection<any>) => {
        assert.strictEqual(cells[0].length, 5);
        cells.each(function(d, i) {
          let cell = d3.select(this);
          assert.closeTo(+cell.attr("height"), 50, 0.5, "Cell height is correct");
          assert.closeTo(+cell.attr("width"), 50, 0.5, "Cell width is correct");
          assert.closeTo(+cell.attr("x"), 25 + 50 * i, 0.5, "Cell x coordinate is correct");
          assert.closeTo(+cell.attr("y"), 25 + 50 * (cells[0].length - i - 1), 0.5, "Cell y coordinate is correct");
        });
      };

      let svg: d3.Selection<void>;

      beforeEach(() => {
        svg = TestMethods.generateSVG(SVG_WIDTH, SVG_HEIGHT);
      });

      afterEach(() => {
        svg.remove();
      });

      it("renders correctly", () => {
        let xScale = new Plottable.Scales.Linear();
        let yScale = new Plottable.Scales.Linear();
        let rectanglePlot = new Plottable.Plots.Rectangle();
        rectanglePlot.addDataset(new Plottable.Dataset(DATA));
        rectanglePlot.x((d) => d.x, xScale)
                     .y((d) => d.y, yScale)
                     .x2((d) => d.x2)
                     .y2((d) => d.y2)
                     .renderTo(svg);
        VERIFY_CELLS((<any> rectanglePlot)._renderArea.selectAll("rect"));
      });

      it("retrieves the correct entity under a point", () => {
        let xScale = new Plottable.Scales.Linear();
        let yScale = new Plottable.Scales.Linear();
        let dataset = new Plottable.Dataset(DATA);
        let plot = new Plottable.Plots.Rectangle()
          .x((d) => d.x, xScale).x2((d) => d.x2)
          .y((d) => d.y, yScale).y2((d) => d.y2);
        plot.addDataset(dataset).renderTo(svg);
        let entities = plot.entitiesAt({ x: xScale.scale(2.5), y: yScale.scale(2.5) });
        assert.lengthOf(entities, 1, "found only one entity when querying a point inside the third rectangle");
        assert.strictEqual(entities[0].index, 2, "entity retrieved is at index 2");
      });

      it("retrieves correct entities under a point", () => {
        let xScale = new Plottable.Scales.Linear();
        let yScale = new Plottable.Scales.Linear();
        let dataset = new Plottable.Dataset([
          { x: 1, y: 1, x2: 3, y2: 3 },
          { x: 4, y: 2, x2: 2, y2: 4 }
        ]);
        let plot = new Plottable.Plots.Rectangle()
          .x((d) => d.x, xScale).x2((d) => d.x2)
          .y((d) => d.y, yScale).y2((d) => d.y2);
        plot.addDataset(dataset).renderTo(svg);
        let entities = plot.entitiesAt({ x: xScale.scale(2), y: xScale.scale(2) });
        assert.lengthOf(entities, 2, "two entities when querying a point in both");
        assert.strictEqual(entities[0].index, 0, "entity retrieved is at index 0");
        assert.strictEqual(entities[1].index, 1, "entity retrieved is at index 1");
        entities = plot.entitiesAt({ x: xScale.scale(4), y: yScale.scale(4) });
        assert.lengthOf(entities, 1, "found only one entity when querying a point inside the second rectangle");
        assert.strictEqual(entities[0].index, 1, "entity retrieved is at index 1");
        entities = plot.entitiesAt({ x: xScale.scale(1), y: yScale.scale(1) });
        assert.lengthOf(entities, 1, "found only one entity when querying a point inside the first rectangle");
        assert.strictEqual(entities[0].index, 0, "entity retrieved is at index 0");
      });

      it("retrieves the entities that intersect with the bounding box", () => {
        let xScale = new Plottable.Scales.Linear();
        let yScale = new Plottable.Scales.Linear();
        let dataset = new Plottable.Dataset(DATA);
        let plot = new Plottable.Plots.Rectangle()
          .x((d) => d.x, xScale).x2((d) => d.x2)
          .y((d) => d.y, yScale).y2((d) => d.y2);
        plot.addDataset(dataset).renderTo(svg);

        let entities = plot.entitiesIn({
          topLeft: { x: xScale.scale(1.5), y: yScale.scale(2.5) },
          bottomRight: { x: xScale.scale(2.5), y: yScale.scale(1.5) } });
        assert.lengthOf(entities, 2, "retrieved 2 entities intersect with the box");
        assert.strictEqual(entities[0].index, 1, "the entity of index 1 is retrieved");
        assert.strictEqual(entities[1].index, 2, "the entity of index 2 is retrieved");
      });

      it("retrieves the entities that intersect with the given ranges", () => {
        let xScale = new Plottable.Scales.Linear();
        let yScale = new Plottable.Scales.Linear();
        let dataset = new Plottable.Dataset(DATA);
        let plot = new Plottable.Plots.Rectangle()
          .x((d) => d.x, xScale).x2((d) => d.x2)
          .y((d) => d.y, yScale).y2((d) => d.y2);
        plot.addDataset(dataset).renderTo(svg);

        let entities = plot.entitiesIn(
          {min: xScale.scale(1.5), max: xScale.scale(2.5)},
          {min: yScale.scale(2.5), max: yScale.scale(1.5)});
        assert.lengthOf(entities, 2, "retrieved 2 entities intersect with the box");
        assert.strictEqual(entities[0].index, 1, "the entity of index 1 is retrieved");
        assert.strictEqual(entities[1].index, 2, "the entity of index 2 is retrieved");
      });

    });

    describe("RectanglePlot - autorangeMode()", () => {
      let SVG_WIDTH = 300;
      let SVG_HEIGHT = 300;

      let svg: d3.Selection<void>;

      beforeEach(() => {
        svg = TestMethods.generateSVG(SVG_WIDTH, SVG_HEIGHT);
      });

      afterEach(() => {
        svg.remove();
      });
<commit_msg>[refactorRectangle] Factored out common code in the basics of the rectangle plot<commit_after>///<reference path="../testReference.ts" />

describe("Plots", () => {
  describe("RectanglePlot", () => {
    describe("RectanglePlot - basics", () => {
      let SVG_WIDTH = 300;
      let SVG_HEIGHT = 300;
      let DATA = [
        { x: 0, y: 0, x2: 1, y2: 1 },
        { x: 1, y: 1, x2: 2, y2: 2 },
        { x: 2, y: 2, x2: 3, y2: 3 },
        { x: 3, y: 3, x2: 4, y2: 4 },
        { x: 4, y: 4, x2: 5, y2: 5 }
      ];
      let VERIFY_CELLS = (cells: d3.Selection<any>) => {
        assert.strictEqual(cells[0].length, 5);
        cells.each(function(d, i) {
          let cell = d3.select(this);
          assert.closeTo(+cell.attr("height"), 50, 0.5, "Cell height is correct");
          assert.closeTo(+cell.attr("width"), 50, 0.5, "Cell width is correct");
          assert.closeTo(+cell.attr("x"), 25 + 50 * i, 0.5, "Cell x coordinate is correct");
          assert.closeTo(+cell.attr("y"), 25 + 50 * (cells[0].length - i - 1), 0.5, "Cell y coordinate is correct");
        });
      };

      let svg: d3.Selection<void>;
      let xScale: Plottable.Scales.Linear;
      let yScale: Plottable.Scales.Linear;
      let plot: Plottable.Plots.Rectangle<number, number>;

      beforeEach(() => {
        svg = TestMethods.generateSVG(SVG_WIDTH, SVG_HEIGHT);
        xScale = new Plottable.Scales.Linear();
        yScale = new Plottable.Scales.Linear();
        plot = new Plottable.Plots.Rectangle<number, number>()
          .x((d) => d.x, xScale).x2((d) => d.x2)
          .y((d) => d.y, yScale).y2((d) => d.y2);
      });

      afterEach(() => {
        svg.remove();
      });

      it("renders correctly", () => {
        plot.addDataset(new Plottable.Dataset(DATA));
        plot.renderTo(svg);
        VERIFY_CELLS((<any> plot)._renderArea.selectAll("rect"));
      });

      it("retrieves the correct entity under a point", () => {
        plot.addDataset(new Plottable.Dataset(DATA))
        plot.renderTo(svg);
        let entities = plot.entitiesAt({ x: xScale.scale(2.5), y: yScale.scale(2.5) });
        assert.lengthOf(entities, 1, "found only one entity when querying a point inside the third rectangle");
        assert.strictEqual(entities[0].index, 2, "entity retrieved is at index 2");
      });

      it("retrieves correct entities under a point", () => {
        let dataset = new Plottable.Dataset([
          { x: 1, y: 1, x2: 3, y2: 3 },
          { x: 4, y: 2, x2: 2, y2: 4 }
        ]);
        plot.addDataset(dataset)
        plot.renderTo(svg);

        let entities = plot.entitiesAt({ x: xScale.scale(2), y: xScale.scale(2) });
        assert.lengthOf(entities, 2, "two entities when querying a point in both");
        assert.strictEqual(entities[0].index, 0, "entity retrieved is at index 0");
        assert.strictEqual(entities[1].index, 1, "entity retrieved is at index 1");
        entities = plot.entitiesAt({ x: xScale.scale(4), y: yScale.scale(4) });
        assert.lengthOf(entities, 1, "found only one entity when querying a point inside the second rectangle");
        assert.strictEqual(entities[0].index, 1, "entity retrieved is at index 1");
        entities = plot.entitiesAt({ x: xScale.scale(1), y: yScale.scale(1) });
        assert.lengthOf(entities, 1, "found only one entity when querying a point inside the first rectangle");
        assert.strictEqual(entities[0].index, 0, "entity retrieved is at index 0");
      });

      it("retrieves the entities that intersect with the bounding box", () => {
        plot.addDataset(new Plottable.Dataset(DATA))
        plot.renderTo(svg);

        let entities = plot.entitiesIn({
          topLeft: { x: xScale.scale(1.5), y: yScale.scale(2.5) },
          bottomRight: { x: xScale.scale(2.5), y: yScale.scale(1.5) } });
        assert.lengthOf(entities, 2, "retrieved 2 entities intersect with the box");
        assert.strictEqual(entities[0].index, 1, "the entity of index 1 is retrieved");
        assert.strictEqual(entities[1].index, 2, "the entity of index 2 is retrieved");
      });

      it("retrieves the entities that intersect with the given ranges", () => {
        plot.addDataset(new Plottable.Dataset(DATA))
        plot.renderTo(svg);

        let entities = plot.entitiesIn(
          {min: xScale.scale(1.5), max: xScale.scale(2.5)},
          {min: yScale.scale(2.5), max: yScale.scale(1.5)});
        assert.lengthOf(entities, 2, "retrieved 2 entities intersect with the box");
        assert.strictEqual(entities[0].index, 1, "the entity of index 1 is retrieved");
        assert.strictEqual(entities[1].index, 2, "the entity of index 2 is retrieved");
      });
    });

    describe("RectanglePlot - autorangeMode()", () => {
      let SVG_WIDTH = 300;
      let SVG_HEIGHT = 300;

      let svg: d3.Selection<void>;

      beforeEach(() => {
        svg = TestMethods.generateSVG(SVG_WIDTH, SVG_HEIGHT);
      });

      afterEach(() => {
        svg.remove();
      });

{'fraction_non_alphanumeric': 0.1628480146052031, 'fraction_numerical': 0.028297581013235967, 'mean_word_length': 2.4496221662468516, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 67, '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': '3607277', 'n_tokens_mistral': 3823, 'n_tokens_neox': 3617, 'n_words': 930}