First and Last line content | Solving the diamond kata with property-based testing
Tags: testing, csharp, dotnet
Published: 2020-07-20 17:19:03

# [Solving the diamond kata with property-based testing series](https://blog.miguelbernard.com/solving-the-diamond-kata-with-property-based-testing-series/)

1. [How to get started with Property-based Testing in C#](https://blog.miguelbernard.com/how-to-get-started-with-property-based-testing-in-csharp/)
1. [Input generators in property-based tests with FsCheck](https://blog.miguelbernard.com/input-generators-in-property-based-tests-with-fscheck)
1. [First and Last line content](https://blog.miguelbernard.com/first-and-last-line-content)
1. [Height equals Width](https://blog.miguelbernard.com/height-equals-width/)
1. [Outside space symmetry](https://blog.miguelbernard.com/outside-space-symmetry/)
1. [Symmetry around the vertical axis](https://blog.miguelbernard.com/symmetry-around-the-vertical-axis/)
1. [Symmetry around the horizontal axis](https://blog.miguelbernard.com/symmetry-around-the-horizontal-axis/)
1. [No padding for input letter row](https://blog.miguelbernard.com/no-padding-for-input-letter-row/)
1. [Letters order](https://blog.miguelbernard.com/letters-order/)

> All code samples are available on [github](https://github.com/mbernard/property-based-testing)

---

# Intro

We continue our adventure trying to solve the Diamond Kata while using Property-Based testing. [Last time](https://blog.miguelbernard.com/input-generators-in-property-based-tests-with-fscheck/), 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.

```text
input: A

A
```

```text
input: E

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

# C# Tests

```csharp
[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.
1. `Diamond.Generate(c)` Generates the diamond
2. `First() / Last()` Takes the first/last line of the generated diamond
3. `Contains('A')` Checks if the line contains the letter `A` and returns a bool
4. `ToProperty()` 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](https://blog.miguelbernard.com/input-generators-in-property-based-tests-with-fscheck/)

# 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](https://blog.miguelbernard.com/height-equals-width/).

---

# [Solving the diamond kata with property-based testing series](https://blog.miguelbernard.com/solving-the-diamond-kata-with-property-based-testing-series/)

1. [How to get started with Property-based Testing in C#](https://blog.miguelbernard.com/how-to-get-started-with-property-based-testing-in-csharp/)
1. [Input generators in property-based tests with FsCheck](https://blog.miguelbernard.com/input-generators-in-property-based-tests-with-fscheck)
1. [First and Last line content](https://blog.miguelbernard.com/first-and-last-line-content)
1. [Height equals Width](https://blog.miguelbernard.com/height-equals-width/)
1. [Outside space symmetry](https://blog.miguelbernard.com/outside-space-symmetry/)
1. [Symmetry around the vertical axis](https://blog.miguelbernard.com/symmetry-around-the-vertical-axis/)
1. [Symmetry around the horizontal axis](https://blog.miguelbernard.com/symmetry-around-the-horizontal-axis/)
1. [No padding for input letter row](https://blog.miguelbernard.com/no-padding-for-input-letter-row/)
1. [Letters order](https://blog.miguelbernard.com/letters-order/)

> All code samples are available on [github](https://github.com/mbernard/property-based-testing)