public void Analyze_ConditionalExpressionBothOneLevelAndTwoLevelProperties_TreeCorrect() { Expression <Func <Person, int> > expression = p => p.Age > 3 ? p.Age : p.Brother.Age; PropertyAccessTree tree = ExpressionPropertyAnalyzer.Analyze(expression); Assert.AreEqual(1, tree.Children.Count); PropertyAccessTreeNode parameterNode = tree.Children[0]; Assert.AreEqual(2, parameterNode.Children.Count); PropertyAccessNode firstLevelAgeNode = (PropertyAccessNode)parameterNode.Children[1]; Assert.AreEqual(_ageProperty, firstLevelAgeNode.Property); Assert.AreEqual(0, firstLevelAgeNode.Children.Count); PropertyAccessNode brotherNode = (PropertyAccessNode)parameterNode.Children[0]; Assert.AreEqual(_brotherProperty, brotherNode.Property); Assert.AreEqual(1, brotherNode.Children.Count); PropertyAccessNode secondLevelAgeNode = (PropertyAccessNode)brotherNode.Children[0]; Assert.AreEqual(_ageProperty, secondLevelAgeNode.Property); Assert.AreEqual(0, secondLevelAgeNode.Children.Count); }
public void Analyze_ExpressionIncludesConstantImplementingINotifyPropertyChanged_TreeContainsTwoBranches() { Person localPersonAppearingAsConstantInExpression = new Person(); Expression <Func <Person, int> > expression = p => p.Age + localPersonAppearingAsConstantInExpression.Age; PropertyAccessTree tree = ExpressionPropertyAnalyzer.Analyze(expression); Assert.AreEqual(2, tree.Children.Count); PropertyAccessTreeNode parameterNode = tree.Children[0]; Assert.IsInstanceOfType(typeof(ParameterNode), parameterNode); Assert.AreEqual(1, parameterNode.Children.Count); PropertyAccessNode parameterAgeNode = (PropertyAccessNode)parameterNode.Children[0]; Assert.AreEqual(_ageProperty, parameterAgeNode.Property); Assert.AreEqual(0, parameterAgeNode.Children.Count); PropertyAccessTreeNode constantNode = tree.Children[1]; Assert.IsInstanceOfType(typeof(ConstantNode), constantNode); Assert.AreEqual(localPersonAppearingAsConstantInExpression, ((ConstantNode)constantNode).Value); Assert.AreEqual(1, constantNode.Children.Count); PropertyAccessNode constantAgeNode = (PropertyAccessNode)constantNode.Children[0]; Assert.AreEqual(_ageProperty, constantAgeNode.Property); Assert.AreEqual(0, constantAgeNode.Children.Count); }
public void Setup() { _target = new PropertyAccessTree(); _person = new NotifyingPerson(); _ageProperty = typeof(NotifyingPerson).GetProperty("Age"); _brotherProperty = typeof(NotifyingPerson).GetProperty("Brother"); }
public DependsOnMethod <T> OnChanged <TResult>(Expression <Func <T, TResult> > propertyAccessor) { PropertyAccessTree propertyAccessTree = ExpressionPropertyAnalyzer.Analyze(propertyAccessor); if (propertyAccessTree.DoesEntireTreeSupportINotifyPropertyChanging) { if (_propertyChangeSubscribers == null) { _propertyChangeSubscribers = new List <IPropertyAccessTreeSubscriber <DependsOnMethod <T> > >(); } var subscriber = propertyAccessTree.CreateCallbackSubscription <DependsOnMethod <T> >(OnAnyPropertyInSubscriptionChanges); _propertyChangeSubscribers.Add(subscriber); } else { if (_accessTreesWithoutNotifyPropertyChanging == null) { _accessTreesWithoutNotifyPropertyChanging = new List <PropertyAccessTree>(); } _accessTreesWithoutNotifyPropertyChanging.Add(propertyAccessTree); } return(this); }
public void Analyze_ExpressionContainsNothingWithINotifyPropertyChanged_ReturnsNull() { Expression <Func <int, bool> > expression = i => i == 0; PropertyAccessTree tree = ExpressionPropertyAnalyzer.Analyze(expression); Assert.IsNull(tree); }
public void Analyze_SpecialTypeFilterWithExpressionThatFailsFilter_ReturnsPropertyAccessTree() { Expression <Func <string, bool> > expression = str => str.Length == 0; PropertyAccessTree tree = ExpressionPropertyAnalyzer.Analyze(expression, type => type == typeof(int)); Assert.IsNull(tree); }
public void Analyze_ExpressionAccessesOnlyParameter_TreeHasOnlyParameter() { Expression <Func <Person, Person> > expression = p => p; PropertyAccessTree tree = ExpressionPropertyAnalyzer.Analyze(expression); Assert.AreEqual(1, tree.Children.Count); Assert.IsInstanceOfType(typeof(ParameterNode), tree.Children[0]); }
public void Analyze_ExpressionAccessesOnlyParameter_TreeHasOnlyParameter() { Expression <Func <Person, Person> > expression = p => p; PropertyAccessTree tree = ExpressionPropertyAnalyzer.Analyze(expression); Assert.AreEqual(1, tree.Children.Count); tree.Children[0].Should().BeOfType <ParameterNode>(); }
public void Setup() { _source = new List <int>() { 0, 1, 2, 3, 4 }; PropertyAccessTree propertyAcessTree = new PropertyAccessTree(); _target = new MockReadOnlyContinuousCollection(_source); }
public void Setup() { _source = ClinqTestFactory.CreateTwoPersonSource(); _propertyAccessTree = new PropertyAccessTree(); ParameterNode parameterNode = new ParameterNode(typeof(Person), "person"); _propertyAccessTree.Children.Add(parameterNode); var agePropertyAccessNode = new PropertyAccessNode(typeof(Person).GetProperty("Age")); parameterNode.Children.Add(agePropertyAccessNode); _target = new NotifyCollectionChangedMonitor <Person>(_propertyAccessTree, _source); }
public void Analyze_ExpressionUsesMethodCallOnOneLevelProperty_TreeHasOneParameterBranchWithOneProperty() { Expression <Func <Person, bool> > expression = p => string.IsNullOrEmpty(p.Name); PropertyAccessTree tree = ExpressionPropertyAnalyzer.Analyze(expression); Assert.AreEqual(1, tree.Children.Count); PropertyAccessTreeNode parameterNode = tree.Children[0]; Assert.AreEqual(1, parameterNode.Children.Count); PropertyAccessNode propertyNode = (PropertyAccessNode)parameterNode.Children[0]; Assert.AreEqual(_nameProperty, propertyNode.Property); Assert.AreEqual(0, propertyNode.Children.Count); }
public void Analyze_ExpressionReturnsOneLevelProperty_TreeHasOneParameterBranchWithOneProperty() { Expression <Func <Person, int> > expression = p => p.Age; PropertyAccessTree tree = ExpressionPropertyAnalyzer.Analyze(expression); Assert.AreEqual(1, tree.Children.Count); PropertyAccessTreeNode parameterNode = tree.Children[0]; Assert.AreEqual(1, parameterNode.Children.Count); PropertyAccessNode propertyNode = (PropertyAccessNode)parameterNode.Children[0]; Assert.AreEqual(_ageProperty, propertyNode.Property); Assert.AreEqual(0, propertyNode.Children.Count); }
public PropertyAccessTreeSubscriber(PropertyAccessTree tree, Action <TListener, object> subscriber_callback) { property_access_tree = tree; subscriptions = new List <ParameterNodeSubscriber <TListener> >(); foreach (var node in property_access_tree.Children) { var parameter_node = node as ParameterNode; if (parameter_node == null) { throw new ArgumentException("Root nodes must be of type ParameterNode"); } var root_subscription = new ParameterNodeSubscriber <TListener>(parameter_node, subscriber_callback); subscriptions.Add(root_subscription); } }
public void Analyze_TwoLevelPropertyWhereSubPropertyIsNotINotifyPropertyChanged_TreeContainsFirstProperty() { Expression <Func <Person, bool> > expression = p => p.Name.Length == 0; PropertyAccessTree tree = ExpressionPropertyAnalyzer.Analyze(expression); Assert.AreEqual(1, tree.Children.Count); PropertyAccessTreeNode parameterNode = tree.Children[0]; Assert.IsInstanceOfType(typeof(ParameterNode), parameterNode); Assert.AreEqual(1, parameterNode.Children.Count); PropertyAccessNode nameNode = (PropertyAccessNode)parameterNode.Children[0]; Assert.AreEqual(_nameProperty, nameNode.Property); Assert.AreEqual(0, nameNode.Children.Count); }
public void Analyze_SpecialTypeFilterWithExpressionThatPassesFilter_ReturnsPropertyAccessTree() { Expression <Func <string, bool> > expression = str => str.Length == 0; PropertyAccessTree tree = ExpressionPropertyAnalyzer.Analyze(expression, type => type == typeof(string)); Assert.AreEqual(1, tree.Children.Count); PropertyAccessTreeNode parameterNode = tree.Children[0]; Assert.IsInstanceOfType(typeof(ParameterNode), parameterNode); Assert.AreEqual(1, parameterNode.Children.Count); PropertyAccessNode lengthNode = (PropertyAccessNode)parameterNode.Children[0]; Assert.AreEqual(typeof(string).GetProperty("Length"), lengthNode.Property); Assert.AreEqual(0, lengthNode.Children.Count); }
public void Analyze_ExpressionContainsThis_TreeContainsOneBranch() { Expression <Func <Person, int> > expression = p => this.TestProperty; PropertyAccessTree tree = ExpressionPropertyAnalyzer.Analyze(expression); Assert.AreEqual(2, tree.Children.Count); PropertyAccessTreeNode constantNode = tree.Children[1]; Assert.IsInstanceOfType(typeof(ConstantNode), constantNode); Assert.AreEqual(this, ((ConstantNode)constantNode).Value); Assert.AreEqual(1, constantNode.Children.Count); PropertyAccessNode testPropertyNode = (PropertyAccessNode)constantNode.Children[0]; Assert.AreEqual(GetType().GetProperty("TestProperty"), testPropertyNode.Property); Assert.AreEqual(0, testPropertyNode.Children.Count); }
private void ShowPropertyAccessTree(PropertyAccessTree tree) { var g = new BidirectionalGraph <object, IEdge <object> >(); foreach (var node in tree.GraphDebug_GetChildren()) { var parameter_node = node as ParameterNode; if (parameter_node == null) { continue; } var parameter_node_name = string.Format("{0} - {1}", parameter_node.Type.UnderlyingSystemType.Name, parameter_node.Name); g.AddVertex(parameter_node_name); BuildPropertyAccessTreeGraph(g, parameter_node_name, parameter_node.Children); } graph_layout.Graph = g; }
public void Analyze_ExpressionReturnsTwoLevelProperty_TreeHasOneParameterBranchWithTwoProperties() { Expression <Func <Person, int> > expression = p => p.Brother.Age; PropertyAccessTree tree = ExpressionPropertyAnalyzer.Analyze(expression); Assert.AreEqual(1, tree.Children.Count); PropertyAccessTreeNode parameterNode = tree.Children[0]; Assert.AreEqual(1, parameterNode.Children.Count); PropertyAccessNode brotherNode = (PropertyAccessNode)parameterNode.Children[0]; Assert.AreEqual(_brotherProperty, brotherNode.Property); Assert.AreEqual(1, brotherNode.Children.Count); PropertyAccessNode ageNode = (PropertyAccessNode)brotherNode.Children[0]; Assert.AreEqual(_ageProperty, ageNode.Property); Assert.AreEqual(0, ageNode.Children.Count); }
public void Analyze_ConditionalExpressionReferencingTwoProperties_TreeContainsOnePropertyBranchWithBothProperties() { Expression <Func <Person, int> > expression = p => string.IsNullOrEmpty(p.Name) ? p.Age : 0; PropertyAccessTree tree = ExpressionPropertyAnalyzer.Analyze(expression); Assert.AreEqual(1, tree.Children.Count); PropertyAccessTreeNode parameterNode = tree.Children[0]; Assert.AreEqual(2, parameterNode.Children.Count); PropertyAccessNode ageNode = (PropertyAccessNode)parameterNode.Children[0]; Assert.AreEqual(_ageProperty, ageNode.Property); Assert.AreEqual(0, ageNode.Children.Count); PropertyAccessNode nameNode = (PropertyAccessNode)parameterNode.Children[1]; Assert.AreEqual(_nameProperty, nameNode.Property); Assert.AreEqual(0, nameNode.Children.Count); }
protected void Register <TResult>(Expression <Func <T, TResult> > propertyAccessor, FireOn fireOn) { PropertyAccessTree propertyAccessTree = ExpressionPropertyAnalyzer.Analyze(propertyAccessor); if (propertyAccessTree.DoesEntireTreeSupportINotifyPropertyChanging) { if (_propertyChangeSubscribers == null) { _propertyChangeSubscribers = new List <IPropertyAccessTreeSubscriber <ReactiveMethod <T> > >(); } Action <ReactiveMethod <T>, object, PropertyChangingEventArgs> onPropertyChanging = OnAnyPropertyInSubscriptionChanging; Action <ReactiveMethod <T>, object, PropertyChangedEventArgs> onPropertyChanged = OnAnyPropertyInSubscriptionChanged; if ((fireOn & FireOn.PropertyChanging) != FireOn.PropertyChanging) { onPropertyChanging = null; } if ((fireOn & FireOn.PropertyChanged) != FireOn.PropertyChanged) { onPropertyChanged = null; } var subscriber = propertyAccessTree.CreateCallbackSubscription <ReactiveMethod <T> >( onPropertyChanging, onPropertyChanged ); _propertyChangeSubscribers.Add(subscriber); } else if ((fireOn & FireOn.PropertyChanged) == FireOn.PropertyChanged) { if (_accessTreesWithoutNotifyPropertyChanging == null) { _accessTreesWithoutNotifyPropertyChanging = new List <PropertyAccessTree>(); } _accessTreesWithoutNotifyPropertyChanging.Add(propertyAccessTree); } }
public DependencyMethod <T> OnChanged <TResult>(Expression <Func <T, TResult> > property_accessor) { // Build property access tree from expression PropertyAccessTree property_access_tree = ExpressionAnalyzer.Analyze(property_accessor); if (!property_access_tree.DoesEntireTreeSupportINotifyPropertyChangedAndChanging()) { throw new ArgumentException("All objects must implement INotifyPropertyChanged and INotifyPropertyChanging"); } if (property_change_subscribers == null) { property_change_subscribers = new List <PropertyAccessTreeSubscriber <DependencyMethod <T> > >(); } // Build subscription tree from property access tree log.Trace("Creating subscription tree"); var subscriber = property_access_tree.CreateSubscriptionTree <DependencyMethod <T> >(OnAnyPropertyInSubscriptionChanges); subscriber.DumpToLog(); property_change_subscribers.Add(subscriber); return(this); }
public void AddDependencies(PropertyAccessTree tree, Action notification_callback) { property_access_tree = tree; property_access_tree.Subscribe(notification_callback); }
public void GetParameterAccessString_NoProperties_ReturnsEmptyString() { _target = new PropertyAccessTree(); Assert.AreEqual(string.Empty, _target.GetParameterPropertyAccessString()); }
protected internal OrderedReadOnlyContinuousCollection(IList <TSource> list, PropertyAccessTree propertyAccessTree) : base(list, propertyAccessTree) { }
public void Setup() { _source = ClinqTestFactory.CreateTwoPersonSource(); _propertyAccessTree = new PropertyAccessTree(); ParameterNode parameterNode = new ParameterNode(typeof(Person), "person"); _propertyAccessTree.Children.Add(parameterNode); var agePropertyAccessNode = new PropertyAccessNode(typeof(Person).GetProperty("Age")); parameterNode.Children.Add(agePropertyAccessNode); _target = new NotifyCollectionChangedMonitor<Person>(_propertyAccessTree, _source); }