Exemplo n.º 1
0
        public void register_a_property_override_for_a_string_conversion_passing_raw_value()
        {
            object passedRawValue = null;

            configure(x =>
            {
                //specific override formatting for Address objects named Shipping
                x.IfPropertyMatches<Address>(prop => prop.Name == "Shipping").ConvertBy((req, value) =>
                {
                    passedRawValue = req.RawValue;
                    return "{1}-{0}".ToFormat(value.Address1, value.City);
                });

                //default formatting for Address objects
                x.IfIsType<Address>().ConvertBy(a => "{0}, {1}".ToFormat(a.Address1, a.City));

            });

            var address = new Address();
            var site = new FakeSite { Billing = address, Shipping = address };

            var shippingRequest = new GetStringRequest(ReflectionHelper.GetAccessor<FakeSite>(s => s.Shipping), address, locator);

            stringifier.GetString(shippingRequest);

            passedRawValue.ShouldBeTheSameAs(address);
        }
Exemplo n.º 2
0
        public void register_a_property_override_for_a_string_conversion()
        {
            configure(x =>
            {
                //specific override formatting for Address objects named Shipping
                x.IfPropertyMatches<Address>(prop => prop.Name == "Shipping").ConvertBy(a => "{1}-{0}".ToFormat(a.Address1, a.City));

                //default formatting for Address objects
                x.IfIsType<Address>().ConvertBy(a => "{0}, {1}".ToFormat(a.Address1, a.City));
            });

            var address = new Address
            {
                Address1 = "2050 Ozark",
                City = "Joplin"
            };

            var site = new FakeSite {Billing = address, Shipping = address};

            const string expectedDefaultFormatting = "2050 Ozark, Joplin";
            const string expectedOverrideFormatting = "Joplin-2050 Ozark";
            var billingRequest = new GetStringRequest(ReflectionHelper.GetAccessor<FakeSite>(s => s.Billing), address, locator);
            var shippingRequest = new GetStringRequest(ReflectionHelper.GetAccessor<FakeSite>(s => s.Shipping), address, locator);
            stringifier.GetString(billingRequest).ShouldEqual(expectedDefaultFormatting);
            stringifier.GetString(shippingRequest).ShouldEqual(expectedOverrideFormatting);
        }