Esempio n. 1
0
        public static void CollectionExtensionTests()
        {
            var nullEnumerable = (IEnumerable <string>)null;
            var empty          = new string[0];
            var notSparse      = new string[] { "a", "b", "c" };
            var sparse         = new string[] { "a", null };
            var numbers        = new int[] { 1, 2, 3 };
            var emptyNumbers   = new int[0];

            // NullOrEmpty
            Assert.True(nullEnumerable.NullOrEmpty());
            Assert.True(new ListWrapper <string>(empty).NullOrEmpty());
            Assert.True(new ReadOnlyListWrapper <string>(empty).NullOrEmpty());
            Assert.True(new EnumerableWrapper <string>(empty).NullOrEmpty());
            Assert.False(new ListWrapper <int>(numbers).NullOrEmpty());
            Assert.False(new ReadOnlyListWrapper <int>(numbers).NullOrEmpty());
            Assert.False(new EnumerableWrapper <int>(numbers).NullOrEmpty());

            // NullOrSparse
            Assert.True(nullEnumerable.NullOrSparse());
            Assert.False(new ListWrapper <string>(empty).NullOrSparse());
            Assert.False(new ReadOnlyListWrapper <string>(empty).NullOrSparse());
            Assert.False(new EnumerableWrapper <string>(empty).NullOrSparse());
            Assert.True(new ListWrapper <string>(sparse).NullOrSparse());
            Assert.True(new ReadOnlyListWrapper <string>(sparse).NullOrSparse());
            Assert.True(new EnumerableWrapper <string>(sparse).NullOrSparse());
            Assert.False(new ListWrapper <string>(notSparse).NullOrSparse());
            Assert.False(new ReadOnlyListWrapper <string>(notSparse).NullOrSparse());
            Assert.False(new EnumerableWrapper <string>(notSparse).NullOrSparse());

            // NullEmptyOrSparse
            Assert.True(nullEnumerable.NullEmptyOrSparse());
            Assert.True(new ListWrapper <string>(empty).NullEmptyOrSparse());
            Assert.True(new ReadOnlyListWrapper <string>(empty).NullEmptyOrSparse());
            Assert.True(new EnumerableWrapper <string>(empty).NullEmptyOrSparse());
            Assert.True(new ListWrapper <string>(sparse).NullEmptyOrSparse());
            Assert.True(new ReadOnlyListWrapper <string>(sparse).NullEmptyOrSparse());
            Assert.True(new EnumerableWrapper <string>(sparse).NullEmptyOrSparse());
            Assert.False(new ListWrapper <string>(notSparse).NullEmptyOrSparse());
            Assert.False(new ReadOnlyListWrapper <string>(notSparse).NullEmptyOrSparse());
            Assert.False(new EnumerableWrapper <string>(notSparse).NullEmptyOrSparse());

            // Contains(predicate)
            // NOTE: mostly tested implicitly by NullOrSparse/NullEmptyOrSparse
            Assert.Throws <ArgumentNullException>(() => CoreExtensions.Contains(sequence: (IEnumerable <int>)null, predicate: i => true));
            Assert.Throws <ArgumentNullException>(() => CoreExtensions.Contains(sequence: numbers, predicate: null));

            // FirstOrNullable()
            Assert.Throws <ArgumentNullException>(() => CoreExtensions.FirstOrNullable(sequence: (IEnumerable <int>)null));
            Assert.False(new ListWrapper <int>(emptyNumbers).FirstOrNullable().HasValue);
            Assert.False(new ReadOnlyListWrapper <int>(emptyNumbers).FirstOrNullable().HasValue);
            Assert.False(new EnumerableWrapper <int>(emptyNumbers).FirstOrNullable().HasValue);
            Assert.AreEqual(1, new ListWrapper <int>(numbers).FirstOrNullable().Value);
            Assert.AreEqual(1, new ReadOnlyListWrapper <int>(numbers).FirstOrNullable().Value);
            Assert.AreEqual(1, new EnumerableWrapper <int>(numbers).FirstOrNullable().Value);

            // FirstOrNullable(predicate)
            Assert.AreEqual(3, numbers.FirstOrNullable(i => i == 3).Value);
            Assert.False(numbers.FirstOrNullable(i => i == 4).HasValue);
        }