Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EnumerableExtensions.cs « Collections « System « UnitTestFramework « Tests « System.ComponentModel.Composition « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: efb9c82994fa6afbaf82d7bee99d7283b4b130d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Internal.Collections
{
    public static class EnumerableExtensions
    {
        public static int Count(this IEnumerable source)
        {
            int count = 0;

            foreach (object o in source)
            {
                count++;
            }

            return count;
        }

        public static IEnumerable<T> ToEnumerable<T>(this IEnumerable source)
        {
            foreach (object value in source)
            {
                yield return (T)value;
            }
        }

        public static List<object> ToList(this IEnumerable source)
        {
            var enumerable = source.ToEnumerable<object>();

            return System.Linq.Enumerable.ToList(enumerable);
        }

        public static T AssertSingle<T>(this IEnumerable<T> source)
        {
            return AssertSingle(source, t => true);
        }

        public static T AssertSingle<T>(this IEnumerable<T> source, string message)
        {
            return AssertSingle(source, t => true, message);
        }

        public static T AssertSingle<T>(this IEnumerable<T> source, Func<T, bool> predicate)
        {
            return AssertSingle(source, predicate, "Expecting a single item matching the predicate in the collection.");
        }

        public static T AssertSingle<T>(this IEnumerable<T> source, Func<T, bool> predicate, string message)
        {
            int count = 0;
            T ret = default(T);
            foreach (T t in source)
            {
                if (predicate(t))
                {
                    count++;
                    ret = t;
                }
            }

            Assert.AreEqual(1, count, message);
            return ret;
        }
    }
}