paint-brush
Property-Based Testing: The Diamond Kata, First and Last Line Contentby@miguel-bernard
378 reads
378 reads

Property-Based Testing: The Diamond Kata, First and Last Line Content

by Miguel BernardAugust 28th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Property-Based Testing: The Diamond Kata, First and Last Line Content. Miguel Bernard is passionate about teaching, developers' communities and everything related to Net.Net. The first and last line of every diamond always contains the letter A. We are making some good progress towards a fully functioning test suite. However, there are still some uncovered areas that we'll address with more tests next time. The tests are simple to read and short to write. It almost reads like a sentence. It's probably because you missed my previous post.
featured image - Property-Based Testing: The Diamond Kata, First and Last Line Content
Miguel Bernard HackerNoon profile picture

Intro

We continue our adventure trying to solve the Diamond Kata while using Property-Based testing. Last time, we added our first test, Non-empty, and discovered how to use input generators. Now let's figure out the next test.

First and last line content

In the diamond Kata, the first and last line of every diamond always contains A. Such regularity is perfect for a property. Even the particular case with input A, where the first line is also the last one, respects that property.

e.g.

input: A

A
input: E

----A----
---B-B---
--C---C--
-D-----D-
E-------E
-D-----D-
--C---C--
---B-B---
----A----

C# Tests

[Property(Arbitrary = new[] { typeof(LetterGenerator) })]
public Property FirstLineContainsA(char c)
{
    return Diamond.Generate(c).First().Contains('A').ToProperty();
}

[Property(Arbitrary = new[] { typeof(LetterGenerator) })]
public Property LastLineContainsA(char c)
{
    return Diamond.Generate(c).Last().Contains('A').ToProperty();
}

Here we used some built-in methods of the .NET library, which makes these tests simple to read and short to write. It almost reads like a sentence.

Diamond.Generate(c) Generates the diamondFirst() / Last() Takes the first/last line of the generated diamondContains('A') Checks if the line contains the letter A and returns a boolToProperty() Transforms a boolean expression to a property

If you are wondering what's [Property(Arbitrary = new[] { typeof(LetterGenerator) })] it's probably because you missed my previous post

Wrapping up

We are making some good progress towards a fully functioning test suite. However, there are still some uncovered areas that we'll address with more tests next time.

Previously published at https://blog.miguelbernard.com/first-and-last-line-content/