Diamond kata via property-based TDD in JavaScript

Riccardo Odone
HackerNoon.com
7 min readJan 24, 2018

--

In the previous post I’ve covered the basic ideas behind property-based testing. Here, I’m going to TDD the diamond kata using that technique.

The post is heavily inspired (i.e. blatantly copied). So be sure to go say hi to Nat Pryce and Mark Seemann doing the same exercise[1][2] (links at the bottom in the references). Luckily I’m going to use JavaScript and JSVerify. That way I can hide myself behind the “but I’m using a different stack” excuse.

Also, I’m gonna keep code snippets to a minimum. Should you be interested into more details, feel free to check the repo.

The diamond kata

As well described by Seb Rose, the problem statement is as follows:

Given a letter, print a diamond starting with ‘A’ with the supplied letter at the widest point.

A few examples are

Ready, set, rock and roll

In the init commit I want to check the wirings. That’s why I use a generator that always returns 5 to check the isFive property.

which of course is green

The generator

Everything works, thus I can create the generator for the diamond kata. In particular, I need to generate characters in the A..Z range.

Since I’m not sure what to use, I decide to check what the jsc.asciichar generator returns

Notice the return true. That way the “property” debug never fails and I can check all the generated asciichar. Since by default JSVerify checks the property 100 times by generating 100 inputs out of the generator, I see

Not quite right, in fact, I need to generate characters in the A..Z range only. Unfortunately, JSVerify doesn’t provide any generators out of the box that satisfy that constraint. Therefore, I create a custom one

This time we get the proper values

Notice that I could have moved the check inside the property

but I would have made a mistake. In fact, in this case, JSVerify would call property 100 times with random jsc.asciichars. Therefore, only a subset of the generated input would get past the if. In other words, I would lose test coverage.

Property: diamond is not empty

The property that kicks off the exercise just checks the diamond has length different than 0 for any char.

Which I make green with

From the REPL

Property: first row contains A

Which I make green with

From the REPL

Property: last row contains A

Which is already green.

Property: first row has symmetrical contour

Which I make green with

From the REPL

Property: rows have symmetrical contour

Well, not only the first row has a symmetrical contour. Let’s modify the property so that all of the rows are checked

Which is already green.

Property: rows contains the correct letters

Which I make green with

The duplication between test and production code is a bad smell. But I decide to leave it there.

From the REPL

Property: rows are as wide as high

which I make green with

and from the REPL

Property: rows except top and bottom have two identical letters

which I make green with

and from the REPL

Property: rows have the correct amount of internal spaces

which I make green with

and from the REPL

Unfortunately,rowsHaveCorrectAmountOfInternalSpaces in the test uses the following

I don’t like this duplication. Therefore, I decide to test the external space (and not the internal one).

Property: rows have the correct amount of external spaces

This time rowsHaveCorrectAmountOfExternalSpaces internally uses a different calculation:

which means I’ve removed the duplication. Plus, the tests are already green since the production code for the internal spaces takes care of the external too.

And.. We are done

As shown above, the last REPL test gave us

which means

And these are all the properties I have discovered:

  • is not empty
  • first row contains A
  • last row contains A
  • rows have symmetrical contour
  • rows contain the correct letters
  • rows are as wide as high
  • rows except top and bottom have two identical letters
  • rows have the correct amount of external spaces

Outro

The first thing I’ve noticed is how hard property-based TDD makes you think. In fact, it’s really easy to come up with examples for this kata. But the same cannot be said for invariants.

At the same time, knowing what properties your problem space has, means having a deep understanding of it. And with property-based TDD, it’s necessary to discover them before writing the actual production code.

Not only that, I found myself writing a property that conflicted with previous ones. In fact, the code that made it green, also turned red some of the existing. The diamond kata is a simple exercise but this happens frequently in the specs we are given on everyday work.

Also, I’ve built my way up from generic properties first and then specialised (i.e. diamond is not empty to rows have the correct amount of external spaces). Which is the opposite of what happens in example-based TDD: from specific to generic[3].

Unfortunately, I cannot compare much with example-based TDD since I haven’t tried the kata that way. Should you be interested into that, please check out the references.

References

  1. Diamond kata with FsCheck by Mark Seemann
  2. Diamond Kata — TDD with only Property-Based Tests by Nat Pryce
  3. Diamond Kata — Thoughts on Incremental Development by Nat Pryce

More Pointers

Get the latest content via email from me personally. Reply with your thoughts. Let’s learn from each other. Subscribe to my PinkLetter!

--

--

Riccardo Odone
HackerNoon.com

🏳️‍🌈 Pronoun.is/he 💣 Maverick & Leader @Lunar_Logic ✉️ PinkLetter Odone.io/#newsletter 🎓 Student & Teacher of Timeless Software Skills