Advent of PBT 2021 - Day 20 - Solution
Tags: challenge, testing, webdev, javascript
Published: 2021-12-20 09:18:15

---
title: Advent of PBT 2021 - Day 20 - Solution
published: true
description: Advent of Property Based Testing 2021 - Day 20 - Solution
tags: challenge,testing,webdev,javascript
//cover_image: https://direct_url_to_image.jpg
---

Our algorithm was: **drawTree**.
[Go to the subject itself for more details](https://dev.to/dubzzz/advent-of-pbt-2021-day-20-31a3)

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:

```ts
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]);
      }
    })
  );
});
```

---

## 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:

```ts
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]);
      }
    })
  );
});
```

---

## 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:

```ts
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] + ")");
      }
    })
  );
});
```

---

## 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:

```ts
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]);
    })
  );
});
```

---

[Back to "Advent of PBT 2021"](https://dev.to/dubzzz/advent-of-pbt-2021-13ee) to see topics covered during the other days and their solutions.

More about this serie on [@ndubien](https://twitter.com/ndubien) or with the hashtag [#AdventOfPBT](https://twitter.com/search?q=%23AdventOfPBT).