Example #1
0
        public void Move_Should_set_description_as_Move()
        {
            IUnDoManager manager = Substitute.For <IUnDoManager>();

            manager.Do(Arg.Do <IUnDo>(i => i.Do()));
            UnDoCollectionOperation?description = null;

            IList <int> unDoCollection = new ObservableCollection <int> {
                1, 2
            }.AsUnDo(manager, a => description ??= a);

            unDoCollection.Move(0, 1);

            Check.That(description.HasValue).IsTrue();
            Check.That(description.Value.Collection).IsEqualTo(unDoCollection);
            Check.That(description.Value.Action).IsEqualTo(UnDoCollectionAction.IListMove);
            Check.That(description.Value.Parameters.Length).IsEqualTo(2);
            Check.That(description.Value.Parameters[0]).IsEqualTo(0);
            Check.That(description.Value.Parameters[1]).IsEqualTo(1);

            description    = null;
            unDoCollection = Substitute.For <IList <int> >().AsUnDo(manager, a => description ??= a);

            unDoCollection.Move(42, 43);

            Check.That(description.HasValue).IsTrue();
            Check.That(description.Value.Collection).IsEqualTo(unDoCollection);
            Check.That(description.Value.Action).IsEqualTo(UnDoCollectionAction.IListMove);
            Check.That(description.Value.Parameters.Length).IsEqualTo(2);
            Check.That(description.Value.Parameters[0]).IsEqualTo(42);
            Check.That(description.Value.Parameters[1]).IsEqualTo(43);
        }
Example #2
0
        public void CanUndo_Should_return_false_When_all_commands_have_been_undone(IUnDoManager manager)
        {
            manager.Do(Substitute.For <IUnDo>());
            manager.Undo();

            Check.That(manager.CanUndo).IsFalse();
        }
Example #3
0
        public void AsUnDo_Should_return_an_IDictionary()
        {
            IDictionary <object, object> source = Substitute.For <IDictionary <object, object> >();
            IUnDoManager manager = Substitute.For <IUnDoManager>();

            Check.That(source.AsUnDo(manager)).IsNotNull();
        }
Example #4
0
        /// <summary>
        /// Removes the item with the specified key from a <see cref="IDictionary{TKey, TValue}"/> as a <see cref="IUnDo"/> operation.
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="manager">The <see cref="IUnDoManager"/>.</param>
        /// <param name="source">The <see cref="IDictionary{TKey, TValue}"/>.</param>
        /// <param name="key">The key 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="key"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="manager"/>, <paramref name="source"/> or <paramref name="key"/> is null.</exception>
        public static bool DoRemove <TKey, TValue>(this IUnDoManager manager, IDictionary <TKey, TValue> source, TKey key, object description = null)
        {
            if (manager is null)
            {
                throw new ArgumentNullException(nameof(manager));
            }
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (key is null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            bool result = false;

            if (source.TryGetValue(key, out TValue value))
            {
                manager.Do(new DictionaryUnDo <TKey, TValue>(description, source, key, value, false));
                result = true;
            }

            return(result);
        }
Example #5
0
        /// <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);
        }
Example #6
0
        public void CanRedo_Should_return_true_When_a_command_has_been_undone(IUnDoManager manager)
        {
            manager.Do(Substitute.For <IUnDo>());
            manager.Undo();

            Check.That(manager.CanRedo).IsTrue();
        }
Example #7
0
 public void Do_Should_throw_ArgumentNullException_When_command_is_null(IUnDoManager manager)
 {
     Check
     .ThatCode(() => manager.Do(null))
     .Throws <ArgumentNullException>()
     .WithProperty("ParamName", "command");
 }
Example #8
0
        public void DoClear_Should_add_old_elements_When_undone()
        {
            ICollection <object> source = new List <object>
            {
                new object(),
                new object(),
                new object()
            };
            IUnDoManager  manager    = Substitute.For <IUnDoManager>();
            IUnDo         undo       = null;
            List <object> sourceCopy = source.ToList();

            manager.Do(Arg.Do <IUnDo>(i => undo = i));

            manager.DoClear(source);

            Check.That(undo).IsNotNull();

            undo.Do();

            Check.That(source.Count).IsEqualTo(0);

            undo.Undo();

            Check.That(source).ContainsExactly(sourceCopy);
        }
