public void ThrowsOnCreateExistingProperty() { var context = new AttachedPropertyContext(); var name = typeof(SomeClass).GetProperties().First().Name; Assert.Throws <ArgumentException>("name", () => new AttachedProperty <SomeClass, object>(name, context)); }
public void ThrowsOnSetNullAttachedProperty() { var context = new AttachedPropertyContext(); var tested = new object(); Assert.Throws <ArgumentNullException>(() => tested.SetAttachedValue(null, "asdf", context)); }
public void ThrowsOnGetNullAttachedProperty() { var context = new AttachedPropertyContext(); var tested = new object(); Assert.Throws <ArgumentNullException>(() => tested.GetAttachedValue((AttachedProperty <object, string>)null, context)); }
public void ThrowsOnSetNullContext() { var context = new AttachedPropertyContext(); var attachedProperty = new AttachedProperty <object, string>("CanSetAndGet", context); var tested = new object(); Assert.Throws <ArgumentNullException>(() => tested.SetAttachedValue(attachedProperty, "asdf", null)); }
public void ThrowsOnGetNullInstance() { var context = new AttachedPropertyContext(); var attachedProperty = new AttachedProperty <object, string>("CanSetAndGet", context); object tested = null; Assert.Throws <ArgumentNullException>(() => tested.GetAttachedValue(attachedProperty, context)); }
public void CanCreate() { var context = new AttachedPropertyContext(); var tested = new AttachedProperty <object, int>("SomeName", context); Assert.Equal("System.Object.SomeName", tested.FullName); Assert.Equal(typeof(object), tested.OwnerType); Assert.Equal(typeof(int), tested.PropertyType); }
public void ThrowsOnCreateDuplicate() { var context = new AttachedPropertyContext(); var first = new AttachedProperty <object, int>("asdf", context); Assert.NotNull(first); Assert.Throws <AttachedPropertyException>(() => new AttachedProperty <object, int>("asdf", context)); }
public void CanGetDefaultWithoutSet() { var context = new AttachedPropertyContext(); var attachedProperty = new AttachedProperty <object, string>("CanGetDefaultWithoutSet", context); var tested = new object(); var value = tested.GetAttachedValue(attachedProperty, context); Assert.Equal(default(string), value); }
public void CanSetAndGet() { var context = new AttachedPropertyContext(); var attachedProperty = new AttachedProperty <object, string>("CanSetAndGet", context); var tested = new object(); tested.SetAttachedValue(attachedProperty, "asdf", context); var value = tested.GetAttachedValue(attachedProperty, context); Assert.Equal("asdf", value); }
public void CanGetInstancesDefaultValue() { var tested = new AttachedPropertyContext(); var attachedProperty = new AttachedProperty <object, int>("PropertyName", tested); var instance = new object(); instance.SetAttachedValue(attachedProperty, default(int), tested); var instances = tested.GetInstances(); Assert.Equal(0, instances.Count); }
public void ThrowsOnGetNotRegistered() { var context = new AttachedPropertyContext(); var tested = new object(); var attachedProperty = new AttachedProperty <object, int>("asdf", context); Assert.Throws <AttachedPropertyException>(() => { var _ = tested.GetAttachedValue(attachedProperty); }); }
public void CanSetDefaultValueDoesNotStore() { var tested = new AttachedPropertyContext(); var attachedProperty = new AttachedProperty <object, string>("CanGetDefaultWithoutSet", tested); var instance = new object(); instance.SetAttachedValue(attachedProperty, default(string), tested); var properties = tested.GetInstanceProperties(instance); Assert.Equal(0, properties.Count); }
public void CanGetNonObjectGetValueNoSet() { var tested = new AttachedPropertyContext(); var attachedProperty = new AttachedProperty <object, int>("CanGetNonObjectGetValueNoSet", tested); var another = new AttachedProperty <object, int>("Another", tested); var instance = new object(); instance.SetAttachedValue(another, 1, tested); var value = instance.GetAttachedValue(attachedProperty, tested); Assert.Equal(0, value); }
public void CanGetProperties() { var tested = new AttachedPropertyContext(); var attachedProperty = new AttachedProperty <object, int>("PropertyName", tested); var properties = tested.GetProperties(); Assert.Equal(1, properties.Count); Assert.All(properties, x => { Assert.Same(attachedProperty, x); }); }
public void CanGetInstancesNoReference() { var tested = new AttachedPropertyContext(); var attachedProperty = new AttachedProperty <object, int>("PropertyName", tested); var instance = new object(); instance.SetAttachedValue(attachedProperty, 1, tested); instance = null; GC.Collect(0, GCCollectionMode.Forced, true); var instances = tested.GetInstances(); Assert.Null(instance); Assert.Equal(0, instances.Count); }
public void CanGetInstances() { var tested = new AttachedPropertyContext(); var attachedProperty = new AttachedProperty <object, int>("PropertyName", tested); var instance = new object(); instance.SetAttachedValue(attachedProperty, 1, tested); var instances = tested.GetInstances(); Assert.Equal(1, instances.Count); Assert.All(instances, x => { Assert.Same(instance, x); }); }
public void CanGetPropertiesForInstance() { var tested = new AttachedPropertyContext(); var attachedProperty = new AttachedProperty <object, int>("PropertyName", tested); var instance = new object(); instance.SetAttachedValue(attachedProperty, 1, tested); var values = tested.GetInstanceProperties(instance); Assert.Equal(1, values.Count); Assert.All(values, x => { Assert.Same(attachedProperty, x.Key); Assert.Equal(1, x.Value); }); }
public void ThrowsOnCreateNullName() { var context = new AttachedPropertyContext(); Assert.Throws <ArgumentNullException>("name", () => new AttachedProperty <object, object>(null, context)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TResult"></typeparam> /// <param name="instance"></param> /// <param name="attachedProperty"></param> /// <param name="context"></param> /// <returns></returns> public static TResult GetAttachedValue <T, TResult>(this T instance, AttachedProperty <T, TResult> attachedProperty, AttachedPropertyContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var value = context.GetInstanceValue(instance, attachedProperty); return(value); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TResult"></typeparam> /// <param name="instance"></param> /// <param name="attachedProperty"></param> /// <param name="value"></param> /// <param name="context"></param> public static void SetAttachedValue <T, TResult>(this T instance, AttachedProperty <T, TResult> attachedProperty, TResult value, AttachedPropertyContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } context.SetInstanceValue(instance, attachedProperty, value); }
/// <summary> /// /// </summary> protected AbstractAttachedProperty(Type ownerType, Type propertyType, string name, AttachedPropertyContext context) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } var propertyInfo = ownerType.GetRuntimeProperty(name); if (propertyInfo != null) { throw new ArgumentException(string.Format("Type {0} already has a property named {1}", ownerType.FullName, name), nameof(name)); } OwnerType = ownerType; PropertyType = propertyType; Name = name; context.Register(this); }