Source

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

Full commit
using System;
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>();

        Dictionary<string, Func<dynamic, dynamic>> _additionalBehavior =
            new Dictionary<string, Func<dynamic, dynamic>>();

        [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);
        }

        [Specification]
        public void dynamic_behavior()
        {
            "Given additional dynamic behavior for a customer"
                .Context(() => make_behavior_for_a_customer());
            "When constructing a customer object"
                .Do(() => _customer = GetCustomer());
            "Then the object should expose the additional behavior"
                .Assert(behaviors_are_exposed);
            "And the behavior should be accessible"
                .Assert(behaviors_are_accessible);
        }

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

        void make_behavior_for_a_customer()
        {
            _additionalBehavior.Clear();
            _additionalBehavior.Add("FullName", c => string.Format("{0} {1}", c.FirstName, c.LastName));
        }

        Customer GetCustomer()
        {
            return new Customer(_additionalBehavior, _additionalData)
                       {
                           FirstName = "Jon",
                           LastName = "Jones"
                       };

            ////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);
        }

        void behaviors_are_exposed()
        {
            (_customer as DynamicEntity).RespondTo("FullName").ShouldBeTrue();
        }

        void behaviors_are_accessible()
        {
            ((string) _customer.FullName).ShouldEqual("Jon Jones");
        }
    }
}