Zebra Game Kata Part 6 - The Australian lives in the house with the red door.

Now that we have all of our enumerations set, we can write a fun test.

2. The Australian lives in the house with the red door.

ZebraGameTest/ZebraProblemTest.cs
[Test]
public void Australian_lives_in_the_house_with_the_red_door()
{
    Assert.That( zebraProblem.Houses.Any( h => h.Nationality == Nationality.Australian 
            && h.DoorColor == DoorColor.Red ),
        "The Australian needs to live in the house with the red door." );
}

Let's talk about this LINQ statement.

We are looking for any house where the nationality is Australian and the door color is red.

If we do not find a house, write out a helpful message.

We can compile, let's run the test.

1 test failed "The Australian needs to live in the house with the red door."

To get the test to pass we need to give the house with the Australian a red door.

ZebraGameProblem/ZebraProblem.cs
new House
{
    Id = 1,
    DoorColor = DoorColor.Red,
    Nationality = Nationality.Australian,
    Pet = Pet.Dog,
    Drink = Drink.Coffee,
    Food = Food.Apple,
},

Let's run the tests again.

1 tests failed "A house was found with the same color door as house 1."

House 1 used to have a blue door. When we switched it to red, we failed our unique door color test.

That's the great thing about test driven development. We can make changes, run the tests, and see if anything went wrong. Let's give house 4 the blue door that used to be on house 1.

ZebraGameProblem/ZebraProblem.cs
new House
{
    Id = 4,
    DoorColor = DoorColor.Blue,
    Nationality = Nationality.Norwegian,
    Pet = Pet.Snails,
    Drink = Drink.Tea,
    Food = Food.Mushroom
},

Let's run the tests again.

8 tests passed

Now we need to do the same thing for these parts:

3. The Italian owns the dog.

4. Coffee is drunk in the house with the green door.

5. The Ukrainian drinks tea.

7. The mushroom-eater owns snails.

8. Apples are eaten in the house with the yellow door.

13. The cake-eater drinks orange juice.

14. The Japanese eats bananas.

At the end of writing these tests, our solution looks like this.

ZebraGameProblem/ZebraProblem.cs
using System.Collections.Generic;

namespace ZebraGameProblem
{
    public class ZebraProblem
    {
        public ZebraProblem()
        {
            const int houseMax = 5;

            Houses = new List<House>( houseMax )
                         {
                             new House
                                 {
                                     Id = 1,
                                     DoorColor = DoorColor.Red,
                                     Nationality = Nationality.Australian,
                                     Pet = Pet.Fox,
                                     Drink = Drink.Milk,
                                     Food = Food.Onion
                                 },
                             new House
                                 {
                                     Id = 2,
                                     DoorColor = DoorColor.Green,
                                     Nationality = Nationality.Italian,
                                     Pet = Pet.Dog,
                                     Drink = Drink.Coffee,
                                     Food = Food.Banana
                                 },
                             new House
                                 {
                                     Id = 3,
                                     DoorColor = DoorColor.Ivory,
                                     Nationality = Nationality.Japanese,
                                     Pet = Pet.Horse,
                                     Drink = Drink.OrangeJuice,
                                     Food = Food.Cake
                                 },
                             new House
                                 {
                                     Id = 4,
                                     DoorColor = DoorColor.Blue,
                                     Nationality = Nationality.Norwegian,
                                     Pet = Pet.Snails,
                                     Drink = Drink.Water,
                                     Food = Food.Mushroom
                                 },
                             new House
                                 {
                                     Id = 5,
                                     DoorColor = DoorColor.Yellow,
                                     Nationality = Nationality.Ukrainian,
                                     Pet = Pet.Zebra,
                                     Drink = Drink.Tea,
                                     Food = Food.Apple
                                 }
                         };
        }

        public List<House> Houses { get; }
    }
}
ZebraGameTests/ZebraProblemTest.cs
using System.Linq;

using NUnit.Framework;

using ZebraGame;

namespace ZebraGameTests
{
    [TestFixture]
    public class ZebraProblemTest
    {
        [SetUp]
        public void Setup()
        {
            zebraProblem = new ZebraProblem();
        }

        private ZebraProblem zebraProblem;

