Case.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using Xunit;
  8. namespace Tests
  9. {
  10. public class Case : Tests
  11. {
  12. [Fact]
  13. public void Case_Arguments()
  14. {
  15. AssertThrows<ArgumentNullException>(() => EnumerableEx.Case<int, int>(null, new Dictionary<int, IEnumerable<int>>()));
  16. AssertThrows<ArgumentNullException>(() => EnumerableEx.Case<int, int>(() => 1, null));
  17. AssertThrows<ArgumentNullException>(() => EnumerableEx.Case<int, int>(null, new Dictionary<int, IEnumerable<int>>(), new[] { 1 }));
  18. AssertThrows<ArgumentNullException>(() => EnumerableEx.Case<int, int>(() => 1, null, new[] { 1 }));
  19. AssertThrows<ArgumentNullException>(() => EnumerableEx.Case<int, int>(() => 1, new Dictionary<int, IEnumerable<int>>(), null));
  20. }
  21. [Fact]
  22. public void Case1()
  23. {
  24. var x = 1;
  25. var d = 'd';
  26. var res = EnumerableEx.Case<int, char>(() => x, new Dictionary<int, IEnumerable<char>>
  27. {
  28. { 0, new[] { 'a' } },
  29. { 1, new[] { 'b' } },
  30. { 2, new[] { 'c' } },
  31. { 3, EnumerableEx.Defer(() => new[] { d }) },
  32. });
  33. Assert.Equal('b', res.Single());
  34. Assert.Equal('b', res.Single());
  35. x = 0;
  36. Assert.Equal('a', res.Single());
  37. x = 2;
  38. Assert.Equal('c', res.Single());
  39. x = 3;
  40. Assert.Equal('d', res.Single());
  41. d = 'e';
  42. Assert.Equal('e', res.Single());
  43. x = 4;
  44. Assert.True(res.IsEmpty());
  45. }
  46. [Fact]
  47. public void Case2()
  48. {
  49. var x = 1;
  50. var d = 'd';
  51. var res = EnumerableEx.Case<int, char>(() => x, new Dictionary<int, IEnumerable<char>>
  52. {
  53. { 0, new[] { 'a' } },
  54. { 1, new[] { 'b' } },
  55. { 2, new[] { 'c' } },
  56. { 3, EnumerableEx.Defer(() => new[] { d }) },
  57. }, new[] { 'z' });
  58. Assert.Equal('b', res.Single());
  59. Assert.Equal('b', res.Single());
  60. x = 0;
  61. Assert.Equal('a', res.Single());
  62. x = 2;
  63. Assert.Equal('c', res.Single());
  64. x = 3;
  65. Assert.Equal('d', res.Single());
  66. d = 'e';
  67. Assert.Equal('e', res.Single());
  68. x = 4;
  69. Assert.Equal('z', res.Single());
  70. }
  71. }
  72. }