If.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Linq;
  6. using Xunit;
  7. namespace Tests
  8. {
  9. public class If : Tests
  10. {
  11. [Fact]
  12. public void If_Arguments()
  13. {
  14. AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(null, new[] { 1 }));
  15. AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(() => true, null));
  16. AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(null, new[] { 1 }, new[] { 1 }));
  17. AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(() => true, null, new[] { 1 }));
  18. AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(() => true, new[] { 1 }, null));
  19. }
  20. [Fact]
  21. public void If1()
  22. {
  23. var x = 5;
  24. var res = EnumerableEx.If(() => x > 0, new[] { +1 }, new[] { -1 });
  25. Assert.Equal(+1, res.Single());
  26. x = -x;
  27. Assert.Equal(-1, res.Single());
  28. }
  29. [Fact]
  30. public void If2()
  31. {
  32. var x = 5;
  33. var res = EnumerableEx.If(() => x > 0, new[] { +1 });
  34. Assert.Equal(+1, res.Single());
  35. x = -x;
  36. Assert.True(res.IsEmpty());
  37. }
  38. }
  39. }