Пример #1
0
        public void LazinatorListWorks(ContainerForLazinatorList containerOption, bool cloneAfterEachStep)
        {
            LazinatorListContainer nonGenericContainer = new LazinatorListContainer()
            {
            };
            LazinatorListContainerGeneric <ExampleChild> genericContainer = new LazinatorListContainerGeneric <ExampleChild>()
            {
            };
            LazinatorList <ExampleChild> withoutContainer = null;

            LazinatorList <ExampleChild> GetList()
            {
                switch (containerOption)
                {
                case ContainerForLazinatorList.NoContainer:
                    return(withoutContainer);

                case ContainerForLazinatorList.NonGenericContainer:
                    return(nonGenericContainer.MyList);

                case ContainerForLazinatorList.GenericContainer:
                    return(genericContainer.MyList);
                }
                throw new NotImplementedException();
            }

            void SetList(LazinatorList <ExampleChild> value)
            {
                switch (containerOption)
                {
                case ContainerForLazinatorList.NoContainer:
                    withoutContainer = value;
                    break;

                case ContainerForLazinatorList.NonGenericContainer:
                    nonGenericContainer.MyList = value;
                    break;

                case ContainerForLazinatorList.GenericContainer:
                    genericContainer.MyList = value;
                    break;
                }
            }

            SetList(new LazinatorList <ExampleChild>()
            {
            });

            LazinatorList <ExampleChild> list = new LazinatorList <ExampleChild>();

            for (int i = 0; i <= 3; i++)
            {
                AddItem(i);
                list.IsDirty.Should().BeTrue();
            }
            void AddItem(int i, int?insertIndex = null)
            {
                if (insertIndex is int insertIndexInt)
                {
                    GetList().Insert(insertIndexInt, GetExampleChild(i));
                    list.Insert(insertIndexInt, GetExampleChild(i));
                }
                else
                {
                    GetList().Add(GetExampleChild(i));
                    list.Add(GetExampleChild(i));
                }
            }

            void RemoveItem(int i, bool useRemoveAt)
            {
                if (useRemoveAt)
                {
                    GetList().RemoveAt(i);
                    list.RemoveAt(i);
                }
                else
                {
                    var item = GetList()[i];
                    GetList().Remove(item);
                    list.RemoveAt(i); // not testing this, so can just do this
                }
            }

            void Clear()
            {
                GetList().Clear();
                list.Clear();
                list.IsDirty.Should().BeTrue();
            }

            void CheckList()
            {
                for (int i = 0; i < list.Count; i++)
                {
                    var currentListItem = GetList()[i];
                    if (currentListItem == null)
                    {
                        list[i].Should().Be(null);
                    }
                    else
                    {
                        ExampleChildEqual(currentListItem, list[i]).Should().BeTrue();
                    }
                }
                // now check another way, using enumerables
                var zipped = GetList().Zip(list, (a, b) => (a, b));

                foreach (var zip in zipped)
                {
                    ExampleChildEqual(zip.a, zip.b).Should().BeTrue();
                }
            }

            void CloneList()
            {
                switch (containerOption)
                {
                case ContainerForLazinatorList.NoContainer:
                    SetList(GetList().CloneLazinatorTyped());
                    break;

                case ContainerForLazinatorList.NonGenericContainer:
                    nonGenericContainer = nonGenericContainer.CloneLazinatorTyped();
                    break;

                case ContainerForLazinatorList.GenericContainer:
                    genericContainer = genericContainer.CloneLazinatorTyped();
                    break;
                }
            }

            void CheckBeforeAndAfterSerialization()
            {
                CheckList();
                CloneList();
                CheckList();
                if (cloneAfterEachStep)
                {
                    CloneList();
                }
            }

            CheckBeforeAndAfterSerialization();
            CheckBeforeAndAfterSerialization(); // do it again
            AddItem(1);
            CheckBeforeAndAfterSerialization();
            AddItem(0);
            CheckBeforeAndAfterSerialization();
            AddItem(2, 1);
            CheckBeforeAndAfterSerialization();
            AddItem(0, 0);
            CheckBeforeAndAfterSerialization();
            RemoveItem(3, true);
            CheckBeforeAndAfterSerialization();
            RemoveItem(3, false);
            CheckBeforeAndAfterSerialization();
            RemoveItem(0, false);
            CheckBeforeAndAfterSerialization();
            RemoveItem(0, true);
            CheckBeforeAndAfterSerialization();
            Clear();
            CheckBeforeAndAfterSerialization();
        }