Skip to content

Commit

Permalink
Added list synchronizer
Browse files Browse the repository at this point in the history
  • Loading branch information
devedse committed Nov 26, 2019
1 parent bb5a63d commit 9f8d3de
Show file tree
Hide file tree
Showing 2 changed files with 449 additions and 0 deletions.
346 changes: 346 additions & 0 deletions DeveCoolLib.Tests/Collections/ListSynchronizerFacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,346 @@
using DeveCoolLib.Collections;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using Xunit;
using Xunit.Abstractions;

namespace DeveCoolLib.Tests.Collections
{
public class ListSynchronizerFacts
{
private readonly ITestOutputHelper _output;
private readonly string _currentTestName;

public ListSynchronizerFacts(ITestOutputHelper output)
{
_output = output;
var type = output.GetType();
var testMember = type.GetField("test", BindingFlags.Instance | BindingFlags.NonPublic);
var test = (ITest)testMember.GetValue(output);
_currentTestName = test.DisplayName;
}

[Fact]
public void AddsItems()
{
//Arrange
var source = new List<string>()
{
"Item 1",
"Item 2",
"Item 3"
};

var dest = new ObservableCollection<string>()
{
"Item 1",
"Item 2"
};

dest.CollectionChanged += (sender, e) => _output.WriteLine($"{_currentTestName}: {e.Action}");

//Act
ListSynchronizer.SynchronizeLists(source, dest);

//Assert
Assert.Equal(source.Count, dest.Count);
for (int i = 0; i < source.Count; i++)
{
Assert.Equal(source[i], dest[i]);
}
}

[Fact]
public void RemovesItems()
{
//Arrange
var source = new List<string>()
{
"Item 1",
"Item 2"
};

var dest = new ObservableCollection<string>()
{
"Item 1",
"Item 2",
"Item 3"
};

dest.CollectionChanged += (sender, e) => _output.WriteLine($"{_currentTestName}: {e.Action}");

//Act
ListSynchronizer.SynchronizeLists(source, dest);

//Assert
Assert.Equal(source.Count, dest.Count);
for (int i = 0; i < source.Count; i++)
{
Assert.Equal(source[i], dest[i]);
}
}

[Fact]
public void CorrectlySortsItems()
{
//Arrange
var source = new List<string>()
{
"Item 1",
"Item 2",
"Item 3"
};

var dest = new ObservableCollection<string>()
{
"Item 3",
"Item 2",
"Item 1"
};

dest.CollectionChanged += (sender, e) => _output.WriteLine($"{_currentTestName}: {e.Action}");

//Act
ListSynchronizer.SynchronizeLists(source, dest);

//Assert
Assert.Equal(source.Count, dest.Count);
for (int i = 0; i < source.Count; i++)
{
Assert.Equal(source[i], dest[i]);
}
}

[Fact]
public void AddsAndSortsItems()
{
//Arrange
var source = new List<string>()
{
"Item 1",
"Item 2",
"Item 3"
};

var dest = new ObservableCollection<string>()
{
"Item 2",
"Item 3"
};

dest.CollectionChanged += (sender, e) => _output.WriteLine($"{_currentTestName}: {e.Action}");

//Act
ListSynchronizer.SynchronizeLists(source, dest);

//Assert
Assert.Equal(source.Count, dest.Count);
for (int i = 0; i < source.Count; i++)
{
Assert.Equal(source[i], dest[i]);
}
}

[Fact]
public void RemovesAndSortsItems()
{
//Arrange
var source = new List<string>()
{
"Item 1",
"Item 2"
};

var dest = new ObservableCollection<string>()
{
"Item 3",
"Item 1",
"Item 2"
};

dest.CollectionChanged += (sender, e) => _output.WriteLine($"{_currentTestName}: {e.Action}");

//Act
ListSynchronizer.SynchronizeLists(source, dest);

//Assert
Assert.Equal(source.Count, dest.Count);
for (int i = 0; i < source.Count; i++)
{
Assert.Equal(source[i], dest[i]);
}
}

[Fact]
public void AddsAndRemovesItems()
{
//Arrange
var source = new List<string>()
{
"Item 2",
"Item 3",
"Item 4"
};

var dest = new ObservableCollection<string>()
{
"Item 1",
"Item 2",
"Item 3"
};

dest.CollectionChanged += (sender, e) => _output.WriteLine($"{_currentTestName}: {e.Action}");

//Act
ListSynchronizer.SynchronizeLists(source, dest);

//Assert
Assert.Equal(source.Count, dest.Count);
for (int i = 0; i < source.Count; i++)
{
Assert.Equal(source[i], dest[i]);
}
}

[Fact]
public void RemovesDuplicateItemsAlso()
{
//Arrange
var source = new List<string>()
{
"Item 1",
"Item 2",
"Item 3"
};

var dest = new ObservableCollection<string>()
{
"Item 1",
"Item 1",
"Item 1",
"Item 2",
"Item 3"
};

dest.CollectionChanged += (sender, e) => _output.WriteLine($"{_currentTestName}: {e.Action}");

//Act
ListSynchronizer.SynchronizeLists(source, dest);

//Assert
Assert.Equal(source.Count, dest.Count);
for (int i = 0; i < source.Count; i++)
{
Assert.Equal(source[i], dest[i]);
}
}

[Fact]
public void RemovesDuplicateItemsThatAreNotInSource()
{
//Arrange
var source = new List<string>()
{
"Item 2",
"Item 3"
};

var dest = new ObservableCollection<string>()
{
"Item 1",
"Item 1",
"Item 1",
"Item 2",
"Item 3"
};

dest.CollectionChanged += (sender, e) => _output.WriteLine($"{_currentTestName}: {e.Action}");

//Act
ListSynchronizer.SynchronizeLists(source, dest);

//Assert
Assert.Equal(source.Count, dest.Count);
for (int i = 0; i < source.Count; i++)
{
Assert.Equal(source[i], dest[i]);
}
}

[Fact]
public void AddsDuplicateItems()
{
//Arrange
var source = new List<string>()
{
"Item 1",
"Item 1",
"Item 1",
"Item 2",
"Item 3"
};

var dest = new ObservableCollection<string>()
{
"Item 1",
"Item 2",
"Item 3"
};

dest.CollectionChanged += (sender, e) => _output.WriteLine($"{_currentTestName}: {e.Action}");

//Act
ListSynchronizer.SynchronizeLists(source, dest);

//Assert
Assert.Equal(source.Count, dest.Count);
for (int i = 0; i < source.Count; i++)
{
Assert.Equal(source[i], dest[i]);
}
}

[Fact]
public void DoesAllTheComplexStuffAtOnce()
{
//Arrange
var source = new List<string>()
{
"Item 6",
"Item 1",
"Item 3",
"Item 1",
"Item 2",
"Item 1"
};

var dest = new ObservableCollection<string>()
{
"Item 1",
"Item 2",
"Item 3",
"Item 3",
"Item 3",
"Item 4",
"Item 4",
"Item 5"
};

dest.CollectionChanged += (sender, e) => _output.WriteLine($"{_currentTestName}: {e.Action}");

//Act
ListSynchronizer.SynchronizeLists(source, dest);

//Assert
Assert.Equal(source.Count, dest.Count);
for (int i = 0; i < source.Count; i++)
{
Assert.Equal(source[i], dest[i]);
}
}
}
}
Loading

0 comments on commit 9f8d3de

Please sign in to comment.