Source

RubyCSharp / Dynamic / CSharpDynamic / DynamicFeaturesSamples.Tests / Dynamic_customer_tests.cs

Full commit
using System.Collections.Generic;
using Should;
using SubSpec;

namespace DynamicFeaturesSamples.Tests
{
    public class Dynamic_customer_tests
    {
        dynamic _customer;
        Dictionary<string, object> _additionalData = new Dictionary<string, object>();

        [Specification]
        public void dynamic_properties()
        {
            "Given additional dynamic data for a customer"
                .Context(() => make_data_for_a_customer());
            "When constructing a customer object"
                .Do(() => { _customer = GetCustomer(); });
            "Then the object should expose the additional data as properties"
                .Assert(properties_are_exposed);
            " And getters are available"
                .Assert(getters_are_exposed);
            " And setters are available"
                .Assert(setters_are_exposed);
        }

        void make_data_for_a_customer()
        {
            _additionalData.Clear();
            _additionalData.Add("Comments", "Some comments...");    
            _additionalData.Add("NumberOfDependents", 3);
        }

        Customer GetCustomer()
        {
            //return new Customer(_additionalData);

            // alternatively...
            return new Customer(new Dictionary<string, object>
                                    {
                                        { "Comments", "Some comments..." }, 
                                        { "NumberOfDependents", 3 }
                                    });
        }

        void properties_are_exposed()
        {
            (_customer as DynamicEntity).RespondTo("Comments").ShouldBeTrue();
            (_customer as DynamicEntity).RespondTo("NumberOfDependents").ShouldBeTrue();
        }

        void getters_are_exposed()
        {
            ((string) _customer.Comments).ShouldEqual("Some comments...");
            ((int) _customer.NumberOfDependents).ShouldEqual(3);
        }

        void setters_are_exposed()
        {
            _customer.Comments = "New comments";
            _customer.NumberOfDependents = 9;

            ((string)_customer.Comments).ShouldEqual("New comments");
            ((int)_customer.NumberOfDependents).ShouldEqual(9);
        }
    }
}