Example #9
0
        public void AsUnDo_Should_return_an_ICollection()
        {
            ICollection <int> source  = Substitute.For <ICollection <int> >();
            IUnDoManager      manager = Substitute.For <IUnDoManager>();

            Check.That(source.AsUnDo(manager)).IsNotNull();
        }
Example #10
0
        public void Version_Should_incremente_When_a_command_is_done(IUnDoManager manager)
        {
            int oldVersion = manager.Version;

            manager.Do(Substitute.For <IUnDo>());

            Check.That(manager.Version).IsStrictlyGreaterThan(oldVersion);
        }
Example #11
0
        public void Version_Should_return_old_value_When_a_command_is_undone(IUnDoManager manager)
        {
            int oldVersion = manager.Version;

            manager.Do(Substitute.For <IUnDo>());
            manager.Undo();

            Check.That(manager.Version).IsEqualTo(oldVersion);
        }
Example #12
0
        public void Implicit_Should_return_value()
        {
            IUnDoManager manager = Substitute.For <IUnDoManager>();

            const int value        = 42;
            int       currentValue = new UnDoField <int>(manager, value);

            Check.That(currentValue).IsEqualTo(value);
        }
Example #13
0
        public void DoAdd_ICollection_Should_throw_ArgumentNullException_When_manager_is_null()
        {
            IUnDoManager         manager = null;
            ICollection <object> source  = null;

            Check
            .ThatCode(() => manager.DoAdd(source, null))
            .Throws <ArgumentNullException>()
            .WithProperty("ParamName", "manager");
        }
Example #14
0
        public void Do_ValueUnDo_Should_throw_ArgumentNullException_When_setter_is_null()
        {
            IUnDoManager    manager = Substitute.For <IUnDoManager>();
            Action <object> setter  = null;

            Check
            .ThatCode(() => manager.Do(setter, null, null))
            .Throws <ArgumentNullException>()
            .WithProperty("ParamName", "setter");
        }
Example #15
0
        public void DoRemoveAt_Should_throw_ArgumentNullException_When_manager_is_null()
        {
            IUnDoManager   manager = null;
            IList <object> source  = null;

            Check
            .ThatCode(() => manager.DoRemoveAt(source, 0))
            .Throws <ArgumentNullException>()
            .WithProperty("ParamName", "manager");
        }
Example #16
0
        public void DoClear_Should_throw_ArgumentNullException_When_source_is_null()
        {
            IUnDoManager         manager = Substitute.For <IUnDoManager>();
            ICollection <object> source  = null;

            Check
            .ThatCode(() => manager.DoClear(source))
            .Throws <ArgumentNullException>()
            .WithProperty("ParamName", "source");
        }
Example #17
0
        public void Version_Should_return_last_value_When_a_command_is_redone(IUnDoManager manager)
        {
            manager.Do(Substitute.For <IUnDo>());

            int lastVersion = manager.Version;

            manager.Undo();
            manager.Redo();

            Check.That(manager.Version).IsEqualTo(lastVersion);
        }
Example #18
0
        public void UnDoCollection_IsReadOnly_Should_return_IsReadOnly()
        {
            ICollection <object> source  = Substitute.For <ICollection <object> >();
            IUnDoManager         manager = Substitute.For <IUnDoManager>();

            source.IsReadOnly.Returns(true);

            ICollection <object> unDoCollection = source.AsUnDo(manager);

            Check.That(unDoCollection.IsReadOnly).IsEqualTo(source.IsReadOnly);
        }
Example #19
0
        public void UnDoCollection_Count_Should_return_Count()
        {
            ICollection <object> source  = Substitute.For <ICollection <object> >();
            IUnDoManager         manager = Substitute.For <IUnDoManager>();

            source.Count.Returns(1337);

            ICollection <object> unDoCollection = source.AsUnDo(manager);

            Check.That(unDoCollection.Count).IsEqualTo(source.Count);
        }
Example #20
0
        public void Do_Should_throw_ArgumentNullException_When_manager_is_null()
        {
            IUnDoManager manager    = null;
            Action       doAction   = null;
            Action       undoAction = null;

            Check
            .ThatCode(() => manager.Do(doAction, undoAction))
            .Throws <ArgumentNullException>()
            .WithProperty("ParamName", "manager");
        }
Example #21
0
        public void Do_IDictionary_Should_throw_ArgumentNullException_When_key_is_null()
        {
            IUnDoManager manager = Substitute.For <IUnDoManager>();
            IDictionary <object, object> source = Substitute.For <IDictionary <object, object> >();
            object key = null;

            Check
            .ThatCode(() => manager.Do(source, key, null))
            .Throws <ArgumentNullException>()
            .WithProperty("ParamName", "key");
        }
