Beispiel #1
0
 public void TestAdd_AddsToEnd()
 {
     var list = new TypedList<ArrayList, int>(new ArrayList() { 1, 2, 3 });
     list.Add(4);
     Assert.AreEqual(4, list.Count, "The count wasn't increased.");
     int[] expected = { 1, 2, 3, 4 };
     int[] actual = toTypedArray(list.List);
     Assert.IsTrue(expected.ToSublist().IsEqualTo(actual.ToSublist()), "The item wa not added as expected.");
 }
Beispiel #2
0
        public void Add(object value)
        {
            if (value != null)
            {
                if (this.list == null)
                {
                    this.list = (ITypedList)Activator.CreateInstance(typeof(TypedList <>).MakeGenericType(value.GetType()));
                }
                else if (!this.list.ItemType.IsAssignableFrom(value.GetType()))
                {
                    var newList = new TypedList <object>();

                    foreach (var item in this.list)
                    {
                        newList.Add(item);
                    }

                    this.list = newList;
                }
            }
            else
            {
                if (this.list == null)
                {
                    this.list = new TypedList <object>();
                }
                else
                {
                    if (this.list.ItemType.IsValueType && Nullable.GetUnderlyingType(this.list.ItemType) != null)
                    {
                        var nullableList = (ITypedList)Activator.CreateInstance(typeof(TypedList <>).MakeGenericType(typeof(Nullable <>).MakeGenericType(this.list.ItemType)));

                        foreach (var item in this.list)
                        {
                            nullableList.Add(item);
                        }

                        this.list = nullableList;
                    }
                }
            }

            this.list.Add(value);
        }
Beispiel #3
0
        public void TypedList_Basics()
        {
            Community c = new Community();

            List <Person> people = new List <Person>
            {
                new Person(c)
                {
                    Name = "One"
                },
                new Person(c)
                {
                    Name = "Two"
                },
                new Person(c)
                {
                    Name = "Three"
                }
            };

            // Null by default
            Assert.Null(c.People);

            // Settable to Empty
            c.People = Array.Empty <Person>();

            TypedList <Person> list = (TypedList <Person>)c.People;

            Assert.Empty(list);

            list.Add(people[0]);
            Assert.Single(list);

            list.Add(people[1]);
            list.Add(people[2]);
            CollectionReadVerifier.VerifySame(people, list);

            // SetTo self works properly
            list.SetTo(list);
            CollectionReadVerifier.VerifySame(people, list);

            // SetTo null works
            list.SetTo(null);
            Assert.Empty(list);

            // SetTo other works
            list.SetTo(people);
            CollectionReadVerifier.VerifySame(people, list);

            // Test Equality members
            CollectionReadVerifier.VerifyEqualityMembers(list, list);

            // Test equality operators
#pragma warning disable CS1718 // Comparison made to same variable
            Assert.True(list == list);
            Assert.False(list != list);
#pragma warning restore CS1718 // Comparison made to same variable

            Assert.False(list == null);
            Assert.True(list != null);

            Assert.False(null == list);
            Assert.True(null != list);

            // SetTo empty works
            list.SetTo(new List <Person>());
            Assert.Empty(list);

            CollectionChangeVerifier.VerifyList(c.People, (i) => new Person(c)
            {
                Age = (byte)i
            });

            // Set null
            IList <Person> cachedPeople = c.People;
            c.People = null;
            Assert.Null(c.People);
            Assert.Equal(0, cachedPeople.Count);

            // SetTo TypedList from another DB/Table
            Community c2 = new Community();
            c2.People = new List <Person>();
            c2.People.Add(new Person(c2)
            {
                Name = "Other"
            });
            list.SetTo(c2.People);

            Assert.Equal("Other", list[0].Name);
        }
Beispiel #4
0
        public static TypedList <Employee> GetData()
        {
            var tlist = new TypedList <Employee>();

            tlist.Add(new Employee(1, -1, "John", "Doe", "Director of sales"));
            tlist.Add(new Employee(10, 1, "Alex", "Red", "Manager"));
            tlist.Add(new Employee(100, 10, "Alfred", "Bon", "Manager's assistant"));
            tlist.Add(new Employee(11, 1, "Elena", "White", "Manager"));
            tlist.Add(new Employee(12, 1, "Eric", "Green", "Manager"));
            tlist.Add(new Employee(13, 1, "Sara", "Blue", "Manager"));
            tlist.Add(new Employee(130, 12, "Mila", "Vong", "Manager's assistant"));
            tlist.Add(new Employee(131, 12, "Alex", "Li", "Manager's assistant"));
            tlist.Add(new Employee(14, 1, "Gloria", "Black", "Manager"));

            tlist.Add(new Employee(2, -1, "Mary", "Sue", "Marketing director"));
            tlist.Add(new Employee(20, 2, "Natalie", "Ming", "Manager"));
            tlist.Add(new Employee(201, 20, "Ivan", "Romanov", "Manager's assistant"));
            tlist.Add(new Employee(202, 20, "Stan", "Heck", "Manager's assistant"));
            tlist.Add(new Employee(203, 20, "Don", "Milman", "Manager's assistant"));
            tlist.Add(new Employee(21, 2, "Joseph", "Anderson", "Manager"));
            tlist.Add(new Employee(210, 21, "Jack", "Gun", "Manager's assistant"));
            tlist.Add(new Employee(22, 2, "Olivia", "Helt", "Manager"));
            tlist.Add(new Employee(220, 22, "Caleb", "Iron", "Manager's assistant"));
            tlist.Add(new Employee(221, 22, "Dylan", "Gold", "Manager's assistant"));
            tlist.Add(new Employee(23, 2, "Samantha", "Silver", "Manager"));
            tlist.Add(new Employee(24, 2, "Tyler", "Steel", "Manager"));

            tlist.Add(new Employee(3, -1, "Victor", "Smith", "Security director"));
            tlist.Add(new Employee(31, 3, "James", "Bond", "Guard"));
            tlist.Add(new Employee(32, 3, "Jason", "Bourne", "Guard"));
            tlist.Add(new Employee(33, 3, "John", "McClane", "Guard"));
            tlist.Add(new Employee(34, 3, "Peter", "Parker", "Guard"));
            tlist.Add(new Employee(35, 3, "Tony", "Stark", "Guard"));

            return(tlist);
        }