        [Test]
        public void Apples_are_eaten_in_the_house_with_the_yellow_door()
        {
            Assert.That( zebraProblem.Houses.Any( h => h.Food == Food.Apple
                                                       && h.DoorColor == DoorColor.Yellow ),
                         "Apples are eaten in the house with the yellow door." );
        }

        [Test]
        public void Australian_lives_in_the_house_with_the_red_door()
        {
            Assert.That( zebraProblem.Houses.Any( h => h.Nationality == Nationality.Australian
                                                       && h.DoorColor == DoorColor.Red ),
                         "The Australian needs to live in the house with the red door." );
        }

        [Test]
        public void Coffee_is_drunk_in_the_house_with_the_green_door()
        {
            Assert.That( zebraProblem.Houses.Any( h => h.Drink == Drink.Coffee
                                                       && h.DoorColor == DoorColor.Green ),
                         "Coffee is drunk in the house with the green door." );
        }

        [Test]
        public void Italian_owns_the_dog()
        {
            Assert.That( zebraProblem.Houses.Any( h => h.Nationality == Nationality.Italian
                                                       && h.Pet == Pet.Dog ),
                         "The Italian owns the dog." );
        }

        [Test]
        public void The_cake_eater_drinks_orange_juice()
        {
            Assert.That( zebraProblem.Houses.Any( h => h.Food == Food.Cake
                                                       && h.Drink == Drink.OrangeJuice ),
                         "The cake-eater drinks orange juice." );
        }

        [Test]
        public void The_mushroom_eater_owns_snails()
        {
            Assert.That( zebraProblem.Houses.Any( h => h.Food == Food.Mushroom
                                                       && h.Pet == Pet.Snails ),
                         "The mushroom-eater owns snails." );
        }

        [Test]
        public void The_Ukrainian_drinks_tea()
        {
            Assert.That( zebraProblem.Houses.Any( h => h.Nationality == Nationality.Ukrainian
                                                       && h.Drink == Drink.Tea ),
                         "The Ukrainian drinks tea." );
        }

        [Test]
        public void There_Are_Five_Houses()
        {
            Assert.That( zebraProblem.Houses.Count,
                         Is.EqualTo( 5 ),
                         "There must be five houses." );
        }

        [Test]
        public void Unique_Drinks()
        {
            zebraProblem.Houses.ForEach( house =>
                 Assert.That( zebraProblem.Houses
                                          .Count( h => h.Drink == house.Drink ),
                              Is.EqualTo( 1 ),
                              "A house was found with the same drink as house {0}.",
                              house.Id ) );
        }

        [Test]
        public void Unique_Foods()
        {
            zebraProblem.Houses.ForEach( house =>
                 Assert.That( zebraProblem.Houses
                                          .Count( h => h.Food == house.Food ),
                              Is.EqualTo( 1 ),
                              "A house was found with the same food as house {0}.",
                              house.Id ) );
        }

        [Test]
        public void Unique_Front_Doors()
        {
            zebraProblem.Houses.ForEach( house =>
                 Assert.That( zebraProblem.Houses
                                          .Count( h => h.DoorColor == house.DoorColor ),
                              Is.EqualTo( 1 ),
                              "A house was found with the same color door as house {0}.",
                              house.Id ) );
        }

        [Test]
        public void Unique_House_Id()
        {
            zebraProblem.Houses.ForEach(
                house => Assert.That( zebraProblem.Houses.Count( h => h.Id == house.Id ),
                                      Is.EqualTo( 1 ),
                                      "A house was found with the same ID as house {0}.",
                                      house.Id ) );
        }

        [Test]
        public void Unique_Nationalities()
        {
            zebraProblem.Houses.ForEach( house =>
                 Assert.That( zebraProblem.Houses
                                          .Count( h => h.Nationality == house.Nationality ),
                              Is.EqualTo( 1 ),
                              "A house was found with the same nationality as house {0}.",
                              house.Id ) );
        }

        [Test]
        public void Unique_Pets()
        {
            zebraProblem.Houses.ForEach( house =>
                 Assert.That( zebraProblem.Houses
                                          .Count( h => h.Pet == house.Pet ),
                              Is.EqualTo( 1 ),
                              "A house was found with the same pet as house {0}.",
                              house.Id ) );
        }
    }
}

Let's run the tests again.

14 tests passed