Exemple #1
0
        public void ColumnDictionary_Basics()
        {
            string sampleName     = "Name";
            string sampleValue    = "Scott";
            string retrievedValue = null;

            string secondName  = "City";
            string secondValue = "Redmond";

            string unusedName = "Unused";

            Dictionary <string, string> expected = new Dictionary <string, string>();

            ColumnDictionary <string, string> row = DictionaryColumnTests.SampleRow();

            Assert.True(0 == ColumnDictionary <string, string> .Empty.Count);
            Assert.False(row.IsReadOnly);

            // Test Empty Dictionary
            Assert.False(row.TryGetValue(sampleName, out retrievedValue));
            Assert.False(row.ContainsKey(sampleName));
            Assert.False(row.Remove(sampleName));
            Assert.True(0 == row.Count);
            Assert.Empty(row.Keys);
            Assert.Empty(row.Values);

            // Add a single value and test results
            expected[sampleName] = sampleValue;
            row[sampleName]      = sampleValue;
            CollectionReadVerifier.VerifySame(expected, row);
            CollectionReadVerifier.VerifySame(expected.Keys, row.Keys);
            CollectionReadVerifier.VerifySame(expected.Values, row.Values);

            // Add a second value and verify
            expected.Add(secondName, secondValue);
            row.Add(new KeyValuePair <string, string>(secondName, secondValue));
            CollectionReadVerifier.VerifySame(expected, row);
            CollectionReadVerifier.VerifySame(expected.Keys, row.Keys);
            CollectionReadVerifier.VerifySame(expected.Values, row.Values);

            // Negative (missing item / already added item) cases
            Assert.True(false == row.Contains(new KeyValuePair <string, string>(sampleName, secondValue)));
            Assert.True(false == row.Contains(new KeyValuePair <string, string>(unusedName, sampleValue)));
            Assert.True(false == row.ContainsKey(unusedName));
            Assert.Throws <KeyNotFoundException>(() => row[unusedName]);
            Assert.Throws <ArgumentException>(() => row.Add(new KeyValuePair <string, string>(sampleName, secondValue)));

            // Change value and verify, then change back
            Assert.Equal(expected[sampleName], row[sampleName]);
            expected[sampleName] = secondValue;
            row[sampleName]      = secondValue;
            Assert.Equal(expected[sampleName], row[sampleName]);
            CollectionReadVerifier.VerifySame(expected, row);
            CollectionReadVerifier.VerifySame(expected.Keys, row.Keys);
            CollectionReadVerifier.VerifySame(expected.Values, row.Values);

            expected[sampleName] = sampleValue;
            row[sampleName]      = sampleValue;
            Assert.Equal(expected[sampleName], row[sampleName]);

            // Remove
            Assert.True(row.Remove(secondName));
            Assert.False(row.Remove(secondName));
            Assert.False(row.ContainsKey(secondName));
            Assert.False(row.Remove(new KeyValuePair <string, string>(unusedName, sampleValue)));
            Assert.False(row.Remove(new KeyValuePair <string, string>(sampleName, secondValue)));
            Assert.True(row.Remove(new KeyValuePair <string, string>(sampleName, sampleValue)));
            Assert.Empty(row);

            // SetTo
            row.SetTo(expected);
            CollectionReadVerifier.VerifySame <KeyValuePair <string, string> >(expected, row);

            // Create another Dictionary with the same values inserted in a different order
            ColumnDictionary <string, string> row2 = DictionaryColumnTests.SampleRow();

            row2[secondName] = secondValue;
            row2[sampleName] = sampleValue;

            // Test Equals and GetHashCode
            CollectionReadVerifier.VerifyEqualityMembers <ColumnDictionary <string, string> >(row, row2);

            // Test equality operators
            Assert.True(row == row2);
            Assert.False(row != row2);

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

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

            // GetHashCode handles null key/values safely
            row[null] = null;
            Assert.Equal(row.GetHashCode(), row2.GetHashCode());

            // Verify other collection manipulation
            // NOTE: Must use unique keys, because Add(KeyValuePair) will throw for a duplicate key
            CollectionChangeVerifier.VerifyCollection(row, (i) => new KeyValuePair <string, string>(i.ToString(), i.ToString()));

            if (!Debugger.IsAttached)
            {
                Assert.Throws <IndexOutOfRangeException>(() => ColumnDictionary <string, string> .Get(null, -1));
            }
        }
Exemple #2
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);
        }