public void CreateProperty_Providers_ThrowsIfAlreadyExists() { var factory = new PropertyFactory(new MethodFactory(new RelatedMethodFinder())); Func <MethodBodyCreationContext, Expression> setBodyProvider = ctx => Expression.Empty(); var indexParameters = ParameterDeclarationObjectMother.CreateMultiple(2); var property = _mutableType.AddProperty("Property", typeof(int), indexParameters, setBodyProvider: setBodyProvider); Assert.That( () => factory.CreateProperty(_mutableType, "OtherName", property.PropertyType, indexParameters, 0, null, setBodyProvider), Throws.Nothing); Assert.That( () => factory.CreateProperty(_mutableType, property.Name, typeof(string), indexParameters, 0, null, setBodyProvider), Throws.Nothing); Assert.That( () => factory.CreateProperty( _mutableType, property.Name, property.PropertyType, ParameterDeclarationObjectMother.CreateMultiple(3), 0, null, setBodyProvider), Throws.Nothing); Assert.That( () => factory.CreateProperty(_mutableType, property.Name, property.PropertyType, indexParameters, 0, null, setBodyProvider), Throws.InvalidOperationException.With.Message.EqualTo("Property with equal name and signature already exists.")); }
public void InvalidPropertyNamesAreSubstituted() { const string name = ""; var p = PropertyFactory.CreateProperty(name, new JValue((object)null), null); Assert.NotEqual(p.Name, name); }
public Entities.Property ReadPropertyFromCaseID(int ID) { using (var database = new EstateBrokerContext()) { return((Entities.Property)PropertyFactory.CreateProperty(database.Properties.FirstOrDefault(a => a.CaseID == ID))); } }
public void Test_List1AndList1AsPropertiesAtSameDepth() { var list1 = new List <object>(); var root = new Root(list1, list1); var factory = new PropertyFactory(new PropertyProvider()); var p = factory.CreateProperty("Root", root); // Always 2 properties in Root var complexProperty = p as ComplexProperty; Assert.IsNotNull(complexProperty); Assert.AreEqual(2, complexProperty.Properties.Count); Assert.AreEqual(1, complexProperty.Reference.Count); Assert.IsFalse(complexProperty.Reference.IsProcessed); // Both lists are of CollectionProperty var lp1 = complexProperty.Properties[0] as CollectionProperty; Assert.IsNotNull(lp1); var lp2 = complexProperty.Properties[1] as CollectionProperty; Assert.IsNotNull(lp2); Assert.AreEqual(lp1.Reference, lp2.Reference); Assert.AreEqual(2, lp1.Reference.Count); }
public void Test_List1ContainsList3_List2ContainsList3() { var list1 = new List <object>(); var list2 = new List <object>(); var list3 = new List <object>(); var root = new Root(list1, list2); list1.Add(list3); list2.Add(list3); var factory = new PropertyFactory(new PropertyProvider()); var p = factory.CreateProperty("Root", root); var rootProperty = (ComplexProperty)p; // Both lists are of CollectionProperty var lp1 = (CollectionProperty)rootProperty.Properties[0]; var lp2 = (CollectionProperty)rootProperty.Properties[1]; var lp1i1 = (CollectionProperty)lp1.Items[0]; var lp2i1 = (CollectionProperty)lp2.Items[0]; Assert.AreNotEqual(lp1i1, lp2i1); Assert.AreEqual(lp1i1.Reference, lp2i1.Reference); Assert.AreEqual(2, lp1i1.Reference.Count); }
public void CreatesProperty() { var s = PropertyFactory.CreateProperty("foo", "bar", Grammar.DefaultDelimiters); var p = new IniProperty(s, null); Assert.AreEqual("foo", p.Name); Assert.AreEqual("bar", p.Value); }
public void CreatePropertyTests() { var p = PropertyFactory.CreateProperty("foo", "bar", Grammar.DefaultDelimiters); Assert.AreEqual("foo", p.Tokens[0]); Assert.AreEqual("=", p.Tokens[1]); // todo should be dynamic Assert.AreEqual("bar", p.Tokens[2]); }
public void PropertiesAreConstructed() { const string name = "Test"; const string value = "Value"; var p = PropertyFactory.CreateProperty(name, new JValue(value), null); Assert.Equal(p.Name, name); var s = Assert.IsType <ScalarValue>(p.Value); Assert.Equal(value, s.Value); }
public void AddsComment() { var s = PropertyFactory.CreateProperty("foo", "bar", Grammar.DefaultDelimiters); var p = new IniProperty(s, new IniFile()); p.AddComment("baz"); p.AddComment("qux"); Assert.IsTrue(p.Comments.Count() == 2); Assert.IsTrue(p.Comments.ElementAt(0) == "baz"); Assert.IsTrue(p.Comments.ElementAt(1) == "qux"); }
/// <summary> /// Reloads all PropertyNodes in PropertyGrid, recreating all PropertyNodes</summary> protected virtual void Reload() { // Destroy and unsubscribe from old properties if (Properties != null) { foreach (PropertyNode node in Properties) { node.UnBind(); node.ValueSet -= new EventHandler(node_ValueSet); node.ValueError -= new EventHandler(node_ValueError); } } object[] instances = Instances.Cast <object>().ToArray(); // TODO: cache and reuse PropertyNodes where possible to prevent having to // rebuild all of the data templates IEnumerable <PropertyDescriptor> descriptors = null; if (CustomPropertyDescriptors != null) { descriptors = CustomPropertyDescriptors; } else if (Instances != null) { descriptors = PropertyUtils.GetSharedProperties(instances); } var propertyNodes = new ObservableCollection <PropertyNode>(); foreach (var descriptor in descriptors) { if (descriptor.IsBrowsable) { PropertyNode node = null; if (PropertyFactory != null) { node = PropertyFactory.CreateProperty(instances, descriptor, true, this); } else { node = new PropertyNode(instances, descriptor, true, this); } node.ValueSet += new EventHandler(node_ValueSet); node.ValueError += new EventHandler(node_ValueError); propertyNodes.Add(node); } } Properties = propertyNodes; }
public List <Entities.Property> GetAllProperties() { List <Entities.Property> workingList = new List <Entities.Property>(); using (var database = new EstateBrokerContext()) { foreach (var Property in database.Properties) { workingList.Add((Entities.Property)PropertyFactory.CreateProperty(Property)); } } return(workingList); }
protected internal override void ReadSelfFromXml(XmlElement xel) { base.ReadSelfFromXml(xel); XmlNodeList xnl = xel.SelectNodes("property.children/property"); foreach (XmlNode xn in xnl) { string subtype = ReadAttribute(xn as XmlElement, "subtype"); Properties propType = PropertyFactory.ConvertToPropertyType(subtype); Property prop = PropertyFactory.CreateProperty(propType); prop.ReadFromXml(xn as XmlElement); Add(prop); } }
public void Serialize(object data, Stream stream) { if (data == null) { throw new ArgumentNullException("data"); } var factory = new PropertyFactory(PropertyProvider); Property property = factory.CreateProperty(RootName, data); try { _serializer.Open(stream); _serializer.Serialize(property); } finally { _serializer.Close(); } }
public void CreateProperty_Providers() { var name = "Property"; var propertyType = ReflectionObjectMother.GetSomeType(); var indexParameters = ParameterDeclarationObjectMother.CreateMultiple(2).ToList(); var accessorAttributes = (MethodAttributes)7; var setterParameters = indexParameters.Concat(new[] { ParameterDeclarationObjectMother.Create(propertyType, "value") }).ToList(); Func <MethodBodyCreationContext, Expression> getBodyProvider = ctx => null; Func <MethodBodyCreationContext, Expression> setBodyProvider = ctx => null; var fakeGetMethod = MutableMethodInfoObjectMother.Create(returnType: propertyType, parameters: indexParameters); var fakeSetMethod = MutableMethodInfoObjectMother.Create(parameters: setterParameters); _methodFactoryMock .Setup( mock => mock.CreateMethod( _mutableType, "get_Property", accessorAttributes | MethodAttributes.SpecialName, GenericParameterDeclaration.None, It.IsAny <Func <GenericParameterContext, Type> >(), It.IsAny <Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > >(), getBodyProvider)) .Callback( ( MutableType declaringType, string nameArgument, MethodAttributes attributes, IEnumerable <GenericParameterDeclaration> genericParameters, Func <GenericParameterContext, Type> returnTypeProvider, Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > parameterProvider, Func <MethodBodyCreationContext, Expression> bodyProvider) => { var returnType = returnTypeProvider(null); Assert.That(returnType, Is.SameAs(propertyType)); var parameters = parameterProvider(null).ToList(); Assert.That(parameters.Select(p => p.Type), Is.EqualTo(indexParameters.Select(p => p.Type))); Assert.That(parameters.Select(p => p.Name), Is.EqualTo(indexParameters.Select(p => p.Name))); }) .Returns(fakeGetMethod); _methodFactoryMock .Setup( mock => mock.CreateMethod( _mutableType, "set_Property", (accessorAttributes | MethodAttributes.SpecialName), GenericParameterDeclaration.None, It.IsAny <Func <GenericParameterContext, Type> >(), It.IsAny <Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > >(), setBodyProvider)) .Callback( ( MutableType declaringType, string nameArgument, MethodAttributes attributes, IEnumerable <GenericParameterDeclaration> genericParameters, Func <GenericParameterContext, Type> returnTypeProvider, Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > parameterProvider, Func <MethodBodyCreationContext, Expression> bodyProvider) => { var returnType = returnTypeProvider(null); Assert.That(returnType, Is.SameAs(typeof(void))); var parameters = parameterProvider(null).ToList(); Assert.That(parameters.Select(p => p.Type), Is.EqualTo(setterParameters.Select(p => p.Type))); Assert.That(parameters.Select(p => p.Name), Is.EqualTo(setterParameters.Select(p => p.Name))); }) .Returns(fakeSetMethod) .Verifiable(); var result = _factory.CreateProperty( _mutableType, name, propertyType, indexParameters.AsOneTime(), accessorAttributes, getBodyProvider, setBodyProvider); _methodFactoryMock.Verify(); Assert.That(result.DeclaringType, Is.SameAs(_mutableType)); Assert.That(result.Name, Is.EqualTo(name)); Assert.That(result.Attributes, Is.EqualTo(PropertyAttributes.None)); Assert.That(result.PropertyType, Is.SameAs(propertyType)); Assert.That(result.MutableGetMethod, Is.SameAs(fakeGetMethod)); Assert.That(result.MutableSetMethod, Is.SameAs(fakeSetMethod)); }
protected virtual void RebuildPopertyNodesImpl() { DestroyPropertyNodes(); object[] instances = Instances == null ? EmptyArray <object> .Instance : Instances.Cast <object>().ToArray(); // TODO: cache and reuse PropertyNodes where possible to prevent having to // rebuild all of the data templates IEnumerable <PropertyDescriptor> descriptors = null; if (CustomPropertyDescriptors != null) { descriptors = CustomPropertyDescriptors; } else if (Instances != null) { descriptors = PropertyUtils.GetSharedPropertiesOriginal(instances); } PropertyNode headerPropertyNode = null; var propertyNodes = new ObservableCollection <PropertyNode>(); if (descriptors != null) { var context = TransactionContext.As <ITransactionContext>(); if (context == null) { context = DataContext.As <ITransactionContext>(); } foreach (var descriptor in descriptors) { if (descriptor.IsBrowsable) { PropertyNode node; if (PropertyFactory != null) { node = PropertyFactory.CreateProperty(instances, descriptor, true, context); } else { node = new PropertyNode(); node.Initialize(instances, descriptor, true); } node.ValueSet += node_ValueSet; node.ValueError += node_ValueError; if (node.Category != null) { bool expansionState; if (m_categoryExpanded.TryGetValue(node.Category, out expansionState)) { node.IsExpanded = expansionState; } } if (headerPropertyNode == null && descriptor.Attributes[typeof(HeaderPropertyAttribute)] != null) { headerPropertyNode = node; } else { propertyNodes.Add(node); } } } } // Listen for expansion state changes so that we can persist through different objects. m_listener = ChangeListener.Create(propertyNodes, "IsExpanded"); m_listener.PropertyChanged += ChildExpandedPropertyChanged; Properties = propertyNodes; HeaderProperty = headerPropertyNode; }
public void CreateProperty_Providers() { var name = "Property"; var propertyType = ReflectionObjectMother.GetSomeType(); var indexParameters = ParameterDeclarationObjectMother.CreateMultiple(2).ToList(); var accessorAttributes = (MethodAttributes)7; var setterParameters = indexParameters.Concat(new[] { ParameterDeclarationObjectMother.Create(propertyType, "value") }).ToList(); Func <MethodBodyCreationContext, Expression> getBodyProvider = ctx => null; Func <MethodBodyCreationContext, Expression> setBodyProvider = ctx => null; var fakeGetMethod = MutableMethodInfoObjectMother.Create(returnType: propertyType, parameters: indexParameters); var fakeSetMethod = MutableMethodInfoObjectMother.Create(parameters: setterParameters); _methodFactoryMock .Expect( mock => mock.CreateMethod( Arg.Is(_mutableType), Arg.Is("get_Property"), Arg.Is(accessorAttributes | MethodAttributes.SpecialName), Arg.Is(GenericParameterDeclaration.None), Arg <Func <GenericParameterContext, Type> > .Is.Anything, Arg <Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > > .Is.Anything, Arg.Is(getBodyProvider))) .WhenCalled( mi => { var returnType = mi.Arguments[4].As <Func <GenericParameterContext, Type> >() (null); Assert.That(returnType, Is.SameAs(propertyType)); var parameters = mi.Arguments[5].As <Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > >() (null).ToList(); Assert.That(parameters.Select(p => p.Type), Is.EqualTo(indexParameters.Select(p => p.Type))); Assert.That(parameters.Select(p => p.Name), Is.EqualTo(indexParameters.Select(p => p.Name))); }) .Return(fakeGetMethod); _methodFactoryMock .Expect( mock => mock.CreateMethod( Arg.Is(_mutableType), Arg.Is("set_Property"), Arg.Is(accessorAttributes | MethodAttributes.SpecialName), Arg.Is(GenericParameterDeclaration.None), Arg <Func <GenericParameterContext, Type> > .Is.Anything, Arg <Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > > .Is.Anything, Arg.Is(setBodyProvider))) .WhenCalled( mi => { var returnType = mi.Arguments[4].As <Func <GenericParameterContext, Type> >() (null); Assert.That(returnType, Is.SameAs(typeof(void))); var parameters = mi.Arguments[5].As <Func <GenericParameterContext, IEnumerable <ParameterDeclaration> > >() (null).ToList(); Assert.That(parameters.Select(p => p.Type), Is.EqualTo(setterParameters.Select(p => p.Type))); Assert.That(parameters.Select(p => p.Name), Is.EqualTo(setterParameters.Select(p => p.Name))); }) .Return(fakeSetMethod); var result = _factory.CreateProperty( _mutableType, name, propertyType, indexParameters.AsOneTime(), accessorAttributes, getBodyProvider, setBodyProvider); _methodFactoryMock.VerifyAllExpectations(); Assert.That(result.DeclaringType, Is.SameAs(_mutableType)); Assert.That(result.Name, Is.EqualTo(name)); Assert.That(result.Attributes, Is.EqualTo(PropertyAttributes.None)); Assert.That(result.PropertyType, Is.SameAs(propertyType)); Assert.That(result.MutableGetMethod, Is.SameAs(fakeGetMethod)); Assert.That(result.MutableSetMethod, Is.SameAs(fakeSetMethod)); }