public void Deep_Clones_Each_Item_Once() { var list = new DeepCloneableList <TestClone>(ListCloneBehavior.CloneOnce); list.Add(new TestClone()); list.Add(new TestClone()); list.Add(new TestClone()); var cloned = list.DeepClone() as DeepCloneableList <TestClone>; //Test that each item in the sequence is equal - based on the equality comparer of TestClone (i.e. it's ID) Assert.IsTrue(list.SequenceEqual(cloned)); //Test that each instance in the list is not the same one foreach (var item in list) { var clone = cloned.Single(x => x.Id == item.Id); Assert.AreNotSame(item, clone); } //clone again from the clone - since it's clone once the items should be the same var cloned2 = cloned.DeepClone() as DeepCloneableList <TestClone>; //Test that each item in the sequence is equal - based on the equality comparer of TestClone (i.e. it's ID) Assert.IsTrue(cloned.SequenceEqual(cloned2)); //Test that each instance in the list is the same one foreach (var item in cloned) { var clone = cloned2.Single(x => x.Id == item.Id); Assert.AreSame(item, clone); } }
public object Clone() { var clone = new DeepCloneableList <T>(); clone.AddRange(this.Select(x => (T)x.Clone())); return(clone); }
/// <inheritdoc /> public override TEntity?GetCached(TId id) { // get all from the cache -- and only the cache, then look for the entity DeepCloneableList <TEntity>?all = Cache.GetCacheItem <DeepCloneableList <TEntity> >(GetEntityTypeCacheKey()); TEntity?entity = all?.FirstOrDefault(x => _entityGetId(x)?.Equals(id) ?? false); // see note in InsertEntities - what we get here is the original // cached entity, not a clone, so we need to manually ensure it is deep-cloned. return((TEntity?)entity?.DeepClone()); }
public void Deep_Clones_All_Elements() { var list = new DeepCloneableList <TestClone>(ListCloneBehavior.Always); list.Add(new TestClone()); list.Add(new TestClone()); list.Add(new TestClone()); var cloned = list.DeepClone() as DeepCloneableList <TestClone>; Assert.IsNotNull(cloned); Assert.AreNotSame(list, cloned); Assert.AreEqual(list.Count, cloned.Count); }
// does NOT clone anything, so be nice with the returned values internal IEnumerable <TEntity> GetAllCached(Func <TId[], IEnumerable <TEntity>?> performGetAll) { // try the cache first DeepCloneableList <TEntity>?all = Cache.GetCacheItem <DeepCloneableList <TEntity> >(GetEntityTypeCacheKey()); if (all != null) { return(all.ToArray()); } // else get from repo and cache TEntity[]? entities = performGetAll(EmptyIds)?.WhereNotNull().ToArray(); InsertEntities(entities); // may be an empty array... return(entities ?? Enumerable.Empty <TEntity>()); }
public void Clones_Each_Item() { var list = new DeepCloneableList <TestClone>(ListCloneBehavior.Always); list.Add(new TestClone()); list.Add(new TestClone()); list.Add(new TestClone()); var cloned = (DeepCloneableList <TestClone>)list.DeepClone(); foreach (var item in cloned) { Assert.IsTrue(item.IsClone); } }
public void Clones_List() { var original = new DeepCloneableList <DeepCloneableListTests.TestClone>(ListCloneBehavior.Always); original.Add(new DeepCloneableListTests.TestClone()); original.Add(new DeepCloneableListTests.TestClone()); original.Add(new DeepCloneableListTests.TestClone()); var val = _provider.GetCacheItem <DeepCloneableList <DeepCloneableListTests.TestClone> >("test", () => original); Assert.AreEqual(original.Count, val.Count); foreach (var item in val) { Assert.IsTrue(item.IsClone); } }
public void Clones_List() { var original = new DeepCloneableList <TestClone>(ListCloneBehavior.Always) { new TestClone(), new TestClone(), new TestClone() }; DeepCloneableList <TestClone> val = _provider.GetCacheItem("test", () => original); Assert.AreEqual(original.Count, val.Count); foreach (TestClone item in val) { Assert.IsTrue(item.IsClone); } }
public void Cloned_Sequence_Equals() { var list = new DeepCloneableList <TestClone>(ListCloneBehavior.Always); list.Add(new TestClone()); list.Add(new TestClone()); list.Add(new TestClone()); var cloned = (DeepCloneableList <TestClone>)list.DeepClone(); //Test that each item in the sequence is equal - based on the equality comparer of TestClone (i.e. it's ID) Assert.IsTrue(list.SequenceEqual(cloned)); //Test that each instance in the list is not the same one foreach (var item in list) { var clone = cloned.Single(x => x.Id == item.Id); Assert.AreNotSame(item, clone); } }