Example #22
0
        public void DoRemove_IDictionary_Should_throw_ArgumentNullException_When_manager_is_null()
        {
            IUnDoManager manager = null;
            IDictionary <object, object> source = null;
            object key = null;

            Check
            .ThatCode(() => manager.DoRemove(source, key))
            .Throws <ArgumentNullException>()
            .WithProperty("ParamName", "manager");
        }
Example #23
0
        public void UnDoDictionary_ContainsKey_Should_return_ContainsKey()
        {
            IDictionary <object, object> source = Substitute.For <IDictionary <object, object> >();
            IUnDoManager manager = Substitute.For <IUnDoManager>();
            object       key     = new();

            source.ContainsKey(key).Returns(true);

            IDictionary <object, object> unDoDictionary = source.AsUnDo(manager);

            Check.That(unDoDictionary.ContainsKey(key)).IsEqualTo(source.ContainsKey(key));
        }
Example #24
0
        public void Do_Should_clear_undone_history(IUnDoManager manager)
        {
            manager.Do(Substitute.For <IUnDo>());
            manager.Do(Substitute.For <IUnDo>());
            manager.Undo();

            Check.That(manager.CanRedo).IsTrue();

            manager.Do(Substitute.For <IUnDo>());

            Check.That(manager.CanRedo).IsFalse();
        }
Example #25
0
        public void Do_Should_not_add_command_in_history_when_a_group_is_going_on(IUnDoManager manager)
        {
            IUnDo undo    = Substitute.For <IUnDo>();
            int   version = manager.Version;

            using (manager.BeginTransaction())
            {
                manager.Do(undo);

                Check.That(manager.Version).IsEqualTo(version);
            }
        }
Example #26
0
        public void Do_Should_Do(IUnDoManager manager)
        {
            IUnDo undo = Substitute.For <IUnDo>();

            bool done = false;

            undo.When(u => u.Do()).Do(_ => done = true);

            manager.Do(undo);

            Check.That(done).IsTrue();
        }
Example #27
0
        public void UnDoCollection_Contains_Should_return_Contains()
        {
            ICollection <object> source  = Substitute.For <ICollection <object> >();
            IUnDoManager         manager = Substitute.For <IUnDoManager>();
            object value = new();

            source.Contains(value).Returns(true);

            ICollection <object> unDoCollection = source.AsUnDo(manager);

            Check.That(unDoCollection.Contains(value)).IsEqualTo(source.Contains(value));
        }
Example #28
0
        /// <summary>
        /// Does a <see cref="IUnDo"/> operation on the manager with the specified doAction and undoAction.
        /// </summary>
        /// <param name="manager">The <see cref="IUnDoManager"/>.</param>
        /// <param name="doAction">The <see cref="Action"/> performed by <see cref="IUnDo.Do"/>.</param>
        /// <param name="undoAction">The <see cref="Action"/> performed by the <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 Do(this IUnDoManager manager, Action doAction, Action undoAction, object description = null)
        {
            if (manager is null)
            {
                throw new ArgumentNullException(nameof(manager));
            }

            if (doAction != null || undoAction != null)
            {
                manager.Do(new UnDo(description, doAction, undoAction));
            }
        }
Example #29
0
        public void UnDoCollection_GetEnumerator_Should_return_GetEnumerator()
        {
            ICollection <object> source     = Substitute.For <ICollection <object> >();
            IUnDoManager         manager    = Substitute.For <IUnDoManager>();
            IEnumerator          enumerator = Substitute.For <IEnumerator>();

            ((IEnumerable)source).GetEnumerator().Returns(enumerator);

            ICollection <object> unDoCollection = source.AsUnDo(manager);

            Check.That(((IEnumerable)unDoCollection).GetEnumerator()).IsEqualTo(((IEnumerable)source).GetEnumerator());
        }
Example #30
0
        public void UnDoList_IndexOf_Should_return_IndexOf()
        {
            IList <object> source  = Substitute.For <IList <object> >();
            IUnDoManager   manager = Substitute.For <IUnDoManager>();
            object         value   = new();

            source.IndexOf(value).Returns(42);

            IList <object> unDoList = source.AsUnDo(manager);

            Check.That(unDoList.IndexOf(value)).IsEqualTo(source.IndexOf(value));
        }