Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
ICanNotifyMethodChanged and ICanNotifyPropertyChanged interfaces
  • Loading branch information
IgorBuchelnikov committed Sep 2, 2021
1 parent d637549 commit 3cfddda
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion src/ObservableComputations.Test/ExpressionWatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ public Item GetChild(string num)
public void SetChild(string num, Item item)
{
_children[num] = item;
MethodChanged?.Invoke(this, new MethodChangedEventArgs(nameof(GetChild), args => args[0].Equals(num)));
Func<object[], bool> predicate = args => args[0].Equals(num);
string methodName = nameof(GetChild);
RaiseMethodChanged(methodName, predicate);
}

public void RaiseMethodChanged(string methodName, Func<object[], bool> predicate)
{
MethodChanged?.Invoke(this, new MethodChangedEventArgs(methodName, predicate));
}

public Item Child
Expand All @@ -66,6 +73,7 @@ public Item Child
}
}


#region INotifyPropertyChanged imlementation
public event PropertyChangedEventHandler PropertyChanged;

Expand All @@ -88,6 +96,36 @@ protected bool updatePropertyValue<T>(ref T field, T value, [CallerMemberName] s
public event EventHandler<MethodChangedEventArgs> MethodChanged;
}

public class ItemWithConstantPropertyAndMethod : Item, ICanNotifyPropertyChanged, ICanNotifyMethodChanged
{
public int ConstantProperty => 1;
public int ConstantMethod() => 1;

public void RaiseConstantsChanged()
{
onPropertyChanged(nameof(ConstantProperty));
RaiseMethodChanged(nameof(ConstantMethod), objects => true);
}

#region Implementation of ICanNotifyPropertyChanged

public bool CanNotifyPropertyChanged(string propertyName, IComputing computing)
{
return propertyName != nameof(ConstantProperty);
}

#endregion

#region Implementation of ICanNotifyMethodChanged

public bool CanNotifyMethodChanged(string methodName, int argumentsCount, IComputing computing)
{
return methodName != nameof(ConstantMethod);
}

#endregion
}

[Test]
public void TestRaiseValueChanged1()
{
Expand Down Expand Up @@ -381,6 +419,23 @@ public void TestRaiseValueChanged13()
expressionWatcher.Dispose();
}

[Test]
public void TestItemWithConstantPropertyAndMethod()
{
int raised = 0;
ItemWithConstantPropertyAndMethod item = new ItemWithConstantPropertyAndMethod();
Expression<Func<string>> expression = () => item.Num + item.ConstantMethod() + item.ConstantProperty;
ExpressionWatcher expressionWatcher = new ExpressionWatcher(null,
ExpressionWatcher.GetExpressionInfo(expression));
expressionWatcher.ValueChanged = (ew, sender, eventArgs) => { raised++; };
item.Num = item.Num + 1;
Assert.AreEqual(raised, 1);
raised = 0;
item.RaiseConstantsChanged();
Assert.AreEqual(raised, 0);
expressionWatcher.Dispose();
}

//[Test]
//public void TestWeakEventHandler()
//{
Expand Down

0 comments on commit 3cfddda

Please sign in to comment.