PageRenderTime 52ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.ComponentModel.Composition/Tests/UnitTestFramework/System/Collections/EnumerableExtensions.cs

https://bitbucket.org/danipen/mono
C# | 72 lines | 58 code | 11 blank | 3 comment | 1 complexity | 1b1a30d19da3c73eee57ab2a25165225 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. namespace Microsoft.Internal.Collections
  9. {
  10. public static class EnumerableExtensions
  11. {
  12. public static int Count(this IEnumerable source)
  13. {
  14. int count = 0;
  15. foreach (object o in source)
  16. {
  17. count++;
  18. }
  19. return count;
  20. }
  21. public static IEnumerable<T> ToEnumerable<T>(this IEnumerable source)
  22. {
  23. foreach (object value in source)
  24. {
  25. yield return (T)value;
  26. }
  27. }
  28. public static List<object> ToList(this IEnumerable source)
  29. {
  30. var enumerable = source.ToEnumerable<object>();
  31. return System.Linq.Enumerable.ToList(enumerable);
  32. }
  33. public static T AssertSingle<T>(this IEnumerable<T> source)
  34. {
  35. return AssertSingle(source, t => true);
  36. }
  37. public static T AssertSingle<T>(this IEnumerable<T> source, string message)
  38. {
  39. return AssertSingle(source, t => true, message);
  40. }
  41. public static T AssertSingle<T>(this IEnumerable<T> source, Func<T, bool> predicate)
  42. {
  43. return AssertSingle(source, predicate, "Expecting a single item matching the predicate in the collection.");
  44. }
  45. public static T AssertSingle<T>(this IEnumerable<T> source, Func<T, bool> predicate, string message)
  46. {
  47. int count = 0;
  48. T ret = default(T);
  49. foreach (T t in source)
  50. {
  51. if (predicate(t))
  52. {
  53. count++;
  54. ret = t;
  55. }
  56. }
  57. Assert.AreEqual(1, count, message);
  58. return ret;
  59. }
  60. }
  61. }