예제 #1
0
        public void Setting_property_wraps_the_instance_on_access()
        {
            var data = CreateData();

            data.Settable = new DataChild()
            {
                Id = 123, Name = "foo"
            };
            Assert.IsTrue(Freezer.IsFreezable(data.Settable));
        }
        public void Can_detect_wrapped_instances()
        {
            var data = new FreezableData()
            {
                Id = 42, Name = "Everything"
            };

            Assert.IsFalse(Freezer.IsFreezable(data));
            var data2 = Freezer.AsFreezable(data);

            Assert.IsTrue(Freezer.IsFreezable(data2));
        }
예제 #3
0
        public void FreezeDry_on_non_freezable_returns_freezable()
        {
            var data = new Data()
            {
                Id = 42, Name = "Everything"
            };
            var data2 = Freezer.FreezeDry(data);

            Assert.AreNotSame(data, data2);
            Assert.IsTrue(Freezer.IsFreezable(data2));
            AssertSameValue(data, data2);
            Assert.IsTrue(Freezer.IsFrozen(data2));
        }
        public void Intercept(IInvocation invocation)
        {
            var methodName = invocation.MethodInvocationTarget.Name;

            switch (methodName)
            {
            case "Freeze":
                _frozen.State = true;
                return;

            case "get_IsFrozen":
                invocation.ReturnValue = _frozen.State;
                return;

            case "FreezeDry":
                if (_frozen.State)
                {
                    invocation.ReturnValue = _this;
                    return;
                }
                invocation.ReturnValue = Clone(Frozen.True);
                return;

            case "Thaw":
                invocation.ReturnValue = Clone(Frozen.False);
                return;

            case "Clone":
                var instance = (T)invocation.MethodInvocationTarget.Invoke(_instance, invocation.Arguments);
                invocation.ReturnValue = instance.Wrap(_generator, Frozen.False);
                return;
            }
            var propertyInfo = invocation.MethodInvocationTarget.GetPropertyMethodInfo();

            if (propertyInfo.IsSetter)
            {
                if (_frozen.State)
                {
                    throw new FrozenAccessException(string.Format("Cannot set '{0}' on frozen instance of '{1}'", methodName.Substring(4), _instance.GetType()));
                }
                var setValue = invocation.Arguments[0];
                if (setValue != null && !Freezer.IsFreezable(setValue))
                {
                    if (propertyInfo.Type.IsGenericList())
                    {
                        _properties[methodName] = CreateProxiedList(methodName, propertyInfo.Type, setValue);
                    }
                    else if (propertyInfo.Type.IsGenericCollection())
                    {
                        _properties[methodName] = CreateProxiedCollection(methodName, propertyInfo.Type, setValue);
                    }
                    else if (propertyInfo.Type.IsGenericEnumerable())
                    {
                        _properties[methodName] = CreateProxiedEnumerable(propertyInfo.Type, setValue);
                    }
                    else if (propertyInfo.Type.IsProxiableType())
                    {
                        _properties[methodName] = setValue.Wrap(_generator, propertyInfo.Type, _frozen);
                    }
                }
            }
            else if (propertyInfo.IsGetter && (propertyInfo.Type.IsGenericEnumerable() || propertyInfo.Type.IsProxiableType()))
            {
                object proxiedValue;
                if (_properties.TryGetValue(methodName, out proxiedValue))
                {
                    invocation.ReturnValue = proxiedValue;
                    return;
                }
            }
            var returnValue = invocation.MethodInvocationTarget.Invoke(_instance, invocation.Arguments);

            if (returnValue != null && propertyInfo.IsGetter)
            {
                if (propertyInfo.Type.IsGenericList())
                {
                    returnValue = CreateProxiedList(methodName, propertyInfo.Type, returnValue);
                }
                else if (propertyInfo.Type.IsGenericCollection())
                {
                    returnValue = CreateProxiedCollection(methodName, propertyInfo.Type, returnValue);
                }
                else if (propertyInfo.Type.IsGenericEnumerable())
                {
                    returnValue = CreateProxiedEnumerable(propertyInfo.Type, returnValue);
                }
                else if (propertyInfo.Type.IsProxiableType())
                {
                    returnValue = returnValue.Wrap(_generator, invocation.MethodInvocationTarget.ReturnType, _frozen);
                }
                _properties[methodName] = returnValue;
            }
            invocation.ReturnValue = returnValue;
        }
예제 #5
0
        public void Child_objects_are_wrapped()
        {
            var data = CreateData();

            Assert.IsTrue(Freezer.IsFreezable(data.Readonly));
        }