## DEV Community 👩‍💻👨‍💻

Nicolas DUBIEN

Posted on • Updated on

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

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

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

## 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);
expect(treeLevels[n + 1]).toEqual(treeLevels);
})
);
});
``````

Back to "Advent of PBT 2021" to see topics covered during the other days and their solutions. Do you want us to help make DEV a better place?