Skip to content

Instantly share code, notes, and snippets.

@RolandPheasant
Last active January 18, 2016 21:24
Show Gist options
  • Save RolandPheasant/a93708fae4a8c38f23f2 to your computer and use it in GitHub Desktop.
Save RolandPheasant/a93708fae4a8c38f23f2 to your computer and use it in GitHub Desktop.
Fixed test for create filter property
[Test]
public async Task TestFilterOnProperty()
{
var listA = new SourceList<X>();
var listB = new SourceList<X>();
using (IObservableList<X> list = listA.Connect()
.Or(listB.Connect())
.AsObservableList())
{
var nameA1 = "A1";
var a1 = new X(nameA1);
var a2 = new X("A2");
listA.Edit(l =>
{
l.Clear();
l.Add(a1);
l.Add(a2);
});
var b1 = new X("B1");
var b2 = new X("B2");
listB.Edit(l =>
{
l.Clear();
l.Add(b1);
l.Add(b2);
});
Assert.AreEqual(4, list.Count);
int count = await list.CountChanged.FirstAsync();
Assert.AreEqual(4, count);
IObservable<IChangeSet<X>> obsFiltered = list.Connect()
.FilterOnProperty(v => v.IsConnected, v => v.IsConnected);
using (IObservableList<X> obsFilteredAsList = obsFiltered.AsObservableList())
{
IObservable<IChangeSet<XVm>> obsTransformed = obsFiltered
.Transform(v => new XVm(v))
.DisposeMany();
var ctorCount = 0;
var dtorCount = 0;
using (IObservableList<XVm> obsTransformedAsList = obsTransformed.AsObservableList())
{
ctorCount += 4;
Assert.That(obsFilteredAsList.Items.Contains(a1));
Assert.That(obsFilteredAsList.Items.Count(), Is.EqualTo(obsTransformedAsList.Items.Count()));
a1.IsConnected = false;
Assert.That(obsFilteredAsList.Items, Has.No.Member(a1));
dtorCount++;
Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));
a1.IsConnected = true;
Assert.That(obsFilteredAsList.Items, Has.Member(a1));
ctorCount++;
Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));
Console.WriteLine("--remove");
listA.Remove(a1);
dtorCount++;
Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));
Console.WriteLine("--add");
listA.Add(a1);
ctorCount++;
Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));
Console.WriteLine("--clear");
listA.Clear();
dtorCount += 2; //FIX: List A contains 2 items (was adding 4)
Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));
Console.WriteLine("--add");
//FIX: Maybe a debate required! List B already contains b1, so not regarded as a new item in the Or result
Debug.Assert(listB.Items.Contains(b1));
listA.Add(b1);
// ctorCount++;
Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));
Console.WriteLine("--disp");
}
dtorCount +=2; //FIX: Should be +2 as this is the count of the Or derived list
Assert.That(XVm.Constructed, Is.EqualTo(ctorCount));
Assert.That(XVm.Destructed, Is.EqualTo(dtorCount));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment