DEV Community

Nicolas DUBIEN
Nicolas DUBIEN

Posted on • Edited on

Advent of PBT 2021 - Day 18

Advent of PBT 2021 — Learn how to use property based testing and fast-check through examples

Our algorithm today is: isPalindrome.
It comes with the following documentation and prototype:

/**
 * A string s is said to be a palindrome
 * if it reads the same backward and forward
 *
 * @param s - The strings to be assessed
 */
declare function isPalindrome(s: string): boolean;
Enter fullscreen mode Exit fullscreen mode

We already wrote some examples based tests for it:

it("should detect palindromes made of even ascii characters", () => {
  expect(isPalindrome("azza")).toBe(true);
});

it("should detect palindromes made of odd ascii characters", () => {
  expect(isPalindrome("azereza")).toBe(true);
});

it("should detect palindromes made of characters outside of bmp plan", () => {
  expect(isPalindrome("🐱🐴🐱")).toBe(true);
});

it("should detect non palindromes made of even ascii characters", () => {
  expect(isPalindrome("azea")).toBe(false);
});

it("should detect non palindromes made of odd ascii characters", () => {
  expect(isPalindrome("azera")).toBe(false);
});
Enter fullscreen mode Exit fullscreen mode

How would you cover it with Property Based Tests?

In order to ease your task we provide you with an already setup CodeSandbox, with examples based tests already written and a possible implementation of the algorithm: https://codesandbox.io/s/advent-of-pbt-day-18-v924d?file=/src/index.spec.ts&previewwindow=tests

You wanna see the solution? Here is the set of properties I came with to cover today's algorithm: https://dev.to/dubzzz/advent-of-pbt-2021-day-18-solution-28h3


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.

Top comments (0)