Skip to content

Instantly share code, notes, and snippets.

@darrencauthon
Created August 3, 2016 11:14

Revisions

  1. darrencauthon created this gist Aug 3, 2016.
    55 changes: 55 additions & 0 deletions example.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    void Main()
    {
    It_should_return_the_name_from_the_person_on_the_invoice_on_the_order();
    It_should_return_nothing_if_the_person_is_not_set();
    It_should_return_nothing_if_the_invoice_is_not_set();
    It_should_return_nothing_if_given_nothing();
    }

    public static string GetNameFrom(Order order)
    {
    return order?.Invoice?.Person?.Name;
    }

    public static void It_should_return_the_name_from_the_person_on_the_invoice_on_the_order()
    {
    var name = Guid.NewGuid().ToString();
    var order = new Order { Invoice = new Invoice { Person = new Person { Name = name } } };

    GetNameFrom(order).ShouldEqual(name);
    }

    public static void It_should_return_nothing_if_the_person_is_not_set()
    {
    var order = new Order { Invoice = new Invoice { Person = null } };

    GetNameFrom(order).ShouldBeNull();
    }

    public void It_should_return_nothing_if_the_invoice_is_not_set()
    {
    var order = new Order { Invoice = null };

    GetNameFrom(order).ShouldBeNull();
    }

    public void It_should_return_nothing_if_given_nothing()
    {
    Order order = null;
    GetNameFrom(order).ShouldBeNull();
    }

    public class Order
    {
    public Invoice Invoice { get; set; }
    }

    public class Invoice
    {
    public Person Person { get; set; }
    }

    public class Person
    {
    public string Name { get; set; }
    }