DEV Community

Nicolas DUBIEN
Nicolas DUBIEN

Posted on • Edited on

2 1

Advent of PBT 2021 - Day 20 - Solution

Our algorithm was: drawTree.
Go to the subject itself for more details

CodeSandbox with a possible set of properties you may have come with: https://codesandbox.io/s/advent-of-pbt-day-20-solution-21h7b?file=/src/index.spec.ts&previewwindow=tests


Property 1: should build a linear trunk

for any requested size of tree n
it should render a tree having a linear trunk

Written with fast-check:

it("should build a linear trunc", () => {
  fc.assert(
    fc.property(fc.integer({ min: 1, max: 1000 }), (n) => {
      // Arrange / Act
      const tree = drawTree(n);

      // Assert
      // Remove all the leaves from the tree to only keep the trunk
      const treeWithoutLeaves = tree
        .split("\n")
        .map((level) => level.replace(/[()]/g, " ").trimRight());
      for (const level of treeWithoutLeaves) {
        expect(level.trimLeft()).toEqual("^");
        expect(level).toEqual(treeWithoutLeaves[0]);
      }
    })
  );
});
Enter fullscreen mode Exit fullscreen mode

Property 2: should create larger and larger levels

for any requested size of tree n
it should render a tree having levels being larger and larger as we move down in the tree

Written with fast-check:

it("should create larger and larger levels", () => {
  fc.assert(
    fc.property(fc.integer({ min: 1, max: 1000 }), (n) => {
      // Arrange / Act
      const tree = drawTree(n);

      // Assert
      const treeLevels = tree.split("\n").map((level) => level.trim());
      for (let index = 1; index < n; ++index) {
        expect(treeLevels[index]).toContain(treeLevels[index - 1]);
      }
    })
  );
});
Enter fullscreen mode Exit fullscreen mode

Property 3: should offset leaves from one level to the next one

for any requested size of tree n
it should render a tree with leaves being offset from one level to the next one

Written with fast-check:

it("should offset leaves from one level to the next one", () => {
  fc.assert(
    fc.property(fc.integer({ min: 1, max: 1000 }), (n) => {
      // Arrange / Act
      const tree = drawTree(n);

      // Assert
      const treeLevels = tree.split("\n").map((level) => level.trim());
      for (let index = 1; index < n; ++index) {
        expect(treeLevels[index]).toEqual("(" + treeLevels[index - 1] + ")");
      }
    })
  );
});
Enter fullscreen mode Exit fullscreen mode

Property 4: should create a base of size two with levels identical to the top

for any requested size of tree n
it should render a tree having a base of size 2

Written with fast-check:

it("should create a base of size two with levels identical to the top", () => {
  fc.assert(
    fc.property(fc.integer({ min: 1, max: 1000 }), (n) => {
      // Arrange / Act
      const tree = drawTree(n);

      // Assert
      const treeLevels = tree.split("\n");
      expect(treeLevels).toHaveLength(n + 2);
      expect(treeLevels[n]).toEqual(treeLevels[0]);
      expect(treeLevels[n + 1]).toEqual(treeLevels[0]);
    })
  );
});
Enter fullscreen mode Exit fullscreen mode

Back to "Advent of PBT 2021" to see topics covered during the other days and their solutions.

More about this serie on @ndubien or with the hashtag #AdventOfPBT.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay