/// <summary> /// Removes an item from 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 remove.</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"/> did not contained <paramref name="item"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="manager"/> or <paramref name="source"/> is null.</exception> public static bool DoRemove <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)); } bool result = false; if (source is IList <T> list) { int index = list.IndexOf(item); if (index >= 0) { manager.DoRemoveAt(list, index, description); result = true; } } else if (source.Contains(item)) { manager.Do(new CollectionUnDo <T>(description, source, item, false)); result = true; } return(result); }
public void DoRemoveAt_Should_throw_ArgumentNullException_When_source_is_null() { IUnDoManager manager = Substitute.For <IUnDoManager>(); IList <object> source = null; Check .ThatCode(() => manager.DoRemoveAt(source, 0)) .Throws <ArgumentNullException>() .WithProperty("ParamName", "source"); }