public void UnDoList_RemoveAt_Should_RemoveAt() { IList <object> source = Substitute.For <IList <object> >(); IUnDoManager manager = Substitute.For <IUnDoManager>(); const int index = 42; bool done = false; source.When(s => s.RemoveAt(index)).Do(_ => done = true); manager.Do(Arg.Do <IUnDo>(i => i.Do())); IList <object> unDoList = source.AsUnDo(manager); unDoList.RemoveAt(index); Check.That(done).IsTrue(); }
public void Value_Should_generate_description() { IUnDoManager manager = Substitute.For <IUnDoManager>(); manager.Do(Arg.Do <IUnDo>(i => i.Do())); UnDoFieldChange <object>?description = null; object value = new(); UnDoField <object> field = new(manager, d => description = d) { Value = value }; Check.That(description.HasValue).IsTrue(); Check.That(description.Value.OldValue).IsNull(); Check.That(description.Value.NewValue).IsEqualTo(value); }
public void UnDoCollection_Clear_Should_generate_Clear_description() { ICollection <object> source = Substitute.For <ICollection <object> >(); IUnDoManager manager = Substitute.For <IUnDoManager>(); UnDoCollectionOperation?description = null; manager.Do(Arg.Do <IUnDo>(i => i.Do())); ICollection <object> unDoCollection = source.AsUnDo(manager, d => description = d); unDoCollection.Clear(); Check.That(description.HasValue).IsTrue(); Check.That(description.Value.Collection).IsEqualTo(unDoCollection); Check.That(description.Value.Action).IsEqualTo(UnDoCollectionAction.ICollectionClear); Check.That(description.Value.Parameters.Length).IsZero(); }
public void UnDoList_this_index_set_Should_set_this_index() { IList <object> source = Substitute.For <IList <object> >(); IUnDoManager manager = Substitute.For <IUnDoManager>(); const int index = 42; object value = new(); bool done = false; source.When(s => s[index] = value).Do(_ => done = true); manager.Do(Arg.Do <IUnDo>(i => i.Do())); IList <object> unDoList = source.AsUnDo(manager); unDoList[index] = value; Check.That(done).IsTrue(); }
public void UnDoDictionary_Add_key_Should_Add_key() { IDictionary <object, object> source = Substitute.For <IDictionary <object, object> >(); IUnDoManager manager = Substitute.For <IUnDoManager>(); object key = new(); object value = new(); bool done = false; source.When(s => s.Add(key, value)).Do(_ => done = true); manager.Do(Arg.Do <IUnDo>(i => i.Do())); IDictionary <object, object> unDoDictionary = source.AsUnDo(manager); unDoDictionary.Add(key, value); Check.That(done).IsTrue(); }
public void UnDoDictionary_this_key_set_Should_set_this_key_When_TryGetValue_is_false() { IDictionary <object, object> source = Substitute.For <IDictionary <object, object> >(); IUnDoManager manager = Substitute.For <IUnDoManager>(); object key = new(); object value = new(); bool done = false; source.TryGetValue(key, out value).ReturnsForAnyArgs(false); source.When(s => s[key] = value).Do(_ => done = true); manager.Do(Arg.Do <IUnDo>(i => i.Do())); IDictionary <object, object> unDoDictionary = source.AsUnDo(manager); unDoDictionary[key] = value; Check.That(done).IsTrue(); }
public void UnDoSet_Add_Should_return_true_When_added() { ISet <int> source = Substitute.For <ISet <int> >(); IUnDoManager manager = Substitute.For <IUnDoManager>(); ISet <int> unDoSet = source.AsUnDo(manager); bool done = false; manager.Do(Arg.Do <IUnDo>(i => i.Do())); source.When(s => ((ICollection <int>)s).Add(42)).Do(_ => done = true); source.Contains(42).Returns(false); Check.That(unDoSet.Add(42)).IsTrue(); Check.That(done).IsTrue(); done = false; source.Contains(42).Returns(true); Check.That(unDoSet.Add(42)).IsFalse(); Check.That(done).IsFalse(); }
/// <summary> /// Adds an item from a <see cref="ISet{T}"/> as a <see cref="IUnDo"/> operation. /// </summary> /// <typeparam name="T">The type of element in the <see cref="ISet{T}"/>.</typeparam> /// <param name="manager">The <see cref="IUnDoManager"/>.</param> /// <param name="source">The <see cref="ISet{T}"/>.</param> /// <param name="item">The item to add.</param> /// <param name="description">The description of the operation.</param> /// <returns>true if the command has been created, false if not because <paramref name="source"/> already contained <paramref name="item"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="manager"/> or <paramref name="source"/> is null.</exception> public static bool DoAdd <T>(this IUnDoManager manager, ISet <T> source, T item, object description = null) { if (manager is null) { throw new ArgumentNullException(nameof(manager)); } if (source is null) { throw new ArgumentNullException(nameof(source)); } bool result = !source.Contains(item); if (result) { manager.Do(new CollectionUnDo <T>(description, source, item, true)); } return(result); }
/// <summary> /// Adds a value to a <see cref="ICollection{T}"/> as a <see cref="IUnDo"/> operation. /// </summary> /// <typeparam name="T">The type of element in the <see cref="ICollection{T}"/>.</typeparam> /// <param name="manager">The <see cref="IUnDoManager"/>.</param> /// <param name="source">The <see cref="ICollection{T}"/>.</param> /// <param name="item">The item to add.</param> /// <param name="description">The description of the operation.</param> /// <exception cref="ArgumentNullException"><paramref name="manager"/> or <paramref name="source"/> is null.</exception> public static void DoAdd <T>(this IUnDoManager manager, ICollection <T> source, T item, object description = null) { if (manager is null) { throw new ArgumentNullException(nameof(manager)); } if (source is null) { throw new ArgumentNullException(nameof(source)); } if (source is IList <T> list) { manager.DoInsert(list, list.Count, item, description); } else { manager.Do(new CollectionUnDo <T>(description, source, item, true)); } }
public void UnDoList_RemoveAt_Should_generate_RemoveAt_description() { IList <object> source = Substitute.For <IList <object> >(); IUnDoManager manager = Substitute.For <IUnDoManager>(); UnDoCollectionOperation?description = null; manager.Do(Arg.Do <IUnDo>(i => i.Do())); IList <object> unDoCollection = source.AsUnDo(manager, d => description = d); const int index = 42; unDoCollection.RemoveAt(index); Check.That(description.HasValue).IsTrue(); Check.That(description.Value.Collection).IsEqualTo(unDoCollection); Check.That(description.Value.Action).IsEqualTo(UnDoCollectionAction.IListRemoveAt); Check.That(description.Value.Parameters.Length).IsEqualTo(1); Check.That(description.Value.Parameters[0]).IsEqualTo(index); }
public void UnDoSet_Add_Should_generate_Add_description() { ISet <object> source = Substitute.For <ISet <object> >(); IUnDoManager manager = Substitute.For <IUnDoManager>(); UnDoCollectionOperation?description = null; manager.Do(Arg.Do <IUnDo>(i => i.Do())); ISet <object> unDoCollection = source.AsUnDo(manager, d => description = d); object item = new(); unDoCollection.Add(item); Check.That(description.HasValue).IsTrue(); Check.That(description.Value.Collection).IsEqualTo(unDoCollection); Check.That(description.Value.Action).IsEqualTo(UnDoCollectionAction.ISetAdd); Check.That(description.Value.Parameters.Length).IsEqualTo(1); Check.That(description.Value.Parameters[0]).IsEqualTo(item); }
public void UnDoSet_UnionWith_Should_generate_UnionWith_description() { ISet <object> source = Substitute.For <ISet <object> >(); IUnDoManager manager = Substitute.For <IUnDoManager>(); UnDoCollectionOperation?description = null; manager.Do(Arg.Do <IUnDo>(i => i.Do())); ISet <object> unDoCollection = source.AsUnDo(manager, d => description = d); IEnumerable <object> other = new object[0]; unDoCollection.UnionWith(other); Check.That(description.HasValue).IsTrue(); Check.That(description.Value.Collection).IsEqualTo(unDoCollection); Check.That(description.Value.Action).IsEqualTo(UnDoCollectionAction.ISetUnionWith); Check.That(description.Value.Parameters.Length).IsEqualTo(1); Check.That(description.Value.Parameters[0]).IsEqualTo(other); }
public void UnDoDictionary_Remove_Should_generate_Remove_description() { IDictionary <object, object> source = Substitute.For <IDictionary <object, object> >(); IUnDoManager manager = Substitute.For <IUnDoManager>(); UnDoCollectionOperation?description = null; manager.Do(Arg.Do <IUnDo>(i => i.Do())); IDictionary <object, object> unDoCollection = source.AsUnDo(manager, d => description = d); object key = new(); unDoCollection.Remove(key); Check.That(description.HasValue).IsTrue(); Check.That(description.Value.Collection).IsEqualTo(unDoCollection); Check.That(description.Value.Action).IsEqualTo(UnDoCollectionAction.IDictionaryRemove); Check.That(description.Value.Parameters.Length).IsEqualTo(1); Check.That(description.Value.Parameters[0]).IsEqualTo(key); }
/// <summary> /// Clears a <see cref="ICollection{T}"/> as a <see cref="IUnDo"/> operation. /// </summary> /// <typeparam name="T">The type of element in the <see cref="ICollection{T}"/>.</typeparam> /// <param name="manager">The <see cref="IUnDoManager"/>.</param> /// <param name="source">The <see cref="ICollection{T}"/>.</param> /// <param name="description">The description of the operation.</param> /// <exception cref="ArgumentNullException"><paramref name="manager"/> or <paramref name="source"/> is null.</exception> public static void DoClear <T>(this IUnDoManager manager, ICollection <T> source, object description = null) { if (manager is null) { throw new ArgumentNullException(nameof(manager)); } if (source is null) { throw new ArgumentNullException(nameof(source)); } if (source.Count > 0) { T[] oldValues = source.ToArray(); manager.Do(source.Clear, () => { foreach (T oldValue in oldValues) { source.Add(oldValue); } }, description); } }
public void UnDoList_this_index_set_Should_generate_this_description() { IList <object> source = Substitute.For <IList <object> >(); IUnDoManager manager = Substitute.For <IUnDoManager>(); UnDoCollectionOperation?description = null; manager.Do(Arg.Do <IUnDo>(i => i.Do())); IList <object> unDoCollection = source.AsUnDo(manager, d => description = d); const int index = 42; object item = new(); unDoCollection[index] = item; Check.That(description.HasValue).IsTrue(); Check.That(description.Value.Collection).IsEqualTo(unDoCollection); Check.That(description.Value.Action).IsEqualTo(UnDoCollectionAction.IListIndexer); Check.That(description.Value.Parameters.Length).IsEqualTo(2); Check.That(description.Value.Parameters[0]).IsEqualTo(index); Check.That(description.Value.Parameters[1]).IsEqualTo(item); }
public void CanUndo_Should_return_true_When_a_command_has_been_done(IUnDoManager manager) { manager.Do(Substitute.For <IUnDo>()); Check.That(manager.CanUndo).IsTrue(); }
/// <summary> /// Does a <see cref="IUnDo"/> operation on the manager with the specified action with no do. /// </summary> /// <param name="manager">The <see cref="IUnDoManager"/>.</param> /// <param name="action">The <see cref="Action"/> performed by <see cref="IUnDo.Undo"/>.</param> /// <param name="description">The description of the operation.</param> /// <exception cref="ArgumentNullException"><paramref name="manager"/> is null.</exception> public static void DoOnUndo(this IUnDoManager manager, Action action, object description = null) => manager.Do(null, action, description);