Zebra Game Kata Part 5 - Nationalities

The third part of the first statement in the @Html.ActionLink("Zebra Problem", "Index") says, "inhabited by people of different nationalities".

This will be just like the @Html.ActionLink("door test", "Doors").

Let's select the house ID of the homes with the same nationality using LINQ.

ZebraGame.Test/ZebraProblemTest.cs
[Test]
public void Unique_Nationalities()
{
    zebraProblem.Houses.ForEach(house => 
    Assert.AreEqual(1, zebraProblem.Houses
        .Where(h => h.Nationality == house.Nationality)
        .Count(),
        "A house was found with the "
        + "same nationality as house {0}.",
        house.Id));
}

Let's talk about this LINQ statement.

For each house in the Zebra Problem, grab a house and assert that all the other houses in the Zebra Problem don't have the same nationality.

If you find a house with the same nationality, stop, and write out a helpful message with the ID.

We can't compile because our House object does not have a nationality. Let's add it.

ZebraGame/House.cs
namespace ZebraGame
{
    public class House
    {
        public int Id { get; set; }

        public DoorColor DoorColor { get; set; }

        public Nationality Nationality { get; set; }
    }
}

Now we add the Nationality enumeration.

ZebraGame/Nationality.cs
namespace ZebraGame
{
    public enum Nationality
    {
    }
}

We can compile, let's run the test.

1 test failed "A house was found with the same nationality as house 1."

There's a house with the same nationality as house 1. We have not assigned nationalities to the homes yet. The first house is ID 1, and all 5 homes have the same nationality (none).

Let's read ahead in the problem and identify all the possible nationalities. Then we can add them to our enumeration.

ZebraGame/Nationality.cs
namespace ZebraGame
{
    public enum Nationality
    {
        Australian,
        Italian,
        Japanese,
        Norwegian,
        Ukrainian,
    }
}

To get the test to pass we need to assign each house a unique nationality.

ZebraGame/ZebraProblem.cs
Houses = new List<House>(houseMax)
{
    new House
    {
        Id = 1,
        DoorColor = DoorColor.Blue,
        Nationality = Nationality.Australian,
    },
    new House
    {
        Id = 2,
        DoorColor = DoorColor.Green,
        Nationality = Nationality.Italian,
    },
    new House
    {
        Id = 3,
        DoorColor = DoorColor.Ivory,
        Nationality = Nationality.Japanese,
    },
    new House
    {
        Id = 4,
        DoorColor = DoorColor.Red,
        Nationality = Nationality.Norwegian,
    },
    new House
    {
        Id = 5,
        DoorColor = DoorColor.Yellow,
        Nationality = Nationality.Ukrainian,
    },
};

Let's run the tests again.

4 tests passed

Now, we need to do the same thing for:

Pets Drinks Food
Dog Coffee Apple
Fox Milk Banana
Horse OrangeJuice Mushroom
Snails Tea Onion
Zebra Water Cake